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

ADOBE CONFIDENTIAL
------------------
Copyright 2015 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.rxlua
local WFRxTestCase = require 'WFRxTestCase'

	-- WF.core.lua
local KSData = require 'KSData'

local KSTableUtils = require "KSTableUtils"


	-- WF.core.utils
local WFTableMerge = require 'WFTableMerge'

	-- WF.core.os
local WFFileUtils = require 'WFFileUtils'

	-- Lrmobile_models
local LrmCatalogTestHelper = require 'LrmCatalogTestHelper'	
local LrmDocValueTestFactory = require 'LrmDocValueTestFactory'
local LrmCatAsset = require 'LrmCatAsset'


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

local LrmCatAssetTest = {}

local catalog

local TEST_TIMEOUT = 10

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

function LrmCatAssetTest.setUp()

	catalog = LrmCatalogTestHelper.makeCatalogService()
	
end

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

function LrmCatAssetTest.tearDown()

	if catalog then
		
		catalog:close()
		
	end
	
end

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

function LrmCatAssetTest.rxTestAssetDeletion( rxTest )

	rxTest:setTimeLimit( TEST_TIMEOUT )

	local assetIds = {}

	rxTest:expectSingleValue( LrmDocValueTestFactory.insertAssets( catalog:docStore(), 200 ), ':ignore:' )

	rxTest:expectSingleValue( catalog:indexStore():queryOnce{ 
			sql = 'SELECT docId from coreInfo', 
		}
		:map( function( rows )
			assetIds = KSTableUtils.flatten( rows )
			
			return assetIds
		end ), ':ignore:' )

	rxTest:expectSingleValue( LrmCatAsset.deleteAssets( catalog, assetIds ), ':ignore:' )

	rxTest:expectSingleValue( catalog:indexStore():queryOnce{ 
			sql = 'SELECT docId from coreInfo', 
			quiesceUntilUpToDate = true 
		}, 
	{} )

end

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

function LrmCatAssetTest.rxTestDeleteAssetWithBinaries( rxTest )

	rxTest:setTimeLimit( TEST_TIMEOUT )

	local docValue = LrmDocValueTestFactory.makeCompleteAsset()
	
	local originalFilePath = LrmCatalogTestHelper.makeTestFilePath()
	local latestOriginalFilePath = LrmCatalogTestHelper.makeTestFilePath()
	local proxyFilePath = LrmCatalogTestHelper.makeTestFilePath()
	
	local function writeTestData( path, stringData )
	
		local data = KSData.make( stringData )
		
		path = KSData.saveToUniquelyNamedFile( data, path )
		
		data:close()
		
		return path
		
	end
	
	originalFilePath = writeTestData( originalFilePath, 'original data' )
	proxyFilePath = writeTestData( proxyFilePath, 'proxy data' )
	latestOriginalFilePath = writeTestData( latestOriginalFilePath, 'latest original data' )
	
	WFTableMerge.applyOverlay( docValue, {
		_localOnly = {
			originalFilePath = originalFilePath,
			proxy = {
				filePath = proxyFilePath,
			},
			latestOriginal = {
				filePath = latestOriginalFilePath,
			}
		}
	} )
	
	rxTest:expectSingleValue( catalog:docStore():applyOverlay( docValue ), ':ignore:' )
	
	rxTest:expectSingleValue( LrmCatAsset.deleteAssets( catalog, { docValue._id } ), ':ignore:' )
	
	rxTest:expectSingleValue( catalog:indexStore():queryOnce{ sql = 'SELECT docId from coreInfo' }, {} )

	rxTest:expectEqual( false, WFFileUtils.exists( originalFilePath ) )

	rxTest:expectEqual( false, WFFileUtils.exists( proxyFilePath ) )

end

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

return WFRxTestCase.wrapSuite(  LrmCatAssetTest )

