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

WFCatExportGenericProviderTest.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2017 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.

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

	-- If ACR is not supported, disable test before we attempt to
	-- require WFAcr. (That will fail.)
if not require 'WFPlatformInfo'.hasACR then return {} end

--------------------------------------------------------------------------------
	-- RxLua
local Rx = require 'Rx'
local Observable = Rx.Observable

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

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

	-- WF.core.util
local KSTableUtils = require 'KSTableUtils'

	-- WF.imaging
local WFXmp = require 'WFXmp'

	-- WF.oz_client.managed_catalog
local WFCatImport = require 'WFCatImport'
local WFCatImportReadMetadataTask = require 'WFCatImportReadMetadataTask'
local WFCatExportGenericProvider = require 'WFCatExportGenericProvider'
local WFCatExportPixelProviderTask = require 'WFCatExportPixelProviderTask'
local WFCatMockCatalog = require 'WFCatMockCatalog'
local MockPixelProviderProcessor = require 'MockPixelProviderProcessor'
local WFCatFileSpec = require 'WFCatFileSpec'

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

local WFCrContext = assert( require 'NmbAcr'.WFCrContext )

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

local createFakeWFCatalog = WFCatMockCatalog.createFakeWFCatalog
local createPixelProcessorStream = MockPixelProviderProcessor.createPixelProcessorStream

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

local testDataPath = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'testdata' ),
										"import-samples" )

local testDataFolder1 = KSPathUtils.child( testDataPath, "folder-1" )

local _formatsSupported = {
	{
		format = 'jpeg',
		extension = 'jpg',
		dcFormat = 'image/jpeg',
	},
	{
		format = 'tiff',
		extension = 'tif',
		dcFormat = 'image/tiff',
	},
	{
		format = 'dng',
		extension = 'dng',
		dcFormat = 'image/dng',
	}
	
}

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

local WFCatExportGenericProviderTest = {}

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

local function sanitizeError( err )
	return err and err:match( '[_%w]+$' )
end

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

local function importSingleFile( rxTest, testFilePath )

	local wfCatalog = createFakeWFCatalog( rxTest )

	local assetId

	rxTest:expectSingleValue(
		WFCatImport.addImageArrayToCatalog( wfCatalog, nil, { {
			filePath = testFilePath,
			width = 2048,
			height = 1024,
			xmpCameraRaw = '<xmp />',
			fileName = KSPathUtils.leafName( testFilePath ),
			fileSize = WFFileUtils.fileAttributes( testFilePath ).fileSize,
			} } )
			:tap( function( result )
				assetId = result.localId
			end ),
		":ignore:" )

	assert( assetId, "Docstore didn't fire doc change. Maybe import didnt succeed." )

	return wfCatalog, assetId
end

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

local function exportSingleFileWithValidation( rxTest, wfCatalog, assetId, exportSettings, exportFileName, maybeOtherRequestParams )
	
	local exportRequest = {
		id = assetId,
		settings = exportSettings,
		fileName = exportFileName,
	}
	
	if maybeOtherRequestParams then
		for k, v in pairs( maybeOtherRequestParams ) do
			exportRequest[ k ] = v
		end
	end
	
	local exportResult, err
	
	local pixelPathProcessorStream = createPixelProcessorStream( rxTest, wfCatalog )
	local exportPixelProviderTask = WFCatExportPixelProviderTask( pixelPathProcessorStream )
	rxTest:expectSingleValue(
		WFCatExportGenericProvider:exportAsset( exportRequest, wfCatalog, exportPixelProviderTask )
			:tap( function( data ) 
				exportResult = data
			end )
			:catch( function( errorString ) 
				err = sanitizeError( errorString )
				return Observable.just( {} )
			end),
		':ignore:' )
	
	exportPixelProviderTask:dispose()
		
	return exportResult, err
end

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

local function getMetadatFromFileWithValidation( rxTest, filePath )
	local result

	rxTest:expectSingleValue(
		WFCatImportReadMetadataTask.readMetadataForFileAtPath( filePath )
			:tap( function( value )
				result = value
			end ),
		':ignore:' )

	rxTest:expectEqual( 'table', type( result ) )
	return result
end

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

