a9b6b00b2d02aa11e1d7c2761e83e6f0f009db56
[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.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
31
32 import static org.onap.cps.ncmp.impl.models.RequiredDmiService.DATA
33 import static org.onap.cps.ncmp.impl.models.RequiredDmiService.MODEL
34
35 class YangModelCmHandleSpec extends Specification {
36
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'
72     }
73
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
80             )
81             def objectUnderTest = YangModelCmHandle.toYangModelCmHandle(dmiPluginRegistration, new NcmpServiceCmHandle(cmHandleId: 'cm-handle-id-1'),'', '', '', '', '')
82         expect:
83             assert objectUnderTest.resolveDmiServiceName(requiredService) == expectedService
84         where:
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
96     }
97
98     def 'Yang Model Cm Handle Deep Copy.'() {
99         given: 'a yang model cm handle'
100             def original = new YangModelCmHandle(id: 'original id',
101                 publicProperties: [new YangModelCmHandle.Property('publicProperty', 'value1')],
102                 additionalProperties: [new YangModelCmHandle.Property('additionalProperty', 'value2')],
103                 compositeState: new CompositeState(cmHandleState: CmHandleState.ADVISED, dataSyncEnabled: false))
104         when: 'a deep copy is created'
105             def copy = YangModelCmHandle.deepCopyOf(original)
106         then: 'the objects are equal'
107             assert original == copy
108             assert original.equals(copy)
109         and: 'have the same hash code'
110             assert original.hashCode() == copy.hashCode()
111         when: 'mutate the original yang model cm handle'
112             original.id = 'changed id'
113             original.publicProperties = [new YangModelCmHandle.Property('updatedPublicProperty', 'some new value')]
114             original.additionalProperties = [new YangModelCmHandle.Property('updatedAdditionalProperty', 'some new value')]
115             original.compositeState.cmHandleState = CmHandleState.READY
116             original.compositeState.dataSyncEnabled = true
117         then: 'there is no change in the copied object'
118             assert copy.id == 'original id'
119             assert copy.publicProperties == [new YangModelCmHandle.Property('publicProperty', 'value1')]
120             assert copy.additionalProperties == [new YangModelCmHandle.Property('additionalProperty', 'value2')]
121             assert copy.compositeState.cmHandleState == CmHandleState.ADVISED
122             assert copy.compositeState.dataSyncEnabled == false
123         and: 'the objects are not equal'
124             assert original != copy
125             assert original.equals(copy) == false
126         and: 'have different hash codes'
127             assert original.hashCode() != copy.hashCode()
128     }
129
130     def 'Yang Model Cm Handle Deep Copy for cm handled without optional properties.'() {
131         given: 'a yang model cm handle'
132             def original = new YangModelCmHandle(id: 'some id')
133         when: 'a deep copy is created'
134             def copy = YangModelCmHandle.deepCopyOf(original)
135         then: 'the objects are equal'
136             assert original == copy
137             assert original.equals(copy)
138     }
139
140 }