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 org.onap.cps.spi.model.ModuleReference
27 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
28 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
29 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
30 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch
31 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
32 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put
34 import com.google.gson.Gson
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()
55 @Value('${rest.api.ncmp-base-path}/v1')
58 def cmHandle = 'some handle'
59 def xpath = 'some xpath'
61 def 'Query data node by cps path for the given cm handle with #scenario.'() {
62 given: 'service method returns a list containing a data node'
63 def dataNode = new DataNodeBuilder().withXpath('/xpath').build()
64 def cpsPath = 'some cps-path'
65 mockNetworkCmProxyDataService.queryDataNodes(cmHandle, cpsPath, expectedCpsDataServiceOption) >> [dataNode]
66 and: 'the query endpoint'
67 def dataNodeEndpoint = "$ncmpBasePathV1/cm-handles/$cmHandle/nodes/query"
68 when: 'query data nodes API is invoked'
69 def response = mvc.perform(get(dataNodeEndpoint)
70 .param('cps-path', cpsPath)
71 .param('include-descendants', includeDescendantsOption))
73 then: 'the response contains the the datanode in json format'
74 response.status == HttpStatus.OK.value()
75 def expectedJsonContent = new Gson().toJson(dataNode)
76 response.getContentAsString().contains(expectedJsonContent)
77 where: 'the following options for include descendants are provided in the request'
78 scenario | includeDescendantsOption || expectedCpsDataServiceOption
79 'no descendants by default' | '' || OMIT_DESCENDANTS
80 'no descendant explicitly' | 'false' || OMIT_DESCENDANTS
81 'descendants' | 'true' || INCLUDE_ALL_DESCENDANTS
84 def 'Create data node: #scenario.'() {
86 def jsonData = 'json data'
87 when: 'post request is performed'
88 def response = mvc.perform(
89 post("$ncmpBasePathV1/cm-handles/$cmHandle/nodes")
90 .contentType(MediaType.APPLICATION_JSON)
92 .param('xpath', reqXpath)
93 ).andReturn().response
94 then: 'the service method is invoked once with expected parameters'
95 1 * mockNetworkCmProxyDataService.createDataNode(cmHandle, usedXpath, jsonData)
96 and: 'response status indicates success'
97 response.status == HttpStatus.CREATED.value()
98 where: 'following parameters were used'
99 scenario | reqXpath || usedXpath
100 'no xpath parameter' | '' || '/'
101 'root xpath' | '/' || '/'
102 'parent node xpath' | '/xpath' || '/xpath'
105 def 'Add list-node elements.'() {
106 given: 'json data and parent node xpath'
107 def jsonData = 'json data'
108 def parentNodeXpath = 'parent node xpath'
109 when: 'post request is performed'
110 def response = mvc.perform(
111 post("$ncmpBasePathV1/cm-handles/$cmHandle/list-node")
112 .contentType(MediaType.APPLICATION_JSON)
114 .param('xpath', parentNodeXpath)
115 ).andReturn().response
116 then: 'the service method is invoked once with expected parameters'
117 1 * mockNetworkCmProxyDataService.addListNodeElements(cmHandle, parentNodeXpath, jsonData)
118 and: 'response status indicates success'
119 response.status == HttpStatus.CREATED.value()
122 def 'Update data node leaves.'() {
124 def jsonData = 'json data'
125 and: 'the query endpoint'
126 def endpoint = "$ncmpBasePathV1/cm-handles/$cmHandle/nodes"
127 when: 'patch request is performed'
128 def response = mvc.perform(
130 .contentType(MediaType.APPLICATION_JSON)
132 .param('xpath', xpath)
133 ).andReturn().response
134 then: 'the service method is invoked once with expected parameters'
135 1 * mockNetworkCmProxyDataService.updateNodeLeaves(cmHandle, xpath, jsonData)
136 and: 'response status indicates success'
137 response.status == HttpStatus.OK.value()
140 def 'Replace data node tree.'() {
142 def jsonData = 'json data'
143 and: 'the query endpoint'
144 def endpoint = "$ncmpBasePathV1/cm-handles/$cmHandle/nodes"
145 when: 'put request is performed'
146 def response = mvc.perform(
148 .contentType(MediaType.APPLICATION_JSON)
150 .param('xpath', xpath)
151 ).andReturn().response
152 then: 'the service method is invoked once with expected parameters'
153 1 * mockNetworkCmProxyDataService.replaceNodeTree(cmHandle, xpath, jsonData)
154 and: 'response status indicates success'
155 response.status == HttpStatus.OK.value()
158 def 'Get data node.'() {
159 given: 'the service returns a data node'
160 def xpath = 'some xpath'
161 def dataNode = new DataNodeBuilder().withXpath(xpath).withLeaves(["leaf": "value"]).build()
162 mockNetworkCmProxyDataService.getDataNode(cmHandle, xpath, OMIT_DESCENDANTS) >> dataNode
163 and: 'the query endpoint'
164 def endpoint = "$ncmpBasePathV1/cm-handles/$cmHandle/node"
165 when: 'get request is performed through REST API'
166 def response = mvc.perform(get(endpoint).param('xpath', xpath)).andReturn().response
167 then: 'a success response is returned'
168 response.status == HttpStatus.OK.value()
169 and: 'response contains expected leaf and value'
170 response.contentAsString.contains('"leaf":"value"')
173 def 'Get Resource Data from pass-through operational.' () {
174 given: 'resource data url'
175 def getUrl = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-operational" +
176 "?resourceIdentifier=parent/child&options=(a=1,b=2)"
177 when: 'get data resource request is performed'
178 def response = mvc.perform(
180 .contentType(MediaType.APPLICATION_JSON)
181 .accept(MediaType.APPLICATION_JSON_VALUE)
182 ).andReturn().response
183 then: 'the NCMP data service is called with getResourceDataOperationalForCmHandle'
184 1 * mockNetworkCmProxyDataService.getResourceDataOperationalForCmHandle('testCmHandle',
188 and: 'response status is Ok'
189 response.status == HttpStatus.OK.value()
192 def 'Get Resource Data from pass-through running with #scenario value in resource identifier param.' () {
193 given: 'resource data url'
194 def getUrl = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-running" +
195 "?resourceIdentifier=" + resourceIdentifier + "&options=(a=1,b=2)"
196 and: 'ncmp service returns json object'
197 mockNetworkCmProxyDataService.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
200 '(a=1,b=2)') >> '{valid-json}'
201 when: 'get data resource request is performed'
202 def response = mvc.perform(
204 .contentType(MediaType.APPLICATION_JSON)
205 .accept(MediaType.APPLICATION_JSON_VALUE)
206 ).andReturn().response
207 then: 'response status is Ok'
208 response.status == HttpStatus.OK.value()
209 and: 'response contains valid object body'
210 response.getContentAsString() == '{valid-json}'
211 where: 'tokens are used in the resource identifier parameter'
212 scenario | resourceIdentifier
213 '/' | 'id/with/slashes'
218 '? needs to be encoded as %3F' | 'idWith%3F'
221 def 'Create Resource Data from pass-through running with #scenario.' () {
222 given: 'resource data url'
223 def getUrl = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-running" +
224 "?resourceIdentifier=parent/child"
225 when: 'get data resource request is performed'
226 def response = mvc.perform(
228 .contentType(MediaType.APPLICATION_JSON_VALUE)
229 .accept(MediaType.APPLICATION_JSON_VALUE).content(requestBody)
230 ).andReturn().response
231 then: 'ncmp service method to create resource called'
232 1 * mockNetworkCmProxyDataService.createResourceDataPassThroughRunningForCmHandle('testCmHandle',
233 'parent/child', requestBody, 'application/json;charset=UTF-8')
234 and: 'resource is created'
235 response.status == HttpStatus.CREATED.value()
236 where: 'given request body'
237 scenario | requestBody
238 'body contains " and new line' | 'body with " quote and \n new line'
239 'body contains normal string' | 'normal request body'
242 def 'Get module references for the given dataspace and cm handle.' () {
243 given: 'get module references url'
244 def getUrl = "$ncmpBasePathV1/ch/some-cmhandle/modules"
245 when: 'get module resource request is performed'
246 def response =mvc.perform(get(getUrl)).andReturn().response
247 then: 'ncmp service method to get yang resource module references is called'
248 mockNetworkCmProxyDataService.getYangResourcesModuleReferences('some-cmhandle')
249 >> [new ModuleReference(moduleName: 'some-name1',revision: 'some-revision1')]
250 and: 'response contains an array with the module name and revision'
251 response.getContentAsString() == '[{"moduleName":"some-name1","revision":"some-revision1"}]'
252 and: 'response returns an OK http code'
253 response.status == HttpStatus.OK.value()