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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.api.inventory
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
35 import java.time.OffsetDateTime
36 import java.time.ZoneOffset
37 import java.time.format.DateTimeFormatter
39 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
40 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
42 class InventoryPersistenceSpec extends Specification {
44 def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
46 def mockCpsDataService = Mock(CpsDataService)
48 def mockCpsDataPersistenceService = Mock(CpsDataPersistenceService)
51 def objectUnderTest = new InventoryPersistence(spiedJsonObjectMapper, mockCpsDataService, mockCpsDataPersistenceService)
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))
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']"
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"])]
65 def childDataNodesForCmHandleWithDMIProperties = [new DataNode(xpath: "/dmi-registry/cm-handles[@id='some-cm-handle']/additional-properties[@name='name1']", leaves: ["name":"name1", "value":"value1"])]
68 def childDataNodesForCmHandleWithPublicProperties = [new DataNode(xpath: "/dmi-registry/cm-handles[@id='some-cm-handle']/public-properties[@name='name2']", leaves: ["name":"name2","value":"value2"])]
71 def childDataNodesForCmHandleWithState = [new DataNode(xpath: "/dmi-registry/cm-handles[@id='some-cm-handle']/state", leaves: ['cm-handle-state': 'ADVISED'])]
74 def static sampleDataNodes = [new DataNode()]
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
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'
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
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
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 'DELETING' | CmHandleState.DELETING || '{"state":{"cm-handle-state":"DELETING","last-update-time":"2022-12-31T20:30:40.000+0000"}}'
150 def 'Get Cm Handles By State'() {
151 given: 'a cm handle state to query'
152 def cmHandleState = CmHandleState.ADVISED
153 and: 'cps data service returns a list of data nodes'
154 mockCpsDataPersistenceService.queryDataNodes('NCMP-Admin', 'ncmp-dmi-registry',
155 '//state[@cm-handle-state="ADVISED"]/ancestor::cm-handles', OMIT_DESCENDANTS) >> sampleDataNodes
156 when: 'get cm handles by state is invoked'
157 def result = objectUnderTest.getCmHandlesByState(cmHandleState)
158 then: 'the returned result is a list of data nodes returned by cps data service'
159 assert result == sampleDataNodes
162 def 'Get Cm Handles By State and Cm-Handle Id'() {
163 given: 'a cm handle state to query'
164 def cmHandleState = CmHandleState.READY
165 and: 'cps data service returns a list of data nodes'
166 mockCpsDataPersistenceService.queryDataNodes('NCMP-Admin', 'ncmp-dmi-registry',
167 '//cm-handles[@id=\'some-cm-handle\']/state[@cm-handle-state="'+ 'READY'+'"]/ancestor::cm-handles', OMIT_DESCENDANTS) >> sampleDataNodes
168 when: 'get cm handles by state and id is invoked'
169 def result = objectUnderTest.getCmHandlesByIdAndState(cmHandleId, cmHandleState)
170 then: 'the returned result is a list of data nodes returned by cps data service'
171 assert result == sampleDataNodes
174 def 'Get Cm Handles By Operational Sync State : UNSYNCHRONIZED'() {
175 given: 'a cm handle state to query'
176 def cmHandleState = CmHandleState.READY
177 and: 'cps data service returns a list of data nodes'
178 mockCpsDataPersistenceService.queryDataNodes('NCMP-Admin', 'ncmp-dmi-registry',
179 '//state/datastores/operational[@sync-state="'+'UNSYNCHRONIZED'+'"]/ancestor::cm-handles', OMIT_DESCENDANTS) >> sampleDataNodes
180 when: 'get cm handles by operational sync state as UNSYNCHRONIZED is invoked'
181 def result = objectUnderTest.getCmHandlesByOperationalSyncState(SyncState.UNSYNCHRONIZED)
182 then: 'the returned result is a list of data nodes returned by cps data service'
183 assert result == sampleDataNodes
186 def 'Retrieve cm handle by cps path '() {
187 given: 'a cm handle state to query based on the cps path'
188 def cmHandleDataNode = new DataNode(xpath: 'xpath', leaves: ['cm-handle-state': 'LOCKED'])
189 def cpsPath = '//cps-path'
190 and: 'cps data service returns a valid data node'
191 mockCpsDataPersistenceService.queryDataNodes('NCMP-Admin', 'ncmp-dmi-registry',
192 cpsPath, INCLUDE_ALL_DESCENDANTS)
193 >> Arrays.asList(cmHandleDataNode)
194 when: 'get cm handles by cps path is invoked'
195 def result = objectUnderTest.getCmHandleDataNodesByCpsPath(cpsPath, INCLUDE_ALL_DESCENDANTS)
196 then: 'the returned result is a list of data nodes returned by cps data service'
197 assert result.contains(cmHandleDataNode)