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

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.

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

	-- RxLua
local Rx = require 'Rx'

	-- WF.core.utils
local KSTableUtils = require 'KSTableUtils'
local WFArguments = require 'WFArguments'

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

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

	-- WF.sqlite.doc_store
local WFRxDocStore = require 'WFRxDocStore'

	-- WF.oz_client.managed_catalog
local WFCatUserSynchronizer = require 'WFCatUserSynchronizer'
local WFRxHttpClientMock = require 'WFRxHttpClientMock'
local WFCatTestSession = require 'WFCatTestSession'

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

local check = assert( WFArguments.check )

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

-- @mark WFCatUserSynchronizerTest namespace

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

local WFCatUserSynchronizerTest = {}

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

local function createMockedUser( rxTest, responses )

	local userId = string.rep( '0', 32 )
	
	local httpMock = WFRxHttpClientMock( rxTest, responses )
	
	local docStore = WFRxDocStore( ':memory:' )
	
	local mockedUser = Rx.Class( 'WFCatUser', {
	
		serverBaseUrl = function( self )
			return 'https://photos.adobe.io'
		end,
		
		userId = function( self )
			return userId
		end,
		
		userInfoCache = function( self )
		
			return docStore
			
		end,
		
		get = function( self, url )
		
			return httpMock.get( url )
			
		end,
		
		close = function( self )
			-- do nothing
		end
		
	} )()
	
	return mockedUser
	
end

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

local function removeDate( docValue )

	local doc = KSTableUtils.deepCopy( docValue )
	
	doc.lastUpdated = nil
	
	return doc
	
end

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

local function removeUnstableData( docValue )

	local doc = KSTableUtils.deepCopy( docValue )
	
	doc.lastUpdated = nil
	
	if doc.links then
	
		doc.links[ '/rels/account' ] = nil
		
	end
	
	return doc
	
end

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

function WFCatUserSynchronizerTest.rxTestEndpointIndexDocument_fromOz( rxTest )

	local user = createMockedUser( rxTest, {
		{
			url = 'https://photos.adobe.io/v2/',
			
			response = [[{
			    "base": "https://api.adbephotos.com",
			    "links": {
			        "/rels/version": {
			            "href": "/v2/version"
			        },
			        "/rels/catalogs": {
			            "href": "/v2/catalogs"
			        },
			        "/rels/lr_catalogs": {
			            "href": "/v2/catalogs?subtype=lightroom"
			        },
			        "/rels/products": {
			            "href": "/v2/products"
			        },
			        "/rels/account": {
			            "href": "/v2/accounts/4bc284af75fe23615d70de44bf88d9fb"
			        }
			    }
			}]]
		} 
	} )
			
	local userSynchronizer = WFCatUserSynchronizer( user )

	rxTest:finally( function()
		userSynchronizer:close()
	end )
	
	rxTest:expectSingleValue(
	
		userSynchronizer:updateEndpointIndexDocumentOnce()
			:map( removeDate ),
		
		{
			_id = "Oz-EndpointIndexDocument",
			base = "https://api.adbephotos.com",
			links = {
				["/rels/account"] = {
					href = "/v2/accounts/4bc284af75fe23615d70de44bf88d9fb",
				},
				["/rels/catalogs"] = {
					href = "/v2/catalogs",
				},
				["/rels/lr_catalogs"] = {
					href = "/v2/catalogs?subtype=lightroom",
				},
				["/rels/products"] = {
					href = "/v2/products",
				},
				["/rels/version"] = {
					href = "/v2/version",
				},
			},
		}
	)
	
end

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

