2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Nordix Foundation
4 * Modifications Copyright (C) 2022 Bell Canada
5 * Modifications Copyright (C) 2023 TechMahindra Ltd.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.cps.ncmp.api.impl
25 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
26 import org.onap.cps.spi.exceptions.DataValidationException
28 import static org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError.CM_HANDLE_DOES_NOT_EXIST
29 import static org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError.CM_HANDLE_INVALID_ID
30 import static org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError.UNKNOWN_ERROR
31 import static org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.Status
33 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
34 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
35 import org.onap.cps.spi.model.DataNode
36 import org.onap.cps.spi.model.DataNodeBuilder
37 import spock.lang.Specification
39 class NetworkCmProxyDataServicePropertyHandlerSpec extends Specification {
41 def mockInventoryPersistence = Mock(InventoryPersistence)
43 def objectUnderTest = new NetworkCmProxyDataServicePropertyHandler(mockInventoryPersistence)
44 def static cmHandleId = 'myHandle1'
45 def static cmHandleXpath = "/dmi-registry/cm-handles[@id='${cmHandleId}']"
47 def static propertyDataNodes = [new DataNodeBuilder().withXpath("/dmi-registry/cm-handles[@id='${cmHandleId}']/additional-properties[@name='additionalProp1']").withLeaves(['name': 'additionalProp1', 'value': 'additionalValue1']).build(),
48 new DataNodeBuilder().withXpath("/dmi-registry/cm-handles[@id='${cmHandleId}']/additional-properties[@name='additionalProp2']").withLeaves(['name': 'additionalProp2', 'value': 'additionalValue2']).build(),
49 new DataNodeBuilder().withXpath("/dmi-registry/cm-handles[@id='${cmHandleId}']/public-properties[@name='publicProp3']").withLeaves(['name': 'publicProp3', 'value': 'publicValue3']).build(),
50 new DataNodeBuilder().withXpath("/dmi-registry/cm-handles[@id='${cmHandleId}']/public-properties[@name='publicProp4']").withLeaves(['name': 'publicProp4', 'value': 'publicValue4']).build()]
51 def static cmHandleDataNodeAsCollection = [new DataNode(xpath: cmHandleXpath, childDataNodes: propertyDataNodes)]
53 def 'Update CM Handle Public Properties: #scenario'() {
54 given: 'the CPS service return a CM handle'
55 mockInventoryPersistence.getCmHandleDataNode(cmHandleId) >> cmHandleDataNodeAsCollection
56 and: 'an update cm handle request with public properties updates'
57 def cmHandleUpdateRequest = [new NcmpServiceCmHandle(cmHandleId: cmHandleId, publicProperties: updatedPublicProperties)]
58 when: 'update data node leaves is called with the update request'
59 objectUnderTest.updateCmHandleProperties(cmHandleUpdateRequest)
60 then: 'the replace list method is called with correct params'
61 1 * mockInventoryPersistence.replaceListContent(cmHandleXpath,_) >> { args ->
63 assert args[1].leaves.size() == expectedPropertiesAfterUpdate.size()
64 assert args[1].leaves.containsAll(convertToProperties(expectedPropertiesAfterUpdate))
67 where: 'following public properties updates are made'
68 scenario | updatedPublicProperties || expectedPropertiesAfterUpdate
69 'property added' | ['newPubProp1': 'pub-val'] || [['publicProp3': 'publicValue3'], ['publicProp4': 'publicValue4'], ['newPubProp1': 'pub-val']]
70 'property updated' | ['publicProp4': 'newPubVal'] || [['publicProp3': 'publicValue3'], ['publicProp4': 'newPubVal']]
71 'property removed' | ['publicProp4': null] || [['publicProp3': 'publicValue3']]
72 'property ignored(value is null)' | ['pub-prop': null] || [['publicProp3': 'publicValue3'], ['publicProp4': 'publicValue4']]
75 def 'Update DMI Properties: #scenario'() {
76 given: 'the CPS service return a CM handle'
77 mockInventoryPersistence.getCmHandleDataNode(cmHandleId) >> cmHandleDataNodeAsCollection
78 and: 'an update cm handle request with DMI properties updates'
79 def cmHandleUpdateRequest = [new NcmpServiceCmHandle(cmHandleId: cmHandleId, dmiProperties: updatedDmiProperties)]
80 when: 'update data node leaves is called with the update request'
81 objectUnderTest.updateCmHandleProperties(cmHandleUpdateRequest)
82 then: 'replace list method should is called with correct params'
83 expectedCallsToReplaceMethod * mockInventoryPersistence.replaceListContent(cmHandleXpath, _) >> { args ->
85 assert args[1].leaves.size() == expectedPropertiesAfterUpdate.size()
86 assert args[1].leaves.containsAll(convertToProperties(expectedPropertiesAfterUpdate))
89 where: 'following DMI properties updates are made'
90 scenario | updatedDmiProperties || expectedPropertiesAfterUpdate | expectedCallsToReplaceMethod
91 'property added' | ['newAdditionalProp1': 'add-value'] || [['additionalProp1': 'additionalValue1'], ['additionalProp2': 'additionalValue2'], ['newAdditionalProp1': 'add-value']] | 1
92 'property updated' | ['additionalProp1': 'newValue'] || [['additionalProp2': 'additionalValue2'], ['additionalProp1': 'newValue']] | 1
93 'property removed' | ['additionalProp1': null] || [['additionalProp2': 'additionalValue2']] | 1
94 'property ignored(value is null)' | ['new-prop': null] || [['additionalProp1': 'additionalValue1'], ['additionalProp2': 'additionalValue2']] | 1
95 'no property changes' | [:] || [['additionalProp1': 'additionalValue1'], ['additionalProp2': 'additionalValue2']] | 0
98 def 'Update CM Handle Properties, remove all properties: #scenario'() {
99 given: 'the CPS service return a CM handle'
100 def cmHandleDataNode = new DataNode(xpath: cmHandleXpath, childDataNodes: originalPropertyDataNodes)
101 mockInventoryPersistence.getCmHandleDataNode(cmHandleId) >> [cmHandleDataNode]
102 and: 'an update cm handle request that removes all public properties(existing and non-existing)'
103 def cmHandleUpdateRequest = [new NcmpServiceCmHandle(cmHandleId: cmHandleId, publicProperties: ['publicProp3': null, 'publicProp4': null])]
104 when: 'update data node leaves is called with the update request'
105 objectUnderTest.updateCmHandleProperties(cmHandleUpdateRequest)
106 then: 'the replace list method is not called'
107 0 * mockInventoryPersistence.replaceListContent(*_)
108 then: 'delete data node will be called for any existing property'
109 expectedCallsToDeleteDataNode * mockInventoryPersistence.deleteDataNode(_) >> { arg ->
111 assert arg[0].contains("@name='publicProp")
114 where: 'following public properties updates are made'
115 scenario | originalPropertyDataNodes || expectedCallsToDeleteDataNode
116 '2 original properties, both removed' | propertyDataNodes || 2
117 'no original properties' | [] || 0
120 def '#scenario error leads to #exception when we try to update cmHandle'() {
121 given: 'cm handles request'
122 def cmHandleUpdateRequest = [new NcmpServiceCmHandle(cmHandleId: cmHandleId, publicProperties: [:], dmiProperties: [:])]
123 and: 'data node cannot be found'
124 mockInventoryPersistence.getCmHandleDataNode(*_) >> { throw exception }
125 when: 'update data node leaves is called using correct parameters'
126 def response = objectUnderTest.updateCmHandleProperties(cmHandleUpdateRequest)
127 then: 'one failed registration response'
129 and: 'it has expected error details'
130 with(response.get(0)) {
131 assert it.status == Status.FAILURE
132 assert it.cmHandle == cmHandleId
133 assert it.registrationError == expectedError
134 assert it.errorText == expectedErrorText
137 scenario | cmHandleId | exception || expectedError | expectedErrorText
138 'Cm Handle does not exist' | 'cmHandleId' | new DataNodeNotFoundException('NCMP-Admin', 'ncmp-dmi-registry') || CM_HANDLE_DOES_NOT_EXIST | 'cm-handle does not exist'
139 'Unknown' | 'cmHandleId' | new RuntimeException('Failed') || UNKNOWN_ERROR | 'Failed'
140 'Invalid cm handle id' | 'cmHandleId with spaces' | new DataValidationException('Name Validation Error.', cmHandleId + 'contains an invalid character') || CM_HANDLE_INVALID_ID | 'cm-handle has an invalid character(s) in id'
143 def 'Multiple update operations in a single request'() {
144 given: 'cm handles request'
145 def cmHandleUpdateRequest = [new NcmpServiceCmHandle(cmHandleId: cmHandleId, publicProperties: ['publicProp1': "value"], dmiProperties: [:]),
146 new NcmpServiceCmHandle(cmHandleId: cmHandleId, publicProperties: ['publicProp1': "value"], dmiProperties: [:]),
147 new NcmpServiceCmHandle(cmHandleId: cmHandleId, publicProperties: ['publicProp1': "value"], dmiProperties: [:])]
148 and: 'data node can be found for 1st and 3rd cm-handle but not for 2nd cm-handle'
149 mockInventoryPersistence.getCmHandleDataNode(*_) >> cmHandleDataNodeAsCollection >> {
150 throw new DataNodeNotFoundException('NCMP-Admin', 'ncmp-dmi-registry') } >> cmHandleDataNodeAsCollection
151 when: 'update data node leaves is called using correct parameters'
152 def cmHandleResponseList = objectUnderTest.updateCmHandleProperties(cmHandleUpdateRequest)
153 then: 'response has 3 values'
154 cmHandleResponseList.size() == 3
155 and: 'the 1st and 3rd requests were processed successfully'
156 with(cmHandleResponseList.get(0)) {
157 assert it.status == Status.SUCCESS
158 assert it.cmHandle == cmHandleId
160 with(cmHandleResponseList.get(2)) {
161 assert it.status == Status.SUCCESS
162 assert it.cmHandle == cmHandleId
164 and: 'the 2nd request failed with correct error code'
165 with(cmHandleResponseList.get(1)) {
166 assert it.status == Status.FAILURE
167 assert it.cmHandle == cmHandleId
168 assert it.registrationError == CM_HANDLE_DOES_NOT_EXIST
169 assert it.errorText == "cm-handle does not exist"
171 then: 'the replace list method is called twice'
172 2 * mockInventoryPersistence.replaceListContent(cmHandleXpath,_)
175 def convertToProperties(expectedPropertiesAfterUpdateAsMap) {
176 def properties = [].withDefault { [:] }
177 expectedPropertiesAfterUpdateAsMap.forEach(property ->
178 property.forEach((key, val) -> {
179 properties.add(['name': key, 'value': val])