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

WFRxBatchedBulkApplyOverlayTest.lua

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

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

NOTICE: All information contained herein is, and remains 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 are protected by all applicable
intellectual property laws, including trade secret and copyright laws.
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'

	-- WF.core.utils
local WFRxBatchedBulkApplyOverlay = require 'WFRxBatchedBulkApplyOverlay'
local KSIdGenerator = require 'KSIdGenerator'

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


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

local createFakeWFCatalog = WFCatMockCatalog.createFakeWFCatalog


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

local WFRxBatchedBulkApplyOverlayTest = {}

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

local function batchedBulkHelper( rxTest, docStore )

	local assetIds = {}
	local bulkOverlay = {}
	
	for i=1,100 do

		local assetId = KSIdGenerator.uuidInLowercaseHex()

		rxTest:expectSingleValue(

			docStore:setDocValue {
					_id = assetId,
					type = 'asset',
					subtype = 'jpeg',
					_localOnly = {
						batchTest = true,
					}
				},

			':ignore:' )

		assetIds[ i ] = assetId

		bulkOverlay[ i ] = {
			_id = assetId,
			_localOnly = {
				batchTest = false,
			}
		}

	end

	return assetIds, bulkOverlay
	
end

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

function WFRxBatchedBulkApplyOverlayTest.rxTestBasic( rxTest )

	rxTest:setTimeLimit( 10 )

	local fakeWFCatalog = createFakeWFCatalog( rxTest )

	local docStore = fakeWFCatalog:docStore()

	local assetIds, bulkOverlay = batchedBulkHelper( rxTest, docStore )
	
	-- Check that the local only annotation is present
	
	for _, assetId in ipairs( assetIds ) do
	
		local batchTest = docStore:docValue( assetId )
			:pluck( "_localOnly", "batchTest" )
		
		rxTest:expectSingleValue( batchTest, true )
		
	end
	
	rxTest:expectSingleValue( WFRxBatchedBulkApplyOverlay( "rxTestBasic", docStore, bulkOverlay, 3 ), true )
	
	-- Check that the local only annotation is gone
	
	for _, assetId in ipairs( assetIds ) do
	
		local batchTest = docStore:docValue( assetId )
			:pluck( "_localOnly", "batchTest" )
		
		rxTest:expectSingleValue( batchTest, false )
		
	end
	
end

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

function WFRxBatchedBulkApplyOverlayTest.rxTestOverlayWithLabel( rxTest )

	rxTest:setTimeLimit( 10 )

	local fakeWFCatalog = createFakeWFCatalog( rxTest )

	local docStore = fakeWFCatalog:docStore()

	local assetIds, bulkOverlay = batchedBulkHelper( rxTest, docStore )
	
	-- Check that the label doesn't exist.
	
	for _, assetId in ipairs( assetIds ) do
	
		local batchTest = docStore:docValue( assetId )
			:pluck( "_labels" )
		
		rxTest:expectSingleValue( batchTest, nil )
		
	end
	
	bulkOverlay.label = 'test'
	
	rxTest:expectSingleValue( WFRxBatchedBulkApplyOverlay( "rxTestOverlayWithLabel", docStore, bulkOverlay, 3 ), true )
	
	-- Check that the label is present on all assets.
	
	for _, assetId in ipairs( assetIds ) do
	
		local batchTest = docStore:docValue( assetId )
			:pluck( "_labels" )
		
		rxTest:expectSingleValue( batchTest, { [ 'test' ] = true } )
		
	end
	
end

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

return WFRxTestCase.wrapSuite( 'WFRxBatchedBulkApplyOverlayTest', WFRxBatchedBulkApplyOverlayTest )
