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.inventory.CmHandleState
28 import org.onap.cps.ncmp.api.inventory.CompositeState
29 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
30 import org.onap.cps.spi.CascadeDeleteAllowed
31 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException
32 import org.onap.cps.spi.model.ModuleReference
33 import spock.lang.Specification
35 class ModuleSyncServiceSpec extends Specification {
38 def mockCpsModuleService = Mock(CpsModuleService)
39 def mockDmiModelOperations = Mock(DmiModelOperations)
40 def mockCpsAdminService = Mock(CpsAdminService)
42 def objectUnderTest = new ModuleSyncService(mockDmiModelOperations, mockCpsModuleService, mockCpsAdminService)
44 def expectedDataspaceName = 'NFP-Operational'
46 def 'Sync model for a (new) cm handle with #scenario'() {
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(moduleName:'module1',revision:'1'),
54 new ModuleReference(moduleName:'module2',revision:'2') ]
55 mockDmiModelOperations.getModuleReferences(yangModelCmHandle) >> moduleReferences
56 and: 'CPS-Core returns list of existing module resources'
57 mockCpsModuleService.getYangResourceModuleReferences(expectedDataspaceName) >> toModuleReference(existingModuleResourcesInCps)
58 and: 'DMI-Plugin returns resource(s) for "new" module(s)'
59 mockDmiModelOperations.getNewYangResourcesFromDmi(yangModelCmHandle, [new ModuleReference('module1', '1')]) >> newModuleNameContentToMap
60 when: 'module sync is triggered'
61 mockCpsModuleService.identifyNewModuleReferences(moduleReferences) >> toModuleReference(identifiedNewModuleReferences)
62 objectUnderTest.syncAndCreateSchemaSetAndAnchor(yangModelCmHandle)
63 then: 'create schema set from module is invoked with correct parameters'
64 1 * mockCpsModuleService.createSchemaSetFromModules('NFP-Operational', 'cmHandleId-1', newModuleNameContentToMap, existingModuleReferencesInCps)
65 and: 'anchor is created with the correct parameters'
66 1 * mockCpsAdminService.createAnchor('NFP-Operational', 'cmHandleId-1', 'cmHandleId-1')
67 where: 'the following parameters are used'
68 scenario | existingModuleResourcesInCps | identifiedNewModuleReferences | newModuleNameContentToMap | existingModuleReferencesInCps
69 'one new module' | [['module2' : '2'], ['module3' : '3']] | [['module1' : '1']] | [module1: 'some yang source'] | [new ModuleReference(moduleName:'module2',revision:'2')]
70 'no add. properties' | [['module2' : '2'], ['module3' : '3']] | [['module1' : '1']] | [module1: 'some yang source'] | [new ModuleReference(moduleName:'module2',revision:'2')]
71 'no new module' | [['module1' : '1'], ['module2' : '2']] | [] | [:] | [new ModuleReference(moduleName:'module1',revision:'1'), new ModuleReference(moduleName:'module2',revision:'2')]
74 def 'Delete Schema Set for CmHandle' () {
75 given: 'a CmHandle in the advised state'
76 def cmHandle = new YangModelCmHandle(id: 'some-cmhandle-id', compositeState: new CompositeState(cmHandleState: CmHandleState.ADVISED))
77 and: 'the Schema Set exists for the CmHandle'
78 1 * mockCpsModuleService.deleteSchemaSet(_ as String, 'some-cmhandle-id',
79 CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED)
80 when: 'delete schema set if exists is called'
81 objectUnderTest.deleteSchemaSetIfExists(cmHandle)
82 then: 'there are no exceptions'
86 def 'Delete a non-existing Schema Set for CmHandle' () {
87 given: 'a CmHandle in the advised state'
88 def cmHandle = new YangModelCmHandle(id: 'some-cmhandle-id', compositeState: new CompositeState(cmHandleState: CmHandleState.ADVISED))
89 and: 'the DB throws an exception because its Schema Set does not exist'
90 1 * mockCpsModuleService.deleteSchemaSet(_ as String, 'some-cmhandle-id',
91 CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED) >> { throw new SchemaSetNotFoundException('some-dataspace-name', 'some-cmhandle-id') }
92 when: 'delete schema set if exists is called'
93 objectUnderTest.deleteSchemaSetIfExists(cmHandle)
94 then: 'there are no exceptions'
98 def 'Delete Schema Set for CmHandle with other exception' () {
99 given: 'a CmHandle in the advised state'
100 def cmHandle = new YangModelCmHandle(id: 'some-cmhandle-id', compositeState: new CompositeState(cmHandleState: CmHandleState.ADVISED))
101 and: 'an exception other than SchemaSetNotFoundException is thrown'
102 UnsupportedOperationException unsupportedOperationException = new UnsupportedOperationException();
103 1 * mockCpsModuleService.deleteSchemaSet(_ as String, 'some-cmhandle-id',
104 CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED) >> { throw unsupportedOperationException }
105 when: 'delete schema set if exists is called'
106 objectUnderTest.deleteSchemaSetIfExists(cmHandle)
107 then: 'an exception is thrown'
108 def result = thrown(UnsupportedOperationException)
109 result == unsupportedOperationException
112 def toModuleReference(moduleReferenceAsMap) {
113 def moduleReferences = [].withDefault { [:] }
114 moduleReferenceAsMap.forEach(property ->
115 property.forEach((moduleName, revision) -> {
116 moduleReferences.add(new ModuleReference('moduleName' : moduleName, 'revision' : revision))
118 return moduleReferences