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
11 * 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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.cps.rest.controller
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
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
50 class DataRestControllerSpec extends Specification {
53 CpsDataService mockCpsDataService = Mock()
56 CpsModuleService mockCpsModuleService = Mock()
59 CpsAdminService mockCpsAdminService = Mock()
62 CpsQueryService mockCpsQueryService = Mock()
65 ModelMapper modelMapper = Mock()
70 @Value('${rest.api.cps-base-path}')
73 def dataNodeBaseEndpoint
74 def dataspaceName = 'my_dataspace'
75 def anchorName = 'my_anchor'
78 static DataNode dataNodeWithLeavesNoChildren = new DataNodeBuilder().withXpath('/xpath')
79 .withLeaves([leaf: 'value', leafList: ['leaveListElement1', 'leaveListElement2']]).build()
82 static DataNode dataNodeWithChild = new DataNodeBuilder().withXpath('/parent')
83 .withChildDataNodes([new DataNodeBuilder().withXpath("/parent/child").build()]).build()
86 dataNodeBaseEndpoint = "$basePath/v1/dataspaces/$dataspaceName"
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'
97 .contentType(MediaType.APPLICATION_JSON)
98 .param('xpath', parentNodeXpath)
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' | '/'
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'
121 .contentType(MediaType.APPLICATION_JSON)
122 .param('xpath', parentNodeXpath)
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)
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)
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)
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'
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"]')
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'
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
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
188 def 'Update data node leaves: #scenario.'() {
190 def jsonData = 'json data'
191 def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
192 when: 'patch request is performed'
196 .contentType(MediaType.APPLICATION_JSON)
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()
205 scenario | inputXpath || xpathServiceParameter
206 'root node by default' | '' || '/'
207 'root node by choice' | '/' || '/'
208 'some xpath by parent' | '/some/xpath' || '/some/xpath'
211 def 'Replace data node tree: #scenario.'() {
213 def jsonData = 'json data'
214 def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
215 when: 'put request is performed'
219 .contentType(MediaType.APPLICATION_JSON)
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()
228 scenario | inputXpath || xpathServiceParameter
229 'root node by default' | '' || '/'
230 'root node by choice' | '/' || '/'
231 'some xpath by parent' | '/some/xpath' || '/some/xpath'
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)
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)