2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.inventory
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.onap.cps.api.CpsDataService
25 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
26 import org.onap.cps.spi.CpsDataPersistenceService
27 import org.onap.cps.spi.FetchDescendantsOption
28 import org.onap.cps.spi.exceptions.DataValidationException
29 import org.onap.cps.spi.model.DataNode
30 import org.onap.cps.utils.JsonObjectMapper
31 import spock.lang.Shared
32 import spock.lang.Specification
34 import java.time.OffsetDateTime
35 import java.time.ZoneOffset
36 import java.time.format.DateTimeFormatter
38 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
39 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
41 class InventoryPersistenceSpec extends Specification {
43 def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
45 def mockCpsDataService = Mock(CpsDataService)
47 def mockCpsDataPersistenceService = Mock(CpsDataPersistenceService)
50 def objectUnderTest = new InventoryPersistence(spiedJsonObjectMapper, mockCpsDataService, mockCpsDataPersistenceService)
52 def formattedDateAndTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
53 .format(OffsetDateTime.of(2022, 12, 31, 20, 30, 40, 1, ZoneOffset.UTC))
55 def cmHandleId = 'some-cm-handle'
56 def leaves = ["dmi-service-name":"common service name","dmi-data-service-name":"data service name","dmi-model-service-name":"model service name"]
57 def xpath = "/dmi-registry/cm-handles[@id='some-cm-handle']"
60 def childDataNodesForCmHandleWithAllProperties = [new DataNode(xpath: "/dmi-registry/cm-handles[@id='some cm handle']/additional-properties[@name='name1']", leaves: ["name":"name1", "value":"value1"]),
61 new DataNode(xpath: "/dmi-registry/cm-handles[@id='some cm handle']/public-properties[@name='name2']", leaves: ["name":"name2","value":"value2"])]
64 def childDataNodesForCmHandleWithDMIProperties = [new DataNode(xpath: "/dmi-registry/cm-handles[@id='some-cm-handle']/additional-properties[@name='name1']", leaves: ["name":"name1", "value":"value1"])]
67 def childDataNodesForCmHandleWithPublicProperties = [new DataNode(xpath: "/dmi-registry/cm-handles[@id='some-cm-handle']/public-properties[@name='name2']", leaves: ["name":"name2","value":"value2"])]
70 def childDataNodesForCmHandleWithState = [new DataNode(xpath: "/dmi-registry/cm-handles[@id='some-cm-handle']/state", leaves: ['cm-handle-state': 'ADVISED'])]
72 def "Retrieve CmHandle using datanode with #scenario."() {
73 given: 'the cps data service returns a data node from the DMI registry'
74 def dataNode = new DataNode(childDataNodes:childDataNodes, leaves: leaves)
75 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry', xpath, INCLUDE_ALL_DESCENDANTS) >> dataNode
76 when: 'retrieving the yang modelled cm handle'
77 def result = objectUnderTest.getYangModelCmHandle(cmHandleId)
78 then: 'the result has the correct id and service names'
79 result.id == cmHandleId
80 result.dmiServiceName == 'common service name'
81 result.dmiDataServiceName == 'data service name'
82 result.dmiModelServiceName == 'model service name'
83 and: 'the expected DMI properties'
84 result.dmiProperties == expectedDmiProperties
85 result.publicProperties == expectedPublicProperties
86 and: 'the state details are returned'
87 result.compositeState.cmHandleState == expectedCompositeState
88 where: 'the following parameters are used'
89 scenario | childDataNodes || expectedDmiProperties || expectedPublicProperties || expectedCompositeState
90 'no properties' | [] || [] || [] || null
91 'DMI and public properties' | childDataNodesForCmHandleWithAllProperties || [new YangModelCmHandle.Property("name1", "value1")] || [new YangModelCmHandle.Property("name2", "value2")] || null
92 'just DMI properties' | childDataNodesForCmHandleWithDMIProperties || [new YangModelCmHandle.Property("name1", "value1")] || [] || null
93 'just public properties' | childDataNodesForCmHandleWithPublicProperties || [] || [new YangModelCmHandle.Property("name2", "value2")] || null
94 'with state details' | childDataNodesForCmHandleWithState || [] || [] || CmHandleState.ADVISED
97 def "Retrieve CmHandle using datanode with invalid CmHandle id."() {
98 when: 'retrieving the yang modelled cm handle with an invalid id'
99 def result = objectUnderTest.getYangModelCmHandle('cm handle id with spaces')
100 then: 'a data validation exception is thrown'
101 thrown(DataValidationException)
102 and: 'the result is not returned'
106 def "Handling missing service names as null CPS-1043."() {
107 given: 'the cps data service returns a data node from the DMI registry with empty child and leaf attributes'
108 def dataNode = new DataNode(childDataNodes:[], leaves: ["cm-handle-state":"ADVISED"])
109 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry', xpath, INCLUDE_ALL_DESCENDANTS) >> dataNode
110 when: 'retrieving the yang modelled cm handle'
111 def result = objectUnderTest.getYangModelCmHandle(cmHandleId)
112 then: 'the service names ae returned as null'
113 result.dmiServiceName == null
114 result.dmiDataServiceName == null
115 result.dmiModelServiceName == null
118 def 'Get a Cm Handle Composite State'() {
119 given: 'a valid cm handle id'
120 def cmHandleId = 'Some-Cm-Handle'
121 def dataNode = new DataNode(leaves: ['cm-handle-state': 'ADVISED'])
122 and: 'cps data service returns a valid data node'
123 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
124 '/dmi-registry/cm-handles[@id=\'Some-Cm-Handle\']/state', FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
125 when: 'get cm handle state is invoked'
126 def result = objectUnderTest.getCmHandleState(cmHandleId)
127 then: 'result has returned the correct cm handle state'
128 result.cmHandleState == CmHandleState.ADVISED
131 def 'Update Cm Handle with #scenario State'() {
132 given: 'a cm handle and a composite state'
133 def cmHandleId = 'Some-Cm-Handle'
134 def compositeState = new CompositeState(cmHandleState: cmHandleState, lastUpdateTime: formattedDateAndTime)
135 when: 'update cm handle state is invoked with the #scenario state'
136 objectUnderTest.saveCmHandleState(cmHandleId, compositeState)
137 then: 'update node leaves is invoked with the correct params'
138 1 * mockCpsDataService.replaceNodeTree('NCMP-Admin', 'ncmp-dmi-registry', '/dmi-registry/cm-handles[@id=\'Some-Cm-Handle\']', expectedJsonData, _ as OffsetDateTime)
139 where: 'the following states are used'
140 scenario | cmHandleState || expectedJsonData
141 'READY' | CmHandleState.READY || '{"state":{"cm-handle-state":"READY","last-update-time":"2022-12-31T20:30:40.000+0000"}}'
142 'LOCKED' | CmHandleState.LOCKED || '{"state":{"cm-handle-state":"LOCKED","last-update-time":"2022-12-31T20:30:40.000+0000"}}'
145 def 'Get Cm Handles By State'() {
146 given: 'a cm handle state to query'
147 def cmHandleState = CmHandleState.ADVISED
148 and: 'cps data service returns a list of data nodes'
149 def dataNodes = [new DataNode()]
150 mockCpsDataPersistenceService.queryDataNodes('NCMP-Admin', 'ncmp-dmi-registry',
151 '//state[@cm-handle-state="ADVISED"]/ancestor::cm-handles', OMIT_DESCENDANTS) >> dataNodes
152 when: 'get cm handles by state is invoked'
153 def result = objectUnderTest.getCmHandlesByState(cmHandleState)
154 then: 'the returned result is a list of data nodes returned by cps data service'
155 assert result == dataNodes