function WFCatExportGenericProviderTest.rxTestExportMetadataAll( rxTest )

	rxTest:setTimeLimit( 12 )
	
	local exportPath = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'temp' ), rxTest:testName() )
	pcall ( WFFileUtils.mkdir, exportPath )

	rxTest:finally( function()
		WFFileUtils.delete( exportPath )
	end )
	
	local testFilePath = KSPathUtils.child( testDataFolder1, "ir-trees.dng" )
	local wfCatalog, assetId = importSingleFile( rxTest, testFilePath )

	local setMockReadMetadataResults = WFCrContext.setMockReadMetadataResults
	if setMockReadMetadataResults then
		setMockReadMetadataResults( 'jpg', { '50' } )
		setMockReadMetadataResults( 'tif', { '50' } )
		setMockReadMetadataResults( 'dng', { '50' } )
	end
	
	for i, formatInfo in ipairs( _formatsSupported ) do
		
		local settings = {
			format = formatInfo.format,
			destinationFolder = exportPath,
			embeddedMetadataOption = 'all',
		}
		
		local exportResult = exportSingleFileWithValidation( rxTest, wfCatalog, assetId, settings )
		
		rxTest:expectEqual( 'table', type( exportResult ) )
		
		local outFilePath = exportResult.path
		rxTest:expectEqual( outFilePath, KSPathUtils.child( settings.destinationFolder, "ir-trees."..formatInfo.extension ) )
		
		rxTest:expectEqual( 'file', WFFileUtils.exists( outFilePath ) )
		
		local result = getMetadatFromFileWithValidation( rxTest, outFilePath )
		
		local xmp = WFXmp.WFXmp()
		xmp:parse( result.metadata )

		rxTest:expectEqual( formatInfo.dcFormat, xmp:get(  WFXMLNS.dc, 'format' ) )
		rxTest:expectEqual( 'string', type( xmp:get( WFXMLNS.dc, 'rights' ) ) )
		
		local speedRatingList = xmp:getStringList( WFXMLNS.exif, 'ISOSpeedRatings' )
		rxTest:expectEqual( 'table', type( speedRatingList ) )
		rxTest:expectEqual( 1, #speedRatingList )
		rxTest:expectEqual( '50', speedRatingList[ 1 ]  )
	end
	
end

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

function WFCatExportGenericProviderTest.rxTestExportMetadataCopyrightOnly( rxTest )

	rxTest:setTimeLimit( 10 )
	
	local exportPath = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'temp' ), rxTest:testName() )
	pcall ( WFFileUtils.mkdir, exportPath )

	rxTest:finally( function()
		WFFileUtils.delete( exportPath )
	end )
	
	local testFilePath = KSPathUtils.child( testDataFolder1, "ir-trees.dng" )
	local wfCatalog, assetId = importSingleFile( rxTest, testFilePath )
	
	for i, formatInfo in ipairs( _formatsSupported ) do
		
		local settings = {
			format = formatInfo.format,
			destinationFolder = exportPath,
			embeddedMetadataOption = 'copyrightOnly',
		}
		
		local exportResult = exportSingleFileWithValidation( rxTest, wfCatalog, assetId, settings )
		
		rxTest:expectEqual( 'table', type( exportResult ) )
		
		local outFilePath = exportResult.path
		rxTest:expectEqual( outFilePath, KSPathUtils.child( settings.destinationFolder, "ir-trees."..formatInfo.extension ) )
		
		
		rxTest:expectEqual( 'file', WFFileUtils.exists( outFilePath ) )
		
		
		local result = getMetadatFromFileWithValidation( rxTest, outFilePath )

		local xmp = WFXmp.WFXmp()
		xmp:parse( result.metadata )

		rxTest:expectEqual( formatInfo.dcFormat, xmp:get( WFXMLNS.dc, 'format' ) )
		rxTest:expectEqual( 'string', type( xmp:get( WFXMLNS.dc, 'rights' ) ) )
		
		-- because dng always exports with full metadata
		if formatInfo.format == "dng" then
			local speedRatingList = xmp:getStringList( WFXMLNS.exif, 'ISOSpeedRatings' )
			rxTest:expectEqual( 'table', type( speedRatingList ) )
			rxTest:expectEqual( 1, #speedRatingList )
			rxTest:expectEqual( '50', speedRatingList[ 1 ]  )
		else
			rxTest:expectEqual( nil, xmp:getStringList( WFXMLNS.exif, 'ISOSpeedRatings' ) )
		end
	end
	
end

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

function WFCatExportGenericProviderTest.rxTestExportMetadataLongEdge( rxTest )

	rxTest:setTimeLimit( 10 )
	
	local exportPath = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'temp' ), rxTest:testName() )
	pcall ( WFFileUtils.mkdir, exportPath )

	rxTest:finally( function()
		WFFileUtils.delete( exportPath )
	end )
	
	local testFilePath = KSPathUtils.child( testDataFolder1, "ir-trees.dng" )
	local wfCatalog, assetId = importSingleFile( rxTest, testFilePath )
	
	for i, formatInfo in ipairs( _formatsSupported ) do
		
		local settings = {
			format = formatInfo.format,
			destinationFolder = exportPath,
			dimensionType = 200,
		}
	
		local exportResult = exportSingleFileWithValidation( rxTest, wfCatalog, assetId, settings )
		
		
		rxTest:expectEqual( 'table', type( exportResult ) )
		
		local outFilePath = exportResult.path
		rxTest:expectEqual( outFilePath, KSPathUtils.child( settings.destinationFolder, "ir-trees."..formatInfo.extension ) )
		
		
		rxTest:expectEqual( 'file', WFFileUtils.exists( outFilePath ) )
		
		local result = getMetadatFromFileWithValidation( rxTest, outFilePath )

		local xmp = WFXmp.WFXmp()
		xmp:parse( result.metadata )

		rxTest:expectEqual( formatInfo.dcFormat, xmp:get( WFXMLNS.dc, 'format' ) )
		
		-- because dng always exports as full size
		if formatInfo.format == "dng" then
			rxTest:expectEqual( '500', xmp:get( WFXMLNS.exif, 'PixelXDimension' ) )
		else
			rxTest:expectEqual( '200', xmp:get( WFXMLNS.exif, 'PixelXDimension' ) )
		end
	end
	
