2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.inventory.sync
23 import org.onap.cps.api.CpsAdminService
24 import org.onap.cps.api.CpsModuleService
25 import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations
26 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
27 import org.onap.cps.ncmp.api.inventory.CmHandleState
28 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
29 import org.onap.cps.spi.model.ModuleReference
30 import spock.lang.Specification
32 class ModuleSyncServiceSpec extends Specification {
35 def mockCpsModuleService = Mock(CpsModuleService)
36 def mockDmiModelOperations = Mock(DmiModelOperations)
37 def mockCpsAdminService = Mock(CpsAdminService)
39 def objectUnderTest = new ModuleSyncService(mockDmiModelOperations, mockCpsModuleService, mockCpsAdminService)
41 def expectedDataspaceName = 'NFP-Operational'
43 def 'Sync model for a (new) cm handle with #scenario'() {
45 def ncmpServiceCmHandle = new NcmpServiceCmHandle()
46 def dmiServiceName = 'some service name'
47 ncmpServiceCmHandle.cmHandleId = 'cmHandleId-1'
48 def yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle(dmiServiceName, '' , '', CmHandleState.ADVISED, ncmpServiceCmHandle)
49 and: 'DMI operations returns some module references'
50 def moduleReferences = [ new ModuleReference(moduleName:'module1',revision:'1'),
51 new ModuleReference(moduleName:'module2',revision:'2') ]
52 mockDmiModelOperations.getModuleReferences(yangModelCmHandle) >> moduleReferences
53 and: 'CPS-Core returns list of existing module resources'
54 mockCpsModuleService.getYangResourceModuleReferences(expectedDataspaceName) >> toModuleReference(existingModuleResourcesInCps)
55 and: 'DMI-Plugin returns resource(s) for "new" module(s)'
56 mockDmiModelOperations.getNewYangResourcesFromDmi(yangModelCmHandle, [new ModuleReference('module1', '1')]) >> newModuleNameContentToMap
57 when: 'module sync is triggered'
58 mockCpsModuleService.identifyNewModuleReferences(moduleReferences) >> toModuleReference(identifiedNewModuleReferences)
59 objectUnderTest.syncAndCreateSchemaSetAndAnchor(yangModelCmHandle)
60 then: 'create schema set from module is invoked with correct parameters'
61 1 * mockCpsModuleService.createSchemaSetFromModules('NFP-Operational', 'cmHandleId-1', newModuleNameContentToMap, existingModuleReferencesInCps)
62 and: 'anchor is created with the correct parameters'
63 1 * mockCpsAdminService.createAnchor('NFP-Operational', 'cmHandleId-1', 'cmHandleId-1')
64 where: 'the following parameters are used'
65 scenario | existingModuleResourcesInCps | identifiedNewModuleReferences | newModuleNameContentToMap | existingModuleReferencesInCps
66 'one new module' | [['module2' : '2'], ['module3' : '3']] | [['module1' : '1']] | [module1: 'some yang source'] | [new ModuleReference(moduleName:'module2',revision:'2')]
67 'no add. properties' | [['module2' : '2'], ['module3' : '3']] | [['module1' : '1']] | [module1: 'some yang source'] | [new ModuleReference(moduleName:'module2',revision:'2')]
68 'no new module' | [['module1' : '1'], ['module2' : '2']] | [] | [:] | [new ModuleReference(moduleName:'module1',revision:'1'), new ModuleReference(moduleName:'module2',revision:'2')]
71 def toModuleReference(moduleReferenceAsMap) {
72 def moduleReferences = [].withDefault { [:] }
73 moduleReferenceAsMap.forEach(property ->
74 property.forEach((moduleName, revision) -> {
75 moduleReferences.add(new ModuleReference('moduleName' : moduleName, 'revision' : revision))
77 return moduleReferences