2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation
4 * Modifications Copyright (C) 2021 Pantheon.tech
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.rest.controller
23 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
24 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
25 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
26 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch
27 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
28 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put
30 import org.modelmapper.ModelMapper
31 import org.onap.cps.api.CpsAdminService
32 import org.onap.cps.api.CpsDataService
33 import org.onap.cps.api.CpsModuleService
34 import org.onap.cps.spi.exceptions.AnchorNotFoundException
35 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
36 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
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
48 import spock.lang.Unroll
51 class DataRestControllerSpec extends Specification {
54 CpsDataService mockCpsDataService = Mock()
57 CpsModuleService mockCpsModuleService = Mock()
60 CpsAdminService mockCpsAdminService = Mock()
63 ModelMapper modelMapper = Mock()
68 @Value('${rest.api.cps-base-path}')
71 def dataNodeBaseEndpoint
72 def dataspaceName = 'my_dataspace'
73 def anchorName = 'my_anchor'
76 static DataNode dataNodeWithLeavesNoChildren = new DataNodeBuilder().withXpath('/xpath')
77 .withLeaves([leaf: 'value', leafList: ['leaveListElement1', 'leaveListElement2']]).build()
80 static DataNode dataNodeWithChild = new DataNodeBuilder().withXpath('/parent')
81 .withChildDataNodes([new DataNodeBuilder().withXpath("/parent/child").build()]).build()
84 dataNodeBaseEndpoint = "$basePath/v1/dataspaces/$dataspaceName"
87 def 'Create a node.'() {
88 given: 'some json to create a data node'
89 def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
90 def json = 'some json (this is not validated)'
91 when: 'post is invoked with datanode endpoint and json'
92 def response = mvc.perform(
93 post(endpoint).contentType(MediaType.APPLICATION_JSON).content(json)
94 ).andReturn().response
95 then: 'a created response is returned'
96 response.status == HttpStatus.CREATED.value()
97 then: 'the java API was called with the correct parameters'
98 1 * mockCpsDataService.saveData(dataspaceName, anchorName, json)
102 def 'Get data node with leaves'() {
103 given: 'the service returns data node leaves'
104 def xpath = 'some xPath'
105 def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/node"
106 mockCpsDataService.getDataNode(dataspaceName, anchorName, xpath, OMIT_DESCENDANTS) >> dataNodeWithLeavesNoChildren
107 when: 'get request is performed through REST API'
108 def response = mvc.perform(
109 get(endpoint).param('xpath', xpath)
110 ).andReturn().response
111 then: 'a success response is returned'
112 response.status == HttpStatus.OK.value()
113 and: 'response contains expected leaf and value'
114 response.contentAsString.contains('"leaf":"value"')
115 and: 'response contains expected leaf-list and values'
116 response.contentAsString.contains('"leafList":["leaveListElement1","leaveListElement2"]')
120 def 'Get data node with #scenario.'() {
121 given: 'the service returns data node with #scenario'
122 def xpath = 'some xPath'
123 def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/node"
124 mockCpsDataService.getDataNode(dataspaceName, anchorName, xpath, expectedCpsDataServiceOption) >> dataNode
125 when: 'get request is performed through REST API'
126 def response = mvc.perform(get(endpoint)
127 .param('xpath', xpath)
128 .param('include-descendants', includeDescendantsOption))
129 .andReturn().response
130 then: 'a success response is returned'
131 response.status == HttpStatus.OK.value()
132 and: 'the response contains child is #expectChildInResponse'
133 response.contentAsString.contains('"child"') == expectChildInResponse
135 scenario | dataNode | includeDescendantsOption || expectedCpsDataServiceOption | expectChildInResponse
136 'no descendants by default' | dataNodeWithLeavesNoChildren | '' || OMIT_DESCENDANTS | false
137 'no descendant explicitly' | dataNodeWithLeavesNoChildren | 'false' || OMIT_DESCENDANTS | false
138 'with descendants' | dataNodeWithChild | 'true' || INCLUDE_ALL_DESCENDANTS | true
142 def 'Get data node error scenario: #scenario.'() {
143 given: 'the service throws an exception'
144 def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/node"
145 mockCpsDataService.getDataNode(dataspaceName, anchorName, xpath, _) >> { throw exception }
146 when: 'get request is performed through REST API'
147 def response = mvc.perform(
148 get(endpoint).param("xpath", xpath)
149 ).andReturn().response
150 then: 'a success response is returned'
151 response.status == httpStatus.value()
153 scenario | xpath | exception || httpStatus
154 'no dataspace' | '/x-path' | new DataspaceNotFoundException('') || HttpStatus.BAD_REQUEST
155 'no anchor' | '/x-path' | new AnchorNotFoundException('', '') || HttpStatus.BAD_REQUEST
156 'no data' | '/x-path' | new DataNodeNotFoundException('', '', '') || HttpStatus.NOT_FOUND
157 'empty path' | '' | new IllegalStateException() || HttpStatus.NOT_IMPLEMENTED
161 def 'Update data node leaves: #scenario.'() {
163 def jsonData = 'json data'
164 def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
165 when: 'patch request is performed'
166 def response = mvc.perform(
168 .contentType(MediaType.APPLICATION_JSON)
170 .param('xpath', xpath)
171 ).andReturn().response
172 then: 'the service method is invoked with expected parameters'
173 1 * mockCpsDataService.updateNodeLeaves(dataspaceName, anchorName, xpathServiceParameter, jsonData)
174 and: 'response status indicates success'
175 response.status == HttpStatus.OK.value()
177 scenario | xpath | xpathServiceParameter
178 'root node by default' | '' | '/'
179 'node by parent xpath' | '/xpath' | '/xpath'
183 def 'Replace data node tree: #scenario.'() {
185 def jsonData = 'json data'
186 def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
187 when: 'put request is performed'
188 def response = mvc.perform(
190 .contentType(MediaType.APPLICATION_JSON)
192 .param('xpath', xpath)
193 ).andReturn().response
194 then: 'the service method is invoked with expected parameters'
195 1 * mockCpsDataService.replaceNodeTree(dataspaceName, anchorName, xpathServiceParameter, jsonData)
196 and: 'response status indicates success'
197 response.status == HttpStatus.OK.value()
199 scenario | xpath | xpathServiceParameter
200 'root node by default' | '' | '/'
201 'node by parent xpath' | '/xpath' | '/xpath'