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.CpsModuleService
24 import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations
25 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
26 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
27 import org.onap.cps.spi.model.ModuleReference
28 import spock.lang.Specification
30 class ModuleSyncServiceSpec extends Specification {
33 def mockCpsModuleService = Mock(CpsModuleService)
34 def mockDmiModelOperations = Mock(DmiModelOperations)
36 def objectUnderTest = new ModuleSyncService(mockDmiModelOperations, mockCpsModuleService)
38 def expectedDataspaceName = 'NFP-Operational'
40 def 'Sync model for a (new) cm handle with #scenario'() {
42 def ncmpServiceCmHandle = new NcmpServiceCmHandle()
43 def dmiServiceName = 'some service name'
44 ncmpServiceCmHandle.cmHandleId = 'cmHandleId-1'
45 def yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle(dmiServiceName, '' , '', ncmpServiceCmHandle)
46 and: 'DMI operations returns some module references'
47 def moduleReferences = [ new ModuleReference(moduleName:'module1',revision:'1'),
48 new ModuleReference(moduleName:'module2',revision:'2') ]
49 mockDmiModelOperations.getModuleReferences(yangModelCmHandle) >> moduleReferences
50 and: 'CPS-Core returns list of existing module resources'
51 mockCpsModuleService.getYangResourceModuleReferences(expectedDataspaceName) >> toModuleReference(existingModuleResourcesInCps)
52 and: 'DMI-Plugin returns resource(s) for "new" module(s)'
53 mockDmiModelOperations.getNewYangResourcesFromDmi(yangModelCmHandle, [new ModuleReference('module1', '1')]) >> yangResourceToContentMap
54 when: 'module sync is triggered'
55 mockCpsModuleService.identifyNewModuleReferences(moduleReferences) >> toModuleReference(identifiedNewModuleReferences)
56 def result = objectUnderTest.syncAndCreateSchemaSet(yangModelCmHandle)
57 then: 'the resulting schema set name is the same as the cm handle id'
58 assert result == 'cmHandleId-1'
59 where: 'the following parameters are used'
60 scenario | existingModuleResourcesInCps | identifiedNewModuleReferences | yangResourceToContentMap
61 'one new module' | [['module2' : '2'], ['module3' : '3']] | [['module1' : '1']] | [module1: 'some yang source']
62 'no add. properties' | [['module2' : '2'], ['module3' : '3']] | [['module1' : '1']] | [module1: 'some yang source']
63 'no new module' | [['module1' : '1'], ['module2' : '2']] | [] | [:]
66 def toModuleReference(moduleReferenceAsMap) {
67 def moduleReferences = [].withDefault { [:] }
68 moduleReferenceAsMap.forEach(property ->
69 property.forEach((moduleName, revision) -> {
70 moduleReferences.add(new ModuleReference('moduleName' : moduleName, 'revision' : revision))
72 return moduleReferences