2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Pantheon.tech
4 * Modifications Copyright (C) 2021 highstreet technologies GmbH
5 * Modifications Copyright (C) 2021-2022 Nordix Foundation
6 * Modifications Copyright (C) 2021-2022 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
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.cps.ncmp.rest.controller
26 import org.mapstruct.factory.Mappers
27 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
28 import spock.lang.Shared
30 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.PATCH
31 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete
32 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
33 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch
34 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
35 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put
36 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.CREATE
37 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.UPDATE
38 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.DELETE
40 import com.fasterxml.jackson.databind.ObjectMapper
41 import org.onap.cps.TestUtils
42 import org.onap.cps.spi.model.ModuleReference
43 import org.onap.cps.utils.JsonObjectMapper
44 import org.onap.cps.ncmp.api.NetworkCmProxyDataService
45 import org.spockframework.spring.SpringBean
46 import org.springframework.beans.factory.annotation.Autowired
47 import org.springframework.beans.factory.annotation.Value
48 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
49 import org.springframework.http.HttpStatus
50 import org.springframework.http.MediaType
51 import org.springframework.test.web.servlet.MockMvc
52 import spock.lang.Specification
54 @WebMvcTest(NetworkCmProxyController)
55 class NetworkCmProxyControllerSpec extends Specification {
61 NetworkCmProxyDataService mockNetworkCmProxyDataService = Mock()
64 ObjectMapper objectMapper = new ObjectMapper()
67 JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(objectMapper)
70 NcmpRestInputMapper ncmpRestInputMapper = Mappers.getMapper(NcmpRestInputMapper)
72 @Value('${rest.api.ncmp-base-path}/v1')
75 def requestBody = '{"some-key":"some-value"}'
79 def NO_REQUEST_ID = null
81 def 'Get Resource Data from pass-through operational.'() {
82 given: 'resource data url'
83 def getUrl = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-operational" +
84 "?resourceIdentifier=parent/child&options=(a=1,b=2)"
85 when: 'get data resource request is performed'
86 def response = mvc.perform(
88 .contentType(MediaType.APPLICATION_JSON)
89 ).andReturn().response
90 then: 'the NCMP data service is called with getResourceDataOperationalForCmHandle'
91 1 * mockNetworkCmProxyDataService.getResourceDataOperationalForCmHandle('testCmHandle',
96 and: 'response status is Ok'
97 response.status == HttpStatus.OK.value()
100 def 'Get Resource Data from #datastoreInUrl with #scenario.'() {
101 given: 'resource data url'
102 def getUrl = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:${datastoreInUrl}" +
103 "?resourceIdentifier=parent/child&options=(a=1,b=2)${topicQueryParam}"
104 when: 'get data resource request is performed'
105 def response = mvc.perform(
107 .contentType(MediaType.APPLICATION_JSON)
108 ).andReturn().response
109 then: 'the NCMP data service is called with operational data for cm handle'
110 expectedNumberOfMethodExecutions
111 * mockNetworkCmProxyDataService."${expectedMethodName}"('testCmHandle',
116 then: 'response status is expected'
117 response.status == expectedHttpStatus
118 where: 'the following parameters are used'
119 scenario | datastoreInUrl | topicQueryParam || expectedTopicName | expectedMethodName | expectedNumberOfMethodExecutions | expectedHttpStatus
120 'url with valid topic' | 'passthrough-operational' | '&topic=my-topic-name' || 'my-topic-name' | 'getResourceDataOperationalForCmHandle' | 1 | HttpStatus.OK.value()
121 'no topic in url' | 'passthrough-operational' | '' || NO_TOPIC | 'getResourceDataOperationalForCmHandle' | 1 | HttpStatus.OK.value()
122 'null topic in url' | 'passthrough-operational' | '&topic=null' || 'null' | 'getResourceDataOperationalForCmHandle' | 1 | HttpStatus.OK.value()
123 'empty topic in url' | 'passthrough-operational' | '&topic=\"\"' || null | 'getResourceDataOperationalForCmHandle' | 0 | HttpStatus.BAD_REQUEST.value()
124 'missing topic in url' | 'passthrough-operational' | '&topic=' || null | 'getResourceDataOperationalForCmHandle' | 0 | HttpStatus.BAD_REQUEST.value()
125 'blank topic value in url' | 'passthrough-operational' | '&topic=\" \"' || null | 'getResourceDataOperationalForCmHandle' | 0 | HttpStatus.BAD_REQUEST.value()
126 'invalid non-empty topic value in url' | 'passthrough-operational' | '&topic=1_5_*_#' || null | 'getResourceDataOperationalForCmHandle' | 0 | HttpStatus.BAD_REQUEST.value()
127 'url with valid topic' | 'passthrough-running' | '&topic=my-topic-name' || 'my-topic-name' | 'getResourceDataPassThroughRunningForCmHandle' | 1 | HttpStatus.OK.value()
128 'no topic in url' | 'passthrough-running' | '' || NO_TOPIC | 'getResourceDataPassThroughRunningForCmHandle' | 1 | HttpStatus.OK.value()
129 'null topic in url' | 'passthrough-running' | '&topic=null' || 'null' | 'getResourceDataPassThroughRunningForCmHandle' | 1 | HttpStatus.OK.value()
130 'empty topic in url' | 'passthrough-running' | '&topic=\"\"' || null | 'getResourceDataPassThroughRunningForCmHandle' | 0 | HttpStatus.BAD_REQUEST.value()
131 'missing topic in url' | 'passthrough-running' | '&topic=' || null | 'getResourceDataPassThroughRunningForCmHandle' | 0 | HttpStatus.BAD_REQUEST.value()
132 'blank topic value in url' | 'passthrough-running' | '&topic=\" \"' || null | 'getResourceDataPassThroughRunningForCmHandle' | 0 | HttpStatus.BAD_REQUEST.value()
133 'invalid non-empty topic value in url' | 'passthrough-running' | '&topic=1_5_*_#' || null | 'getResourceDataPassThroughRunningForCmHandle' | 0 | HttpStatus.BAD_REQUEST.value()
136 def 'Get Resource Data from pass-through running with #scenario value in resource identifier param.'() {
137 given: 'resource data url'
138 def getUrl = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-running" +
139 "?resourceIdentifier=" + resourceIdentifier + "&options=(a=1,b=2)"
140 and: 'ncmp service returns json object'
141 mockNetworkCmProxyDataService.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
145 NO_REQUEST_ID) >> '{valid-json}'
146 when: 'get data resource request is performed'
147 def response = mvc.perform(
149 .contentType(MediaType.APPLICATION_JSON)
150 ).andReturn().response
151 then: 'response status is Ok'
152 response.status == HttpStatus.OK.value()
153 and: 'response contains valid object body'
154 response.getContentAsString() == '{valid-json}'
155 where: 'tokens are used in the resource identifier parameter'
156 scenario | resourceIdentifier
157 '/' | 'id/with/slashes'
162 '? needs to be encoded as %3F' | 'idWith%3F'
165 def 'Update resource data from pass-through running.' () {
166 given: 'update resource data url'
167 def updateUrl = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-running" +
168 "?resourceIdentifier=parent/child"
169 when: 'update data resource request is performed'
170 def response = mvc.perform(
172 .contentType(MediaType.APPLICATION_JSON_VALUE).content(requestBody)
173 ).andReturn().response
174 then: 'ncmp service method to update resource is called'
175 1 * mockNetworkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
176 'parent/child', UPDATE, requestBody, 'application/json;charset=UTF-8')
177 and: 'the response status is OK'
178 response.status == HttpStatus.OK.value()
181 def 'Create Resource Data from pass-through running with #scenario.' () {
182 given: 'resource data url'
183 def url = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-running" +
184 "?resourceIdentifier=parent/child"
185 def requestBody = '{"some-key":"some-value"}'
186 when: 'create resource request is performed'
187 def response = mvc.perform(
189 .contentType(MediaType.APPLICATION_JSON_VALUE).content(requestBody)
190 ).andReturn().response
191 then: 'ncmp service method to create resource called'
192 1 * mockNetworkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
193 'parent/child', CREATE, requestBody, 'application/json;charset=UTF-8')
194 and: 'resource is created'
195 response.status == HttpStatus.CREATED.value()
198 def 'Get module references for the given dataspace and cm handle.' () {
199 given: 'get module references url'
200 def getUrl = "$ncmpBasePathV1/ch/some-cmhandle/modules"
201 when: 'get module resource request is performed'
202 def response =mvc.perform(get(getUrl)).andReturn().response
203 then: 'ncmp service method to get yang resource module references is called'
204 mockNetworkCmProxyDataService.getYangResourcesModuleReferences('some-cmhandle')
205 >> [new ModuleReference(moduleName: 'some-name1',revision: '2021-10-03')]
206 and: 'response contains an array with the module name and revision'
207 response.getContentAsString() == '[{"moduleName":"some-name1","revision":"2021-10-03"}]'
208 and: 'response returns an OK http code'
209 response.status == HttpStatus.OK.value()
212 def 'Retrieve cm handles.'() {
213 given: 'an endpoint and json data'
214 def searchesEndpoint = "$ncmpBasePathV1/ch/searches"
215 String jsonString = TestUtils.getResourceFileContent('cmhandle-search.json')
216 and: 'the service method is invoked with module names and returns two cm handle ids'
217 mockNetworkCmProxyDataService.executeCmHandleHasAllModulesSearch(['module1', 'module2']) >> ['some-cmhandle-id1', 'some-cmhandle-id2']
218 when: 'the searches api is invoked'
219 def response = mvc.perform(post(searchesEndpoint)
220 .contentType(MediaType.APPLICATION_JSON)
221 .content(jsonString)).andReturn().response
222 then: 'response status returns OK'
223 response.status == HttpStatus.OK.value()
224 and: 'the expected response content is returned'
225 response.contentAsString == '{"cmHandles":[{"cmHandleId":"some-cmhandle-id1"},{"cmHandleId":"some-cmhandle-id2"}]}'
228 def 'Get Cm Handle details by Cm Handle id.' () {
229 given: 'an endpoint and a cm handle'
230 def cmHandleDetailsEndpoint = "$ncmpBasePathV1/ch/some-cm-handle"
231 and: 'an existing ncmp service cm handle'
232 def cmHandleId = 'some-cm-handle'
233 def dmiProperties = [ prop:'some DMI property' ]
234 def publicProperties = [ "public prop":'some public property' ]
235 def ncmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: cmHandleId, dmiProperties: dmiProperties, publicProperties: publicProperties)
236 and: 'the service method is invoked with the cm handle id'
237 1 * mockNetworkCmProxyDataService.getNcmpServiceCmHandle('some-cm-handle') >> ncmpServiceCmHandle
238 when: 'the cm handle details api is invoked'
239 def response = mvc.perform(get(cmHandleDetailsEndpoint)).andReturn().response
240 then: 'the correct response is returned'
241 response.status == HttpStatus.OK.value()
242 and: 'the response returns public properties and the correct properties'
243 response.contentAsString.contains('publicCmHandleProperties')
244 response.contentAsString.contains('public prop')
245 response.contentAsString.contains('some public property')
246 and: 'the content does not contain dmi properties'
247 !response.contentAsString.contains("some DMI property")
250 def 'Get Cm Handle public properties by Cm Handle id.' () {
251 given: 'a cm handle properties endpoint'
252 def cmHandlePropertiesEndpoint = "$ncmpBasePathV1/ch/some-cm-handle/properties"
253 and: 'some cm handle public properties'
254 def publicProperties = [ 'public prop':'some public property' ]
255 and: 'the service method is invoked with the cm handle id returning the cm handle public properties'
256 1 * mockNetworkCmProxyDataService.getCmHandlePublicProperties('some-cm-handle') >> publicProperties
257 when: 'the cm handle properties api is invoked'
258 def response = mvc.perform(get(cmHandlePropertiesEndpoint)).andReturn().response
259 then: 'the correct response is returned'
260 response.status == HttpStatus.OK.value()
261 and: 'the response returns public properties and the correct properties'
262 response.contentAsString.equals('{"publicCmHandleProperties":[{"public prop":"some public property"}]}')
265 def 'Call execute cm handle searches with unrecognized condition name.'() {
266 given: 'an endpoint and json data'
267 def searchesEndpoint = "$ncmpBasePathV1/ch/searches"
268 String jsonString = TestUtils.getResourceFileContent('invalid-cmhandle-search.json')
269 when: 'the searches api is invoked'
270 def response = mvc.perform(post(searchesEndpoint)
271 .contentType(MediaType.APPLICATION_JSON)
272 .content(jsonString)).andReturn().response
273 then: 'an empty cm handle identifier is returned'
274 response.contentAsString == '{"cmHandles":[]}'
277 def 'Query for cm handles matching query parameters'() {
278 given: 'an endpoint and json data'
279 def searchesEndpoint = "$ncmpBasePathV1/data/ch/searches"
280 String jsonString = '{"publicCmHandleProperties": {"name": "Contact", "value": "newemailforstore@bookstore.com"}}'
281 and: 'the service method is invoked with module names and returns cm handle ids'
282 1 * mockNetworkCmProxyDataService.queryCmHandles(_) >> ['some-cmhandle-id1', 'some-cmhandle-id2']
283 when: 'the searches api is invoked'
284 def response = mvc.perform(post(searchesEndpoint)
285 .contentType(MediaType.APPLICATION_JSON)
286 .content(jsonString)).andReturn().response
287 then: 'cm handle ids are returned'
288 response.contentAsString == '["some-cmhandle-id1","some-cmhandle-id2"]'
291 def 'Query for cm handles with invalid request payload'() {
292 when: 'the searches api is invoked'
293 def searchesEndpoint = "$ncmpBasePathV1/data/ch/searches"
294 def invalidInputData = '{invalidJson}'
295 def response = mvc.perform(post(searchesEndpoint)
296 .contentType(MediaType.APPLICATION_JSON)
297 .content(invalidInputData)).andReturn().response
298 then: 'BAD_REQUEST is returned'
299 response.getStatus() == 400
302 def 'Patch resource data in pass-through running datastore.' () {
303 given: 'patch resource data url'
304 def url = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-running" +
305 "?resourceIdentifier=parent/child"
306 when: 'patch data resource request is performed'
307 def response = mvc.perform(
309 .contentType(MediaType.APPLICATION_JSON)
310 .accept(MediaType.APPLICATION_JSON).content(requestBody)
311 ).andReturn().response
312 then: 'ncmp service method to update resource is called'
313 1 * mockNetworkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
314 'parent/child', PATCH, requestBody, 'application/json;charset=UTF-8')
315 and: 'the response status is OK'
316 response.status == HttpStatus.OK.value()
319 def 'Delete resource data in pass-through running datastore.' () {
320 given: 'delete resource data url'
321 def url = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-running" +
322 "?resourceIdentifier=parent/child"
323 when: 'delete data resource request is performed'
324 def response = mvc.perform(
325 delete(url).contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)).andReturn().response
326 then: 'the ncmp service method to delete resource is called (with null as body)'
327 1 * mockNetworkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
328 'parent/child', DELETE, null, 'application/json;charset=UTF-8')
329 and: 'the response is No Content'
330 response.status == HttpStatus.NO_CONTENT.value()
333 def 'Get resource data from DMI with valid topic i.e. async request for #scenario'() {
334 given: 'resource data url'
335 def getUrl = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:${datastoreInUrl}" +
336 "?resourceIdentifier=parent/child&options=(a=1,b=2)&topic=my-topic-name"
337 when: 'get data resource request is performed'
338 def response = mvc.perform(
340 .contentType(MediaType.APPLICATION_JSON)
341 .accept(MediaType.APPLICATION_JSON_VALUE)
342 ).andReturn().response
343 then: 'async request id is generated'
344 assert response.contentAsString.contains("requestId")
345 where: 'the following parameters are used'
346 scenario | datastoreInUrl
347 ':passthrough-operational' | 'passthrough-operational'
348 ':passthrough-running' | 'passthrough-running'