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.impl
23 import org.onap.cps.api.CpsDataService
24 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
25 import org.onap.cps.spi.FetchDescendantsOption
26 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
27 import org.onap.cps.spi.exceptions.DataValidationException
28 import org.onap.cps.spi.model.DataNode
29 import org.onap.cps.spi.model.DataNodeBuilder
30 import spock.lang.Specification
32 class NetworkCmProxyDataServicePropertyHandlerSpec extends Specification {
34 def mockCpsDataService = Mock(CpsDataService)
36 def objectUnderTest = new NetworkCmProxyDataServicePropertyHandler(mockCpsDataService)
37 def dataspaceName = 'NCMP-Admin'
38 def anchorName = 'ncmp-dmi-registry'
39 def static cmHandleId = 'myHandle1'
40 def static cmHandleXpath = "/dmi-registry/cm-handles[@id='${cmHandleId}']"
41 def noTimeStamp = null
43 def static propertyDataNodes = [new DataNodeBuilder().withXpath("/dmi-registry/cm-handles[@id='${cmHandleId}']/additional-properties[@name='additionalProp1']").withLeaves(['name': 'additionalProp1', 'value': 'additionalValue1']).build(),
44 new DataNodeBuilder().withXpath("/dmi-registry/cm-handles[@id='${cmHandleId}']/additional-properties[@name='additionalProp2']").withLeaves(['name': 'additionalProp2', 'value': 'additionalValue2']).build(),
45 new DataNodeBuilder().withXpath("/dmi-registry/cm-handles[@id='${cmHandleId}']/public-properties[@name='publicProp3']").withLeaves(['name': 'publicProp3', 'value': 'publicValue3']).build(),
46 new DataNodeBuilder().withXpath("/dmi-registry/cm-handles[@id='${cmHandleId}']/public-properties[@name='publicProp4']").withLeaves(['name': 'publicProp4', 'value': 'publicValue4']).build()]
47 def static cmHandleDataNode = new DataNode(xpath: cmHandleXpath, childDataNodes: propertyDataNodes)
49 def 'Update CM Handle Public Properties: #scenario'() {
50 given: 'the CPS service return a CM handle'
51 mockCpsDataService.getDataNode(dataspaceName, anchorName, cmHandleXpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> cmHandleDataNode
52 and: 'an update cm handle request with public properties updates'
53 def cmHandleUpdateRequest = [new NcmpServiceCmHandle(cmHandleID: cmHandleId, publicProperties: updatedPublicProperties)]
54 when: 'update data node leaves is called with the update request'
55 objectUnderTest.updateCmHandleProperties(cmHandleUpdateRequest)
56 then: 'the replace list method is called with correct params'
57 1 * mockCpsDataService.replaceListContent(dataspaceName, anchorName, cmHandleXpath, _, noTimeStamp) >> { args ->
59 assert args[3].leaves.size() == expectedPropertiesAfterUpdate.size()
60 assert args[3].leaves.containsAll(convertToProperties(expectedPropertiesAfterUpdate))
63 where: 'following public properties updates are made'
64 scenario | updatedPublicProperties || expectedPropertiesAfterUpdate
65 'property added' | ['newPubProp1': 'pub-val'] || [['publicProp3': 'publicValue3'], ['publicProp4': 'publicValue4'], ['newPubProp1': 'pub-val']]
66 'property updated' | ['publicProp4': 'newPubVal'] || [['publicProp3': 'publicValue3'], ['publicProp4': 'newPubVal']]
67 'property removed' | ['publicProp4': null] || [['publicProp3': 'publicValue3']]
68 'property ignored(value is null)' | ['pub-prop': null] || [['publicProp3': 'publicValue3'], ['publicProp4': 'publicValue4']]
71 def 'Update DMI Properties: #scenario'() {
72 given: 'the CPS service return a CM handle'
73 mockCpsDataService.getDataNode(dataspaceName, anchorName, cmHandleXpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> cmHandleDataNode
74 and: 'an update cm handle request with DMI properties updates'
75 def cmHandleUpdateRequest = [new NcmpServiceCmHandle(cmHandleID: cmHandleId, dmiProperties: updatedDmiProperties)]
76 when: 'update data node leaves is called with the update request'
77 objectUnderTest.updateCmHandleProperties(cmHandleUpdateRequest)
78 then: 'replace list method should is called with correct params'
79 expectedCallsToReplaceMethod * mockCpsDataService.replaceListContent(dataspaceName, anchorName, cmHandleXpath, _, noTimeStamp) >> { args ->
81 assert args[3].leaves.size() == expectedPropertiesAfterUpdate.size()
82 assert args[3].leaves.containsAll(convertToProperties(expectedPropertiesAfterUpdate))
85 where: 'following DMI properties updates are made'
86 scenario | updatedDmiProperties || expectedPropertiesAfterUpdate | expectedCallsToReplaceMethod
87 'property added' | ['newAdditionalProp1': 'add-value'] || [['additionalProp1': 'additionalValue1'], ['additionalProp2': 'additionalValue2'], ['newAdditionalProp1': 'add-value']] | 1
88 'property updated' | ['additionalProp1': 'newValue'] || [['additionalProp2': 'additionalValue2'], ['additionalProp1': 'newValue']] | 1
89 'property removed' | ['additionalProp1': null] || [['additionalProp2': 'additionalValue2']] | 1
90 'property ignored(value is null)' | ['new-prop': null] || [['additionalProp1': 'additionalValue1'], ['additionalProp2': 'additionalValue2']] | 1
91 'no property changes' | [:] || [['additionalProp1': 'additionalValue1'], ['additionalProp2': 'additionalValue2']] | 0
94 def 'Update CM Handle Properties, remove all properties: #scenario'() {
95 given: 'the CPS service return a CM handle'
96 def cmHandleDataNode = new DataNode(xpath: cmHandleXpath, childDataNodes: originalPropertyDataNodes)
97 mockCpsDataService.getDataNode(dataspaceName, anchorName, cmHandleXpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> cmHandleDataNode
98 and: 'an update cm handle request that removes all public properties(existing and non-existing)'
99 def cmHandleUpdateRequest = [new NcmpServiceCmHandle(cmHandleID: cmHandleId, publicProperties: ['publicProp3': null, 'publicProp4': null])]
100 when: 'update data node leaves is called with the update request'
101 objectUnderTest.updateCmHandleProperties(cmHandleUpdateRequest)
102 then: 'the replace list method is not called'
103 0 * mockCpsDataService.replaceListContent(*_)
104 then: 'delete data node will be called for any existing property'
105 expectedCallsToDeleteDataNode * mockCpsDataService.deleteDataNode(dataspaceName, anchorName, _, noTimeStamp) >> { arg ->
107 assert arg[2].contains("@name='publicProp")
110 where: 'following public properties updates are made'
111 scenario | originalPropertyDataNodes || expectedCallsToDeleteDataNode
112 '2 original properties, both removed' | propertyDataNodes || 2
113 'no original properties' | [] || 0
116 def 'Exception thrown when we try to update cmHandle'() {
117 given: 'cm handles request'
118 def cmHandleUpdateRequest = [new NcmpServiceCmHandle(cmHandleID: cmHandleId, publicProperties: [:], dmiProperties: [:])]
119 and: 'data node cannot be found'
120 mockCpsDataService.getDataNode(*_) >> { throw new DataNodeNotFoundException(dataspaceName, anchorName, cmHandleXpath) }
121 when: 'update data node leaves is called using correct parameters'
122 objectUnderTest.updateCmHandleProperties(cmHandleUpdateRequest)
123 then: 'data validation exception is thrown'
124 def exceptionThrown = thrown(DataValidationException.class)
125 assert exceptionThrown.getMessage().contains('DataNode not found')
128 def convertToProperties(expectedPropertiesAfterUpdateAsMap) {
129 def properties = [].withDefault { [:] }
130 expectedPropertiesAfterUpdateAsMap.forEach(property ->
131 property.forEach((key, val) -> {
132 properties.add(['name': key, 'value': val])