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

KSDataCacheTest.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.

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

Test the KSDataCache.

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

	-- WF.core.self_test
local WFAsyncTestCase = require 'WFAsyncTestCase'
local WFRxTestCase = require 'WFRxTestCase'

local KSData = require "KSData"

local KSDataCache = require "KSDataCache"

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

local KSDataCacheTest = {}

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

function KSDataCacheTest.rxTestRxGetEmpty( rxTest )

	local dataCache = KSDataCache( 'rxTestRxGetEmpty' )

	local data = KSData.make( 'random_bytes_with_low_entropy_rxTestRxGetEmpty' )
	
	local dataSHA = data:digest( 'SHA256' )
	
	rxTest:expectSingleValue( dataCache:get( dataSHA ), nil )
	
end

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

function KSDataCacheTest.rxTestRxGetEmptyTwice( rxTest )

	local dataCache = KSDataCache( 'rxTestRxGetEmptyTwice' )

	local data = KSData.make( 'random_bytes_with_low_entropy_rxTestRxGetEmpty' )
	
	local dataSHA = data:digest( 'SHA256' )
	
	rxTest:expectSingleValue( dataCache:get( dataSHA ), nil )

	rxTest:expectSingleValue( dataCache:get( dataSHA ), nil )
	
end

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

function KSDataCacheTest.rxTestRxPutGet( rxTest )

	local dataCache = KSDataCache( 'rxTestRxPutGet' )

	local data = KSData.make( 'random_bytes_with_low_entropy_rxTestRxPutGet' )
	
	local dataSHA = data:digest( 'SHA256' )
	
	rxTest:expectSingleValue( dataCache:put( dataSHA, data )
		:map( tostring ),
		tostring( data ) )

	-- read (from cache)	

	rxTest:expectSingleValue( dataCache:get( dataSHA )
		:map( tostring ),
		tostring( data ) )

	
	rxTest:expectSingleValue( dataCache:get( dataSHA )
		:map( tostring ),
		tostring( data ) )

	rxTest:expectSingleValue( dataCache:haltAndDelete(), true )
	
end

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

function KSDataCacheTest.rxTestRxRemove( rxTest )

	local dataCache = KSDataCache( 'rxTestRxRemove' )

	local data = KSData.make( 'random_bytes_with_low_entropy_rxTestRxRemove' )
	
	local dataSHA = data:digest( 'SHA256' )
	
	rxTest:expectSingleValue( dataCache:put( dataSHA, data )
		:map( tostring ),
		tostring( data ) )

	rxTest:expectSingleValue( dataCache:get( dataSHA )
		:map( tostring ),
		tostring( data ) )
	
	rxTest:expectOnlyCompleted( dataCache:remove( dataSHA ) )
	
	rxTest:expectSingleValue( dataCache:get( dataSHA ),
		nil )

	rxTest:expectSingleValue( dataCache:haltAndDelete(), true )
	
end

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

function KSDataCacheTest.rxTestRxBulkRemove( rxTest )

	rxTest:setTimeLimit( 20 )

	local dataCache = KSDataCache( 'rxTestRxBulkRemove' )

	local keyCount = 100

	local keys = {}	

	for i = 1, keyCount do
		local data = KSData.make( 'random_bytes_with_low_entropy_rxTestRxRemove_' .. tostring( i ) )
		
		keys[ i ] = data:digest( 'SHA256' )

		rxTest:expectSingleValue( dataCache:put( keys[ i ], data )
			:map( tostring ),
			tostring( data ) )

	end
	
	rxTest:expectSingleValue( dataCache:removeBulk( keys ), keyCount )
		
	for i = 1, keyCount do
		rxTest:expectSingleValue( dataCache:get( keys[ i ] ),
			nil )
	end

	rxTest:expectSingleValue( dataCache:haltAndDelete(), true )
	
end


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

function KSDataCacheTest.rxTestRxRemoveAll( rxTest )

	local dataCache = KSDataCache( 'rxTestRxRemoveAll' )

	local data = KSData.make( 'random_bytes_with_low_entropy_rxTestRxRemoveAll' )
	
	local dataSHA = data:digest( 'SHA256' )
	
	rxTest:expectSingleValue( dataCache:put( dataSHA, data )
		:map( tostring ),
		tostring( data ) )

	rxTest:expectSingleValue( dataCache:get( dataSHA )
		:map( tostring ),
		tostring( data ) )
	
	rxTest:expectOnlyCompleted( dataCache:removeAll() )
	
	rxTest:expectSingleValue( dataCache:get( dataSHA ),
		nil )

	rxTest:expectSingleValue( dataCache:haltAndDelete(), true )
	
end

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

function KSDataCacheTest.testGetEmpty( test )

	local dataCache = KSDataCache( 'testGetEmpty' )

	local data = KSData.make( 'random_bytes_with_low_entropy' )

	local dataSHA = data:digest( 'SHA256' ) 

	WFAsyncTestCase( test, 10 )
	
		:waitFor( dataCache:getP( dataSHA )  )
		:continueWhenValueMatches( 'data', nil )
		:waitFor( function()
			return dataCache:haltAndDeleteP() 
		end )
		:continueWhenValueMatches( 'data', true )
		
end

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

function KSDataCacheTest.testPutGet( test )

	local dataCache = KSDataCache( 'testPutGet' )

	local data = KSData.make( 'this_is_a_test_data_string' )
	
	local dataSHA = data:digest( 'SHA256' )
	
	-- KSData does not implement equality, so we have to 
	-- to convert to strings

	WFAsyncTestCase( test, 10 )

		:waitFor( function()
			return dataCache:putP( dataSHA, data )
				:mapP(function ( status, d )
					return status, tostring( d )
				end )
		end )
		:continueWhenValueMatches( 'data', tostring( data ) )
		:waitFor( function ()
			return dataCache:haltAndDeleteP() 
		end )
		:continueWhenValueMatches( 'data', true )
	
end

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

function KSDataCacheTest.testEnumerate( test )

	local dataCache = KSDataCache( 'testEnumerate' )

	local d1 = KSData.make( 'testEnumerate_1' )
	local d1SHA = d1:digest( 'SHA256' )

	local d2 = KSData.make( 'testEnumerate_2' )
	local d2SHA = d2:digest( 'SHA256' )

	WFAsyncTestCase( test, 10 )

		:waitFor( dataCache:putP( d1SHA, d1 ) )
		:continueWhenValueMatches( 'data', ':ignore:' )
		:waitFor( dataCache:putP( d2SHA, d2 ) )
		:continueWhenValueMatches( 'data', ':ignore:' )
		:waitFor( dataCache:enumerateKeysP() )
		:whenResolvedDo(function( status, keys )
			
			-- set comparison by hand
			
			local expected = { [ d1SHA ]=true, [ d2SHA ]=true}
			
			for i, key in ipairs(keys) do
				if expected[key] == nil then
					test:notifyTestDidFail( key .. " not found in " .. tostring( expected ) )
				end
				
				if #keys ~= 2 then
					test:notifyTestDidFail( ' expetcted 2 keys, got ' .. tostring( #keys ) )
				end
			end
			
		end )
		:waitFor( dataCache:haltAndDeleteP() )
		:continueWhenValueMatches( 'data', true )
	
end

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

local WFPlatformInfo = require 'WFPlatformInfo'

if WFPlatformInfo.isMobile() then
	return WFRxTestCase.wrapSuite( 'KSDataCacheTest', KSDataCacheTest )
else
	return {} 
		-- Desktop does not use KSDataCache, and the KSDataCache test cases
		-- cause Mac permissions issues on the build machines. 
end
