Merge "RTD change to document migration to Spring Boot 3.0"
[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)
51         then: 'the result has the right size'
52             assert objectUnderTest.dmiProperties.size() == 1
53         and: 'the DMI property in the result has the correct name and value'
54             assert objectUnderTest.dmiProperties[0].name == 'myDmiProperty'
55             assert objectUnderTest.dmiProperties[0].value == 'value1'
56         and: 'the public property in the result has the correct name and value'
57             assert objectUnderTest.publicProperties[0].name == 'myPublicProperty'
58             assert objectUnderTest.publicProperties[0].value == 'value2'
59         and: 'the composite state matches the composite state of the ncmpServiceCmHandle'
60             objectUnderTest.getCompositeState().cmHandleState == CmHandleState.LOCKED
61             objectUnderTest.getCompositeState() == ncmpServiceCmHandle.getCompositeState()
62     }
63
64     def 'Resolve DMI service name: #scenario and #requiredService service require.'() {
65         given: 'a yang model cm handle'
66             def objectUnderTest = YangModelCmHandle.toYangModelCmHandle(dmiServiceName, dmiDataServiceName, dmiModelServiceName, new NcmpServiceCmHandle(cmHandleId: 'cm-handle-id-1'))
67         expect:
68             assert objectUnderTest.resolveDmiServiceName(requiredService) == expectedService
69         where:
70             scenario                        | dmiServiceName     | dmiDataServiceName | dmiModelServiceName | requiredService || expectedService
71             'common service registered'     | 'common service'   | 'does not matter'  | 'does not matter'   | DATA            || 'common service'
72             'common service registered'     | 'common service'   | 'does not matter'  | 'does not matter'   | MODEL           || 'common service'
73             'common service empty'          | ''                 | 'data service'     | 'does not matter'   | DATA            || 'data service'
74             'common service empty'          | ''                 | 'does not matter'  | 'model service'     | MODEL           || 'model service'
75             'common service blank'          | '   '              | 'data service'     | 'does not matter'   | DATA            || 'data service'
76             'common service blank'          | '   '              | 'does not matter'  | 'model service'     | MODEL           || 'model service'
77             'common service null '          | null               | 'data service'     | 'does not matter'   | DATA            || 'data service'
78             'common service null'           | null               | 'does not matter'  | 'model service'     | MODEL           || 'model service'
79             'only model service registered' | null               | null               | 'does not matter'   | DATA            || null
80             'only data service registered'  | null               | 'does not matter'  | null                | MODEL           || null
81     }
82
83     def 'Yang Model Cm Handle Deep Copy'() {
84         given: 'a yang model cm handle'
85             def currentYangModelCmHandle = new YangModelCmHandle(id: 'cmhandle',
86                 publicProperties: [new YangModelCmHandle.Property('publicProperty1', 'value1')],
87                 dmiProperties: [new YangModelCmHandle.Property('dmiProperty1', 'value1')],
88                 compositeState: new CompositeState(cmHandleState: CmHandleState.ADVISED, dataSyncEnabled: false))
89         when: 'a deep copy is created'
90             def yangModelCmhandleDeepCopy = YangModelCmHandle.deepCopyOf(currentYangModelCmHandle)
91         and: 'we try to mutate current yang model cm handle'
92             currentYangModelCmHandle.id = 'cmhandle-changed'
93             currentYangModelCmHandle.dmiProperties = [new YangModelCmHandle.Property('updatedPublicProperty1', 'value1')]
94             currentYangModelCmHandle.publicProperties = [new YangModelCmHandle.Property('updatedDmiProperty1', 'value1')]
95             currentYangModelCmHandle.compositeState.cmHandleState = CmHandleState.READY
96             currentYangModelCmHandle.compositeState.dataSyncEnabled = true
97         then: 'there is no change in the deep copied object'
98             assert yangModelCmhandleDeepCopy.id == 'cmhandle'
99             assert yangModelCmhandleDeepCopy.dmiProperties == [new YangModelCmHandle.Property('dmiProperty1', 'value1')]
100             assert yangModelCmhandleDeepCopy.publicProperties == [new YangModelCmHandle.Property('publicProperty1', 'value1')]
101             assert yangModelCmhandleDeepCopy.compositeState.cmHandleState == CmHandleState.ADVISED
102             assert yangModelCmhandleDeepCopy.compositeState.dataSyncEnabled == false
103         and: 'equality on reference and hashcode behave as expected'
104             assert currentYangModelCmHandle.hashCode() != yangModelCmhandleDeepCopy.hashCode()
105             assert currentYangModelCmHandle != yangModelCmhandleDeepCopy
106
107     }
108
109
110 }