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
49 @WebMvcTest(DataRestController)
50 class DataRestControllerSpec extends Specification {
53 CpsDataService mockCpsDataService = Mock()
58 @Value('${rest.api.cps-base-path}')
61 def dataNodeBaseEndpoint
62 def dataspaceName = 'my_dataspace'
63 def anchorName = 'my_anchor'
66 static DataNode dataNodeWithLeavesNoChildren = new DataNodeBuilder().withXpath('/xpath')
67 .withLeaves([leaf: 'value', leafList: ['leaveListElement1', 'leaveListElement2']]).build()
70 static DataNode dataNodeWithChild = new DataNodeBuilder().withXpath('/parent')
71 .withChildDataNodes([new DataNodeBuilder().withXpath("/parent/child").build()]).build()
74 dataNodeBaseEndpoint = "$basePath/v1/dataspaces/$dataspaceName"
77 def 'Create a node: #scenario.'() {
78 given: 'some json to create a data node'
79 def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
80 def json = 'some json (this is not validated)'
81 when: 'post is invoked with datanode endpoint and json'
85 .contentType(MediaType.APPLICATION_JSON)
86 .param('xpath', parentNodeXpath)
88 ).andReturn().response
89 then: 'a created response is returned'
90 response.status == HttpStatus.CREATED.value()
91 then: 'the java API was called with the correct parameters'
92 1 * mockCpsDataService.saveData(dataspaceName, anchorName, json)
93 where: 'following xpath parameters are are used'
94 scenario | parentNodeXpath
95 'no xpath parameter' | ''
96 'xpath parameter point root' | '/'
99 def 'Create a child node'() {
100 given: 'some json to create a data node'
101 def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
102 def json = 'some json (this is not validated)'
103 and: 'parent node xpath'
104 def parentNodeXpath = 'some xpath'
105 when: 'post is invoked with datanode endpoint and json'
109 .contentType(MediaType.APPLICATION_JSON)
110 .param('xpath', parentNodeXpath)
112 ).andReturn().response
113 then: 'a created response is returned'
114 response.status == HttpStatus.CREATED.value()
115 then: 'the java API was called with the correct parameters'
116 1 * mockCpsDataService.saveData(dataspaceName, anchorName, parentNodeXpath, json)
119 def 'Create list node child elements.'() {
120 given: 'parent node xpath and json data inputs'
121 def parentNodeXpath = 'parent node xpath'
122 def jsonData = 'json data'
123 when: 'post is invoked list-node endpoint'
124 def response = mvc.perform(
125 post("$dataNodeBaseEndpoint/anchors/$anchorName/list-node")
126 .contentType(MediaType.APPLICATION_JSON)
127 .param('xpath', parentNodeXpath)
129 ).andReturn().response
130 then: 'a created response is returned'
131 response.status == HttpStatus.CREATED.value()
132 then: 'the java API was called with the correct parameters'
133 1 * mockCpsDataService.saveListNodeData(dataspaceName, anchorName, parentNodeXpath, jsonData)
136 def 'Get data node with leaves'() {
137 given: 'the service returns data node leaves'
138 def xpath = 'some xPath'
139 def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/node"
140 mockCpsDataService.getDataNode(dataspaceName, anchorName, xpath, OMIT_DESCENDANTS) >> dataNodeWithLeavesNoChildren
141 when: 'get request is performed through REST API'
143 mvc.perform(get(endpoint).param('xpath', xpath))
144 .andReturn().response
145 then: 'a success response is returned'
146 response.status == HttpStatus.OK.value()
147 and: 'response contains expected leaf and value'
148 response.contentAsString.contains('"leaf":"value"')
149 and: 'response contains expected leaf-list and values'
150 response.contentAsString.contains('"leafList":["leaveListElement1","leaveListElement2"]')
153 def 'Get data node with #scenario.'() {
154 given: 'the service returns data node with #scenario'
155 def xpath = 'some xPath'
156 def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/node"
157 mockCpsDataService.getDataNode(dataspaceName, anchorName, xpath, expectedCpsDataServiceOption) >> dataNode
158 when: 'get request is performed through REST API'
162 .param('xpath', xpath)
163 .param('include-descendants', includeDescendantsOption))
164 .andReturn().response
165 then: 'a success response is returned'
166 response.status == HttpStatus.OK.value()
167 and: 'the response contains child is #expectChildInResponse'
168 response.contentAsString.contains('"child"') == expectChildInResponse
170 scenario | dataNode | includeDescendantsOption || expectedCpsDataServiceOption | expectChildInResponse
171 'no descendants by default' | dataNodeWithLeavesNoChildren | '' || OMIT_DESCENDANTS | false
172 'no descendant explicitly' | dataNodeWithLeavesNoChildren | 'false' || OMIT_DESCENDANTS | false
173 'with descendants' | dataNodeWithChild | 'true' || INCLUDE_ALL_DESCENDANTS | true
176 def 'Update data node leaves: #scenario.'() {
178 def jsonData = 'json data'
179 def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
180 when: 'patch request is performed'
184 .contentType(MediaType.APPLICATION_JSON)
186 .param('xpath', inputXpath)
187 ).andReturn().response
188 then: 'the service method is invoked with expected parameters'
189 1 * mockCpsDataService.updateNodeLeaves(dataspaceName, anchorName, xpathServiceParameter, jsonData)
190 and: 'response status indicates success'
191 response.status == HttpStatus.OK.value()
193 scenario | inputXpath || xpathServiceParameter
194 'root node by default' | '' || '/'
195 'root node by choice' | '/' || '/'
196 'some xpath by parent' | '/some/xpath' || '/some/xpath'
199 def 'Replace data node tree: #scenario.'() {
201 def jsonData = 'json data'
202 def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
203 when: 'put request is performed'
207 .contentType(MediaType.APPLICATION_JSON)
209 .param('xpath', inputXpath))
210 .andReturn().response
211 then: 'the service method is invoked with expected parameters'
212 1 * mockCpsDataService.replaceNodeTree(dataspaceName, anchorName, xpathServiceParameter, jsonData)
213 and: 'response status indicates success'
214 response.status == HttpStatus.OK.value()
216 scenario | inputXpath || xpathServiceParameter
217 'root node by default' | '' || '/'
218 'root node by choice' | '/' || '/'
219 'some xpath by parent' | '/some/xpath' || '/some/xpath'
222 def 'Replace list node child elements.'() {
223 given: 'parent node xpath and json data inputs'
224 def parentNodeXpath = 'parent node xpath'
225 def jsonData = 'json data'
226 when: 'patch is invoked list-node endpoint'
227 def response = mvc.perform(
228 patch("$dataNodeBaseEndpoint/anchors/$anchorName/list-node")
229 .contentType(MediaType.APPLICATION_JSON)
230 .param('xpath', parentNodeXpath)
232 ).andReturn().response
233 then: 'a success response is returned'
234 response.status == HttpStatus.OK.value()
235 then: 'the java API was called with the correct parameters'
236 1 * mockCpsDataService.replaceListNodeData(dataspaceName, anchorName, parentNodeXpath, jsonData)