Merge "Replacing ModelMapper with MapStruct"
[cps.git] / cps-ncmp-rest / src / test / groovy / org / onap / cps / ncmp / rest / controller / NetworkCmProxyControllerSpec.groovy
1 /*
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
11  *
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.
18  *
19  *  SPDX-License-Identifier: Apache-2.0
20  *  ============LICENSE_END=========================================================
21  */
22
23 package org.onap.cps.ncmp.rest.controller
24
25 import org.mapstruct.factory.Mappers
26 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
27 import spock.lang.Shared
28
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
38
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
52
53 @WebMvcTest(NetworkCmProxyController)
54 class NetworkCmProxyControllerSpec extends Specification {
55
56     @Autowired
57     MockMvc mvc
58
59     @SpringBean
60     NetworkCmProxyDataService mockNetworkCmProxyDataService = Mock()
61
62     @SpringBean
63     JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
64
65     @SpringBean
66     NcmpRestInputMapper ncmpRestInputMapper = Mappers.getMapper(NcmpRestInputMapper)
67
68     @Value('${rest.api.ncmp-base-path}/v1')
69     def ncmpBasePathV1
70
71     def requestBody = '{"some-key":"some-value"}'
72
73     @Shared
74     def NO_TOPIC = null
75
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(
82                     get(getUrl)
83                             .contentType(MediaType.APPLICATION_JSON)
84                     .accept(MediaType.APPLICATION_JSON_VALUE)
85             ).andReturn().response
86         then: 'the NCMP data service is called with getResourceDataOperationalForCmHandle'
87             1 * mockNetworkCmProxyDataService.getResourceDataOperationalForCmHandle('testCmHandle',
88                     'parent/child',
89                     'application/json',
90                     '(a=1,b=2)',
91                     NO_TOPIC)
92         and: 'response status is Ok'
93             response.status == HttpStatus.OK.value()
94     }
95
96     def 'Get Resource Data from pass-through operational with #scenario.'() {
97         given: 'resource data url'
98             def getUrl = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-operational" +
99                     "?resourceIdentifier=parent/child&options=(a=1,b=2)${topicQueryParam}"
100         when: 'get data resource request is performed'
101             def response = mvc.perform(
102                     get(getUrl)
103                     .contentType(MediaType.APPLICATION_JSON)
104                     .accept(MediaType.APPLICATION_JSON_VALUE)
105             ).andReturn().response
106         then: 'the NCMP data service is called with operational data for cm handle'
107             1 * mockNetworkCmProxyDataService.getResourceDataOperationalForCmHandle('testCmHandle',
108                     'parent/child',
109                     'application/json',
110                     '(a=1,b=2)',
111                     expectedTopicName)
112         and: 'response status is Ok'
113             response.status == HttpStatus.OK.value()
114         where: 'the following parameters are used'
115             scenario               | topicQueryParam        || expectedTopicName
116             'Url with valid topic' | "&topic=my-topic-name" || "my-topic-name"
117             'No topic in url'      | ''                     || NO_TOPIC
118             'Null topic in url'    | "&topic=null"          || "null"
119             'Empty topic in url'   | "&topic=\"\""          || "\"\""
120             'Missing topic in url' | "&topic="              || ""
121     }
122
123     def 'Get Resource Data from pass-through running with #scenario value in resource identifier param.'() {
124         given: 'resource data url'
125             def getUrl = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-running" +
126                     "?resourceIdentifier=" + resourceIdentifier + "&options=(a=1,b=2)"
127         and: 'ncmp service returns json object'
128             mockNetworkCmProxyDataService.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
129                     resourceIdentifier,
130                     'application/json',
131                     '(a=1,b=2)',
132                     NO_TOPIC) >> '{valid-json}'
133         when: 'get data resource request is performed'
134             def response = mvc.perform(
135                     get(getUrl)
136                             .contentType(MediaType.APPLICATION_JSON)
137                             .accept(MediaType.APPLICATION_JSON_VALUE)
138             ).andReturn().response
139         then: 'response status is Ok'
140             response.status == HttpStatus.OK.value()
141         and: 'response contains valid object body'
142             response.getContentAsString() == '{valid-json}'
143         where: 'tokens are used in the resource identifier parameter'
144             scenario                       | resourceIdentifier
145             '/'                            | 'id/with/slashes'
146             '?'                            | 'idWith?'
147             ','                            | 'idWith,'
148             '='                            | 'idWith='
149             '[]'                           | 'idWith[]'
150             '? needs to be encoded as %3F' | 'idWith%3F'
151     }
152
153     def 'Update resource data from pass-through running.' () {
154         given: 'update resource data url'
155             def updateUrl = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-running" +
156                 "?resourceIdentifier=parent/child"
157         when: 'update data resource request is performed'
158             def response = mvc.perform(
159                 put(updateUrl)
160                     .contentType(MediaType.APPLICATION_JSON_VALUE)
161                     .accept(MediaType.APPLICATION_JSON_VALUE).content(requestBody)
162             ).andReturn().response
163         then: 'ncmp service method to update resource is called'
164             1 * mockNetworkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
165                 'parent/child', UPDATE, requestBody, 'application/json;charset=UTF-8')
166         and: 'the response status is OK'
167             response.status == HttpStatus.OK.value()
168     }
169
170     def 'Create Resource Data from pass-through running with #scenario.' () {
171         given: 'resource data url'
172             def url = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-running" +
173                     "?resourceIdentifier=parent/child"
174             def requestBody = '{"some-key":"some-value"}'
175         when: 'create resource request is performed'
176             def response = mvc.perform(
177                     post(url)
178                             .contentType(MediaType.APPLICATION_JSON_VALUE)
179                             .accept(MediaType.APPLICATION_JSON_VALUE).content(requestBody)
180             ).andReturn().response
181         then: 'ncmp service method to create resource called'
182             1 * mockNetworkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
183                 'parent/child', CREATE, requestBody, 'application/json;charset=UTF-8')
184         and: 'resource is created'
185             response.status == HttpStatus.CREATED.value()
186     }
187
188     def 'Get module references for the given dataspace and cm handle.' () {
189         given: 'get module references url'
190             def getUrl = "$ncmpBasePathV1/ch/some-cmhandle/modules"
191         when: 'get module resource request is performed'
192             def response =mvc.perform(get(getUrl)).andReturn().response
193         then: 'ncmp service method to get yang resource module references is called'
194             mockNetworkCmProxyDataService.getYangResourcesModuleReferences('some-cmhandle')
195                     >> [new ModuleReference(moduleName: 'some-name1',revision: '2021-10-03')]
196         and: 'response contains an array with the module name and revision'
197             response.getContentAsString() == '[{"moduleName":"some-name1","revision":"2021-10-03"}]'
198         and: 'response returns an OK http code'
199             response.status == HttpStatus.OK.value()
200     }
201
202     def 'Retrieve cm handles.'() {
203         given: 'an endpoint and json data'
204             def searchesEndpoint = "$ncmpBasePathV1/ch/searches"
205             String jsonString = TestUtils.getResourceFileContent('cmhandle-search.json')
206         and: 'the service method is invoked with module names and returns two cm handle ids'
207             mockNetworkCmProxyDataService.executeCmHandleHasAllModulesSearch(['module1', 'module2']) >> ['some-cmhandle-id1', 'some-cmhandle-id2']
208         when: 'the searches api is invoked'
209             def response = mvc.perform(post(searchesEndpoint)
210                     .contentType(MediaType.APPLICATION_JSON)
211                     .content(jsonString)).andReturn().response
212         then: 'response status returns OK'
213             response.status == HttpStatus.OK.value()
214         and: 'the expected response content is returned'
215             response.contentAsString == '{"cmHandles":[{"cmHandleId":"some-cmhandle-id1"},{"cmHandleId":"some-cmhandle-id2"}]}'
216     }
217
218     def 'Get Cm Handle details by Cm Handle id.' () {
219         given: 'an endpoint and a cm handle'
220             def cmHandleDetailsEndpoint = "$ncmpBasePathV1/ch/Some-Cm-Handle"
221         and: 'an existing ncmp service cm handle'
222             def cmHandleId = 'Some-Cm-Handle'
223             def dmiProperties = [ prop:'some DMI property' ]
224             def publicProperties = [ "public prop":'some public property' ]
225             def ncmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleID: cmHandleId, dmiProperties: dmiProperties, publicProperties: publicProperties)
226         and: 'the service method is invoked with the cm handle id'
227             1 * mockNetworkCmProxyDataService.getNcmpServiceCmHandle('Some-Cm-Handle') >> ncmpServiceCmHandle
228         when: 'the cm handle details api is invoked'
229             def response = mvc.perform(get(cmHandleDetailsEndpoint)).andReturn().response
230         then: 'the correct response is returned'
231             response.status == HttpStatus.OK.value()
232         and: 'the response returns public properties and the correct properties'
233             response.contentAsString.contains('publicCmHandleProperties')
234             response.contentAsString.contains('public prop')
235             response.contentAsString.contains('some public property')
236         and: 'the content does not contain dmi properties'
237             !response.contentAsString.contains("some DMI property")
238     }
239
240     def 'Call execute cm handle searches with unrecognized condition name.'() {
241         given: 'an endpoint and json data'
242             def searchesEndpoint = "$ncmpBasePathV1/ch/searches"
243             String jsonString = TestUtils.getResourceFileContent('invalid-cmhandle-search.json')
244         when: 'the searches api is invoked'
245             def response = mvc.perform(post(searchesEndpoint)
246                     .contentType(MediaType.APPLICATION_JSON)
247                     .content(jsonString)).andReturn().response
248         then: 'an empty cm handle identifier is returned'
249             response.contentAsString == '{"cmHandles":[]}'
250     }
251
252     def 'Patch resource data in pass-through running datastore.' () {
253         given: 'patch resource data url'
254             def url = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-running" +
255                     "?resourceIdentifier=parent/child"
256         when: 'patch data resource request is performed'
257             def response = mvc.perform(
258                     patch(url)
259                             .contentType(MediaType.APPLICATION_JSON)
260                             .accept(MediaType.APPLICATION_JSON).content(requestBody)
261             ).andReturn().response
262         then: 'ncmp service method to update resource is called'
263             1 * mockNetworkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
264                     'parent/child', PATCH, requestBody, 'application/json;charset=UTF-8')
265         and: 'the response status is OK'
266             response.status == HttpStatus.OK.value()
267     }
268
269     def 'Delete resource data in pass-through running datastore.' () {
270         given: 'delete resource data url'
271             def url = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-running" +
272                      "?resourceIdentifier=parent/child"
273         when: 'delete data resource request is performed'
274             def response = mvc.perform(
275                 delete(url).contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)).andReturn().response
276         then: 'the ncmp service method to delete resource is called (with null as body)'
277             1 * mockNetworkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
278                 'parent/child', DELETE, null, 'application/json;charset=UTF-8')
279         and: 'the response is No Content'
280             response.status == HttpStatus.NO_CONTENT.value()
281     }
282 }
283