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