Unable to change state from LOCKED to ADVISED
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / inventory / sync / ModuleSyncServiceSpec.groovy
1 /*
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.inventory.sync
22
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.models.NcmpServiceCmHandle
28 import org.onap.cps.spi.model.ModuleReference
29 import spock.lang.Specification
30
31 class ModuleSyncServiceSpec extends Specification {
32
33
34     def mockCpsModuleService = Mock(CpsModuleService)
35     def mockDmiModelOperations = Mock(DmiModelOperations)
36     def mockCpsAdminService = Mock(CpsAdminService)
37
38     def objectUnderTest = new ModuleSyncService(mockDmiModelOperations, mockCpsModuleService, mockCpsAdminService)
39
40     def expectedDataspaceName = 'NFP-Operational'
41
42     def 'Sync model for a (new) cm handle with #scenario'() {
43         given: 'a cm handle'
44             def ncmpServiceCmHandle = new NcmpServiceCmHandle()
45             def dmiServiceName = 'some service name'
46             ncmpServiceCmHandle.cmHandleId = 'cmHandleId-1'
47             def yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle(dmiServiceName, '', '', ncmpServiceCmHandle)
48         and: 'DMI operations returns some module references'
49             def moduleReferences =  [ new ModuleReference(moduleName:'module1',revision:'1'),
50                                                             new ModuleReference(moduleName:'module2',revision:'2') ]
51             mockDmiModelOperations.getModuleReferences(yangModelCmHandle) >> moduleReferences
52         and: 'CPS-Core returns list of existing module resources'
53             mockCpsModuleService.getYangResourceModuleReferences(expectedDataspaceName) >> toModuleReference(existingModuleResourcesInCps)
54         and: 'DMI-Plugin returns resource(s) for "new" module(s)'
55             mockDmiModelOperations.getNewYangResourcesFromDmi(yangModelCmHandle, [new ModuleReference('module1', '1')]) >> newModuleNameContentToMap
56         when: 'module sync is triggered'
57             mockCpsModuleService.identifyNewModuleReferences(moduleReferences) >> toModuleReference(identifiedNewModuleReferences)
58             objectUnderTest.syncAndCreateSchemaSetAndAnchor(yangModelCmHandle)
59         then: 'create schema set from module is invoked with correct parameters'
60             1 * mockCpsModuleService.createSchemaSetFromModules('NFP-Operational', 'cmHandleId-1', newModuleNameContentToMap, existingModuleReferencesInCps)
61         and: 'anchor is created with the correct parameters'
62             1 * mockCpsAdminService.createAnchor('NFP-Operational', 'cmHandleId-1', 'cmHandleId-1')
63         where: 'the following parameters are used'
64             scenario             | existingModuleResourcesInCps           | identifiedNewModuleReferences | newModuleNameContentToMap       | existingModuleReferencesInCps
65             'one new module'     | [['module2' : '2'], ['module3' : '3']] | [['module1' : '1']]           | [module1: 'some yang source']   | [new ModuleReference(moduleName:'module2',revision:'2')]
66             'no add. properties' | [['module2' : '2'], ['module3' : '3']] | [['module1' : '1']]           | [module1: 'some yang source']   | [new ModuleReference(moduleName:'module2',revision:'2')]
67             'no new module'      | [['module1' : '1'], ['module2' : '2']] | []                            | [:]                             | [new ModuleReference(moduleName:'module1',revision:'1'), new ModuleReference(moduleName:'module2',revision:'2')]
68     }
69
70     def toModuleReference(moduleReferenceAsMap) {
71         def moduleReferences = [].withDefault { [:] }
72         moduleReferenceAsMap.forEach(property ->
73             property.forEach((moduleName, revision) -> {
74                 moduleReferences.add(new ModuleReference('moduleName' : moduleName, 'revision' : revision))
75             }))
76         return moduleReferences
77     }
78
79 }