Expose REST endpoint to update YANG schema set using moduleSetTag
[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-2023 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.spi.FetchDescendantsOption
24
25 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME
26 import static org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory.MODULE_UPGRADE
27
28 import org.onap.cps.api.CpsAdminService
29 import org.onap.cps.api.CpsDataService
30 import org.onap.cps.api.CpsModuleService
31 import org.onap.cps.ncmp.api.impl.inventory.sync.ModuleSyncService
32 import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations
33 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
34 import org.onap.cps.ncmp.api.impl.inventory.CmHandleQueries
35 import org.onap.cps.ncmp.api.impl.inventory.CompositeStateBuilder
36 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
37 import org.onap.cps.spi.CascadeDeleteAllowed
38 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException
39 import org.onap.cps.spi.model.ModuleReference
40 import org.onap.cps.utils.JsonObjectMapper
41 import spock.lang.Specification
42
43 class ModuleSyncServiceSpec extends Specification {
44
45     def mockCpsModuleService = Mock(CpsModuleService)
46     def mockDmiModelOperations = Mock(DmiModelOperations)
47     def mockCpsAdminService = Mock(CpsAdminService)
48     def mockCmHandleQueries = Mock(CmHandleQueries)
49     def mockCpsDataService = Mock(CpsDataService)
50     def mockJsonObjectMapper = Mock(JsonObjectMapper)
51
52     def objectUnderTest = new ModuleSyncService(mockDmiModelOperations, mockCpsModuleService, mockCpsAdminService,
53             mockCmHandleQueries, mockCpsDataService, mockJsonObjectMapper)
54
55     def expectedDataspaceName = NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME
56
57     def 'Sync model for a (new) cm handle with #scenario'() {
58         given: 'a cm handle having lock reason : MODULE_UPGRADE'
59             def ncmpServiceCmHandle = new NcmpServiceCmHandle()
60             ncmpServiceCmHandle.setCompositeState(new CompositeStateBuilder()
61                 .withLockReason(MODULE_UPGRADE, 'new moduleSetTag: someModuleSetTag').build())
62             def dmiServiceName = 'some service name'
63             ncmpServiceCmHandle.cmHandleId = 'cmHandleId-1'
64             def yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle(dmiServiceName, '', '', ncmpServiceCmHandle,'someModuleSetTag')
65         and: 'DMI operations returns some module references'
66             def moduleReferences =  [ new ModuleReference('module1','1'), new ModuleReference('module2','2') ]
67             mockDmiModelOperations.getModuleReferences(yangModelCmHandle) >> moduleReferences
68         and: 'CPS-Core returns list of existing module resources'
69             mockCpsModuleService.getYangResourceModuleReferences(expectedDataspaceName) >> toModuleReference(existingModuleResourcesInCps)
70         and: 'DMI-Plugin returns resource(s) for "new" module(s)'
71             mockDmiModelOperations.getNewYangResourcesFromDmi(yangModelCmHandle, [new ModuleReference('module1', '1')]) >> newModuleNameContentToMap
72         and: 'empty data node list is returned by cps path a query'
73         mockCmHandleQueries.queryNcmpRegistryByCpsPath("//cm-handles[@module-set-tag='someModuleSetTag']",
74                 FetchDescendantsOption.OMIT_DESCENDANTS) >> Collections.emptyList()
75         when: 'module sync is triggered'
76             mockCpsModuleService.identifyNewModuleReferences(moduleReferences) >> toModuleReference(identifiedNewModuleReferences)
77             objectUnderTest.syncAndCreateOrUpgradeSchemaSetAndAnchor(yangModelCmHandle)
78         then: 'create schema set from module is invoked with correct parameters'
79             1 * mockCpsModuleService.createOrUpgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, 'cmHandleId-1', newModuleNameContentToMap, moduleReferences)
80         and: 'anchor is created with the correct parameters'
81             1 * mockCpsAdminService.createAnchor(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, 'cmHandleId-1', 'cmHandleId-1')
82         where: 'the following parameters are used'
83             scenario             | existingModuleResourcesInCps           | identifiedNewModuleReferences | newModuleNameContentToMap
84             'one new module'     | [['module2' : '2'], ['module3' : '3']] | [['module1' : '1']]           | [module1: 'some yang source']
85             'no add. properties' | [['module2' : '2'], ['module3' : '3']] | [['module1' : '1']]           | [module1: 'some yang source']
86             'no new module'      | [['module1' : '1'], ['module2' : '2']] | []                            | [:]
87     }
88
89     def 'Delete Schema Set for CmHandle' () {
90         when: 'delete schema set if exists is called'
91             objectUnderTest.deleteSchemaSetIfExists('some-cmhandle-id')
92         then: 'the module service is invoked to delete the correct schema set'
93             1 * mockCpsModuleService.deleteSchemaSet(expectedDataspaceName, 'some-cmhandle-id', CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED)
94     }
95
96     def 'Delete a non-existing Schema Set for CmHandle' () {
97         given: 'the DB throws an exception because its Schema Set does not exist'
98            mockCpsModuleService.deleteSchemaSet(*_) >> { throw new SchemaSetNotFoundException('some-dataspace-name', 'some-cmhandle-id') }
99         when: 'delete schema set if exists is called'
100             objectUnderTest.deleteSchemaSetIfExists('some-cmhandle-id')
101         then: 'the exception from the DB is ignored; there are no exceptions'
102             noExceptionThrown()
103     }
104
105     def 'Delete Schema Set for CmHandle with other exception' () {
106         given: 'an exception other than SchemaSetNotFoundException is thrown'
107             UnsupportedOperationException unsupportedOperationException = new UnsupportedOperationException();
108             1 * mockCpsModuleService.deleteSchemaSet(*_) >> { throw unsupportedOperationException }
109         when: 'delete schema set if exists is called'
110             objectUnderTest.deleteSchemaSetIfExists('some-cmhandle-id')
111         then: 'an exception is thrown'
112             def result = thrown(UnsupportedOperationException)
113             result == unsupportedOperationException
114     }
115
116     def toModuleReference(moduleReferenceAsMap) {
117         def moduleReferences = [].withDefault { [:] }
118         moduleReferenceAsMap.forEach(property ->
119             property.forEach((moduleName, revision) -> {
120                 moduleReferences.add(new ModuleReference('moduleName' : moduleName, 'revision' : revision))
121             }))
122         return moduleReferences
123     }
124
125 }