Replace list-node content (part 2): CPS REST layer
[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  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.rest.controller
23
24 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
25 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
26 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
27 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch
28 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
29 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put
30
31 import org.modelmapper.ModelMapper
32 import org.onap.cps.api.CpsAdminService
33 import org.onap.cps.api.CpsDataService
34 import org.onap.cps.api.CpsModuleService
35 import org.onap.cps.api.CpsQueryService
36 import org.onap.cps.spi.model.DataNode
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.Shared
46 import spock.lang.Specification
47
48 @WebMvcTest
49 class DataRestControllerSpec extends Specification {
50
51     @SpringBean
52     CpsDataService mockCpsDataService = Mock()
53
54     @SpringBean
55     CpsModuleService mockCpsModuleService = Mock()
56
57     @SpringBean
58     CpsAdminService mockCpsAdminService = Mock()
59
60     @SpringBean
61     CpsQueryService mockCpsQueryService = Mock()
62
63     @SpringBean
64     ModelMapper modelMapper = Mock()
65
66     @Autowired
67     MockMvc mvc
68
69     @Value('${rest.api.cps-base-path}')
70     def basePath
71
72     def dataNodeBaseEndpoint
73     def dataspaceName = 'my_dataspace'
74     def anchorName = 'my_anchor'
75
76     @Shared
77     static DataNode dataNodeWithLeavesNoChildren = new DataNodeBuilder().withXpath('/xpath')
78             .withLeaves([leaf: 'value', leafList: ['leaveListElement1', 'leaveListElement2']]).build()
79
80     @Shared
81     static DataNode dataNodeWithChild = new DataNodeBuilder().withXpath('/parent')
82             .withChildDataNodes([new DataNodeBuilder().withXpath("/parent/child").build()]).build()
83
84     def setup() {
85         dataNodeBaseEndpoint = "$basePath/v1/dataspaces/$dataspaceName"
86     }
87
88     def 'Create a node: #scenario.'() {
89         given: 'some json to create a data node'
90             def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
91             def json = 'some json (this is not validated)'
92         when: 'post is invoked with datanode endpoint and json'
93             def response =
94                     mvc.perform(
95                             post(endpoint)
96                                     .contentType(MediaType.APPLICATION_JSON)
97                                     .param('xpath', parentNodeXpath)
98                                     .content(json)
99                     ).andReturn().response
100         then: 'a created response is returned'
101             response.status == HttpStatus.CREATED.value()
102         then: 'the java API was called with the correct parameters'
103             1 * mockCpsDataService.saveData(dataspaceName, anchorName, json)
104         where: 'following xpath parameters are are used'
105             scenario                     | parentNodeXpath
106             'no xpath parameter'         | ''
107             'xpath parameter point root' | '/'
108     }
109
110     def 'Create a child node'() {
111         given: 'some json to create a data node'
112             def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
113             def json = 'some json (this is not validated)'
114         and: 'parent node xpath'
115             def parentNodeXpath = 'some xpath'
116         when: 'post is invoked with datanode endpoint and json'
117             def response =
118                     mvc.perform(
119                             post(endpoint)
120                                     .contentType(MediaType.APPLICATION_JSON)
121                                     .param('xpath', parentNodeXpath)
122                                     .content(json)
123                     ).andReturn().response
124         then: 'a created response is returned'
125             response.status == HttpStatus.CREATED.value()
126         then: 'the java API was called with the correct parameters'
127             1 * mockCpsDataService.saveData(dataspaceName, anchorName, parentNodeXpath, json)
128     }
129
130     def 'Create list node child elements.'() {
131         given: 'parent node xpath and json data inputs'
132             def parentNodeXpath = 'parent node xpath'
133             def jsonData = 'json data'
134         when: 'post is invoked list-node endpoint'
135             def response = mvc.perform(
136                     post("$dataNodeBaseEndpoint/anchors/$anchorName/list-node")
137                             .contentType(MediaType.APPLICATION_JSON)
138                             .param('xpath', parentNodeXpath)
139                             .content(jsonData)
140             ).andReturn().response
141         then: 'a created response is returned'
142             response.status == HttpStatus.CREATED.value()
143         then: 'the java API was called with the correct parameters'
144             1 * mockCpsDataService.saveListNodeData(dataspaceName, anchorName, parentNodeXpath, jsonData)
145     }
146
147     def 'Get data node with leaves'() {
148         given: 'the service returns data node leaves'
149             def xpath = 'some xPath'
150             def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/node"
151             mockCpsDataService.getDataNode(dataspaceName, anchorName, xpath, OMIT_DESCENDANTS) >> dataNodeWithLeavesNoChildren
152         when: 'get request is performed through REST API'
153             def response =
154                     mvc.perform(get(endpoint).param('xpath', xpath))
155                             .andReturn().response
156         then: 'a success response is returned'
157             response.status == HttpStatus.OK.value()
158         and: 'response contains expected leaf and value'
159             response.contentAsString.contains('"leaf":"value"')
160         and: 'response contains expected leaf-list and values'
161             response.contentAsString.contains('"leafList":["leaveListElement1","leaveListElement2"]')
162     }
163
164     def 'Get data node with #scenario.'() {
165         given: 'the service returns data node with #scenario'
166             def xpath = 'some xPath'
167             def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/node"
168             mockCpsDataService.getDataNode(dataspaceName, anchorName, xpath, expectedCpsDataServiceOption) >> dataNode
169         when: 'get request is performed through REST API'
170             def response =
171                     mvc.perform(
172                             get(endpoint)
173                                     .param('xpath', xpath)
174                                     .param('include-descendants', includeDescendantsOption))
175                             .andReturn().response
176         then: 'a success response is returned'
177             response.status == HttpStatus.OK.value()
178         and: 'the response contains child is #expectChildInResponse'
179             response.contentAsString.contains('"child"') == expectChildInResponse
180         where:
181             scenario                    | dataNode                     | includeDescendantsOption || expectedCpsDataServiceOption | expectChildInResponse
182             'no descendants by default' | dataNodeWithLeavesNoChildren | ''                       || OMIT_DESCENDANTS             | false
183             'no descendant explicitly'  | dataNodeWithLeavesNoChildren | 'false'                  || OMIT_DESCENDANTS             | false
184             'with descendants'          | dataNodeWithChild            | 'true'                   || INCLUDE_ALL_DESCENDANTS      | true
185     }
186
187     def 'Update data node leaves: #scenario.'() {
188         given: 'json data'
189             def jsonData = 'json data'
190             def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
191         when: 'patch request is performed'
192             def response =
193                     mvc.perform(
194                             patch(endpoint)
195                                     .contentType(MediaType.APPLICATION_JSON)
196                                     .content(jsonData)
197                                     .param('xpath', inputXpath)
198                     ).andReturn().response
199         then: 'the service method is invoked with expected parameters'
200             1 * mockCpsDataService.updateNodeLeaves(dataspaceName, anchorName, xpathServiceParameter, jsonData)
201         and: 'response status indicates success'
202             response.status == HttpStatus.OK.value()
203         where:
204             scenario               | inputXpath    || xpathServiceParameter
205             'root node by default' | ''            || '/'
206             'root node by choice'  | '/'           || '/'
207             'some xpath by parent' | '/some/xpath' || '/some/xpath'
208     }
209
210     def 'Replace data node tree: #scenario.'() {
211         given: 'json data'
212             def jsonData = 'json data'
213             def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
214         when: 'put request is performed'
215             def response =
216                     mvc.perform(
217                             put(endpoint)
218                                     .contentType(MediaType.APPLICATION_JSON)
219                                     .content(jsonData)
220                                     .param('xpath', inputXpath))
221                             .andReturn().response
222         then: 'the service method is invoked with expected parameters'
223             1 * mockCpsDataService.replaceNodeTree(dataspaceName, anchorName, xpathServiceParameter, jsonData)
224         and: 'response status indicates success'
225             response.status == HttpStatus.OK.value()
226         where:
227             scenario               | inputXpath    || xpathServiceParameter
228             'root node by default' | ''            || '/'
229             'root node by choice'  | '/'           || '/'
230             'some xpath by parent' | '/some/xpath' || '/some/xpath'
231     }
232
233     def 'Replace list node child elements.'() {
234         given: 'parent node xpath and json data inputs'
235             def parentNodeXpath = 'parent node xpath'
236             def jsonData = 'json data'
237         when: 'patch is invoked list-node endpoint'
238             def response = mvc.perform(
239                     patch("$dataNodeBaseEndpoint/anchors/$anchorName/list-node")
240                             .contentType(MediaType.APPLICATION_JSON)
241                             .param('xpath', parentNodeXpath)
242                             .content(jsonData)
243             ).andReturn().response
244         then: 'a success response is returned'
245             response.status == HttpStatus.OK.value()
246         then: 'the java API was called with the correct parameters'
247             1 * mockCpsDataService.replaceListNodeData(dataspaceName, anchorName, parentNodeXpath, jsonData)
248     }
249 }