end

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

function WFCatExportGenericProviderTest.rxTestExportMetadataLongEdgeFull( rxTest )

	rxTest:setTimeLimit( 10 )
	
	local exportPath = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'temp' ), rxTest:testName() )
	pcall ( WFFileUtils.mkdir, exportPath )

	rxTest:finally( function()
		WFFileUtils.delete( exportPath )
	end )

	local setMockReadMetadataResults = WFCrContext.setMockReadMetadataResults
	if setMockReadMetadataResults then
		setMockReadMetadataResults( 'jpg', { '50' }, 500, 334 )
		setMockReadMetadataResults( 'tif', { '50' }, 500, 334 )
		setMockReadMetadataResults( 'dng', { '50' }, 500, 334 )
	end

	local testFilePath = KSPathUtils.child( testDataFolder1, "ir-trees.dng" )
	local wfCatalog, assetId = importSingleFile( rxTest, testFilePath )
	
	for i, formatInfo in ipairs( _formatsSupported ) do
	
		local settings = {
			format = formatInfo.format,
			destinationFolder = exportPath,
			dimensionType = 'full',
		}
	
		local exportResult = exportSingleFileWithValidation( rxTest, wfCatalog, assetId, settings )
		
		
		rxTest:expectEqual( 'table', type( exportResult ) )
		
		local outFilePath = exportResult.path
		rxTest:expectEqual( outFilePath, KSPathUtils.child( settings.destinationFolder, "ir-trees."..formatInfo.extension ) )
		
		
		rxTest:expectEqual( 'file', WFFileUtils.exists( outFilePath ) )
		
		local result = getMetadatFromFileWithValidation( rxTest, outFilePath )

		local xmp = WFXmp.WFXmp()
		xmp:parse( result.metadata )

		rxTest:expectEqual( formatInfo.dcFormat, xmp:get( WFXMLNS.dc, 'format' ) )
		rxTest:expectEqual( '500', xmp:get( WFXMLNS.exif, 'PixelXDimension' ) )
		rxTest:expectEqual( '334', xmp:get( WFXMLNS.exif, 'PixelYDimension' ) )
	end
end

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

