ec31c7fe0389dc1d8935387acef0ee7f7b7b5ca1
[cps.git] / cps-rest / src / test / groovy / org / onap / cps / rest / controller / DataRestControllerSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation
4  *  Modifications Copyright (C) 2021 Pantheon.tech
5  *  Modifications Copyright (C) 2021 Bell Canada.
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
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.rest.controller
24
25 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
26 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
27 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
28 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch
29 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
30 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put
31
32 import org.modelmapper.ModelMapper
33 import org.onap.cps.api.CpsAdminService
34 import org.onap.cps.api.CpsDataService
35 import org.onap.cps.api.CpsModuleService
36 import org.onap.cps.api.CpsQueryService
37 import org.onap.cps.spi.model.DataNode
38 import org.onap.cps.spi.model.DataNodeBuilder
39 import org.spockframework.spring.SpringBean
40 import org.springframework.beans.factory.annotation.Autowired
41 import org.springframework.beans.factory.annotation.Value
42 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
43 import org.springframework.http.HttpStatus
44 import org.springframework.http.MediaType
45 import org.springframework.test.web.servlet.MockMvc
46 import spock.lang.Shared
47 import spock.lang.Specification
48
49 @WebMvcTest
50 class DataRestControllerSpec extends Specification {
51
52     @SpringBean
53     CpsDataService mockCpsDataService = Mock()
54
55     @SpringBean
56     CpsModuleService mockCpsModuleService = Mock()
57
58     @SpringBean
59     CpsAdminService mockCpsAdminService = Mock()
60
61     @SpringBean
62     CpsQueryService mockCpsQueryService = Mock()
63
64     @SpringBean
65     ModelMapper modelMapper = Mock()
66
67     @Autowired
68     MockMvc mvc
69
70     @Value('${rest.api.cps-base-path}')
71     def basePath
72
73     def dataNodeBaseEndpoint
74     def dataspaceName = 'my_dataspace'
75     def anchorName = 'my_anchor'
76
77     @Shared
78     static DataNode dataNodeWithLeavesNoChildren = new DataNodeBuilder().withXpath('/xpath')
79             .withLeaves([leaf: 'value', leafList: ['leaveListElement1', 'leaveListElement2']]).build()
80
81     @Shared
82     static DataNode dataNodeWithChild = new DataNodeBuilder().withXpath('/parent')
83             .withChildDataNodes([new DataNodeBuilder().withXpath("/parent/child").build()]).build()
84
85     def setup() {
86         dataNodeBaseEndpoint = "$basePath/v1/dataspaces/$dataspaceName"
87     }
88
89     def 'Create a node: #scenario.'() {
90         given: 'some json to create a data node'
91             def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
92             def json = 'some json (this is not validated)'
93         when: 'post is invoked with datanode endpoint and json'
94             def response =
95                     mvc.perform(
96                             post(endpoint)
97                                     .contentType(MediaType.APPLICATION_JSON)
98                                     .param('xpath', parentNodeXpath)
99                                     .content(json)
100                     ).andReturn().response
101         then: 'a created response is returned'
102             response.status == HttpStatus.CREATED.value()
103         then: 'the java API was called with the correct parameters'
104             1 * mockCpsDataService.saveData(dataspaceName, anchorName, json)
105         where: 'following xpath parameters are are used'
106             scenario                     | parentNodeXpath
107             'no xpath parameter'         | ''
108             'xpath parameter point root' | '/'
109     }
110
111     def 'Create a child node'() {
112         given: 'some json to create a data node'
113             def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
114             def json = 'some json (this is not validated)'
115         and: 'parent node xpath'
116             def parentNodeXpath = 'some xpath'
117         when: 'post is invoked with datanode endpoint and json'
118             def response =
119                     mvc.perform(
120                             post(endpoint)
121                                     .contentType(MediaType.APPLICATION_JSON)
122                                     .param('xpath', parentNodeXpath)
123                                     .content(json)
124                     ).andReturn().response
125         then: 'a created response is returned'
126             response.status == HttpStatus.CREATED.value()
127         then: 'the java API was called with the correct parameters'
128             1 * mockCpsDataService.saveData(dataspaceName, anchorName, parentNodeXpath, json)
129     }
130
131     def 'Create list node child elements.'() {
132         given: 'parent node xpath and json data inputs'
133             def parentNodeXpath = 'parent node xpath'
134             def jsonData = 'json data'
135         when: 'post is invoked list-node endpoint'
136             def response = mvc.perform(
137                     post("$dataNodeBaseEndpoint/anchors/$anchorName/list-node")
138                             .contentType(MediaType.APPLICATION_JSON)
139                             .param('xpath', parentNodeXpath)
140                             .content(jsonData)
141             ).andReturn().response
142         then: 'a created response is returned'
143             response.status == HttpStatus.CREATED.value()
144         then: 'the java API was called with the correct parameters'
145             1 * mockCpsDataService.saveListNodeData(dataspaceName, anchorName, parentNodeXpath, jsonData)
146     }
147
148     def 'Get data node with leaves'() {
149         given: 'the service returns data node leaves'
150             def xpath = 'some xPath'
151             def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/node"
152             mockCpsDataService.getDataNode(dataspaceName, anchorName, xpath, OMIT_DESCENDANTS) >> dataNodeWithLeavesNoChildren
153         when: 'get request is performed through REST API'
154             def response =
155                     mvc.perform(get(endpoint).param('xpath', xpath))
156                             .andReturn().response
157         then: 'a success response is returned'
158             response.status == HttpStatus.OK.value()
159         and: 'response contains expected leaf and value'
160             response.contentAsString.contains('"leaf":"value"')
161         and: 'response contains expected leaf-list and values'
162             response.contentAsString.contains('"leafList":["leaveListElement1","leaveListElement2"]')
163     }
164
165     def 'Get data node with #scenario.'() {
166         given: 'the service returns data node with #scenario'
167             def xpath = 'some xPath'
168             def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/node"
169             mockCpsDataService.getDataNode(dataspaceName, anchorName, xpath, expectedCpsDataServiceOption) >> dataNode
170         when: 'get request is performed through REST API'
171             def response =
172                     mvc.perform(
173                             get(endpoint)
174                                     .param('xpath', xpath)
175                                     .param('include-descendants', includeDescendantsOption))
176                             .andReturn().response
177         then: 'a success response is returned'
178             response.status == HttpStatus.OK.value()
179         and: 'the response contains child is #expectChildInResponse'
180             response.contentAsString.contains('"child"') == expectChildInResponse
181         where:
182             scenario                    | dataNode                     | includeDescendantsOption || expectedCpsDataServiceOption | expectChildInResponse
183             'no descendants by default' | dataNodeWithLeavesNoChildren | ''                       || OMIT_DESCENDANTS             | false
184             'no descendant explicitly'  | dataNodeWithLeavesNoChildren | 'false'                  || OMIT_DESCENDANTS             | false
185             'with descendants'          | dataNodeWithChild            | 'true'                   || INCLUDE_ALL_DESCENDANTS      | true
186     }
187
188     def 'Update data node leaves: #scenario.'() {
189         given: 'json data'
190             def jsonData = 'json data'
191             def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
192         when: 'patch request is performed'
193             def response =
194                     mvc.perform(
195                             patch(endpoint)
196                                     .contentType(MediaType.APPLICATION_JSON)
197                                     .content(jsonData)
198                                     .param('xpath', inputXpath)
199                     ).andReturn().response
200         then: 'the service method is invoked with expected parameters'
201             1 * mockCpsDataService.updateNodeLeaves(dataspaceName, anchorName, xpathServiceParameter, jsonData)
202         and: 'response status indicates success'
203             response.status == HttpStatus.OK.value()
204         where:
205             scenario               | inputXpath    || xpathServiceParameter
206             'root node by default' | ''            || '/'
207             'root node by choice'  | '/'           || '/'
208             'some xpath by parent' | '/some/xpath' || '/some/xpath'
209     }
210
211     def 'Replace data node tree: #scenario.'() {
212         given: 'json data'
213             def jsonData = 'json data'
214             def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
215         when: 'put request is performed'
216             def response =
217                     mvc.perform(
218                             put(endpoint)
219                                     .contentType(MediaType.APPLICATION_JSON)
220                                     .content(jsonData)
221                                     .param('xpath', inputXpath))
222                             .andReturn().response
223         then: 'the service method is invoked with expected parameters'
224             1 * mockCpsDataService.replaceNodeTree(dataspaceName, anchorName, xpathServiceParameter, jsonData)
225         and: 'response status indicates success'
226             response.status == HttpStatus.OK.value()
227         where:
228             scenario               | inputXpath    || xpathServiceParameter
229             'root node by default' | ''            || '/'
230             'root node by choice'  | '/'           || '/'
231             'some xpath by parent' | '/some/xpath' || '/some/xpath'
232     }
233
234     def 'Replace list node child elements.'() {
235         given: 'parent node xpath and json data inputs'
236             def parentNodeXpath = 'parent node xpath'
237             def jsonData = 'json data'
238         when: 'patch is invoked list-node endpoint'
239             def response = mvc.perform(
240                     patch("$dataNodeBaseEndpoint/anchors/$anchorName/list-node")
241                             .contentType(MediaType.APPLICATION_JSON)
242                             .param('xpath', parentNodeXpath)
243                             .content(jsonData)
244             ).andReturn().response
245         then: 'a success response is returned'
246             response.status == HttpStatus.OK.value()
247         then: 'the java API was called with the correct parameters'
248             1 * mockCpsDataService.replaceListNodeData(dataspaceName, anchorName, parentNodeXpath, jsonData)
249     }
250 }