function WFCatUserSynchronizerTest.rxTestEndpointIndexDocument_noOzChanges( rxTest )

	local user = createMockedUser( rxTest, {
		{
			url = 'https://photos.adobe.io/v2/',
			
			response = [[{
			    "base": "https://api.adbephotos.com",
			    "links": {
			        "/rels/version": {
			            "href": "/v2/version"
			        },
			        "/rels/catalogs": {
			            "href": "/v2/catalogs"
			        },
			        "/rels/lr_catalogs": {
			            "href": "/v2/catalogs?subtype=lightroom"
			        },
			        "/rels/products": {
			            "href": "/v2/products"
			        },
			        "/rels/account": {
			            "href": "/v2/accounts/4bc284af75fe23615d70de44bf88d9fb"
			        }
			    }
			}]]
		} 
	} )
	
	rxTest:expectSingleValue(
	
		user:userInfoCache():setLocalDocValue( {
			_id = "Oz-EndpointIndexDocument",
			base = "https://api.adbephotos.com",
			lastUpdated = '2016-01-01T02:52:09.116Z',
			links = {
				["/rels/account"] = {
					href = "/v2/accounts/4bc284af75fe23615d70de44bf88d9fb",
				},
				["/rels/catalogs"] = {
					href = "/v2/catalogs",
				},
				["/rels/lr_catalogs"] = {
					href = "/v2/catalogs?subtype=lightroom",
				},
				["/rels/products"] = {
					href = "/v2/products",
				},
				["/rels/version"] = {
					href = "/v2/version",
				},
			},
		} ), true )
		
	local userSynchronizer = WFCatUserSynchronizer( user )

	rxTest:finally( function()
		userSynchronizer:close()
	end )
	
	rxTest:expectSingleValue(
	
		userSynchronizer:updateEndpointIndexDocumentOnce()
			:map( removeDate ),
		
		{
			_id = "Oz-EndpointIndexDocument",
			base = "https://api.adbephotos.com",
			links = {
				["/rels/account"] = {
					href = "/v2/accounts/4bc284af75fe23615d70de44bf88d9fb",
				},
				["/rels/catalogs"] = {
					href = "/v2/catalogs",
				},
				["/rels/lr_catalogs"] = {
					href = "/v2/catalogs?subtype=lightroom",
				},
				["/rels/products"] = {
					href = "/v2/products",
				},
				["/rels/version"] = {
					href = "/v2/version",
				},
			},
		}
	)
	
end

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

function WFCatUserSynchronizerTest.rxTestEndpointIndexDocument_localStillValid( rxTest )

	local user = createMockedUser( rxTest, {} )
	
	rxTest:expectSingleValue(
	
		user:userInfoCache():setLocalDocValue( {
			_id = "Oz-EndpointIndexDocument",
			base = "https://api.adbephotos.com",
			lastUpdated = KSDate.timeInIso8601Format( nil, false ),
			links = {
				["/rels/account"] = {
					href = "/v2/accounts/4bc284af75fe23615d70de44bf88d9fb",
				},
				["/rels/catalogs"] = {
					href = "/v2/catalogs",
				},
				["/rels/lr_catalogs"] = {
					href = "/v2/catalogs?subtype=lightroom",
				},
				["/rels/products"] = {
					href = "/v2/products",
				},
				["/rels/version"] = {
					href = "/v2/version",
				},
			},
		} ), true )
		
	local userSynchronizer = WFCatUserSynchronizer( user )

	rxTest:finally( function()
		userSynchronizer:close()
	end )
	
	rxTest:expectSingleValue(
	
		userSynchronizer:updateEndpointIndexDocumentOnce()
			:map( removeDate ),
		
		{
			_id = "Oz-EndpointIndexDocument",
			base = "https://api.adbephotos.com",
			links = {
				["/rels/account"] = {
					href = "/v2/accounts/4bc284af75fe23615d70de44bf88d9fb",
				},
				["/rels/catalogs"] = {
					href = "/v2/catalogs",
				},
				["/rels/lr_catalogs"] = {
					href = "/v2/catalogs?subtype=lightroom",
				},
				["/rels/products"] = {
					href = "/v2/products",
				},
				["/rels/version"] = {
					href = "/v2/version",
				},
			},
		}
	)
	
end

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

