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

PixelProviderTaskCacheTest.lua

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

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.

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

	-- wf.core
local WFRxTestCase = require 'WFRxTestCase'

	-- pixel_provider
local PixelProviderTaskCache = require 'PixelProviderTaskCache'

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

local function clientId( i )

	return string.format( "clientId%d", i )

end

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

local function localAssetId( i )

	return i

end

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

local function setupCaches()

	local simplePerClientLocalAssetCache = 
		PixelProviderTaskCache.makePerClientLocalAssetSimpleDataCache()

	local simpleLocalAssetCache =
			PixelProviderTaskCache.makeLocalAssetSimpleDataCache()

	return simplePerClientLocalAssetCache,
			simpleLocalAssetCache

end

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

local function rxTestFail( rxTest )

	local simplePerClientLocalAssetCache, 
			simpleLocalAssetCache = setupCaches( rxTest )

	-- test push bad keys
	rxTest:expectError( simplePerClientLocalAssetCache.pushCacheItem, 
								simplePerClientLocalAssetCache )

	rxTest:expectError( simplePerClientLocalAssetCache.pushCacheItem, 
								simplePerClientLocalAssetCache, 
								clientId( 1 ), 'bogus'  )

	rxTest:expectError( simpleLocalAssetCache.pushCacheItem, 
								simpleLocalAssetCache )

	-- test peek bad keys
	rxTest:expectError( simplePerClientLocalAssetCache.peekCacheItem, 
								simplePerClientLocalAssetCache )

	rxTest:expectError( simplePerClientLocalAssetCache.peekCacheItem, 
								simplePerClientLocalAssetCache, 
								clientId( 1 ), 'bogus'  )

	rxTest:expectError( simpleLocalAssetCache.peekCacheItem, 
								simpleLocalAssetCache )

	-- test destroy bad keys
	rxTest:expectError( simplePerClientLocalAssetCache.destroyCacheItem, 
								simplePerClientLocalAssetCache )

	rxTest:expectError( simplePerClientLocalAssetCache.destroyAllLocalAssetCacheItems, 
								simplePerClientLocalAssetCache )

	rxTest:expectError( simplePerClientLocalAssetCache.destroyCacheItem, 
								simplePerClientLocalAssetCache, 
								clientId( 1 ), 'bogus'  )

	rxTest:expectError( simpleLocalAssetCache.destroyCacheItem, 
								simpleLocalAssetCache, 'bogus' )

	rxTest:expectError( simpleLocalAssetCache.destroyCacheItem, 
								simpleLocalAssetCache )

	-- test push nil
	rxTest:expectError( simplePerClientLocalAssetCache.pushCacheItem, 
								simplePerClientLocalAssetCache, 
								clientId( 1 ), localAssetId( 1 )  )

	rxTest:expectError( simpleLocalAssetCache.pushCacheItem, 
								simpleLocalAssetCache, 
								localAssetId( 1 )  )

end

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

local function rxTestPeek( rxTest )

	local simplePerClientLocalAssetCache, 
			simpleLocalAssetCache = setupCaches( rxTest )

	local cacheItem = 'a'

	-- test peek when cache is empty
	rxTest:expectEqual( nil, 
							simplePerClientLocalAssetCache:peekCacheItem( 
								clientId( 1 ), localAssetId( 1 ) ) )

	rxTest:expectEqual( nil, simpleLocalAssetCache:peekCacheItem( 
								localAssetId( 1 ) ) )
	-- test caching when there are no requests
	rxTest:expectEqual( true, 
							simplePerClientLocalAssetCache:pushCacheItem( 
								clientId( 1 ), localAssetId( 1 ), 
								cacheItem ) )

	rxTest:expectEqual( true, 
							simpleLocalAssetCache:pushCacheItem( 
								localAssetId( 1 ), 
								cacheItem ) )

	-- test peek 
	rxTest:expectEqual( cacheItem, 
							simplePerClientLocalAssetCache:peekCacheItem( 
								clientId( 1 ), localAssetId( 1 ) ) )

	rxTest:expectEqual( cacheItem, simpleLocalAssetCache:peekCacheItem( 
								localAssetId( 1 ) ) )

