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

LrmOzMaintenanceModelImpTest.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2020 Adobe
All Rights Reserved.

NOTICE: All information contained herein is, and remains
the property of Adobe and its suppliers, if any. The intellectual
and technical concepts contained herein are proprietary to Adobe
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.

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

local Rx = require "Rx"

	-- com.adobe.wichitafoundation.core.utils
local WFModelsUtils = require 'WFModelsUtils'
local WFPlatformInfo = require 'WFPlatformInfo'

-- This model test is only valid on the mobile platforms.

if not WFPlatformInfo.isMobile() then
	return {}
end

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

	-- WF.oz_client.catalog
local OzCatalogServiceViaWFCatalog = require 'OzCatalogServiceViaWFCatalog'

	-- WF.lrmobile
local LrmSyncClientImp = require 'LrmSyncClientImp' 

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

--[[

To set up unit tests against a pass-through model such as 'LrmOzMaintenanceModelImp', 
we must call the associated test factory with a table that maps the utility function 
name (getOzMaintenanceNotificationStream) to the corresponding pass-through submodel.
See 'subModelsByImplementation' below.


This is necessary because the pass-through models don't always share the same names 
as the utility functions that include the actual implementation.

We must also a provide a method for constructing the parent model. This is necessary
as many submodels reference state within their parent model. See 'createParentModelForTest'
below.

]]--

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

local submodelsByImplementation

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

local function createParentModelForTest( rxTest, wfCatalog )
	
	-- Create the submodels by iterating through submodelsByImplementation
	
	local submodels = {}

	for _, implementationFunctionMappings in pairs( submodelsByImplementation ) do
	
		for _, implementationFunctionMapping in pairs( implementationFunctionMappings ) do
	
			local submodelNamespace = implementationFunctionMapping.submodelNamespace
		
			submodels = WFModelsUtils.aggregateModels( submodels, require( submodelNamespace ) )
	
		end
	
	end

	local wfCatalogStream = Rx.Observable.just( wfCatalog )

	local ozCatalog = OzCatalogServiceViaWFCatalog( wfCatalogStream )

	local wfCatSpec = wfCatalog:ozCatalogSpec()

	local options = {
		serverBase = wfCatalog:ozUser():serverBaseUrl(),
		userId = wfCatalog:ozUserId(),
		userInfoCacheFolder = wfCatalog:ozUser():userInfoCacheFolder(),
		catalogId = wfCatalog:ozCatalogId(),
		rootCatalogPath = wfCatalog:fileSpec():rootPath(),
		docStorePath = wfCatalog:fileSpec():docStorePath(),
		indexSpecScript = 'LrmCatalogIndexDefinitions.lua',
		rxTest = rxTest,
		wfCatalog = wfCatalog,
		wfCatSpec = wfCatSpec,
		wfCatUser = wfCatSpec:wfOzUser()
	}
	
	local syncClient = LrmSyncClientImp( options )
			
	ozCatalog:setSyncClient( syncClient )

	return {
		_wfCatalogStream = wfCatalogStream,
		catalog = ozCatalog,
		value = Rx.Observable.just( true ),
		models = submodels,
		close = function()
			syncClient:close()
		end
	}
	
end

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

--[[
	The following maps the implementation function to the corresponding pass-through
	submodel that we'd like to test. It follow the pattern:

	subModelsByImpFnName = {

		[ <implementation_namespace> ] = {
		
			[ <implementation_function_name> ] = {

				submodelNamespace = <submodel_namespace>
				submodelName = <submodel_function_name>
				parentModelConstructorFn = <parent_model_constructor_function>
			}
			
		}
		
	}
]]--

submodelsByImplementation = {

	[ 'WFCatOzMaintenanceTracker' ] = {
	
		[ 'getOzMaintenanceNotificationStream' ] = {
		
			submodelNamespace = 'LrmOzMaintenanceModelImp',
			submodelName = 'getOzMaintenanceNotificationStream',
			parentModelConstructorFn = createParentModelForTest
			
		}
		
	}
	
}

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

return WFCatOzMaintenanceTrackerTestFactory( submodelsByImplementation )
