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