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