end

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

local function rxTestPushTwice( rxTest )

	local simplePerClientLocalAssetCache,
			simpleLocalAssetCache = setupCaches( rxTest )

	local cacheItem = 'a'

	local cacheItem2 = {}

	-- test caching when there are no requests
	rxTest:expectEqual( true, 
							simplePerClientLocalAssetCache:pushCacheItem( 
								clientId( 1 ), localAssetId( 1 ), 
								cacheItem ) )

	rxTest:expectEqual( true, 
							simplePerClientLocalAssetCache:pushCacheItem( 
								clientId( 1 ), localAssetId( 1 ), 
								cacheItem2 ) )

	rxTest:expectEqual( true, 
							simpleLocalAssetCache:pushCacheItem( 
								localAssetId( 1 ), 
								cacheItem ) )

	rxTest:expectEqual( true, 
							simpleLocalAssetCache:pushCacheItem( 
								localAssetId( 1 ), 
								cacheItem2 ) )

	-- test peek 
	rxTest:expectEqual( cacheItem2, 
							simplePerClientLocalAssetCache:peekCacheItem( 
								clientId( 1 ), localAssetId( 1 ) ) )

	rxTest:expectEqual( cacheItem2, simpleLocalAssetCache:peekCacheItem( 
								localAssetId( 1 ) ) )

end

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

local function rxTestPop( rxTest )

	local simplePerClientLocalAssetCache,
			simpleLocalAssetCache = setupCaches( rxTest )

	local cacheItem = 'a'

	rxTest:expectEqual( true, 
							simplePerClientLocalAssetCache:pushCacheItem( 
								clientId( 1 ), localAssetId( 1 ), 
								cacheItem ) )

	rxTest:expectEqual( true, 
							simpleLocalAssetCache:pushCacheItem( 
								localAssetId( 1 ), 
								cacheItem ) )

	-- test pop 
	rxTest:expectEqual( cacheItem, 
							simplePerClientLocalAssetCache:popCacheItem( 
								clientId( 1 ), localAssetId( 1 ) ) )

	rxTest:expectEqual( cacheItem, 
							simpleLocalAssetCache:popCacheItem( 
								localAssetId( 1 ) ) )

	-- test cache is empty after pop
	rxTest:expectEqual( nil, 
							simplePerClientLocalAssetCache:peekCacheItem( 
								clientId( 1 ), localAssetId( 1 ) ) )

	rxTest:expectEqual( nil, 
							simpleLocalAssetCache:peekCacheItem( 
								localAssetId( 1 ) ) )

end

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

