578225e87d5429ea1d31bc4ebfa5b186c81cbf66
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / inventory / InventoryPersistenceSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 Nordix Foundation
4  *  Modifications Copyright (C) 2022 Bell Canada
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.api.inventory
23
24 import com.fasterxml.jackson.databind.ObjectMapper
25 import org.onap.cps.api.CpsDataService
26 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
27 import org.onap.cps.spi.CpsDataPersistenceService
28 import org.onap.cps.spi.FetchDescendantsOption
29 import org.onap.cps.spi.exceptions.DataValidationException
30 import org.onap.cps.spi.model.DataNode
31 import org.onap.cps.utils.JsonObjectMapper
32 import spock.lang.Shared
33 import spock.lang.Specification
34
35 import java.time.OffsetDateTime
36 import java.time.ZoneOffset
37 import java.time.format.DateTimeFormatter
38
39 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
40 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
41
42 class InventoryPersistenceSpec extends Specification {
43
44     def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
45
46     def mockCpsDataService = Mock(CpsDataService)
47
48     def mockCpsDataPersistenceService = Mock(CpsDataPersistenceService)
49
50
51     def objectUnderTest = new InventoryPersistence(spiedJsonObjectMapper, mockCpsDataService, mockCpsDataPersistenceService)
52
53     def formattedDateAndTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
54             .format(OffsetDateTime.of(2022, 12, 31, 20, 30, 40, 1, ZoneOffset.UTC))
55
56     def cmHandleId = 'some-cm-handle'
57     def leaves = ["dmi-service-name":"common service name","dmi-data-service-name":"data service name","dmi-model-service-name":"model service name"]
58     def xpath = "/dmi-registry/cm-handles[@id='some-cm-handle']"
59
60     @Shared
61     def childDataNodesForCmHandleWithAllProperties = [new DataNode(xpath: "/dmi-registry/cm-handles[@id='some cm handle']/additional-properties[@name='name1']", leaves: ["name":"name1", "value":"value1"]),
62                                                       new DataNode(xpath: "/dmi-registry/cm-handles[@id='some cm handle']/public-properties[@name='name2']", leaves: ["name":"name2","value":"value2"])]
63
64     @Shared
65     def childDataNodesForCmHandleWithDMIProperties = [new DataNode(xpath: "/dmi-registry/cm-handles[@id='some-cm-handle']/additional-properties[@name='name1']", leaves: ["name":"name1", "value":"value1"])]
66
67     @Shared
68     def childDataNodesForCmHandleWithPublicProperties = [new DataNode(xpath: "/dmi-registry/cm-handles[@id='some-cm-handle']/public-properties[@name='name2']", leaves: ["name":"name2","value":"value2"])]
69
70     @Shared
71     def childDataNodesForCmHandleWithState = [new DataNode(xpath: "/dmi-registry/cm-handles[@id='some-cm-handle']/state", leaves: ['cm-handle-state': 'ADVISED'])]
72
73     @Shared
74     def static sampleDataNodes = [new DataNode()]
75
76     def "Retrieve CmHandle using datanode with #scenario."() {
77         given: 'the cps data service returns a data node from the DMI registry'
78             def dataNode = new DataNode(childDataNodes:childDataNodes, leaves: leaves)
79             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry', xpath, INCLUDE_ALL_DESCENDANTS) >> dataNode
80         when: 'retrieving the yang modelled cm handle'
81             def result = objectUnderTest.getYangModelCmHandle(cmHandleId)
82         then: 'the result has the correct id and service names'
83             result.id == cmHandleId
84             result.dmiServiceName == 'common service name'
85             result.dmiDataServiceName == 'data service name'
86             result.dmiModelServiceName == 'model service name'
87         and: 'the expected DMI properties'
88             result.dmiProperties == expectedDmiProperties
89             result.publicProperties == expectedPublicProperties
90         and: 'the state details are returned'
91             result.compositeState.cmHandleState == expectedCompositeState
92         where: 'the following parameters are used'
93             scenario                    | childDataNodes                                || expectedDmiProperties                               || expectedPublicProperties                              || expectedCompositeState
94             'no properties'             | []                                            || []                                                  || []                                                    || null
95             'DMI and public properties' | childDataNodesForCmHandleWithAllProperties    || [new YangModelCmHandle.Property("name1", "value1")] || [new YangModelCmHandle.Property("name2", "value2")] || null
96             'just DMI properties'       | childDataNodesForCmHandleWithDMIProperties    || [new YangModelCmHandle.Property("name1", "value1")] || []                                                    || null
97             'just public properties'    | childDataNodesForCmHandleWithPublicProperties || []                                                  || [new YangModelCmHandle.Property("name2", "value2")]   || null
98             'with state details'        | childDataNodesForCmHandleWithState            || []                                                  || []                                                    || CmHandleState.ADVISED
99     }
100
101     def "Retrieve CmHandle using datanode with invalid CmHandle id."() {
102         when: 'retrieving the yang modelled cm handle with an invalid id'
103             def result = objectUnderTest.getYangModelCmHandle('cm handle id with spaces')
104         then: 'a data validation exception is thrown'
105             thrown(DataValidationException)
106         and: 'the result is not returned'
107             result == null
108     }
109
110     def "Handling missing service names as null CPS-1043."() {
111         given: 'the cps data service returns a data node from the DMI registry with empty child and leaf attributes'
112             def dataNode = new DataNode(childDataNodes:[], leaves: [:])
113             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry', xpath, INCLUDE_ALL_DESCENDANTS) >> dataNode
114         when: 'retrieving the yang modelled cm handle'
115             def result = objectUnderTest.getYangModelCmHandle(cmHandleId)
116         then: 'the service names ae returned as null'
117             result.dmiServiceName == null
118             result.dmiDataServiceName == null
119             result.dmiModelServiceName == null
120     }
121
122     def 'Get a Cm Handle Composite State'() {
123         given: 'a valid cm handle id'
124             def cmHandleId = 'Some-Cm-Handle'
125             def dataNode = new DataNode(leaves: ['cm-handle-state': 'ADVISED'])
126         and: 'cps data service returns a valid data node'
127             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
128                     '/dmi-registry/cm-handles[@id=\'Some-Cm-Handle\']/state', FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
129         when: 'get cm handle state is invoked'
130             def result = objectUnderTest.getCmHandleState(cmHandleId)
131         then: 'result has returned the correct cm handle state'
132             result.cmHandleState == CmHandleState.ADVISED
133     }
134
135     def 'Update Cm Handle with #scenario State'() {
136         given: 'a cm handle and a composite state'
137             def cmHandleId = 'Some-Cm-Handle'
138             def compositeState = new CompositeState(cmHandleState: cmHandleState, lastUpdateTime: formattedDateAndTime)
139         when: 'update cm handle state is invoked with the #scenario state'
140             objectUnderTest.saveCmHandleState(cmHandleId, compositeState)
141         then: 'update node leaves is invoked with the correct params'
142             1 * mockCpsDataService.replaceNodeTree('NCMP-Admin', 'ncmp-dmi-registry', '/dmi-registry/cm-handles[@id=\'Some-Cm-Handle\']', expectedJsonData, _ as OffsetDateTime)
143         where: 'the following states are used'
144             scenario | cmHandleState        || expectedJsonData
145             'READY'   | CmHandleState.READY  || '{"state":{"cm-handle-state":"READY","last-update-time":"2022-12-31T20:30:40.000+0000"}}'
146             'LOCKED'  | CmHandleState.LOCKED || '{"state":{"cm-handle-state":"LOCKED","last-update-time":"2022-12-31T20:30:40.000+0000"}}'
147     }
148
149     def 'Get Cm Handles By State'() {
150         given: 'a cm handle state to query'
151             def cmHandleState = CmHandleState.ADVISED
152         and: 'cps data service returns a list of data nodes'
153             mockCpsDataPersistenceService.queryDataNodes('NCMP-Admin', 'ncmp-dmi-registry',
154                 '//state[@cm-handle-state="ADVISED"]/ancestor::cm-handles', OMIT_DESCENDANTS) >> sampleDataNodes
155         when: 'get cm handles by state is invoked'
156             def result = objectUnderTest.getCmHandlesByState(cmHandleState)
157         then: 'the returned result is a list of data nodes returned by cps data service'
158             assert result == sampleDataNodes
159     }
160
161     def 'Get Cm Handles By State and Cm-Handle Id'() {
162         given: 'a cm handle state to query'
163             def cmHandleState = CmHandleState.READY
164         and: 'cps data service returns a list of data nodes'
165             mockCpsDataPersistenceService.queryDataNodes('NCMP-Admin', 'ncmp-dmi-registry',
166                 '//cm-handles[@id=\'some-cm-handle\']/state[@cm-handle-state="'+ 'READY'+'"]/ancestor::cm-handles', OMIT_DESCENDANTS) >> sampleDataNodes
167         when: 'get cm handles by state and id is invoked'
168             def result = objectUnderTest.getCmHandlesByIdAndState(cmHandleId, cmHandleState)
169         then: 'the returned result is a list of data nodes returned by cps data service'
170             assert result == sampleDataNodes
171     }
172
173     def 'Get Cm Handles By Operational Sync State : UNSYNCHRONIZED'() {
174         given: 'a cm handle state to query'
175             def cmHandleState = CmHandleState.READY
176         and: 'cps data service returns a list of data nodes'
177             mockCpsDataPersistenceService.queryDataNodes('NCMP-Admin', 'ncmp-dmi-registry',
178                 '//state/datastores/operational[@sync-state="'+'UNSYNCHRONIZED'+'"]/ancestor::cm-handles', OMIT_DESCENDANTS) >> sampleDataNodes
179         when: 'get cm handles by operational sync state as UNSYNCHRONIZED is invoked'
180             def result = objectUnderTest.getCmHandlesByOperationalSyncState(SyncState.UNSYNCHRONIZED)
181         then: 'the returned result is a list of data nodes returned by cps data service'
182             assert result == sampleDataNodes
183     }
184
185     def 'Retrieve cm handle by cps path '() {
186         given: 'a cm handle state to query based on the cps path'
187             def cmHandleDataNode = new DataNode(xpath: 'xpath', leaves: ['cm-handle-state': 'LOCKED'])
188             def cpsPath = '//cps-path'
189         and: 'cps data service returns a valid data node'
190             mockCpsDataPersistenceService.queryDataNodes('NCMP-Admin', 'ncmp-dmi-registry',
191                     cpsPath, OMIT_DESCENDANTS)
192                     >> Arrays.asList(cmHandleDataNode)
193         when: 'get cm handles by cps path is invoked'
194             def result = objectUnderTest.getCmHandleDataNodesByCpsPath(cpsPath)
195         then: 'the returned result is a list of data nodes returned by cps data service'
196             assert result.contains(cmHandleDataNode)
197     }
198
199 }