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.impl
24 import org.onap.cps.spi.exceptions.DataValidationException
26 import static org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError.CM_HANDLE_DOES_NOT_EXIST
27 import static org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError.CM_HANDLE_INVALID_ID
28 import static org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError.UNKNOWN_ERROR
29 import static org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.Status
31 import org.onap.cps.api.CpsDataService
32 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
33 import org.onap.cps.spi.FetchDescendantsOption
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 mockCpsDataService = Mock(CpsDataService)
43 def objectUnderTest = new NetworkCmProxyDataServicePropertyHandler(mockCpsDataService)
44 def dataspaceName = 'NCMP-Admin'
45 def anchorName = 'ncmp-dmi-registry'
46 def static cmHandleId = 'myHandle1'
47 def static cmHandleXpath = "/dmi-registry/cm-handles[@id='${cmHandleId}']"
48 def noTimeStamp = null
50 def static propertyDataNodes = [new DataNodeBuilder().withXpath("/dmi-registry/cm-handles[@id='${cmHandleId}']/additional-properties[@name='additionalProp1']").withLeaves(['name': 'additionalProp1', 'value': 'additionalValue1']).build(),
51 new DataNodeBuilder().withXpath("/dmi-registry/cm-handles[@id='${cmHandleId}']/additional-properties[@name='additionalProp2']").withLeaves(['name': 'additionalProp2', 'value': 'additionalValue2']).build(),
52 new DataNodeBuilder().withXpath("/dmi-registry/cm-handles[@id='${cmHandleId}']/public-properties[@name='publicProp3']").withLeaves(['name': 'publicProp3', 'value': 'publicValue3']).build(),
53 new DataNodeBuilder().withXpath("/dmi-registry/cm-handles[@id='${cmHandleId}']/public-properties[@name='publicProp4']").withLeaves(['name': 'publicProp4', 'value': 'publicValue4']).build()]
54 def static cmHandleDataNode = new DataNode(xpath: cmHandleXpath, childDataNodes: propertyDataNodes)
56 def 'Update CM Handle Public Properties: #scenario'() {
57 given: 'the CPS service return a CM handle'
58 mockCpsDataService.getDataNode(dataspaceName, anchorName, cmHandleXpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> cmHandleDataNode
59 and: 'an update cm handle request with public properties updates'
60 def cmHandleUpdateRequest = [new NcmpServiceCmHandle(cmHandleId: cmHandleId, publicProperties: updatedPublicProperties)]
61 when: 'update data node leaves is called with the update request'
62 objectUnderTest.updateCmHandleProperties(cmHandleUpdateRequest)
63 then: 'the replace list method is called with correct params'
64 1 * mockCpsDataService.replaceListContent(dataspaceName, anchorName, cmHandleXpath, _, noTimeStamp) >> { args ->
66 assert args[3].leaves.size() == expectedPropertiesAfterUpdate.size()
67 assert args[3].leaves.containsAll(convertToProperties(expectedPropertiesAfterUpdate))
70 where: 'following public properties updates are made'
71 scenario | updatedPublicProperties || expectedPropertiesAfterUpdate
72 'property added' | ['newPubProp1': 'pub-val'] || [['publicProp3': 'publicValue3'], ['publicProp4': 'publicValue4'], ['newPubProp1': 'pub-val']]
73 'property updated' | ['publicProp4': 'newPubVal'] || [['publicProp3': 'publicValue3'], ['publicProp4': 'newPubVal']]
74 'property removed' | ['publicProp4': null] || [['publicProp3': 'publicValue3']]
75 'property ignored(value is null)' | ['pub-prop': null] || [['publicProp3': 'publicValue3'], ['publicProp4': 'publicValue4']]
78 def 'Update DMI Properties: #scenario'() {
79 given: 'the CPS service return a CM handle'
80 mockCpsDataService.getDataNode(dataspaceName, anchorName, cmHandleXpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> cmHandleDataNode
81 and: 'an update cm handle request with DMI properties updates'
82 def cmHandleUpdateRequest = [new NcmpServiceCmHandle(cmHandleId: cmHandleId, dmiProperties: updatedDmiProperties)]
83 when: 'update data node leaves is called with the update request'
84 objectUnderTest.updateCmHandleProperties(cmHandleUpdateRequest)
85 then: 'replace list method should is called with correct params'
86 expectedCallsToReplaceMethod * mockCpsDataService.replaceListContent(dataspaceName, anchorName, cmHandleXpath, _, noTimeStamp) >> { args ->
88 assert args[3].leaves.size() == expectedPropertiesAfterUpdate.size()
89 assert args[3].leaves.containsAll(convertToProperties(expectedPropertiesAfterUpdate))
92 where: 'following DMI properties updates are made'
93 scenario | updatedDmiProperties || expectedPropertiesAfterUpdate | expectedCallsToReplaceMethod
94 'property added' | ['newAdditionalProp1': 'add-value'] || [['additionalProp1': 'additionalValue1'], ['additionalProp2': 'additionalValue2'], ['newAdditionalProp1': 'add-value']] | 1
95 'property updated' | ['additionalProp1': 'newValue'] || [['additionalProp2': 'additionalValue2'], ['additionalProp1': 'newValue']] | 1
96 'property removed' | ['additionalProp1': null] || [['additionalProp2': 'additionalValue2']] | 1
97 'property ignored(value is null)' | ['new-prop': null] || [['additionalProp1': 'additionalValue1'], ['additionalProp2': 'additionalValue2']] | 1
98 'no property changes' | [:] || [['additionalProp1': 'additionalValue1'], ['additionalProp2': 'additionalValue2']] | 0
101 def 'Update CM Handle Properties, remove all properties: #scenario'() {
102 given: 'the CPS service return a CM handle'
103 def cmHandleDataNode = new DataNode(xpath: cmHandleXpath, childDataNodes: originalPropertyDataNodes)
104 mockCpsDataService.getDataNode(dataspaceName, anchorName, cmHandleXpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> cmHandleDataNode
105 and: 'an update cm handle request that removes all public properties(existing and non-existing)'
106 def cmHandleUpdateRequest = [new NcmpServiceCmHandle(cmHandleId: cmHandleId, publicProperties: ['publicProp3': null, 'publicProp4': null])]
107 when: 'update data node leaves is called with the update request'
108 objectUnderTest.updateCmHandleProperties(cmHandleUpdateRequest)
109 then: 'the replace list method is not called'
110 0 * mockCpsDataService.replaceListContent(*_)
111 then: 'delete data node will be called for any existing property'
112 expectedCallsToDeleteDataNode * mockCpsDataService.deleteDataNode(dataspaceName, anchorName, _, noTimeStamp) >> { arg ->
114 assert arg[2].contains("@name='publicProp")
117 where: 'following public properties updates are made'
118 scenario | originalPropertyDataNodes || expectedCallsToDeleteDataNode
119 '2 original properties, both removed' | propertyDataNodes || 2
120 'no original properties' | [] || 0
123 def '#scenario error leads to #exception when we try to update cmHandle'() {
124 given: 'cm handles request'
125 def cmHandleUpdateRequest = [new NcmpServiceCmHandle(cmHandleId: cmHandleId, publicProperties: [:], dmiProperties: [:])]
126 and: 'data node cannot be found'
127 mockCpsDataService.getDataNode(*_) >> { throw exception }
128 when: 'update data node leaves is called using correct parameters'
129 def response = objectUnderTest.updateCmHandleProperties(cmHandleUpdateRequest)
130 then: 'one failed registration response'
132 and: 'it has expected error details'
133 with(response.get(0)) {
134 assert it.status == Status.FAILURE
135 assert it.cmHandle == cmHandleId
136 assert it.registrationError == expectedError
137 assert it.errorText == expectedErrorText
140 scenario | cmHandleId | exception || expectedError | expectedErrorText
141 'Cm Handle does not exist' | 'cmHandleId' | new DataNodeNotFoundException('NCMP-Admin', 'ncmp-dmi-registry') || CM_HANDLE_DOES_NOT_EXIST | 'cm-handle does not exist'
142 'Unknown' | 'cmHandleId' | new RuntimeException('Failed') || UNKNOWN_ERROR | 'Failed'
143 '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'
146 def 'Multiple update operations in a single request'() {
147 given: 'cm handles request'
148 def cmHandleUpdateRequest = [new NcmpServiceCmHandle(cmHandleId: cmHandleId, publicProperties: ['publicProp1': "value"], dmiProperties: [:]),
149 new NcmpServiceCmHandle(cmHandleId: cmHandleId, publicProperties: ['publicProp1': "value"], dmiProperties: [:]),
150 new NcmpServiceCmHandle(cmHandleId: cmHandleId, publicProperties: ['publicProp1': "value"], dmiProperties: [:])]
151 and: 'data node can be found for 1st and 3rd cm-handle but not for 2nd cm-handle'
152 mockCpsDataService.getDataNode(*_) >> cmHandleDataNode >> { throw new DataNodeNotFoundException('NCMP-Admin', 'ncmp-dmi-registry') } >> cmHandleDataNode
153 when: 'update data node leaves is called using correct parameters'
154 def cmHandleResponseList = objectUnderTest.updateCmHandleProperties(cmHandleUpdateRequest)
155 then: 'response has 3 values'
156 cmHandleResponseList.size() == 3
157 and: 'the 1st and 3rd requests were processed successfully'
158 with(cmHandleResponseList.get(0)) {
159 assert it.status == Status.SUCCESS
160 assert it.cmHandle == cmHandleId
162 with(cmHandleResponseList.get(2)) {
163 assert it.status == Status.SUCCESS
164 assert it.cmHandle == cmHandleId
166 and: 'the 2nd request failed with correct error code'
167 with(cmHandleResponseList.get(1)) {
168 assert it.status == Status.FAILURE
169 assert it.cmHandle == cmHandleId
170 assert it.registrationError == CM_HANDLE_DOES_NOT_EXIST
171 assert it.errorText == "cm-handle does not exist"
173 then: 'the replace list method is called twice'
174 2 * mockCpsDataService.replaceListContent(*_)
177 def convertToProperties(expectedPropertiesAfterUpdateAsMap) {
178 def properties = [].withDefault { [:] }
179 expectedPropertiesAfterUpdateAsMap.forEach(property ->
180 property.forEach((key, val) -> {
181 properties.add(['name': key, 'value': val])