local function rxTestDestroy( rxTest )

	local simplePerClientLocalAssetCache,
			simpleLocalAssetCache = setupCaches( rxTest )

	local cacheItem1 = 'a'
	local cacheItem2 = 'b'
	local cacheItem3 = 'c'

	-- test caching 

	-- test caching when there are no requests
	rxTest:expectEqual( true, 
							simplePerClientLocalAssetCache:pushCacheItem( 
								clientId( 1 ), localAssetId( 1 ), 
								cacheItem1 ) )

	rxTest:expectEqual( true, 
							simplePerClientLocalAssetCache:pushCacheItem( 
								clientId( 2 ), localAssetId( 1 ), 
								cacheItem2 ) )

	rxTest:expectEqual( true, 
							simplePerClientLocalAssetCache:pushCacheItem( 
								clientId( 3 ), localAssetId( 1 ), 
								cacheItem3 ) )

	simplePerClientLocalAssetCache:destroyCacheItem( 
								clientId( 1 ), localAssetId( 1 ) )

	rxTest:expectEqual( nil, 
							simplePerClientLocalAssetCache:peekCacheItem( 
								clientId( 1 ), localAssetId( 1 ) ) )

	rxTest:expectEqual( cacheItem2, 
							simplePerClientLocalAssetCache:peekCacheItem( 
								clientId( 2 ), localAssetId( 1 ) ) )

	rxTest:expectEqual( cacheItem3, 
							simplePerClientLocalAssetCache:peekCacheItem( 
								clientId( 3 ), localAssetId( 1 ) ) )

	simplePerClientLocalAssetCache:destroyAllLocalAssetCacheItems( 
								localAssetId( 1 ) )

	rxTest:expectEqual( nil, 
							simplePerClientLocalAssetCache:peekCacheItem( 
								clientId( 2 ), localAssetId( 1 ) ) )

	rxTest:expectEqual( nil, 
							simplePerClientLocalAssetCache:peekCacheItem( 
								clientId( 3 ), localAssetId( 1 ) ) )

	-- simpleLocalAssetCache 

	rxTest:expectEqual( true, 
							simpleLocalAssetCache:pushCacheItem( 
								localAssetId( 1 ), cacheItem1 ) )

	rxTest:expectEqual( true, 
							simpleLocalAssetCache:pushCacheItem( 
								localAssetId( 2 ), cacheItem2 ) )

	simpleLocalAssetCache:destroyCacheItem( localAssetId( 1 ) )

	rxTest:expectEqual( nil, 
						simpleLocalAssetCache:peekCacheItem( 
												localAssetId( 1 ) ) )

	rxTest:expectEqual( cacheItem2, 
						simpleLocalAssetCache:peekCacheItem( 
												localAssetId( 2 ) ) )


	simpleLocalAssetCache:destroyCacheItem( localAssetId( 1 ) )

	simpleLocalAssetCache:destroyCacheItem( localAssetId( 2 ) )

	rxTest:expectEqual( nil, 
						simpleLocalAssetCache:peekCacheItem( 
												localAssetId( 1 ) ) )

	rxTest:expectEqual( nil, 
						simpleLocalAssetCache:peekCacheItem( 
												localAssetId( 2 ) ) )

end

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

local function rxTestDestroyAll( rxTest )

	local simplePerClientLocalAssetCache = setupCaches( rxTest )

	local cacheItem1 = 'a'
	local cacheItem2 = 'b'
	local cacheItem3 = 'c'
	local cacheItem4 = 'd'

	-- test caching 

	-- test caching when there are no requests
	rxTest:expectEqual( true, 
							simplePerClientLocalAssetCache:pushCacheItem( 
								clientId( 1 ), localAssetId( 1 ), 
								cacheItem1 ) )

	rxTest:expectEqual( true, 
							simplePerClientLocalAssetCache:pushCacheItem( 
								clientId( 2 ), localAssetId( 1 ), 
								cacheItem2 ) )

	rxTest:expectEqual( true, 
							simplePerClientLocalAssetCache:pushCacheItem( 
								clientId( 3 ), localAssetId( 1 ), 
								cacheItem3 ) )

	rxTest:expectEqual( true, 
							simplePerClientLocalAssetCache:pushCacheItem( 
								clientId( 3 ), localAssetId( 2 ), 
								cacheItem4 ) )

	simplePerClientLocalAssetCache:destroyAll()

	rxTest:expectEqual( nil, 
							simplePerClientLocalAssetCache:peekCacheItem( 
								clientId( 1 ), localAssetId( 1 ) ) )

	rxTest:expectEqual( nil, 
							simplePerClientLocalAssetCache:peekCacheItem( 
								clientId( 2 ), localAssetId( 1 ) ) )

	rxTest:expectEqual( nil, 
							simplePerClientLocalAssetCache:peekCacheItem( 
								clientId( 3 ), localAssetId( 1 ) ) )

	rxTest:expectEqual( nil, 
							simplePerClientLocalAssetCache:peekCacheItem( 
								clientId( 3 ), localAssetId( 2 ) ) )

end

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

return WFRxTestCase.wrapSuite( {

	rxTestFail = rxTestFail,

	rxTestPeek = rxTestPeek,

	rxTestPushTwice = rxTestPushTwice,

	rxTestPop = rxTestPop,

	rxTestDestroy = rxTestDestroy,

	rxTestDestroyAll = rxTestDestroyAll,

} )
