Modify cmHandle registration for alternateId
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / yangmodels / YangModelCmHandleSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2023 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
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.api.impl.yangmodels
22
23 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState
24 import org.onap.cps.ncmp.api.impl.inventory.CompositeState
25 import org.onap.cps.ncmp.api.impl.inventory.CompositeStateBuilder
26 import org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory
27 import org.onap.cps.ncmp.api.impl.inventory.DataStoreSyncState
28 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
29 import spock.lang.Specification
30
31 import static org.onap.cps.ncmp.api.impl.operations.RequiredDmiService.DATA
32 import static org.onap.cps.ncmp.api.impl.operations.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.dmiProperties = [myDmiProperty:'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')
51         then: 'the result has the right size'
52             assert objectUnderTest.dmiProperties.size() == 1
53         and: 'the result has the correct values for module set tag and alternate ID'
54             assert objectUnderTest.moduleSetTag == 'my-module-set-tag'
55             assert objectUnderTest.alternateId == 'my-alternate-id'
56         and: 'the DMI property in the result has the correct name and value'
57             assert objectUnderTest.dmiProperties[0].name == 'myDmiProperty'
58             assert objectUnderTest.dmiProperties[0].value == 'value1'
59         and: 'the public property in the result has the correct name and value'
60             assert objectUnderTest.publicProperties[0].name == 'myPublicProperty'
61             assert objectUnderTest.publicProperties[0].value == 'value2'
62         and: 'the composite state matches the composite state of the ncmpServiceCmHandle'
63             objectUnderTest.getCompositeState().cmHandleState == CmHandleState.LOCKED
64             objectUnderTest.getCompositeState() == ncmpServiceCmHandle.getCompositeState()
65     }
66
67     def 'Resolve DMI service name: #scenario and #requiredService service require.'() {
68         given: 'a yang model cm handle'
69             def objectUnderTest = YangModelCmHandle.toYangModelCmHandle(dmiServiceName, dmiDataServiceName,
70                     dmiModelServiceName, new NcmpServiceCmHandle(cmHandleId: 'cm-handle-id-1'),'', '')
71         expect:
72             assert objectUnderTest.resolveDmiServiceName(requiredService) == expectedService
73         where:
74             scenario                        | dmiServiceName     | dmiDataServiceName | dmiModelServiceName | requiredService || expectedService
75             'common service registered'     | 'common service'   | 'does not matter'  | 'does not matter'   | DATA            || 'common service'
76             'common service registered'     | 'common service'   | 'does not matter'  | 'does not matter'   | MODEL           || 'common service'
77             'common service empty'          | ''                 | 'data service'     | 'does not matter'   | DATA            || 'data service'
78             'common service empty'          | ''                 | 'does not matter'  | 'model service'     | MODEL           || 'model service'
79             'common service blank'          | '   '              | 'data service'     | 'does not matter'   | DATA            || 'data service'
80             'common service blank'          | '   '              | 'does not matter'  | 'model service'     | MODEL           || 'model service'
81             'common service null '          | null               | 'data service'     | 'does not matter'   | DATA            || 'data service'
82             'common service null'           | null               | 'does not matter'  | 'model service'     | MODEL           || 'model service'
83             'only model service registered' | null               | null               | 'does not matter'   | DATA            || null
84             'only data service registered'  | null               | 'does not matter'  | null                | MODEL           || null
85     }
86
87     def 'Yang Model Cm Handle Deep Copy'() {
88         given: 'a yang model cm handle'
89             def currentYangModelCmHandle = new YangModelCmHandle(id: 'cmhandle',
90                 publicProperties: [new YangModelCmHandle.Property('publicProperty1', 'value1')],
91                 dmiProperties: [new YangModelCmHandle.Property('dmiProperty1', 'value1')],
92                 compositeState: new CompositeState(cmHandleState: CmHandleState.ADVISED, dataSyncEnabled: false))
93         when: 'a deep copy is created'
94             def yangModelCmhandleDeepCopy = YangModelCmHandle.deepCopyOf(currentYangModelCmHandle)
95         and: 'we try to mutate current yang model cm handle'
96             currentYangModelCmHandle.id = 'cmhandle-changed'
97             currentYangModelCmHandle.dmiProperties = [new YangModelCmHandle.Property('updatedPublicProperty1', 'value1')]
98             currentYangModelCmHandle.publicProperties = [new YangModelCmHandle.Property('updatedDmiProperty1', 'value1')]
99             currentYangModelCmHandle.compositeState.cmHandleState = CmHandleState.READY
100             currentYangModelCmHandle.compositeState.dataSyncEnabled = true
101         then: 'there is no change in the deep copied object'
102             assert yangModelCmhandleDeepCopy.id == 'cmhandle'
103             assert yangModelCmhandleDeepCopy.dmiProperties == [new YangModelCmHandle.Property('dmiProperty1', 'value1')]
104             assert yangModelCmhandleDeepCopy.publicProperties == [new YangModelCmHandle.Property('publicProperty1', 'value1')]
105             assert yangModelCmhandleDeepCopy.compositeState.cmHandleState == CmHandleState.ADVISED
106             assert yangModelCmhandleDeepCopy.compositeState.dataSyncEnabled == false
107         and: 'equality on reference and hashcode behave as expected'
108             assert currentYangModelCmHandle.hashCode() != yangModelCmhandleDeepCopy.hashCode()
109             assert currentYangModelCmHandle != yangModelCmhandleDeepCopy
110
111     }
112
113
114 }