function WFCatExportGenericProviderTest.rxTestExportMetadataLongEdgeCustomMaxWidth( rxTest )

	rxTest:setTimeLimit( 10 )
	
	local exportPath = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'temp' ), rxTest:testName() )
	pcall ( WFFileUtils.mkdir, exportPath )

	rxTest:finally( function()
		WFFileUtils.delete( exportPath )
	end )
	
	local testFilePath = KSPathUtils.child( testDataFolder1, "ir-trees.dng" )
	local wfCatalog, assetId = importSingleFile( rxTest, testFilePath )
	
	for i, formatInfo in ipairs( _formatsSupported ) do
	
		local settings = {
			format = formatInfo.format,
			destinationFolder = exportPath,
			dimensionType = 'custom',
			customMaxSize = 200,
		}
		local exportResult = exportSingleFileWithValidation( rxTest, wfCatalog, assetId, settings )
		
		rxTest:expectEqual( 'table', type( exportResult ) )
		
		local outFilePath = exportResult.path
		rxTest:expectEqual( outFilePath, KSPathUtils.child( settings.destinationFolder, "ir-trees."..formatInfo.extension ) )
		
		
		rxTest:expectEqual( 'file', WFFileUtils.exists( outFilePath ) )
		
		local result = getMetadatFromFileWithValidation( rxTest, outFilePath )

		local xmp = WFXmp.WFXmp()
		xmp:parse( result.metadata )

		rxTest:expectEqual( formatInfo.dcFormat, xmp:get( WFXMLNS.dc, 'format' ) )
		
		-- because dng always exports as full size
		if formatInfo.format == "dng" then
			rxTest:expectEqual( '500', xmp:get( WFXMLNS.exif, 'PixelXDimension' ) )
		else
			rxTest:expectEqual( '200', xmp:get( WFXMLNS.exif, 'PixelXDimension' ) )
		end
	end
end
--------------------------------------------------------------------------------

function WFCatExportGenericProviderTest.rxTestExportMetadataLongEdgeCustomMaxHeight( rxTest )

	rxTest:setTimeLimit( 10 )
	
	local exportPath = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'temp' ), rxTest:testName() )
	pcall ( WFFileUtils.mkdir, exportPath )

	rxTest:finally( function()
		WFFileUtils.delete( exportPath )
	end )
	
	local testFilePath = KSPathUtils.child( testDataFolder1, "ir-trees.dng" )
	local wfCatalog, assetId = importSingleFile( rxTest, testFilePath )
	
	for i, formatInfo in ipairs( _formatsSupported ) do
	
		local settings = {
			format = formatInfo.format,
			destinationFolder = exportPath,
			dimensionType = 'custom',
			customMaxSize = 200,
		}
		
		local exportResult = exportSingleFileWithValidation( rxTest, wfCatalog, assetId, settings )
		
		
		rxTest:expectEqual( 'table', type( exportResult ) )
		
		local outFilePath = exportResult.path
		rxTest:expectEqual( outFilePath, KSPathUtils.child( settings.destinationFolder, "ir-trees."..formatInfo.extension ) )
		
		
		rxTest:expectEqual( 'file', WFFileUtils.exists( outFilePath ) )
		
		local result = getMetadatFromFileWithValidation( rxTest, outFilePath )

		local xmp = WFXmp.WFXmp()
		xmp:parse( result.metadata )

		rxTest:expectEqual( formatInfo.dcFormat, xmp:get( WFXMLNS.dc, 'format' ) )
		
		-- because dng always exports as full size
		if formatInfo.format == "dng" then
			rxTest:expectEqual( '334', xmp:get( WFXMLNS.exif, 'PixelYDimension' ) )
		else
			rxTest:expectEqual( '200', xmp:get( WFXMLNS.exif, 'PixelYDimension' ) )
		end
	end
	
end
--------------------------------------------------------------------------------

