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