18d20a8e20e14d5f1c5e690256be0efe46450cb1
[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
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 }