function WFCatUserSynchronizerTest.rxTestEndpointIndexDocument_FetchFromOzOnce( rxTest )

	rxTest:setTimeLimit( 10 )
	
	local user = createMockedUser( rxTest, {
		{
			url = 'https://photos.adobe.io/v2/',
			
			response = [[{
			    "base": "https://api.adbephotos.com",
			    "links": {
			        "/rels/version": {
			            "href": "/v2/version"
			        },
			        "/rels/catalogs": {
			            "href": "/v2/catalogs"
			        },
			        "/rels/lr_catalogs": {
			            "href": "/v2/catalogs?subtype=lightroom"
			        },
			        "/rels/products": {
			            "href": "/v2/products"
			        },
			        "/rels/account": {
			            "href": "/v2/accounts/4bc284af75fe23615d70de44bf88d9fb"
			        }
			    }
			}]]
		}
	} )
	
	rxTest:expectSingleValue(
	
		user:userInfoCache():setLocalDocValue( {
			_id = "Oz-EndpointIndexDocument",
			base = "https://api.adbephotos.com",
			lastUpdated = '2016-01-01T02:52:09.116Z',
			links = {
				["/rels/account"] = {
					href = "/v2/accounts/4bc284af75fe23615d70de44bf88d9fb",
				},
				["/rels/catalogs"] = {
					href = "/v2/catalogs",
				},
				["/rels/lr_catalogs"] = {
					href = "/v2/catalogs?subtype=lightroom",
				},
				["/rels/products"] = {
					href = "/v2/products",
				},
				["/rels/version"] = {
					href = "/v2/version",
				},
			},
		} ), true )
		
	local userSynchronizer = WFCatUserSynchronizer( user )

	rxTest:finally( function()
		userSynchronizer:close()
	end )
	
	rxTest:expectSingleValue(
	
		userSynchronizer:updateEndpointIndexDocumentOnce()
			:map( removeDate ),
		
		{
			_id = "Oz-EndpointIndexDocument",
			base = "https://api.adbephotos.com",
			links = {
				["/rels/account"] = {
					href = "/v2/accounts/4bc284af75fe23615d70de44bf88d9fb",
				},
				["/rels/catalogs"] = {
					href = "/v2/catalogs",
				},
				["/rels/lr_catalogs"] = {
					href = "/v2/catalogs?subtype=lightroom",
				},
				["/rels/products"] = {
					href = "/v2/products",
				},
				["/rels/version"] = {
					href = "/v2/version",
				},
			},
		}
	)
	
	rxTest:expectSingleValue(
	
		userSynchronizer:updateEndpointIndexDocumentOnce()
			:map( removeDate ),
		
		{
			_id = "Oz-EndpointIndexDocument",
			base = "https://api.adbephotos.com",
			links = {
				["/rels/account"] = {
					href = "/v2/accounts/4bc284af75fe23615d70de44bf88d9fb",
				},
				["/rels/catalogs"] = {
					href = "/v2/catalogs",
				},
				["/rels/lr_catalogs"] = {
					href = "/v2/catalogs?subtype=lightroom",
				},
				["/rels/products"] = {
					href = "/v2/products",
				},
				["/rels/version"] = {
					href = "/v2/version",
				},
			},
		}
	)
	
end

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

function WFCatUserSynchronizerTest.rxTestEndpointIndexDocument_OzChanges( rxTest )

	local user = createMockedUser( rxTest, {
		{
			url = 'https://photos.adobe.io/v2/',
			
			response = [[{
			    "base": "https://api.adbephotos.com",
			    "links": {
			        "/rels/version": {
			            "href": "/v2/version"
			        },
			        "/rels/catalogs": {
			            "href": "/v2/catalogs"
			        },
			        "/rels/lr_catalogs": {
			            "href": "/v2/catalogs?subtype=lightroom"
			        },
			        "/rels/products": {
			            "href": "/v2/products"
			        },
			        "/rels/account": {
			            "href": "/v2/accounts/4bc284af75fe23615d70de44bf88d9fb"
			        }
			    }
			}]]
		} 
	} )
	
	rxTest:expectSingleValue(
	
		user:userInfoCache():setLocalDocValue( {
			_id = "Oz-EndpointIndexDocument",
			base = "https://api.adbephotos.com",
			lastUpdated = '2016-01-01',
			links = {
				["/rels/account"] = {
					href = "/v2/accounts/4bc284af75fe23615d70de44bf88d9fb",
				},
				["/rels/catalogs"] = {
					href = "/v2/catalogs",
				},
				["/rels/lr_catalogs"] = {
					href = "/v2/catalogs?subtype=lightroom",
				},
				["/rels/products"] = {
					href = "/v2/products",
				},
				["/rels/version"] = {
					href = "/v2/version",
				},
			},
		} ), true )
		
	local userSynchronizer = WFCatUserSynchronizer( user )

	rxTest:finally( function()
		userSynchronizer:close()
	end )
	
	rxTest:expectSingleValue(
	
		userSynchronizer:updateEndpointIndexDocumentOnce()
			:map( removeDate ),
		
		{
			_id = "Oz-EndpointIndexDocument",
			base = "https://api.adbephotos.com",
			links = {
				["/rels/account"] = {
					href = "/v2/accounts/4bc284af75fe23615d70de44bf88d9fb",
				},
				["/rels/catalogs"] = {
					href = "/v2/catalogs",
				},
				["/rels/lr_catalogs"] = {
					href = "/v2/catalogs?subtype=lightroom",
				},
				["/rels/products"] = {
					href = "/v2/products",
				},
				["/rels/version"] = {
					href = "/v2/version",
				},
			},
		}
	)
	
