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