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