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

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.

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

	-- WF.core.self_test
local WFRxTestCase = require 'WFRxTestCase'
	
	-- WF.oz_client.managed_catalog.sync
local WFCatUrlUtils = require 'WFCatUrlUtils'

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

local WFCatUrlUtilsTest = {}

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

function WFCatUrlUtilsTest.rxTestInvalidParameters( rxTest )

	local resolveLinks = assert( WFCatUrlUtils.resolveLinksToAbsoluteUrls )
	local resolveUrl = assert( WFCatUrlUtils.resolveToAbsoluteUrl )
	
	rxTest:expectError( function()
	
		resolveLinks( false, {} )
		
	end )
	
	rxTest:expectError( function()
	
		resolveLinks( 42, {} )
		
	end )
	
	rxTest:expectError( function()
	
		resolveLinks( {}, {} )
		
	end )
	
	rxTest:expectError( function()
	
		resolveLinks( '', {} )
		
	end )
	
	rxTest:expectError( function()
	
		resolveLinks( 'https://', false )
		
	end )
	
	rxTest:expectError( function()
	
		resolveLinks( 'https://', 42 )
		
	end )
	
	rxTest:expectError( function()
	
		resolveLinks( 'https://', '' )
		
	end )
	
	--------------------------------------------------------------------------------

	rxTest:expectError( function()
	
		resolveUrl( '', '' )
		
	end )
	
	rxTest:expectError( function()
	
		resolveUrl( 42, '' )
		
	end )
	
	rxTest:expectError( function()
	
		resolveUrl( true, '' )
		
	end )
	
	rxTest:expectError( function()
	
		resolveUrl( {}, '' )
		
	end )
	
end

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

function WFCatUrlUtilsTest.rxTestResolveLinksToAbsolutePaths( rxTest )

	local resolver = assert( WFCatUrlUtils.resolveLinksToAbsoluteUrls )
	
	local base = "https://api.adbephotos.com/v2/catalogs/eb6e3bc94bf642e093062e6c199fc65b/"
	
	rxTest:expectEqual( nil, resolver( base, nil ) )
	
	local serverProvidedMetadata = {
		links = {
			["/rels/first_revision"] = {
				href = "assets/aadec352a4ae46408d50b4e2bb7d34c8/revisions/2bf2095437b848e4b410c5bab5fd18fa",
			},
			["/rels/latest_revision"] = {
				href = "assets/aadec352a4ae46408d50b4e2bb7d34c8/revisions/2bf2095437b848e4b410c5bab5fd18fa",
			},
			["self"] = {
				href = "assets/aadec352a4ae46408d50b4e2bb7d34c8",
			},
		}
	}
	
	local links = resolver( base, serverProvidedMetadata.links )
	
	rxTest:expectEqual( links, serverProvidedMetadata.links )
	
	local expected = {
		["/rels/first_revision"] = {
			href = "/v2/catalogs/eb6e3bc94bf642e093062e6c199fc65b/assets/aadec352a4ae46408d50b4e2bb7d34c8/revisions/2bf2095437b848e4b410c5bab5fd18fa",
		},
		["/rels/latest_revision"] = {
			href = "/v2/catalogs/eb6e3bc94bf642e093062e6c199fc65b/assets/aadec352a4ae46408d50b4e2bb7d34c8/revisions/2bf2095437b848e4b410c5bab5fd18fa",
		},
		["self"] = {
			href = "/v2/catalogs/eb6e3bc94bf642e093062e6c199fc65b/assets/aadec352a4ae46408d50b4e2bb7d34c8",
		},
	}
		
	rxTest:expectEqual( expected, serverProvidedMetadata.links )
	
	--------------------------------------------------------------------------------

	serverProvidedMetadata = {
		links = {
			["/rels/first_revision"] = {
				href = "assets/aadec352a4ae46408d50b4e2bb7d34c8/revisions/2bf2095437b848e4b410c5bab5fd18fa",
			},
			["/rels/latest_revision"] = {
				href = "https://api.adbephotos-stage.com/v2/catalogs/eb6e3bc94bf642e093062e6c199fc65b/assets/aadec352a4ae46408d50b4e2bb7d34c8/revisions/2bf2095437b848e4b410c5bab5fd18fa",
			},
			["self"] = {
				href = "assets/aadec352a4ae46408d50b4e2bb7d34c8",
			},
		}
	}
	
	expected = {
		["/rels/first_revision"] = {
			href = "/v2/catalogs/eb6e3bc94bf642e093062e6c199fc65b/assets/aadec352a4ae46408d50b4e2bb7d34c8/revisions/2bf2095437b848e4b410c5bab5fd18fa",
		},
		["/rels/latest_revision"] = {
			href = "https://api.adbephotos-stage.com/v2/catalogs/eb6e3bc94bf642e093062e6c199fc65b/assets/aadec352a4ae46408d50b4e2bb7d34c8/revisions/2bf2095437b848e4b410c5bab5fd18fa",
		},
		["self"] = {
			href = "/v2/catalogs/eb6e3bc94bf642e093062e6c199fc65b/assets/aadec352a4ae46408d50b4e2bb7d34c8",
		},
	}
	
	resolver( base, serverProvidedMetadata.links )
	
	rxTest:expectEqual( expected, serverProvidedMetadata.links )
	
	--------------------------------------------------------------------------------

	serverProvidedMetadata = {
		links = {
			["/rels/first_revision"] = {
				href = "assets/aadec352a4ae46408d50b4e2bb7d34c8/revisions/2bf2095437b848e4b410c5bab5fd18fa",
			},
			["/rels/latest_revision"] = {
			},
			["self"] = {
				href = "assets/aadec352a4ae46408d50b4e2bb7d34c8",
			},
		}
	}
	
	expected = {
		["/rels/first_revision"] = {
			href = "/v2/catalogs/eb6e3bc94bf642e093062e6c199fc65b/assets/aadec352a4ae46408d50b4e2bb7d34c8/revisions/2bf2095437b848e4b410c5bab5fd18fa",
		},
		["/rels/latest_revision"] = {
		},
		["self"] = {
			href = "/v2/catalogs/eb6e3bc94bf642e093062e6c199fc65b/assets/aadec352a4ae46408d50b4e2bb7d34c8",
		},
	}
	
	resolver( base, serverProvidedMetadata.links )
	
	rxTest:expectEqual( expected, serverProvidedMetadata.links )
	
	--------------------------------------------------------------------------------

	serverProvidedMetadata = {
		links = {
			["/rels/first_revision"] = {
				href = "assets/aadec352a4ae46408d50b4e2bb7d34c8/revisions/2bf2095437b848e4b410c5bab5fd18fa?subtype=image",
			},
			["/rels/latest_revision"] = {
			},
			["self"] = {
				href = "assets/aadec352a4ae46408d50b4e2bb7d34c8?subtype=image&updated=2016-04-03",
			},
		}
	}
	
	expected = {
		["/rels/first_revision"] = {
			href = "/v2/catalogs/eb6e3bc94bf642e093062e6c199fc65b/assets/aadec352a4ae46408d50b4e2bb7d34c8/revisions/2bf2095437b848e4b410c5bab5fd18fa?subtype=image",
		},
		["/rels/latest_revision"] = {
		},
		["self"] = {
			href = "/v2/catalogs/eb6e3bc94bf642e093062e6c199fc65b/assets/aadec352a4ae46408d50b4e2bb7d34c8?subtype=image&updated=2016-04-03",
		},
	}
	
	resolver( base, serverProvidedMetadata.links )
	
	rxTest:expectEqual( expected, serverProvidedMetadata.links )
	