function WFCatExportGenericProviderTest.rxTestExportMetadataLongEdgeCustomMaxDimension( rxTest )

	rxTest:setTimeLimit( 10 )
	
	local exportPath = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'temp' ), rxTest:testName() )
	pcall ( WFFileUtils.mkdir, exportPath )

	rxTest:finally( function()
		WFFileUtils.delete( exportPath )
	end )
	
	local testFilePath = KSPathUtils.child( testDataFolder1, "ir-trees.dng" )
	local wfCatalog, assetId = importSingleFile( rxTest, testFilePath )
	
	for i, formatInfo in ipairs( _formatsSupported ) do
	
		local settings = {
			format = formatInfo.format,
			destinationFolder = exportPath,
			dimensionType = 'custom',
			customMaxSize = 200,
		}
		
		local exportResult = exportSingleFileWithValidation( rxTest, wfCatalog, assetId, settings )
		
		
		rxTest:expectEqual( 'table', type( exportResult ) )
		
		local outFilePath = exportResult.path
		rxTest:expectEqual( outFilePath, KSPathUtils.child( settings.destinationFolder, "ir-trees."..formatInfo.extension ) )
		
		
		rxTest:expectEqual( 'file', WFFileUtils.exists( outFilePath ) )
		
		local result = getMetadatFromFileWithValidation( rxTest, outFilePath )

		local xmp = WFXmp.WFXmp()
		xmp:parse( result.metadata )

		rxTest:expectEqual( formatInfo.dcFormat, xmp:get( WFXMLNS.dc, 'format' ) )
		
		-- because dng always exports as full size
		if formatInfo.format == "dng" then
			rxTest:expectEqual( '500', xmp:get( WFXMLNS.exif, 'PixelXDimension' ) )
		else
			rxTest:expectEqual( '200', xmp:get( WFXMLNS.exif, 'PixelXDimension' ) )
		end
	end
end
--------------------------------------------------------------------------------

function WFCatExportGenericProviderTest.rxTestExportMetadataCustomDimensionLongEdge( rxTest )

	rxTest:setTimeLimit( 10 )
	
	local exportPath = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'temp' ), rxTest:testName() )
	pcall ( WFFileUtils.mkdir, exportPath )

	rxTest:finally( function()
		WFFileUtils.delete( exportPath )
	end )
	
	local testFilePath = KSPathUtils.child( testDataFolder1, "ir-trees.dng" )
	local wfCatalog, assetId = importSingleFile( rxTest, testFilePath )
	
	for i, formatInfo in ipairs( _formatsSupported ) do
	
		local settings = {
			format = formatInfo.format,
			destinationFolder = exportPath,
			dimensionType = 'custom',
			resizeType = 'longEdge',
			customMaxSize = 200,
		}
		
		local exportResult = exportSingleFileWithValidation( rxTest, wfCatalog, assetId, settings )
		
		
		rxTest:expectEqual( 'table', type( exportResult ) )
		
		local outFilePath = exportResult.path
		rxTest:expectEqual( outFilePath, KSPathUtils.child( settings.destinationFolder, "ir-trees."..formatInfo.extension ) )
		
		
		rxTest:expectEqual( 'file', WFFileUtils.exists( outFilePath ) )
		
		local result = getMetadatFromFileWithValidation( rxTest, outFilePath )

		local xmp = WFXmp.WFXmp()
		xmp:parse( result.metadata )

		rxTest:expectEqual( formatInfo.dcFormat, xmp:get( WFXMLNS.dc, 'format' ) )
		
		-- because dng always exports as full size
		if formatInfo.format == "dng" then
			rxTest:expectEqual( '500', xmp:get( WFXMLNS.exif, 'PixelXDimension' ) )
		else
			rxTest:expectEqual( '200', xmp:get( WFXMLNS.exif, 'PixelXDimension' ) )
		end
	end
	
end

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

function WFCatExportGenericProviderTest.rxTestExportMetadataCustomDimensionShortEdge( rxTest )

	rxTest:setTimeLimit( 10 )
	
	local exportPath = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'temp' ), rxTest:testName() )
	pcall ( WFFileUtils.mkdir, exportPath )

	rxTest:finally( function()
		WFFileUtils.delete( exportPath )
	end )
	
	local testFilePath = KSPathUtils.child( testDataFolder1, "ir-trees.dng" )
	local wfCatalog, assetId = importSingleFile( rxTest, testFilePath )
	
	for i, formatInfo in ipairs( _formatsSupported ) do
	
		local settings = {
			format = formatInfo.format,
			destinationFolder = exportPath,
			dimensionType = 'custom',
			resizeType = 'shortEdge',
			customMaxSize = 200,
		}
		
		local exportResult = exportSingleFileWithValidation( rxTest, wfCatalog, assetId, settings )
		
		
		rxTest:expectEqual( 'table', type( exportResult ) )
		
		local outFilePath = exportResult.path
		rxTest:expectEqual( outFilePath, KSPathUtils.child( settings.destinationFolder, "ir-trees."..formatInfo.extension ) )
		
		
		rxTest:expectEqual( 'file', WFFileUtils.exists( outFilePath ) )
		
		local result = getMetadatFromFileWithValidation( rxTest, outFilePath )

		local xmp = WFXmp.WFXmp()
		xmp:parse( result.metadata )

		rxTest:expectEqual( formatInfo.dcFormat, xmp:get( WFXMLNS.dc, 'format' ) )
		
		-- because dng always exports as full size
		if formatInfo.format == "dng" then
			rxTest:expectEqual( '334', xmp:get( WFXMLNS.exif, 'PixelYDimension' ) )
		else
			rxTest:expectEqual( '200', xmp:get( WFXMLNS.exif, 'PixelYDimension' ) )
		end
	end
	
