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

WFCatDeniedListHelperTest.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.self_test
local WFRxTestCase = require 'WFRxTestCase'
local WFCatMockCatalog = require 'WFCatMockCatalog'

	-- WF.oz_client.managed_catalog
local WFCatDeniedListHelper = require 'WFCatDeniedListHelper'


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

local WFCatDeniedListHelperTest = {}

	-- The deniedList helper is implemented using the WFRxLowFidelityTimer, so we
	-- should always wait at least this amount for an event to occur.
local timerFidelity = 3

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

local function addSingleAsset( rxTest, docStore, isDenied )

	local counter = string.format( '%032x', 1 )

	local localOnly

	if isDenied then
	
		localOnly = {
			deniedBy = {
				bogusDeniedList = true,
			},
		}
	
	end

	rxTest:expectSingleValue(

		docStore:setDocValue {
			type = 'asset',
			subtype = 'image',
			_id = counter,
			_localOnly = localOnly,
			captureDate = '1995-05-27T14:00:00.00',
		},

		':ignore:' )

	return counter

end

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

function WFCatDeniedListHelperTest.rxTestBasic( rxTest )

	local wfCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest )
	local docStore = wfCatalog:docStore()
	
	local deniedListHelper = WFCatDeniedListHelper( docStore, 'rxTestBasic' )

	deniedListHelper:close()

end

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

function WFCatDeniedListHelperTest.rxTestDeniedListSimple( rxTest )

	rxTest:setTimeLimit( 10 )

	local wfCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest )
	local docStore = wfCatalog:docStore()

	local deniedListAfterNFailures = 2
	local deniedListTimeout = 0.25

	local deniedListHelper = WFCatDeniedListHelper( docStore, 'rxTestDeniedListSimple', deniedListAfterNFailures, deniedListTimeout )

	local id = 1

	rxTest:expectEqual( false, deniedListHelper:maybeDeny( id ) )
	rxTest:expectEqual( true, deniedListHelper:maybeDeny( id ) )
	rxTest:expectEqual( true, deniedListHelper:isDenied( id ) )
	
	rxTest:sleep( deniedListTimeout * 2 + timerFidelity )
	
	rxTest:expectEqual( false, deniedListHelper:isDenied( id ) )
	
end

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

function WFCatDeniedListHelperTest.rxTestDeniedListWithAnnotations( rxTest )

	rxTest:setTimeLimit( 10 )

	local wfCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest )
	local docStore = wfCatalog:docStore()

	local id = addSingleAsset( rxTest, docStore )

	local deniedListAfterNFailures = 2
	local deniedListTimeout = 0.25

	local deniedListHelper = WFCatDeniedListHelper( docStore, 'rxTestDeniedListWithAnnotations', deniedListAfterNFailures, deniedListTimeout, true )

	rxTest:expectEqual( false, deniedListHelper:maybeDeny( id ) )
	rxTest:expectEqual( true, deniedListHelper:maybeDeny( id ) )
	rxTest:expectEqual( true, deniedListHelper:isDenied( id ) )
	
	local docValue = docStore:docValue( id )
		
		:pluck( '_localOnly', 'deniedBy' )
		
	rxTest:expectSingleValue( docValue, { rxTestDeniedListWithAnnotations = true } )
	
	rxTest:sleep( deniedListTimeout * 2 + timerFidelity )
	
	rxTest:expectEqual( false, deniedListHelper:isDenied( id ) )
	
	docValue = docStore:docValue( id )
		
		:pluck( '_localOnly', 'deniedBy' )
		
	rxTest:expectSingleValue( docValue, {} )
	
end

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

function WFCatDeniedListHelperTest.rxTestAlreadyDenied( rxTest )

	rxTest:setTimeLimit( 10 )

	local wfCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest )
	local docStore = wfCatalog:docStore()

	local deniedListAfterNFailures = 2
	local deniedListTimeout = 0.25

	local deniedListHelper = WFCatDeniedListHelper( docStore, 'rxTestAlreadyDenied', deniedListAfterNFailures, deniedListTimeout )

	local id = 1

	rxTest:expectEqual( false, deniedListHelper:maybeDeny( id ) )
	rxTest:expectEqual( true, deniedListHelper:maybeDeny( id ) )
	rxTest:expectEqual( true, deniedListHelper:maybeDeny( id ) )
	rxTest:expectEqual( true, deniedListHelper:maybeDeny( id ) )
	rxTest:expectEqual( true, deniedListHelper:isDenied( id ) )
	
	rxTest:sleep( deniedListTimeout * 2 + timerFidelity)
	
	rxTest:expectEqual( false, deniedListHelper:isDenied( id ) )
	
end

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

function WFCatDeniedListHelperTest.rxTestBogusDeniedListAnnotation( rxTest )

	rxTest:setTimeLimit( 10 )

	local wfCatalog = WFCatMockCatalog.createFakeWFCatalog( rxTest )
	local docStore = wfCatalog:docStore()

	local id = addSingleAsset( rxTest, docStore, true )
	
	local actualDocValue
	
	local docValue = docStore:docValue( id )

		:tap( function( docValue )
		
			actualDocValue = docValue
		
		end )

	rxTest:expectSingleValue( docValue:pluck( '_localOnly', 'deniedBy' ), { bogusDeniedList = true } )

	local deniedListHelper = WFCatDeniedListHelper( docStore, 'bogusDeniedList', 1, 10, true )

	rxTest:expectEqual( false, deniedListHelper:maybeResetDeniedListStatus( id ) )

	rxTest:sleep( 0.5 )

	-- The bogus annotation should have been removed
	
	docValue = docStore:docValue( id )
		
		:pluck( '_localOnly', 'deniedBy' )
		
	rxTest:expectSingleValue( docValue, {} )
	
end

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

return WFRxTestCase.wrapSuite( WFCatDeniedListHelperTest )
