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