--[[----------------------------------------------------------------------------

ADOBE CONFIDENTIAL
------------------
Copyright 2016 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remain the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law.  Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

----------------------------------------------------------------------------]]--

local WFOzRenditionServer = require 'WFOzRenditionServer'
local KSIdGenerator = require 'KSIdGenerator'
local LrmCatalogTestHelper = require 'LrmCatalogTestHelper'

local KSDataCache = require "KSDataCache"

local WFTableMerge = require 'WFTableMerge'
local WFRxTestCase = require 'WFRxTestCase'

--==============================================================================

-- @mark WFOzRenditionServerTest namespace

--------------------------------------------------------------------------------

local LrmOzRenditionServerTest = {}

--------------------------------------------------------------------------------

local function setUpCatalogAndRenditionServer( rxTest )

	local dataCache = KSDataCache()

	local catalogService, wfCatalog = LrmCatalogTestHelper.makeCatalogService()
	local renditionServer = WFOzRenditionServer( catalogService, 
								wfCatalog:ozUserId(), wfCatalog:ozCatalogId(), dataCache, wfCatalog )
								
	rxTest:finally( function()
		wfCatalog:close()
		catalogService:close()
		renditionServer:close()
	end )
		
	return catalogService, wfCatalog, renditionServer
	
end

--------------------------------------------------------------------------------

function LrmOzRenditionServerTest.rxTestShouldDownload( rxTest )

	local _, _, renditionServer = setUpCatalogAndRenditionServer( rxTest )

	local assetId = KSIdGenerator.uuidInLowercaseHex()
	
	local function shouldDownload( docValue, renditionName )
	
		return renditionServer:shouldDownloadRendition( docValue, assetId, renditionName )
		
	end
	
	local docValue = {
		_serverProvidedMetadata = {
			links = {
				[ '/rels/rendition_type/thumbnail2x' ] = {
					href = 'ffff/renditions/aaaa',
				}
			},
		}
	}
	
	rxTest:expectEqual( shouldDownload( docValue, 'thumbnail2x' ), true )
	
	docValue = WFTableMerge.applyOverlay( docValue, {
			_localOnly = {
				thumbnail2x = {
					revId = 'ffff',
					renditionId = 'aaaa',
					md5 = '<hash>',
				}
			},
		} )
		
	rxTest:expectEqual( shouldDownload( docValue, 'thumbnail2x' ), false )
	
	docValue = WFTableMerge.applyOverlay( docValue, {
			_serverProvidedMetadata = {
				links = {
					[ '/rels/rendition_type/thumbnail2x' ] = {
						href = 'ffff/renditions/aaaa',
						invalid = true,
					}
				},
			}
		} )
		
	rxTest:expectEqual( shouldDownload( docValue, 'thumbnail2x' ), false )
	
	docValue = WFTableMerge.applyOverlay( docValue, {
			_serverProvidedMetadata = {
				links = {
					[ '/rels/rendition_type/thumbnail2x' ] = {
						href = 'gggg/renditions/dddd',
						invalid = true,
					}
				},
			}
		} )
		
	rxTest:expectEqual( shouldDownload( docValue, 'thumbnail2x' ), false )
	
	docValue = WFTableMerge.applyOverlay( docValue, {
			_serverProvidedMetadata = {
				links = {
					[ '/rels/rendition_type/thumbnail2x' ] = {
						href = 'gggg/renditions/dddd',
						invalid = false
					}
				},
			}
		} )
		
	rxTest:expectEqual( shouldDownload( docValue, 'thumbnail2x' ), false )
	
	docValue = {
		_serverProvidedMetadata = {
			links = {
				[ '/rels/rendition_type/2048' ] = {
					href = 'ffff/renditions/aaaa',
				}
			},
		}
	}
	
	rxTest:expectEqual( shouldDownload( docValue, 'preview' ), true )
	
	docValue = {
		_localOnly = {
				thumbnail2x = {
					revId = 'ffff',
					renditionId = 'aaaa',
					md5 = '<hash>',
				}
			},
		_serverProvidedMetadata = {
			links = {
				[ '/rels/rendition_type/thumbnail2x' ] = {
					href = 'ffff/renditions/aaab',
				}
			},
		}
	}
	
	rxTest:expectEqual( shouldDownload( docValue, 'thumbnail2x' ), false )
	
	docValue = {
		_localOnly = {
				thumbnail2x = {
					revId = 'ffff',
					renditionId = 'aaaa',
					md5 = '<hash>',
				}
			},
		_serverProvidedMetadata = {
			links = {
			},
		}
	}
	
	rxTest:expectEqual( shouldDownload( docValue, 'thumbnail2x' ), false )
	
	
	docValue = {
		_serverProvidedMetadata = {
			links = {
				[ '/rels/rendition_type/thumbnail2x' ] = {
					href = 'ffff/renditions/aaaa',
					invalid = true,
				}
			},
		}
	}
	
	rxTest:expectEqual( shouldDownload( docValue, 'thumbnail2x' ), true )
	
end

--------------------------------------------------------------------------------

return WFRxTestCase.wrapSuite( 'LrmOzRenditionServerTest', LrmOzRenditionServerTest )
