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