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-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
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.mapstruct.factory.Mappers
26 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
27 import spock.lang.Shared
29 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.PATCH
30 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete
31 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
32 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch
33 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
34 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put
35 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.CREATE
36 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.UPDATE
37 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.DELETE
39 import com.fasterxml.jackson.databind.ObjectMapper
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 JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
66 NcmpRestInputMapper ncmpRestInputMapper = Mappers.getMapper(NcmpRestInputMapper)
68 @Value('${rest.api.ncmp-base-path}/v1')
71 def requestBody = '{"some-key":"some-value"}'
76 def 'Get Resource Data from pass-through operational.'() {
77 given: 'resource data url'
78 def getUrl = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-operational" +
79 "?resourceIdentifier=parent/child&options=(a=1,b=2)"
80 when: 'get data resource request is performed'
81 def response = mvc.perform(
83 .contentType(MediaType.APPLICATION_JSON)
84 ).andReturn().response
85 then: 'the NCMP data service is called with getResourceDataOperationalForCmHandle'
86 1 * mockNetworkCmProxyDataService.getResourceDataOperationalForCmHandle('testCmHandle',
90 and: 'response status is Ok'
91 response.status == HttpStatus.OK.value()
94 def 'Get Resource Data from pass-through operational with #scenario.'() {
95 given: 'resource data url'
96 def getUrl = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-operational" +
97 "?resourceIdentifier=parent/child&options=(a=1,b=2)${topicQueryParam}"
98 when: 'get data resource request is performed'
99 def response = mvc.perform(
101 .contentType(MediaType.APPLICATION_JSON)
102 ).andReturn().response
103 then: 'the NCMP data service is called with operational data for cm handle'
104 1 * mockNetworkCmProxyDataService.getResourceDataOperationalForCmHandle('testCmHandle',
108 and: 'response status is Ok'
109 response.status == HttpStatus.OK.value()
110 where: 'the following parameters are used'
111 scenario | topicQueryParam || expectedTopicName
112 'Url with valid topic' | "&topic=my-topic-name" || "my-topic-name"
113 'No topic in url' | '' || NO_TOPIC
114 'Null topic in url' | "&topic=null" || "null"
115 'Empty topic in url' | "&topic=\"\"" || "\"\""
116 'Missing topic in url' | "&topic=" || ""
119 def 'Get Resource Data from pass-through running with #scenario value in resource identifier param.'() {
120 given: 'resource data url'
121 def getUrl = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-running" +
122 "?resourceIdentifier=" + resourceIdentifier + "&options=(a=1,b=2)"
123 and: 'ncmp service returns json object'
124 mockNetworkCmProxyDataService.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
127 NO_TOPIC) >> '{valid-json}'
128 when: 'get data resource request is performed'
129 def response = mvc.perform(
131 .contentType(MediaType.APPLICATION_JSON)
132 ).andReturn().response
133 then: 'response status is Ok'
134 response.status == HttpStatus.OK.value()
135 and: 'response contains valid object body'
136 response.getContentAsString() == '{valid-json}'
137 where: 'tokens are used in the resource identifier parameter'
138 scenario | resourceIdentifier
139 '/' | 'id/with/slashes'
144 '? needs to be encoded as %3F' | 'idWith%3F'
147 def 'Update resource data from pass-through running.' () {
148 given: 'update resource data url'
149 def updateUrl = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-running" +
150 "?resourceIdentifier=parent/child"
151 when: 'update data resource request is performed'
152 def response = mvc.perform(
154 .contentType(MediaType.APPLICATION_JSON_VALUE).content(requestBody)
155 ).andReturn().response
156 then: 'ncmp service method to update resource is called'
157 1 * mockNetworkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
158 'parent/child', UPDATE, requestBody, 'application/json;charset=UTF-8')
159 and: 'the response status is OK'
160 response.status == HttpStatus.OK.value()
163 def 'Create Resource Data from pass-through running with #scenario.' () {
164 given: 'resource data url'
165 def url = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-running" +
166 "?resourceIdentifier=parent/child"
167 def requestBody = '{"some-key":"some-value"}'
168 when: 'create resource request is performed'
169 def response = mvc.perform(
171 .contentType(MediaType.APPLICATION_JSON_VALUE).content(requestBody)
172 ).andReturn().response
173 then: 'ncmp service method to create resource called'
174 1 * mockNetworkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
175 'parent/child', CREATE, requestBody, 'application/json;charset=UTF-8')
176 and: 'resource is created'
177 response.status == HttpStatus.CREATED.value()
180 def 'Get module references for the given dataspace and cm handle.' () {
181 given: 'get module references url'
182 def getUrl = "$ncmpBasePathV1/ch/some-cmhandle/modules"
183 when: 'get module resource request is performed'
184 def response =mvc.perform(get(getUrl)).andReturn().response
185 then: 'ncmp service method to get yang resource module references is called'
186 mockNetworkCmProxyDataService.getYangResourcesModuleReferences('some-cmhandle')
187 >> [new ModuleReference(moduleName: 'some-name1',revision: '2021-10-03')]
188 and: 'response contains an array with the module name and revision'
189 response.getContentAsString() == '[{"moduleName":"some-name1","revision":"2021-10-03"}]'
190 and: 'response returns an OK http code'
191 response.status == HttpStatus.OK.value()
194 def 'Retrieve cm handles.'() {
195 given: 'an endpoint and json data'
196 def searchesEndpoint = "$ncmpBasePathV1/ch/searches"
197 String jsonString = TestUtils.getResourceFileContent('cmhandle-search.json')
198 and: 'the service method is invoked with module names and returns two cm handle ids'
199 mockNetworkCmProxyDataService.executeCmHandleHasAllModulesSearch(['module1', 'module2']) >> ['some-cmhandle-id1', 'some-cmhandle-id2']
200 when: 'the searches api is invoked'
201 def response = mvc.perform(post(searchesEndpoint)
202 .contentType(MediaType.APPLICATION_JSON)
203 .content(jsonString)).andReturn().response
204 then: 'response status returns OK'
205 response.status == HttpStatus.OK.value()
206 and: 'the expected response content is returned'
207 response.contentAsString == '{"cmHandles":[{"cmHandleId":"some-cmhandle-id1"},{"cmHandleId":"some-cmhandle-id2"}]}'
210 def 'Get Cm Handle details by Cm Handle id.' () {
211 given: 'an endpoint and a cm handle'
212 def cmHandleDetailsEndpoint = "$ncmpBasePathV1/ch/Some-Cm-Handle"
213 and: 'an existing ncmp service cm handle'
214 def cmHandleId = 'Some-Cm-Handle'
215 def dmiProperties = [ prop:'some DMI property' ]
216 def publicProperties = [ "public prop":'some public property' ]
217 def ncmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleID: cmHandleId, dmiProperties: dmiProperties, publicProperties: publicProperties)
218 and: 'the service method is invoked with the cm handle id'
219 1 * mockNetworkCmProxyDataService.getNcmpServiceCmHandle('Some-Cm-Handle') >> ncmpServiceCmHandle
220 when: 'the cm handle details api is invoked'
221 def response = mvc.perform(get(cmHandleDetailsEndpoint)).andReturn().response
222 then: 'the correct response is returned'
223 response.status == HttpStatus.OK.value()
224 and: 'the response returns public properties and the correct properties'
225 response.contentAsString.contains('publicCmHandleProperties')
226 response.contentAsString.contains('public prop')
227 response.contentAsString.contains('some public property')
228 and: 'the content does not contain dmi properties'
229 !response.contentAsString.contains("some DMI property")
232 def 'Call execute cm handle searches with unrecognized condition name.'() {
233 given: 'an endpoint and json data'
234 def searchesEndpoint = "$ncmpBasePathV1/ch/searches"
235 String jsonString = TestUtils.getResourceFileContent('invalid-cmhandle-search.json')
236 when: 'the searches api is invoked'
237 def response = mvc.perform(post(searchesEndpoint)
238 .contentType(MediaType.APPLICATION_JSON)
239 .content(jsonString)).andReturn().response
240 then: 'an empty cm handle identifier is returned'
241 response.contentAsString == '{"cmHandles":[]}'
244 def 'Patch resource data in pass-through running datastore.' () {
245 given: 'patch resource data url'
246 def url = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-running" +
247 "?resourceIdentifier=parent/child"
248 when: 'patch data resource request is performed'
249 def response = mvc.perform(
251 .contentType(MediaType.APPLICATION_JSON)
252 .accept(MediaType.APPLICATION_JSON).content(requestBody)
253 ).andReturn().response
254 then: 'ncmp service method to update resource is called'
255 1 * mockNetworkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
256 'parent/child', PATCH, requestBody, 'application/json;charset=UTF-8')
257 and: 'the response status is OK'
258 response.status == HttpStatus.OK.value()
261 def 'Delete resource data in pass-through running datastore.' () {
262 given: 'delete resource data url'
263 def url = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-running" +
264 "?resourceIdentifier=parent/child"
265 when: 'delete data resource request is performed'
266 def response = mvc.perform(
267 delete(url).contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)).andReturn().response
268 then: 'the ncmp service method to delete resource is called (with null as body)'
269 1 * mockNetworkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
270 'parent/child', DELETE, null, 'application/json;charset=UTF-8')
271 and: 'the response is No Content'
272 response.status == HttpStatus.NO_CONTENT.value()