end

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

function WFCatUserSynchronizerTest.rxTestEndpointIndexDocument_HttpError( rxTest )

	rxTest:setTimeLimit( 10 )
	
	local user = createMockedUser( rxTest, {
		{
			url = 'https://photos.adobe.io/v2/',
			
			fail = {
			}
		}
	} )
	
	rxTest:expectSingleValue(
	
		user:userInfoCache():setLocalDocValue( {
			_id = "Oz-EndpointIndexDocument",
			base = "https://api.adbephotos.com",
			lastUpdated = '2016-01-01T02:52:09.116Z',
			links = {
				["/rels/account"] = {
					href = "/v2/accounts/4bc284af75fe23615d70de44bf88d9fb",
				},
				["/rels/catalogs"] = {
					href = "/v2/catalogs",
				},
				["/rels/lr_catalogs"] = {
					href = "/v2/catalogs?subtype=lightroom",
				},
				["/rels/products"] = {
					href = "/v2/products",
				},
				["/rels/version"] = {
					href = "/v2/version",
				},
			},
		} ), true )
		
	local userSynchronizer = WFCatUserSynchronizer( user )

	rxTest:finally( function()
		userSynchronizer:close()
	end )
	
	rxTest:expectSingleValue(
	
		userSynchronizer:updateEndpointIndexDocumentOnce()
			:map( removeDate ),
		
		{
			_id = "Oz-EndpointIndexDocument",
			base = "https://api.adbephotos.com",
			links = {
				["/rels/account"] = {
					href = "/v2/accounts/4bc284af75fe23615d70de44bf88d9fb",
				},
				["/rels/catalogs"] = {
					href = "/v2/catalogs",
				},
				["/rels/lr_catalogs"] = {
					href = "/v2/catalogs?subtype=lightroom",
				},
				["/rels/products"] = {
					href = "/v2/products",
				},
				["/rels/version"] = {
					href = "/v2/version",
				},
			},
		}
	)
	
end

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

function WFCatUserSynchronizerTest.rxTestFetchEndpointIndex_testNightly( rxTest )

	rxTest:setTimeLimit( 120 )

	local catalogTestSession = WFCatTestSession( rxTest )

	local catalog

	rxTest:expectSingleValue(

		catalogTestSession:catalog()
			:tap( function( _catalog )
				check( 'WFCatalog' )
				catalog = _catalog
			end ),

		':ignore:' )
		
	local userSynchronizer = WFCatUserSynchronizer( catalog:ozUser() )
	
	rxTest:finally( function()
		userSynchronizer:close()
	end )
	
	rxTest:expectSingleValue(
	
		userSynchronizer:updateEndpointIndexDocumentOnce()
			:map( removeUnstableData ),
		
		{
			_id = "Oz-EndpointIndexDocument",
			base = "https://photos-stage.adobe.io",
			links = {
				["/rels/catalogs"] = {
					href = "/v2/catalogs",
				},
				["/rels/imaging"] = {
				base = "https://image-stage.adobe.io",
				},
				["/rels/lr_catalogs"] = {
					href = "/v2/catalogs?subtype=lightroom%3BX-lightroom",
				},
				["/rels/spaces/updates"] = {
					href = "/v2/spaces/updates",
				},
				["/rels/storage"] = {
					base = "https://storage.adbephotos-stage.com",
				},
				["/rels/version"] = {
					href = "/v2/version",
				},
			},
		}
	)
	
end

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

return WFRxTestCase.wrapSuite( 'WFCatUserSynchronizerTest', WFCatUserSynchronizerTest )
