Merge "Update operation passthrough running - Service Layer"
[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 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.onap.cps.TestUtils
26 import org.onap.cps.spi.model.ModuleReference
27
28 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
29 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
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
35 import com.google.gson.Gson
36 import org.onap.cps.ncmp.api.NetworkCmProxyDataService
37 import org.onap.cps.spi.model.DataNodeBuilder
38 import org.spockframework.spring.SpringBean
39 import org.springframework.beans.factory.annotation.Autowired
40 import org.springframework.beans.factory.annotation.Value
41 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
42 import org.springframework.http.HttpStatus
43 import org.springframework.http.MediaType
44 import org.springframework.test.web.servlet.MockMvc
45 import spock.lang.Specification
46
47 @WebMvcTest(NetworkCmProxyController)
48 class NetworkCmProxyControllerSpec extends Specification {
49
50     @Autowired
51     MockMvc mvc
52
53     @SpringBean
54     NetworkCmProxyDataService mockNetworkCmProxyDataService = Mock()
55
56     @Value('${rest.api.ncmp-base-path}/v1')
57     def ncmpBasePathV1
58
59     def cmHandle = 'some handle'
60     def xpath = 'some xpath'
61
62     def 'Query data node by cps path for the given cm handle with #scenario.'() {
63         given: 'service method returns a list containing a data node'
64             def dataNode = new DataNodeBuilder().withXpath('/xpath').build()
65             def cpsPath = 'some cps-path'
66             mockNetworkCmProxyDataService.queryDataNodes(cmHandle, cpsPath, expectedCpsDataServiceOption) >> [dataNode]
67         and: 'the query endpoint'
68             def dataNodeEndpoint = "$ncmpBasePathV1/cm-handles/$cmHandle/nodes/query"
69         when: 'query data nodes API is invoked'
70             def response = mvc.perform(get(dataNodeEndpoint)
71                     .param('cps-path', cpsPath)
72                     .param('include-descendants', includeDescendantsOption))
73                     .andReturn().response
74         then: 'the response contains the the datanode in json format'
75             response.status == HttpStatus.OK.value()
76             def expectedJsonContent = new Gson().toJson(dataNode)
77             response.getContentAsString().contains(expectedJsonContent)
78         where: 'the following options for include descendants are provided in the request'
79             scenario                    | includeDescendantsOption || expectedCpsDataServiceOption
80             'no descendants by default' | ''                       || OMIT_DESCENDANTS
81             'no descendant explicitly'  | 'false'                  || OMIT_DESCENDANTS
82             'descendants'               | 'true'                   || INCLUDE_ALL_DESCENDANTS
83     }
84
85     def 'Create data node: #scenario.'() {
86         given: 'json data'
87             def jsonData = 'json data'
88         when: 'post request is performed'
89             def response = mvc.perform(
90                     post("$ncmpBasePathV1/cm-handles/$cmHandle/nodes")
91                             .contentType(MediaType.APPLICATION_JSON)
92                             .content(jsonData)
93                             .param('xpath', reqXpath)
94             ).andReturn().response
95         then: 'the service method is invoked once with expected parameters'
96             1 * mockNetworkCmProxyDataService.createDataNode(cmHandle, usedXpath, jsonData)
97         and: 'response status indicates success'
98             response.status == HttpStatus.CREATED.value()
99         where: 'following parameters were used'
100             scenario             | reqXpath || usedXpath
101             'no xpath parameter' | ''       || '/'
102             'root xpath'         | '/'      || '/'
103             'parent node xpath'  | '/xpath' || '/xpath'
104     }
105
106     def 'Add list-node elements.'() {
107         given: 'json data and parent node xpath'
108             def jsonData = 'json data'
109             def parentNodeXpath = 'parent node xpath'
110         when: 'post request is performed'
111             def response = mvc.perform(
112                     post("$ncmpBasePathV1/cm-handles/$cmHandle/list-node")
113                             .contentType(MediaType.APPLICATION_JSON)
114                             .content(jsonData)
115                             .param('xpath', parentNodeXpath)
116             ).andReturn().response
117         then: 'the service method is invoked once with expected parameters'
118             1 * mockNetworkCmProxyDataService.addListNodeElements(cmHandle, parentNodeXpath, jsonData)
119         and: 'response status indicates success'
120             response.status == HttpStatus.CREATED.value()
121     }
122
123     def 'Update data node leaves.'() {
124         given: 'json data'
125             def jsonData = 'json data'
126         and: 'the query endpoint'
127             def endpoint = "$ncmpBasePathV1/cm-handles/$cmHandle/nodes"
128         when: 'patch request is performed'
129             def response = mvc.perform(
130                     patch(endpoint)
131                             .contentType(MediaType.APPLICATION_JSON)
132                             .content(jsonData)
133                             .param('xpath', xpath)
134             ).andReturn().response
135         then: 'the service method is invoked once with expected parameters'
136             1 * mockNetworkCmProxyDataService.updateNodeLeaves(cmHandle, xpath, jsonData)
137         and: 'response status indicates success'
138             response.status == HttpStatus.OK.value()
139     }
140
141     def 'Replace data node tree.'() {
142         given: 'json data'
143             def jsonData = 'json data'
144         and: 'the query endpoint'
145             def endpoint = "$ncmpBasePathV1/cm-handles/$cmHandle/nodes"
146         when: 'put request is performed'
147             def response = mvc.perform(
148                     put(endpoint)
149                             .contentType(MediaType.APPLICATION_JSON)
150                             .content(jsonData)
151                             .param('xpath', xpath)
152             ).andReturn().response
153         then: 'the service method is invoked once with expected parameters'
154             1 * mockNetworkCmProxyDataService.replaceNodeTree(cmHandle, xpath, jsonData)
155         and: 'response status indicates success'
156             response.status == HttpStatus.OK.value()
157     }
158
159     def 'Get data node.'() {
160         given: 'the service returns a data node'
161             def xpath = 'some xpath'
162             def dataNode = new DataNodeBuilder().withXpath(xpath).withLeaves(["leaf": "value"]).build()
163             mockNetworkCmProxyDataService.getDataNode(cmHandle, xpath, OMIT_DESCENDANTS) >> dataNode
164         and: 'the query endpoint'
165             def endpoint = "$ncmpBasePathV1/cm-handles/$cmHandle/node"
166         when: 'get request is performed through REST API'
167             def response = mvc.perform(get(endpoint).param('xpath', xpath)).andReturn().response
168         then: 'a success response is returned'
169             response.status == HttpStatus.OK.value()
170         and: 'response contains expected leaf and value'
171             response.contentAsString.contains('"leaf":"value"')
172     }
173
174     def 'Get Resource Data from passthrough operational.' () {
175         given: 'resource data url'
176             def getUrl = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-operational" +
177                     "?resourceIdentifier=parent/child&options=(a=1,b=2)"
178         when: 'get data resource request is performed'
179             def response = mvc.perform(
180                     get(getUrl)
181                             .contentType(MediaType.APPLICATION_JSON)
182                     .accept(MediaType.APPLICATION_JSON_VALUE)
183             ).andReturn().response
184         then: 'the NCMP data service is called with getResourceDataOperationalForCmHandle'
185             1 * mockNetworkCmProxyDataService.getResourceDataOperationalForCmHandle('testCmHandle',
186                     'parent/child',
187                     'application/json',
188                     '(a=1,b=2)')
189         and: 'response status is Ok'
190             response.status == HttpStatus.OK.value()
191     }
192
193     def 'Get Resource Data from passthrough running with #scenario value in resource identifier param.' () {
194         given: 'resource data url'
195             def getUrl = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-running" +
196                     "?resourceIdentifier=" + resourceIdentifier + "&options=(a=1,b=2)"
197         and: 'ncmp service returns json object'
198             mockNetworkCmProxyDataService.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
199                     resourceIdentifier,
200                     'application/json',
201                     '(a=1,b=2)') >> '{valid-json}'
202         when: 'get data resource request is performed'
203             def response = mvc.perform(
204                     get(getUrl)
205                             .contentType(MediaType.APPLICATION_JSON)
206                             .accept(MediaType.APPLICATION_JSON_VALUE)
207             ).andReturn().response
208         then: 'response status is Ok'
209             response.status == HttpStatus.OK.value()
210         and: 'response contains valid object body'
211             response.getContentAsString() == '{valid-json}'
212         where: 'tokens are used in the resource identifier parameter'
213             scenario                       | resourceIdentifier
214             '/'                            | 'id/with/slashes'
215             '?'                            | 'idWith?'
216             ','                            | 'idWith,'
217             '='                            | 'idWith='
218             '[]'                           | 'idWith[]'
219             '? needs to be encoded as %3F' | 'idWith%3F'
220     }
221
222     def 'Create Resource Data from passthrough running with #scenario.' () {
223         given: 'resource data url'
224             def getUrl = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-running" +
225                     "?resourceIdentifier=parent/child"
226         when: 'get data resource request is performed'
227             def response = mvc.perform(
228                     post(getUrl)
229                             .contentType(MediaType.APPLICATION_JSON_VALUE)
230                             .accept(MediaType.APPLICATION_JSON_VALUE).content(requestBody)
231             ).andReturn().response
232         then: 'ncmp service method to create resource called'
233             1 * mockNetworkCmProxyDataService.createResourceDataPassThroughRunningForCmHandle('testCmHandle',
234                     'parent/child', requestBody, 'application/json;charset=UTF-8')
235         and: 'resource is created'
236             response.status == HttpStatus.CREATED.value()
237         where: 'given request body'
238             scenario                        |  requestBody
239             'body contains " and new line'  |  'body with " quote and \n new line'
240             'body contains normal string'   |  'normal request body'
241     }
242
243     def 'Get module references for the given dataspace and cm handle.' () {
244         given: 'get module references url'
245             def getUrl = "$ncmpBasePathV1/ch/some-cmhandle/modules"
246         when: 'get module resource request is performed'
247             def response =mvc.perform(get(getUrl)).andReturn().response
248         then: 'ncmp service method to get yang resource module references is called'
249             mockNetworkCmProxyDataService.getYangResourcesModuleReferences('some-cmhandle')
250                     >> [new ModuleReference(moduleName: 'some-name1',revision: 'some-revision1')]
251         and: 'response contains an array with the module name and revision'
252             response.getContentAsString() == '[{"moduleName":"some-name1","revision":"some-revision1"}]'
253         and: 'response returns an OK http code'
254             response.status == HttpStatus.OK.value()
255     }
256
257     def 'Retrieve cm handles.'() {
258         given: 'an endpoint and json data'
259             def searchesEndpoint = "$ncmpBasePathV1/ch/searches"
260             String jsonData = TestUtils.getResourceFileContent('cmhandle-search.json')
261         and: 'the service method is invoked with module names and returns two cm handle ids'
262             mockNetworkCmProxyDataService.executeCmHandleHasAllModulesSearch(['module1', 'module2']) >> ['some-cmhandle-id1', 'some-cmhandle-id2']
263         when: 'the searches api is invoked'
264             def response = mvc.perform(post(searchesEndpoint)
265                     .contentType(MediaType.APPLICATION_JSON)
266                     .content(jsonData)).andReturn().response
267         then: 'response status returns OK'
268             response.status == HttpStatus.OK.value()
269         and: 'the expected response content is returned'
270             response.contentAsString == '{"cmHandles":[{"cmHandleId":"some-cmhandle-id1"},{"cmHandleId":"some-cmhandle-id2"}]}'
271     }
272
273     def 'Call execute cm handle searches with unrecognized condition name.'() {
274         given: 'an endpoint and json data'
275             def searchesEndpoint = "$ncmpBasePathV1/ch/searches"
276             String jsonData = TestUtils.getResourceFileContent('invalid-cmhandle-search.json')
277         when: 'the searches api is invoked'
278             def response = mvc.perform(post(searchesEndpoint)
279                     .contentType(MediaType.APPLICATION_JSON)
280                     .content(jsonData)).andReturn().response
281         then: 'an empty cm handle identifier is returned'
282             response.contentAsString == '{"cmHandles":[]}'
283     }
284
285     def 'Update resource data from passthrough running.' () {
286         given: 'update resource data url'
287             def updateUrl = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-running" +
288                     "?resourceIdentifier=parent/child"
289         when: 'update data resource request is performed'
290             def response = mvc.perform(
291                     put(updateUrl)
292                             .contentType(MediaType.APPLICATION_JSON_VALUE)
293                             .accept(MediaType.APPLICATION_JSON_VALUE).content('some-request-body')
294             ).andReturn().response
295         then: 'ncmp service method to update resource is called'
296             1 * mockNetworkCmProxyDataService.updateResourceDataPassThroughRunningForCmHandle('testCmHandle',
297                     'parent/child', 'some-request-body', 'application/json;charset=UTF-8')
298         and: 'the response status is OK'
299             response.status == HttpStatus.OK.value()
300     }
301 }
302