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
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.rest.controller
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
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
49 class DataRestControllerSpec extends Specification {
52 CpsDataService mockCpsDataService = Mock()
55 CpsModuleService mockCpsModuleService = Mock()
58 CpsAdminService mockCpsAdminService = Mock()
61 CpsQueryService mockCpsQueryService = Mock()
64 ModelMapper modelMapper = Mock()
69 @Value('${rest.api.cps-base-path}')
72 def dataNodeBaseEndpoint
73 def dataspaceName = 'my_dataspace'
74 def anchorName = 'my_anchor'
77 static DataNode dataNodeWithLeavesNoChildren = new DataNodeBuilder().withXpath('/xpath')
78 .withLeaves([leaf: 'value', leafList: ['leaveListElement1', 'leaveListElement2']]).build()
81 static DataNode dataNodeWithChild = new DataNodeBuilder().withXpath('/parent')
82 .withChildDataNodes([new DataNodeBuilder().withXpath("/parent/child").build()]).build()
85 dataNodeBaseEndpoint = "$basePath/v1/dataspaces/$dataspaceName"
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'
96 .contentType(MediaType.APPLICATION_JSON)
97 .param('xpath', parentNodeXpath)
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' | '/'
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'
120 .contentType(MediaType.APPLICATION_JSON)
121 .param('xpath', parentNodeXpath)
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)
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)
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)
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'
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"]')
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'
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
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
187 def 'Update data node leaves: #scenario.'() {
189 def jsonData = 'json data'
190 def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
191 when: 'patch request is performed'
195 .contentType(MediaType.APPLICATION_JSON)
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()
204 scenario | inputXpath || xpathServiceParameter
205 'root node by default' | '' || '/'
206 'root node by choice' | '/' || '/'
207 'some xpath by parent' | '/some/xpath' || '/some/xpath'
210 def 'Replace data node tree: #scenario.'() {
212 def jsonData = 'json data'
213 def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
214 when: 'put request is performed'
218 .contentType(MediaType.APPLICATION_JSON)
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()
227 scenario | inputXpath || xpathServiceParameter
228 'root node by default' | '' || '/'
229 'root node by choice' | '/' || '/'
230 'some xpath by parent' | '/some/xpath' || '/some/xpath'
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)
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)