2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Pantheon.tech
4 * Modification Copyright (C) 2021 highstreet technologies GmbH
5 * Modification Copyright (C) 2021 Nordix Foundation
6 * Modification Copyright (C) 2021 Bell Canada.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * 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.rest.controller
25 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
26 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
27 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
28 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch
29 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
30 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put
32 import com.fasterxml.jackson.databind.ObjectMapper
33 import com.google.gson.Gson
34 import org.onap.cps.TestUtils
35 import org.onap.cps.ncmp.api.NetworkCmProxyDataService
36 import org.onap.cps.spi.model.DataNodeBuilder
37 import org.spockframework.spring.SpringBean
38 import org.springframework.beans.factory.annotation.Autowired
39 import org.springframework.beans.factory.annotation.Value
40 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
41 import org.springframework.http.HttpStatus
42 import org.springframework.http.MediaType
43 import org.springframework.test.web.servlet.MockMvc
44 import spock.lang.Specification
46 @WebMvcTest(NetworkCmProxyController)
47 class NetworkCmProxyControllerSpec extends Specification {
53 NetworkCmProxyDataService mockNetworkCmProxyDataService = Mock()
56 ObjectMapper objectMapper = new ObjectMapper()
58 @Value('${rest.api.ncmp-base-path}/v1')
61 def cmHandle = 'some handle'
62 def xpath = 'some xpath'
64 def 'Query data node by cps path for the given cm handle with #scenario.'() {
65 given: 'service method returns a list containing a data node'
66 def dataNode = new DataNodeBuilder().withXpath('/xpath').build()
67 def cpsPath = 'some cps-path'
68 mockNetworkCmProxyDataService.queryDataNodes(cmHandle, cpsPath, expectedCpsDataServiceOption) >> [dataNode]
69 and: 'the query endpoint'
70 def dataNodeEndpoint = "$ncmpBasePathV1/cm-handles/$cmHandle/nodes/query"
71 when: 'query data nodes API is invoked'
72 def response = mvc.perform(get(dataNodeEndpoint)
73 .param('cps-path', cpsPath)
74 .param('include-descendants', includeDescendantsOption))
76 then: 'the response contains the the datanode in json format'
77 response.status == HttpStatus.OK.value()
78 def expectedJsonContent = new Gson().toJson(dataNode)
79 response.getContentAsString().contains(expectedJsonContent)
80 where: 'the following options for include descendants are provided in the request'
81 scenario | includeDescendantsOption || expectedCpsDataServiceOption
82 'no descendants by default' | '' || OMIT_DESCENDANTS
83 'no descendant explicitly' | 'false' || OMIT_DESCENDANTS
84 'descendants' | 'true' || INCLUDE_ALL_DESCENDANTS
87 def 'Create data node: #scenario.'() {
89 def jsonData = 'json data'
90 when: 'post request is performed'
91 def response = mvc.perform(
92 post("$ncmpBasePathV1/cm-handles/$cmHandle/nodes")
93 .contentType(MediaType.APPLICATION_JSON)
95 .param('xpath', reqXpath)
96 ).andReturn().response
97 then: 'the service method is invoked once with expected parameters'
98 1 * mockNetworkCmProxyDataService.createDataNode(cmHandle, usedXpath, jsonData)
99 and: 'response status indicates success'
100 response.status == HttpStatus.CREATED.value()
101 where: 'following parameters were used'
102 scenario | reqXpath || usedXpath
103 'no xpath parameter' | '' || '/'
104 'root xpath' | '/' || '/'
105 'parent node xpath' | '/xpath' || '/xpath'
108 def 'Add list-node elements.'() {
109 given: 'json data and parent node xpath'
110 def jsonData = 'json data'
111 def parentNodeXpath = 'parent node xpath'
112 when: 'post request is performed'
113 def response = mvc.perform(
114 post("$ncmpBasePathV1/cm-handles/$cmHandle/list-node")
115 .contentType(MediaType.APPLICATION_JSON)
117 .param('xpath', parentNodeXpath)
118 ).andReturn().response
119 then: 'the service method is invoked once with expected parameters'
120 1 * mockNetworkCmProxyDataService.addListNodeElements(cmHandle, parentNodeXpath, jsonData)
121 and: 'response status indicates success'
122 response.status == HttpStatus.CREATED.value()
125 def 'Update data node leaves.'() {
127 def jsonData = 'json data'
128 and: 'the query endpoint'
129 def endpoint = "$ncmpBasePathV1/cm-handles/$cmHandle/nodes"
130 when: 'patch request is performed'
131 def response = mvc.perform(
133 .contentType(MediaType.APPLICATION_JSON)
135 .param('xpath', xpath)
136 ).andReturn().response
137 then: 'the service method is invoked once with expected parameters'
138 1 * mockNetworkCmProxyDataService.updateNodeLeaves(cmHandle, xpath, jsonData)
139 and: 'response status indicates success'
140 response.status == HttpStatus.OK.value()
143 def 'Replace data node tree.'() {
145 def jsonData = 'json data'
146 and: 'the query endpoint'
147 def endpoint = "$ncmpBasePathV1/cm-handles/$cmHandle/nodes"
148 when: 'put request is performed'
149 def response = mvc.perform(
151 .contentType(MediaType.APPLICATION_JSON)
153 .param('xpath', xpath)
154 ).andReturn().response
155 then: 'the service method is invoked once with expected parameters'
156 1 * mockNetworkCmProxyDataService.replaceNodeTree(cmHandle, xpath, jsonData)
157 and: 'response status indicates success'
158 response.status == HttpStatus.OK.value()
161 def 'Get data node.'() {
162 given: 'the service returns a data node'
163 def xpath = 'some xpath'
164 def dataNode = new DataNodeBuilder().withXpath(xpath).withLeaves(["leaf": "value"]).build()
165 mockNetworkCmProxyDataService.getDataNode(cmHandle, xpath, OMIT_DESCENDANTS) >> dataNode
166 and: 'the query endpoint'
167 def endpoint = "$ncmpBasePathV1/cm-handles/$cmHandle/node"
168 when: 'get request is performed through REST API'
169 def response = mvc.perform(get(endpoint).param('xpath', xpath)).andReturn().response
170 then: 'a success response is returned'
171 response.status == HttpStatus.OK.value()
172 and: 'response contains expected leaf and value'
173 response.contentAsString.contains('"leaf":"value"')
176 def 'Register CM Handle Event' () {
178 def jsonData = TestUtils.getResourceFileContent('dmi-registration.json')
179 when: 'post request is performed'
180 def response = mvc.perform(
181 post("$ncmpBasePathV1/ch")
182 .contentType(MediaType.APPLICATION_JSON)
184 ).andReturn().response
185 then: 'the cm handles are registered with the service'
186 1 * mockNetworkCmProxyDataService.updateDmiRegistrationAndSyncModule(_)
187 and: 'response status is created'
188 response.status == HttpStatus.CREATED.value()
191 def 'Get Resource Data from pass-through operational.' () {
192 given: 'resource data url'
193 def getUrl = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-operational" +
194 "/testResourceIdentifier?fields=testFields&depth=5"
195 when: 'get data resource request is performed'
196 def response = mvc.perform(
198 .contentType(MediaType.APPLICATION_JSON)
199 .accept(MediaType.APPLICATION_JSON_VALUE)
200 ).andReturn().response
201 then: 'the NCMP data service is called with getResourceDataOperationalForCmHandle'
202 1 * mockNetworkCmProxyDataService.getResourceDataOperationalForCmHandle('testCmHandle',
203 'testResourceIdentifier',
207 and: 'response status is Ok'
208 response.status == HttpStatus.OK.value()
211 def 'Get Resource Data from pass-through running.' () {
212 given: 'resource data url'
213 def getUrl = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-running" +
214 "/testResourceIdentifier?fields=testFields&depth=5"
215 and: 'ncmp service returns json object'
216 mockNetworkCmProxyDataService.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
217 'testResourceIdentifier',
221 when: 'get data resource request is performed'
222 def response = mvc.perform(
224 .contentType(MediaType.APPLICATION_JSON)
225 .accept(MediaType.APPLICATION_JSON_VALUE)
226 ).andReturn().response
227 then: 'response status is Ok'
228 response.status == HttpStatus.OK.value()
229 and: 'response contains valid object body'
230 response.getContentAsString() == '{valid-json}'
233 def 'Create Resource Data from pass-through running using POST.' () {
234 given: 'resource data url'
235 def getUrl = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-running" +
236 "/testResourceIdentifier"
237 when: 'get data resource request is performed'
238 def response = mvc.perform(
240 .contentType(MediaType.APPLICATION_JSON_VALUE)
241 .accept(MediaType.APPLICATION_JSON_VALUE).content('{"some-json":"value"}')
242 ).andReturn().response
243 then: 'ncmp service method to create resource called'
244 1 * mockNetworkCmProxyDataService.createResourceDataPassThroughRunningForCmHandle('testCmHandle',
245 'testResourceIdentifier', ['some-json':'value'], 'application/json;charset=UTF-8')
246 and: 'resource is created'
247 response.status == HttpStatus.CREATED.value()