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

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'

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

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

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

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


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

local LrmCatAssetTest = {}

local TEST_TIMEOUT = 10

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

local function createCatalogService( rxTest )

	local catalogService, wfCatalog = LrmCatalogTestHelper.makeCatalogService( rxTest )
	
	rxTest:finally( function()
		catalogService:close()
	end )
	
	return catalogService, wfCatalog

end

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

function LrmCatAssetTest.rxTestAssetDeletion( rxTest )

	rxTest:setTimeLimit( TEST_TIMEOUT )
	
	local catalogService, wfCatalog = createCatalogService( rxTest )

	local assetIds = rxTest:getFirstValue( WFCatAssetTestUtils.insertAssets( rxTest, wfCatalog, 200 ) )

	local obs = rxTest:observe( LrmCatAsset.deleteAssets( catalogService, assetIds ) )
	obs:expectCompletedWithinTime( 5 )
	obs:dispose()

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

end

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

function LrmCatAssetTest.rxTestDeleteAssetWithBinaries( rxTest )

	rxTest:setTimeLimit( TEST_TIMEOUT )

	local catalogService, wfCatalog = createCatalogService( rxTest )
	
	local docValue = WFCatAssetTestUtils.createAssetDocument( rxTest, wfCatalog )
	
	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:waitFor( wfCatalog:docStore():applyOverlay( docValue ) )
	
	rxTest:waitFor( LrmCatAsset.deleteAssets( catalogService, { docValue._id } ) )
	
	rxTest:expectSingleValue( wfCatalog:indexStore():queryOnce{ sql = 'SELECT docId from coreInfo' }, {} )

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

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

end

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

return WFRxTestCase.wrapSuite( 'LrmCatAssetTest',  LrmCatAssetTest )

