2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Pantheon.tech
4 * Modification Copyright (C) 2021 highstreet technologies GmbH
5 * Modification Copyright (C) 2021-2022 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
26 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
28 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.PATCH
29 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete
30 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
31 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch
32 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
33 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put
34 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.CREATE
35 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.UPDATE
36 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.DELETE
38 import com.fasterxml.jackson.databind.ObjectMapper
39 import org.modelmapper.ModelMapper
40 import org.onap.cps.TestUtils
41 import org.onap.cps.spi.model.ModuleReference
42 import org.onap.cps.utils.JsonObjectMapper
43 import org.onap.cps.ncmp.api.NetworkCmProxyDataService
44 import org.spockframework.spring.SpringBean
45 import org.springframework.beans.factory.annotation.Autowired
46 import org.springframework.beans.factory.annotation.Value
47 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
48 import org.springframework.http.HttpStatus
49 import org.springframework.http.MediaType
50 import org.springframework.test.web.servlet.MockMvc
51 import spock.lang.Specification
53 @WebMvcTest(NetworkCmProxyController)
54 class NetworkCmProxyControllerSpec extends Specification {
60 NetworkCmProxyDataService mockNetworkCmProxyDataService = Mock()
63 ModelMapper modelMapper = new ModelMapper()
66 JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
68 @Value('${rest.api.ncmp-base-path}/v1')
71 def requestBody = '{"some-key":"some-value"}'
73 def 'Get Resource Data from pass-through operational.' () {
74 given: 'resource data url'
75 def getUrl = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-operational" +
76 "?resourceIdentifier=parent/child&options=(a=1,b=2)"
77 when: 'get data resource request is performed'
78 def response = mvc.perform(
80 .contentType(MediaType.APPLICATION_JSON)
81 .accept(MediaType.APPLICATION_JSON_VALUE)
82 ).andReturn().response
83 then: 'the NCMP data service is called with getResourceDataOperationalForCmHandle'
84 1 * mockNetworkCmProxyDataService.getResourceDataOperationalForCmHandle('testCmHandle',
88 and: 'response status is Ok'
89 response.status == HttpStatus.OK.value()
92 def 'Get Resource Data from pass-through running with #scenario value in resource identifier param.' () {
93 given: 'resource data url'
94 def getUrl = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-running" +
95 "?resourceIdentifier=" + resourceIdentifier + "&options=(a=1,b=2)"
96 and: 'ncmp service returns json object'
97 mockNetworkCmProxyDataService.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
100 '(a=1,b=2)') >> '{valid-json}'
101 when: 'get data resource request is performed'
102 def response = mvc.perform(
104 .contentType(MediaType.APPLICATION_JSON)
105 .accept(MediaType.APPLICATION_JSON_VALUE)
106 ).andReturn().response
107 then: 'response status is Ok'
108 response.status == HttpStatus.OK.value()
109 and: 'response contains valid object body'
110 response.getContentAsString() == '{valid-json}'
111 where: 'tokens are used in the resource identifier parameter'
112 scenario | resourceIdentifier
113 '/' | 'id/with/slashes'
118 '? needs to be encoded as %3F' | 'idWith%3F'
121 def 'Update resource data from pass-through running.' () {
122 given: 'update resource data url'
123 def updateUrl = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-running" +
124 "?resourceIdentifier=parent/child"
125 when: 'update data resource request is performed'
126 def response = mvc.perform(
128 .contentType(MediaType.APPLICATION_JSON_VALUE)
129 .accept(MediaType.APPLICATION_JSON_VALUE).content(requestBody)
130 ).andReturn().response
131 then: 'ncmp service method to update resource is called'
132 1 * mockNetworkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
133 'parent/child', UPDATE, requestBody, 'application/json;charset=UTF-8')
134 and: 'the response status is OK'
135 response.status == HttpStatus.OK.value()
138 def 'Create Resource Data from pass-through running with #scenario.' () {
139 given: 'resource data url'
140 def url = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-running" +
141 "?resourceIdentifier=parent/child"
142 def requestBody = '{"some-key":"some-value"}'
143 when: 'create resource request is performed'
144 def response = mvc.perform(
146 .contentType(MediaType.APPLICATION_JSON_VALUE)
147 .accept(MediaType.APPLICATION_JSON_VALUE).content(requestBody)
148 ).andReturn().response
149 then: 'ncmp service method to create resource called'
150 1 * mockNetworkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
151 'parent/child', CREATE, requestBody, 'application/json;charset=UTF-8')
152 and: 'resource is created'
153 response.status == HttpStatus.CREATED.value()
156 def 'Get module references for the given dataspace and cm handle.' () {
157 given: 'get module references url'
158 def getUrl = "$ncmpBasePathV1/ch/some-cmhandle/modules"
159 when: 'get module resource request is performed'
160 def response =mvc.perform(get(getUrl)).andReturn().response
161 then: 'ncmp service method to get yang resource module references is called'
162 mockNetworkCmProxyDataService.getYangResourcesModuleReferences('some-cmhandle')
163 >> [new ModuleReference(moduleName: 'some-name1',revision: '2021-10-03')]
164 and: 'response contains an array with the module name and revision'
165 response.getContentAsString() == '[{"moduleName":"some-name1","revision":"2021-10-03"}]'
166 and: 'response returns an OK http code'
167 response.status == HttpStatus.OK.value()
170 def 'Retrieve cm handles.'() {
171 given: 'an endpoint and json data'
172 def searchesEndpoint = "$ncmpBasePathV1/ch/searches"
173 String jsonString = TestUtils.getResourceFileContent('cmhandle-search.json')
174 and: 'the service method is invoked with module names and returns two cm handle ids'
175 mockNetworkCmProxyDataService.executeCmHandleHasAllModulesSearch(['module1', 'module2']) >> ['some-cmhandle-id1', 'some-cmhandle-id2']
176 when: 'the searches api is invoked'
177 def response = mvc.perform(post(searchesEndpoint)
178 .contentType(MediaType.APPLICATION_JSON)
179 .content(jsonString)).andReturn().response
180 then: 'response status returns OK'
181 response.status == HttpStatus.OK.value()
182 and: 'the expected response content is returned'
183 response.contentAsString == '{"cmHandles":[{"cmHandleId":"some-cmhandle-id1"},{"cmHandleId":"some-cmhandle-id2"}]}'
186 def 'Get Cm Handle details by Cm Handle id.' () {
187 given: 'an endpoint and a cm handle'
188 def cmHandleDetailsEndpoint = "$ncmpBasePathV1/ch/Some-Cm-Handle"
189 and: 'an existing ncmp service cm handle'
190 def cmHandleId = 'Some-Cm-Handle'
191 def dmiProperties = [ prop:'some DMI property' ]
192 def publicProperties = [ "public prop":'some public property' ]
193 def ncmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleID: cmHandleId, dmiProperties: dmiProperties, publicProperties: publicProperties)
194 and: 'the service method is invoked with the cm handle id'
195 1 * mockNetworkCmProxyDataService.getNcmpServiceCmHandle('Some-Cm-Handle') >> ncmpServiceCmHandle
196 when: 'the cm handle details api is invoked'
197 def response = mvc.perform(get(cmHandleDetailsEndpoint)).andReturn().response
198 then: 'the correct response is returned'
199 response.status == HttpStatus.OK.value()
200 and: 'the response returns public properties and the correct properties'
201 response.contentAsString.contains('publicCmHandleProperties')
202 response.contentAsString.contains('public prop')
203 response.contentAsString.contains('some public property')
204 and: 'the content does not contain dmi properties'
205 !response.contentAsString.contains("some DMI property")
208 def 'Call execute cm handle searches with unrecognized condition name.'() {
209 given: 'an endpoint and json data'
210 def searchesEndpoint = "$ncmpBasePathV1/ch/searches"
211 String jsonString = TestUtils.getResourceFileContent('invalid-cmhandle-search.json')
212 when: 'the searches api is invoked'
213 def response = mvc.perform(post(searchesEndpoint)
214 .contentType(MediaType.APPLICATION_JSON)
215 .content(jsonString)).andReturn().response
216 then: 'an empty cm handle identifier is returned'
217 response.contentAsString == '{"cmHandles":[]}'
220 def 'Patch resource data in pass-through running datastore.' () {
221 given: 'patch resource data url'
222 def url = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-running" +
223 "?resourceIdentifier=parent/child"
224 when: 'patch data resource request is performed'
225 def response = mvc.perform(
227 .contentType(MediaType.APPLICATION_JSON)
228 .accept(MediaType.APPLICATION_JSON).content(requestBody)
229 ).andReturn().response
230 then: 'ncmp service method to update resource is called'
231 1 * mockNetworkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
232 'parent/child', PATCH, requestBody, 'application/json;charset=UTF-8')
233 and: 'the response status is OK'
234 response.status == HttpStatus.OK.value()
237 def 'Delete resource data in pass-through running datastore.' () {
238 given: 'delete resource data url'
239 def url = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-running" +
240 "?resourceIdentifier=parent/child"
241 when: 'delete data resource request is performed'
242 def response = mvc.perform(
243 delete(url).contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)).andReturn().response
244 then: 'the ncmp service method to delete resource is called (with null as body)'
245 1 * mockNetworkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
246 'parent/child', DELETE, null, 'application/json;charset=UTF-8')
247 and: 'the response is No Content'
248 response.status == HttpStatus.NO_CONTENT.value()