2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2025 OpenInfra Foundation Europe. All rights reserved.
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.impl.inventory.models
23 import org.onap.cps.ncmp.api.inventory.models.CmHandleState
24 import org.onap.cps.ncmp.api.inventory.models.CompositeState
25 import org.onap.cps.ncmp.api.inventory.models.CompositeStateBuilder
26 import org.onap.cps.ncmp.api.inventory.models.DmiPluginRegistration
27 import org.onap.cps.ncmp.api.inventory.models.LockReasonCategory
28 import org.onap.cps.ncmp.api.inventory.models.NcmpServiceCmHandle
29 import org.onap.cps.ncmp.api.inventory.DataStoreSyncState
30 import spock.lang.Specification
32 import static org.onap.cps.ncmp.impl.models.RequiredDmiService.DATA
33 import static org.onap.cps.ncmp.impl.models.RequiredDmiService.MODEL
35 class YangModelCmHandleSpec extends Specification {
37 def 'Creating yang model cm handle from a service api cm handle.'() {
38 given: 'a cm handle with properties'
39 def ncmpServiceCmHandle = new NcmpServiceCmHandle()
40 ncmpServiceCmHandle.cmHandleId = 'cm-handle-id01'
41 ncmpServiceCmHandle.additionalProperties = [myAdditionalProperty:'value1']
42 ncmpServiceCmHandle.publicProperties = [myPublicProperty:'value2']
43 ncmpServiceCmHandle.dmiProperties = [myDmiProperty:'value3']
44 and: 'with a composite state'
45 def compositeState = new CompositeStateBuilder()
46 .withCmHandleState(CmHandleState.LOCKED)
47 .withLastUpdatedTime('some-update-time')
48 .withLockReason(LockReasonCategory.MODULE_SYNC_FAILED, 'locked details')
49 .withOperationalDataStores(DataStoreSyncState.SYNCHRONIZED, 'some-sync-time').build()
50 ncmpServiceCmHandle.setCompositeState(compositeState)
51 when: 'it is converted to a yang model cm handle'
52 def objectUnderTest = YangModelCmHandle.toYangModelCmHandle(new DmiPluginRegistration(), ncmpServiceCmHandle,'my-module-set-tag', 'my-alternate-id', 'my-data-producer-identifier', 'ADVISED', 'my-dmi-property')
53 then: 'the result has the right size'
54 assert objectUnderTest.additionalProperties.size() == 1
55 and: 'the result has the correct values for module set tag, alternate ID, and data producer identifier'
56 assert objectUnderTest.moduleSetTag == 'my-module-set-tag'
57 assert objectUnderTest.alternateId == 'my-alternate-id'
58 assert objectUnderTest.dataProducerIdentifier == 'my-data-producer-identifier'
59 and: 'the additional property in the result has the correct name and value'
60 assert objectUnderTest.additionalProperties[0].name == 'myAdditionalProperty'
61 assert objectUnderTest.additionalProperties[0].value == 'value1'
62 and: 'the public property in the result has the correct name and value'
63 assert objectUnderTest.publicProperties[0].name == 'myPublicProperty'
64 assert objectUnderTest.publicProperties[0].value == 'value2'
65 and: 'the composite state matches the composite state of the ncmpServiceCmHandle'
66 objectUnderTest.getCompositeState().cmHandleState == CmHandleState.LOCKED
67 objectUnderTest.getCompositeState() == ncmpServiceCmHandle.getCompositeState()
68 and: 'the cm-handle-state is correct'
69 assert objectUnderTest.cmHandleStatus == 'ADVISED'
70 and: 'the dmi-properties are correct'
71 assert objectUnderTest.dmiProperties == 'my-dmi-property'
74 def 'Resolve DMI service name: #scenario and #requiredService service require.'() {
75 given: 'a yang model cm handle'
76 def dmiPluginRegistration = new DmiPluginRegistration(
77 dmiPlugin: dmiServiceName,
78 dmiDataPlugin: dmiDataServiceName,
79 dmiModelPlugin: dmiModelServiceName
81 def objectUnderTest = YangModelCmHandle.toYangModelCmHandle(dmiPluginRegistration, new NcmpServiceCmHandle(cmHandleId: 'cm-handle-id-1'),'', '', '', '', '')
83 assert objectUnderTest.resolveDmiServiceName(requiredService) == expectedService
85 scenario | dmiServiceName | dmiDataServiceName | dmiModelServiceName | requiredService || expectedService
86 'common service registered' | 'common service' | 'does not matter' | 'does not matter' | DATA || 'common service'
87 'common service registered' | 'common service' | 'does not matter' | 'does not matter' | MODEL || 'common service'
88 'common service empty' | '' | 'data service' | 'does not matter' | DATA || 'data service'
89 'common service empty' | '' | 'does not matter' | 'model service' | MODEL || 'model service'
90 'common service blank' | ' ' | 'data service' | 'does not matter' | DATA || 'data service'
91 'common service blank' | ' ' | 'does not matter' | 'model service' | MODEL || 'model service'
92 'common service null ' | null | 'data service' | 'does not matter' | DATA || 'data service'
93 'common service null' | null | 'does not matter' | 'model service' | MODEL || 'model service'
94 'only model service registered' | null | null | 'does not matter' | DATA || null
95 'only data service registered' | null | 'does not matter' | null | MODEL || null
98 def 'Yang Model Cm Handle Deep Copy'() {
99 given: 'a yang model cm handle'
100 def currentYangModelCmHandle = new YangModelCmHandle(id: 'cmhandle',
101 publicProperties: [new YangModelCmHandle.Property('publicProperty1', 'value1')],
102 additionalProperties: [new YangModelCmHandle.Property('additionalProperty1', 'value1')],
103 compositeState: new CompositeState(cmHandleState: CmHandleState.ADVISED, dataSyncEnabled: false))
104 when: 'a deep copy is created'
105 def yangModelCmhandleDeepCopy = YangModelCmHandle.deepCopyOf(currentYangModelCmHandle)
106 and: 'we try to mutate current yang model cm handle'
107 currentYangModelCmHandle.id = 'cmhandle-changed'
108 currentYangModelCmHandle.additionalProperties = [new YangModelCmHandle.Property('updatedAdditionalProperty1', 'value1')]
109 currentYangModelCmHandle.publicProperties = [new YangModelCmHandle.Property('updatedPublicProperty1', 'value1')]
110 currentYangModelCmHandle.compositeState.cmHandleState = CmHandleState.READY
111 currentYangModelCmHandle.compositeState.dataSyncEnabled = true
112 then: 'there is no change in the deep copied object'
113 assert yangModelCmhandleDeepCopy.id == 'cmhandle'
114 assert yangModelCmhandleDeepCopy.additionalProperties == [new YangModelCmHandle.Property('additionalProperty1', 'value1')]
115 assert yangModelCmhandleDeepCopy.publicProperties == [new YangModelCmHandle.Property('publicProperty1', 'value1')]
116 assert yangModelCmhandleDeepCopy.compositeState.cmHandleState == CmHandleState.ADVISED
117 assert yangModelCmhandleDeepCopy.compositeState.dataSyncEnabled == false
118 and: 'equality on reference and hashcode behave as expected'
119 assert currentYangModelCmHandle.hashCode() != yangModelCmhandleDeepCopy.hashCode()
120 assert currentYangModelCmHandle != yangModelCmhandleDeepCopy