end
--------------------------------------------------------------------------------

function WFCatExportGenericProviderTest.rxTestExportMissingOriginal( rxTest )

	rxTest:setTimeLimit( 10 )
	
	local exportPath = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'temp' ), rxTest:testName() )
	pcall ( WFFileUtils.mkdir, exportPath )

	rxTest:finally( function()
		WFFileUtils.delete( exportPath )
	end )
	
	local testFilePath = KSPathUtils.child( testDataFolder1, "ir-trees.dng" )
	local wfCatalog, assetId = importSingleFile( rxTest, testFilePath )
	
	-- Delete the original link entry
	rxTest:expectSingleValue(
		wfCatalog:docStore():docValue( assetId )
			:flatMap( function( docValue )
			
				local volumeGuid = KSTableUtils.derefOpt( docValue, "_localOnly", "files", "original", "master", "volumeGuid" )
				
				local originalPath = WFCatFileSpec.buildOriginalPathFromDocValue( docValue )
				local localProxyPath = wfCatalog:fileSpec():pathForProxy( docValue._id, "2560" )
				WFFileUtils.copy( originalPath, localProxyPath )
				
				WFFileUtils.delete( originalPath )
				
				return wfCatalog:docStore():applyOverlay {
					_id = assetId,
					_localOnly = {
						files = {
							original = { _delete = true },
							proxy = {
								['2560'] = {
									relativePath = WFFileUtils.removeVolumeFromPath( localProxyPath ),
									volumeGuid = volumeGuid,
								},
							}
						}
					},
				}
				
			end ),
		':ignore:' )
	
	-- Do not use proxy for exporting( tryWithProxy = false )
	for i, formatInfo in ipairs( _formatsSupported ) do
	
		local settings = {
			format = formatInfo.format,
			destinationFolder = exportPath,
			dimensionType = 'custom',
			customMaxSize = 200,
			tryWithProxy = false,
		}
		
		local exportResult, err = exportSingleFileWithValidation( rxTest, wfCatalog, assetId, settings )
		
		rxTest:expectEqual( nil, exportResult )
		rxTest:expectEqual( 'original_unavailable', err )
	end
	
	-- Use proxy for exporting( tryWithProxy = true )
	-- Size is less than proxy size
	for i, formatInfo in ipairs( _formatsSupported ) do
	
		local settings = {
			format = formatInfo.format,
			destinationFolder = exportPath,
			dimensionType = 'custom',
			customMaxSize = 200,
			tryWithProxy = true,
		}
		
		local exportResult = exportSingleFileWithValidation( rxTest, wfCatalog, assetId, settings )
		
		rxTest:expectEqual( 'table', type( exportResult ) )
		
		local outFilePath = exportResult.path
		rxTest:expectEqual( 'file', WFFileUtils.exists( outFilePath ) )
	end
	
	-- Use proxy for exporting( tryWithProxy = true )
	-- Size is more than proxy size
	for i, formatInfo in ipairs( _formatsSupported ) do
	
		local settings = {
			format = formatInfo.format,
			destinationFolder = exportPath,
			dimensionType = 'custom',
			customMaxSize = 3000,
			tryWithProxy = true,
		}
		
		local exportResult, err = exportSingleFileWithValidation( rxTest, wfCatalog, assetId, settings )
		
		rxTest:expectEqual( nil, exportResult )
		rxTest:expectEqual( 'original_unavailable', err )
	end
	
	-- Use fallbackToProxySize
	for i, formatInfo in ipairs( _formatsSupported ) do
	
		local settings = {
			format = formatInfo.format,
			destinationFolder = exportPath,
			dimensionType = 'custom',
			customMaxSize = 3000,
			fallbackToProxySize = true,
		}
		
		local exportResult = exportSingleFileWithValidation( rxTest, wfCatalog, assetId, settings )
		
		rxTest:expectEqual( 'table', type( exportResult ) )
		
		local outFilePath = exportResult.path
		rxTest:expectEqual( 'file', WFFileUtils.exists( outFilePath ) )
	end
