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.models.NcmpServiceCmHandle
28 import org.onap.cps.spi.CascadeDeleteAllowed
29 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException
30 import org.onap.cps.spi.model.ModuleReference
31 import spock.lang.Specification
33 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, '', '', ncmpServiceCmHandle)
49 and: 'DMI operations returns some module references'
50 def moduleReferences = [ new ModuleReference('module1','1'), new ModuleReference('module2','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, moduleReferences)
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
65 'one new module' | [['module2' : '2'], ['module3' : '3']] | [['module1' : '1']] | [module1: 'some yang source']
66 'no add. properties' | [['module2' : '2'], ['module3' : '3']] | [['module1' : '1']] | [module1: 'some yang source']
67 'no new module' | [['module1' : '1'], ['module2' : '2']] | [] | [:]
70 def 'Delete Schema Set for CmHandle' () {
71 when: 'delete schema set if exists is called'
72 objectUnderTest.deleteSchemaSetIfExists('some-cmhandle-id')
73 then: 'the module service is invoked to delete the correct schema set'
74 1 * mockCpsModuleService.deleteSchemaSet(expectedDataspaceName, 'some-cmhandle-id', CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED)
77 def 'Delete a non-existing Schema Set for CmHandle' () {
78 given: 'the DB throws an exception because its Schema Set does not exist'
79 mockCpsModuleService.deleteSchemaSet(*_) >> { throw new SchemaSetNotFoundException('some-dataspace-name', 'some-cmhandle-id') }
80 when: 'delete schema set if exists is called'
81 objectUnderTest.deleteSchemaSetIfExists('some-cmhandle-id')
82 then: 'the exception from the DB is ignored; there are no exceptions'
86 def 'Delete Schema Set for CmHandle with other exception' () {
87 given: 'an exception other than SchemaSetNotFoundException is thrown'
88 UnsupportedOperationException unsupportedOperationException = new UnsupportedOperationException();
89 1 * mockCpsModuleService.deleteSchemaSet(*_) >> { throw unsupportedOperationException }
90 when: 'delete schema set if exists is called'
91 objectUnderTest.deleteSchemaSetIfExists('some-cmhandle-id')
92 then: 'an exception is thrown'
93 def result = thrown(UnsupportedOperationException)
94 result == unsupportedOperationException
97 def toModuleReference(moduleReferenceAsMap) {
98 def moduleReferences = [].withDefault { [:] }
99 moduleReferenceAsMap.forEach(property ->
100 property.forEach((moduleName, revision) -> {
101 moduleReferences.add(new ModuleReference('moduleName' : moduleName, 'revision' : revision))
103 return moduleReferences