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.exceptions.AnchorNotFoundException
37 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
38 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
39 import org.onap.cps.spi.model.DataNode
40 import org.onap.cps.spi.model.DataNodeBuilder
41 import org.spockframework.spring.SpringBean
42 import org.springframework.beans.factory.annotation.Autowired
43 import org.springframework.beans.factory.annotation.Value
44 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
45 import org.springframework.http.HttpStatus
46 import org.springframework.http.MediaType
47 import org.springframework.test.web.servlet.MockMvc
48 import spock.lang.Shared
49 import spock.lang.Specification
50 import spock.lang.Unroll
53 class DataRestControllerSpec extends Specification {
56 CpsDataService mockCpsDataService = Mock()
59 CpsModuleService mockCpsModuleService = Mock()
62 CpsAdminService mockCpsAdminService = Mock()
65 CpsQueryService mockCpsQueryService = Mock()
68 ModelMapper modelMapper = Mock()
73 @Value('${rest.api.cps-base-path}')
76 def dataNodeBaseEndpoint
77 def dataspaceName = 'my_dataspace'
78 def anchorName = 'my_anchor'
81 static DataNode dataNodeWithLeavesNoChildren = new DataNodeBuilder().withXpath('/xpath')
82 .withLeaves([leaf: 'value', leafList: ['leaveListElement1', 'leaveListElement2']]).build()
85 static DataNode dataNodeWithChild = new DataNodeBuilder().withXpath('/parent')
86 .withChildDataNodes([new DataNodeBuilder().withXpath("/parent/child").build()]).build()
89 dataNodeBaseEndpoint = "$basePath/v1/dataspaces/$dataspaceName"
92 def 'Create a node.'() {
93 given: 'some json to create a data node'
94 def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
95 def json = 'some json (this is not validated)'
96 when: 'post is invoked with datanode endpoint and json'
100 .contentType(MediaType.APPLICATION_JSON).content(json))
101 .andReturn().response
102 then: 'a created response is returned'
103 response.status == HttpStatus.CREATED.value()
104 then: 'the java API was called with the correct parameters'
105 1 * mockCpsDataService.saveData(dataspaceName, anchorName, json)
109 def 'Get data node with leaves'() {
110 given: 'the service returns data node leaves'
111 def xpath = 'some xPath'
112 def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/node"
113 mockCpsDataService.getDataNode(dataspaceName, anchorName, xpath, OMIT_DESCENDANTS) >> dataNodeWithLeavesNoChildren
114 when: 'get request is performed through REST API'
116 mvc.perform(get(endpoint).param('xpath', xpath))
117 .andReturn().response
118 then: 'a success response is returned'
119 response.status == HttpStatus.OK.value()
120 and: 'response contains expected leaf and value'
121 response.contentAsString.contains('"leaf":"value"')
122 and: 'response contains expected leaf-list and values'
123 response.contentAsString.contains('"leafList":["leaveListElement1","leaveListElement2"]')
127 def 'Get data node with #scenario.'() {
128 given: 'the service returns data node with #scenario'
129 def xpath = 'some xPath'
130 def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/node"
131 mockCpsDataService.getDataNode(dataspaceName, anchorName, xpath, expectedCpsDataServiceOption) >> dataNode
132 when: 'get request is performed through REST API'
136 .param('xpath', xpath)
137 .param('include-descendants', includeDescendantsOption))
138 .andReturn().response
139 then: 'a success response is returned'
140 response.status == HttpStatus.OK.value()
141 and: 'the response contains child is #expectChildInResponse'
142 response.contentAsString.contains('"child"') == expectChildInResponse
144 scenario | dataNode | includeDescendantsOption || expectedCpsDataServiceOption | expectChildInResponse
145 'no descendants by default' | dataNodeWithLeavesNoChildren | '' || OMIT_DESCENDANTS | false
146 'no descendant explicitly' | dataNodeWithLeavesNoChildren | 'false' || OMIT_DESCENDANTS | false
147 'with descendants' | dataNodeWithChild | 'true' || INCLUDE_ALL_DESCENDANTS | true
151 def 'Get data node error scenario: #scenario.'() {
152 given: 'the service throws an exception'
153 def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/node"
154 mockCpsDataService.getDataNode(dataspaceName, anchorName, xpath, _) >> { throw exception }
155 when: 'get request is performed through REST API'
157 mvc.perform(get(endpoint).param("xpath", xpath))
158 .andReturn().response
159 then: 'a success response is returned'
160 response.status == httpStatus.value()
162 scenario | xpath | exception || httpStatus
163 'no dataspace' | '/x-path' | new DataspaceNotFoundException('') || HttpStatus.BAD_REQUEST
164 'no anchor' | '/x-path' | new AnchorNotFoundException('', '') || HttpStatus.BAD_REQUEST
165 'no data' | '/x-path' | new DataNodeNotFoundException('', '', '') || HttpStatus.NOT_FOUND
166 'empty path' | '' | new IllegalStateException() || HttpStatus.NOT_IMPLEMENTED
170 def 'Update data node leaves: #scenario.'() {
172 def jsonData = 'json data'
173 def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
174 when: 'patch request is performed'
178 .contentType(MediaType.APPLICATION_JSON)
180 .param('xpath', xpath)
181 ).andReturn().response
182 then: 'the service method is invoked with expected parameters'
183 1 * mockCpsDataService.updateNodeLeaves(dataspaceName, anchorName, xpathServiceParameter, jsonData)
184 and: 'response status indicates success'
185 response.status == HttpStatus.OK.value()
187 scenario | xpath | xpathServiceParameter
188 'root node by default' | '' | '/'
189 'node by parent xpath' | '/xpath' | '/xpath'
193 def 'Replace data node tree: #scenario.'() {
195 def jsonData = 'json data'
196 def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
197 when: 'put request is performed'
201 .contentType(MediaType.APPLICATION_JSON)
203 .param('xpath', xpath))
204 .andReturn().response
205 then: 'the service method is invoked with expected parameters'
206 1 * mockCpsDataService.replaceNodeTree(dataspaceName, anchorName, xpathServiceParameter, jsonData)
207 and: 'response status indicates success'
208 response.status == HttpStatus.OK.value()
210 scenario | xpath | xpathServiceParameter
211 'root node by default' | '' | '/'
212 'node by parent xpath' | '/xpath' | '/xpath'