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

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.

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

local Rx = require "Rx"

local WFOzRenditionServer = require 'WFOzRenditionServer'
local KSIdGenerator = require 'KSIdGenerator'
local LrmCatalogTestHelper = require 'LrmCatalogTestHelper'

local KSDataCache = require "KSDataCache"

local WFRxHttpClientMock = require "WFRxHttpClientMock"

local WFPlatformInfo = require "WFPlatformInfo"

local WFTableMerge = require 'WFTableMerge'
local WFRxTestCase = require 'WFRxTestCase'

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

-- @mark WFOzRenditionServerTest namespace

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

local LrmOzRenditionServerTest = {}

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

local catalogService, wfCatalog, renditionServer

function LrmOzRenditionServerTest.setUp()

	local dataCache = KSDataCache()

	catalogService, wfCatalog = LrmCatalogTestHelper.makeCatalogService()
	renditionServer = WFOzRenditionServer( catalogService, 
								wfCatalog:ozUserId(), wfCatalog:ozCatalogId(), dataCache, wfCatalog )
								
end

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

function LrmOzRenditionServerTest.tearDown( rxTest )

	if catalogService then
		catalogService:close()
		catalogService = nil
	end
	
	if wfCatalog then
		wfCatalog:close()
		wfCatalog = nil
	end
	
	if renditionServer then
		renditionServer:close()
		renditionServer = nil
	end

end

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

function LrmOzRenditionServerTest.rxTestShouldDownload( rxTest )

	local assetId = KSIdGenerator.uuidInLowercaseHex()
	
	local function shouldDownload( docValue, renditionName )
	
		return renditionServer:shouldDownloadRendition( docValue, assetId, renditionName )
		
	end
	
	local docValue = {
		_serverProvidedMetadata = {
			links = {
				[ '/rels/rendition_type/thumbnail2x' ] = {
					href = 'ffff/renditions/aaaa',
				}
			},
		}
	}
	
	rxTest:expectEqual( shouldDownload( docValue, 'thumbnail2x' ), true )
	
	docValue = WFTableMerge.applyOverlay( docValue, {
			_localOnly = {
				thumbnail2x = {
					revId = 'ffff',
					renditionId = 'aaaa',
					md5 = '<hash>',
				}
			},
		} )
		
	rxTest:expectEqual( shouldDownload( docValue, 'thumbnail2x' ), false )
	
	docValue = WFTableMerge.applyOverlay( docValue, {
			_serverProvidedMetadata = {
				links = {
					[ '/rels/rendition_type/thumbnail2x' ] = {
						href = 'ffff/renditions/aaaa',
						invalid = true,
					}
				},
			}
		} )
		
	rxTest:expectEqual( shouldDownload( docValue, 'thumbnail2x' ), false )
	
	docValue = WFTableMerge.applyOverlay( docValue, {
			_serverProvidedMetadata = {
				links = {
					[ '/rels/rendition_type/thumbnail2x' ] = {
						href = 'gggg/renditions/dddd',
						invalid = true,
					}
				},
			}
		} )
		
	rxTest:expectEqual( shouldDownload( docValue, 'thumbnail2x' ), false )
	
	docValue = WFTableMerge.applyOverlay( docValue, {
			_serverProvidedMetadata = {
				links = {
					[ '/rels/rendition_type/thumbnail2x' ] = {
						href = 'gggg/renditions/dddd',
						invalid = false
					}
				},
			}
		} )
		
	rxTest:expectEqual( shouldDownload( docValue, 'thumbnail2x' ), false )
	
	docValue = {
		_serverProvidedMetadata = {
			links = {
				[ '/rels/rendition_type/2048' ] = {
					href = 'ffff/renditions/aaaa',
				}
			},
		}
	}
	
	rxTest:expectEqual( shouldDownload( docValue, 'preview' ), true )
	
	docValue = {
		_localOnly = {
				thumbnail2x = {
					revId = 'ffff',
					renditionId = 'aaaa',
					md5 = '<hash>',
				}
			},
		_serverProvidedMetadata = {
			links = {
				[ '/rels/rendition_type/thumbnail2x' ] = {
					href = 'ffff/renditions/aaab',
				}
			},
		}
	}
	
	rxTest:expectEqual( shouldDownload( docValue, 'thumbnail2x' ), false )
	
	docValue = {
		_localOnly = {
				thumbnail2x = {
					revId = 'ffff',
					renditionId = 'aaaa',
					md5 = '<hash>',
				}
			},
		_serverProvidedMetadata = {
			links = {
			},
		}
	}
	
	rxTest:expectEqual( shouldDownload( docValue, 'thumbnail2x' ), false )
	
	
	docValue = {
		_serverProvidedMetadata = {
			links = {
				[ '/rels/rendition_type/thumbnail2x' ] = {
					href = 'ffff/renditions/aaaa',
					invalid = true,
				}
			},
		}
	}
	
	rxTest:expectEqual( shouldDownload( docValue, 'thumbnail2x' ), true )
	
	catalogService:close()
	
end

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

return WFRxTestCase.wrapSuite( LrmOzRenditionServerTest )