end

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

function WFCatUrlUtilsTest.rxTestResolveToAbsoluteUrl( rxTest )

	local resolveToAbsoluteUrl = assert( WFCatUrlUtils.resolveToAbsoluteUrl )
	
	local baseUrl = "https://api.adbephotos.com/v2/catalogs/eb6e3bc94bf642e093062e6c199fc65b/"
	local maybeRelativeUrl = 'assets/aadec352a4ae46408d50b4e2bb7d34c8/revisions/2bf2095437b848e4b410c5bab5fd18fa'
	local expectedUrl = '/v2/catalogs/eb6e3bc94bf642e093062e6c199fc65b/assets/aadec352a4ae46408d50b4e2bb7d34c8/revisions/2bf2095437b848e4b410c5bab5fd18fa'
	
	rxTest:expectEqual( expectedUrl, resolveToAbsoluteUrl( baseUrl, maybeRelativeUrl ) )
	
	--------------------------------------------------------------------------------

	baseUrl = "https://api.adbephotos.com/v2/catalogs/eb6e3bc94bf642e093062e6c199fc65b/"
	maybeRelativeUrl = 'assets/aadec352a4ae46408d50b4e2bb7d34c8/revisions/2bf2095437b848e4b410c5bab5fd18fa?subtype=image&updated=2016-04-03'
	expectedUrl = '/v2/catalogs/eb6e3bc94bf642e093062e6c199fc65b/assets/aadec352a4ae46408d50b4e2bb7d34c8/revisions/2bf2095437b848e4b410c5bab5fd18fa?subtype=image&updated=2016-04-03'
	
	rxTest:expectEqual( expectedUrl, resolveToAbsoluteUrl( baseUrl, maybeRelativeUrl ) )
	
	--------------------------------------------------------------------------------

	baseUrl = "https://api.adbephotos.com/v2/catalogs/eb6e3bc94bf642e093062e6c199fc65b/assets/aadec352a4ae46408d50b4e2bb7d34c8/revisions/"
	maybeRelativeUrl = '2bf2095437b848e4b410c5bab5fd18fa?subtype=image&updated=2016-04-03'
	expectedUrl = '/v2/catalogs/eb6e3bc94bf642e093062e6c199fc65b/assets/aadec352a4ae46408d50b4e2bb7d34c8/revisions/2bf2095437b848e4b410c5bab5fd18fa?subtype=image&updated=2016-04-03'
	
	rxTest:expectEqual( expectedUrl, resolveToAbsoluteUrl( baseUrl, maybeRelativeUrl ) )
	
end

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

return WFRxTestCase.wrapSuite( WFCatUrlUtilsTest )