end
--------------------------------------------------------------------------------

function WFCatExportGenericProviderTest.rxTestExportCustomFilename( rxTest )

	rxTest:setTimeLimit( 10 )
	
	local exportPath = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'temp' ), rxTest:testName() )
	pcall ( WFFileUtils.mkdir, exportPath )

	rxTest:finally( function()
		WFFileUtils.delete( exportPath )
	end )
	
	local testFilePath = KSPathUtils.child( testDataFolder1, "ir-trees.dng" )
	local wfCatalog, assetId = importSingleFile( rxTest, testFilePath )
	
	for i, formatInfo in ipairs( _formatsSupported ) do
		
		local settings = {
			format = formatInfo.format,
			destinationFolder = exportPath,
			dimensionType = 200,
		}
		
		local exportFileName = "customFileName.ext"
		
		local exportResult = exportSingleFileWithValidation( rxTest, wfCatalog, assetId, settings, exportFileName )

		rxTest:expectEqual( 'table', type( exportResult ) )
		
		local outFilePath = exportResult.path
		rxTest:expectEqual( outFilePath, KSPathUtils.child( settings.destinationFolder, "customFileName."..formatInfo.extension ) )
		
		rxTest:expectEqual( 'file', WFFileUtils.exists( outFilePath ) )
		
	end
	
end

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

function WFCatExportGenericProviderTest.rxTestExportFromRendition( rxTest )

	rxTest:setTimeLimit( 10 )
	
	local exportPath = KSPathUtils.child( KSPathUtils.getStandardFilePath( 'temp' ), rxTest:testName() )
	pcall ( WFFileUtils.mkdir, exportPath )

	rxTest:finally( function()
		WFFileUtils.delete( exportPath )
	end )
	
	local testFilePath = KSPathUtils.child( testDataFolder1, "ir-trees.dng" )
	local wfCatalog, assetId = importSingleFile( rxTest, testFilePath )
	
	local renditionPath = KSPathUtils.child( testDataFolder1, "IMG_7763.JPG" )
	
	local settings = {
		format = "jpeg",
		destinationFolder = exportPath,
		applyXmp = false,
		fallbackToProxySize = false,
	}
	
	local requestParams = {
		sourceImagePath = renditionPath,
		tryWithProxy = false,
	}
	
	local exportResult = exportSingleFileWithValidation( rxTest, wfCatalog, assetId, settings, nil, requestParams )
	
	
	rxTest:expectEqual( 'table', type( exportResult ) )
	
	local outFilePath = exportResult.path
	rxTest:expectEqual( outFilePath, KSPathUtils.child( settings.destinationFolder, "ir-trees.jpg" ) )
	
	rxTest:expectEqual( 'file', WFFileUtils.exists( outFilePath ) )
	
end

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

local WFPlatformInfo = require 'WFPlatformInfo'
local KSDate = require 'KSDate'

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

local function skipAllTestsUntilDateIf( untilDate, os, target, testSuiteName, testSuite )

	local untilDateNumeric =
			KSDate.iso8601ToTimeIntervalSinceReference( untilDate )

	if KSDate.timeNow() > untilDateNumeric or
		WFPlatformInfo.getOS() ~= os or
		WFPlatformInfo.getBuildTarget() ~= target then

		return testSuite

	end

	return {
		
		rxTestSkipAllTests = 
			function( rxTest )

				rxTest:logf( "Skipping all test suite tests in '%s' until %s for %s.%s", 
								testSuiteName, untilDate, os, target )

			end

	}

end

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

WFCatExportGenericProviderTest =
		skipAllTestsUntilDateIf( "2020-02-12T00:00:00", "Windows", "Debug",	
			"WFCatExportGenericProviderTest", WFCatExportGenericProviderTest )

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

return WFRxTestCase.wrapSuite( WFCatExportGenericProviderTest )
