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}')
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 dataNodeEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors/$anchorName/nodes"
87 def 'Create a node.'() {
88 given: 'some json to create a data node'
89 def json = 'some json (this is not validated)'
90 when: 'post is invoked with datanode endpoint and json'
91 def response = mvc.perform(
92 post(dataNodeEndpoint).contentType(MediaType.APPLICATION_JSON).content(json)
93 ).andReturn().response
94 then: 'a created response is returned'
95 response.status == HttpStatus.CREATED.value()
96 then: 'the java API was called with the correct parameters'
97 1 * mockCpsDataService.saveData(dataspaceName, anchorName, json)
101 def 'Get data node with leaves'() {
102 given: 'the service returns data node leaves'
103 def xpath = 'some xPath'
104 mockCpsDataService.getDataNode(dataspaceName, anchorName, xpath, OMIT_DESCENDANTS) >> dataNodeWithLeavesNoChildren
105 when: 'get request is performed through REST API'
106 def response = mvc.perform(
107 get(dataNodeEndpoint).param('xpath', xpath)
108 ).andReturn().response
109 then: 'a success response is returned'
110 response.status == HttpStatus.OK.value()
111 and: 'response contains expected leaf and value'
112 response.contentAsString.contains('"leaf":"value"')
113 and: 'response contains expected leaf-list and values'
114 response.contentAsString.contains('"leafList":["leaveListElement1","leaveListElement2"]')
118 def 'Get data node with #scenario.'() {
119 given: 'the service returns data node with #scenario'
120 def xpath = 'some xPath'
121 mockCpsDataService.getDataNode(dataspaceName, anchorName, xpath, expectedCpsDataServiceOption) >> dataNode
122 when: 'get request is performed through REST API'
123 def response = mvc.perform(get(dataNodeEndpoint)
124 .param('xpath', xpath)
125 .param('include-descendants', includeDescendantsOption))
126 .andReturn().response
127 then: 'a success response is returned'
128 response.status == HttpStatus.OK.value()
129 and: 'the response contains child is #expectChildInResponse'
130 response.contentAsString.contains('"child"') == expectChildInResponse
132 scenario | dataNode | includeDescendantsOption || expectedCpsDataServiceOption | expectChildInResponse
133 'no descendants by default' | dataNodeWithLeavesNoChildren | '' || OMIT_DESCENDANTS | false
134 'no descendant explicitly' | dataNodeWithLeavesNoChildren | 'false' || OMIT_DESCENDANTS | false
135 'with descendants' | dataNodeWithChild | 'true' || INCLUDE_ALL_DESCENDANTS | true
139 def 'Get data node error scenario: #scenario.'() {
140 given: 'the service throws an exception'
141 mockCpsDataService.getDataNode(dataspaceName, anchorName, xpath, _) >> { throw exception }
142 when: 'get request is performed through REST API'
143 def response = mvc.perform(
144 get(dataNodeEndpoint).param("xpath", xpath)
145 ).andReturn().response
146 then: 'a success response is returned'
147 response.status == httpStatus.value()
149 scenario | xpath | exception || httpStatus
150 'no dataspace' | '/x-path' | new DataspaceNotFoundException('') || HttpStatus.BAD_REQUEST
151 'no anchor' | '/x-path' | new AnchorNotFoundException('', '') || HttpStatus.BAD_REQUEST
152 'no data' | '/x-path' | new DataNodeNotFoundException('', '', '') || HttpStatus.NOT_FOUND
153 'empty path' | '' | new IllegalStateException() || HttpStatus.NOT_IMPLEMENTED
157 def 'Update data node leaves: #scenario.'() {
159 def jsonData = 'json data'
160 when: 'patch request is performed'
161 def response = mvc.perform(
162 patch(dataNodeEndpoint)
163 .contentType(MediaType.APPLICATION_JSON)
165 .param('xpath', xpath)
166 ).andReturn().response
167 then: 'the service method is invoked with expected parameters'
168 1 * mockCpsDataService.updateNodeLeaves(dataspaceName, anchorName, xpathServiceParameter, jsonData)
169 and: 'response status indicates success'
170 response.status == HttpStatus.OK.value()
172 scenario | xpath | xpathServiceParameter
173 'root node by default' | '' | '/'
174 'node by parent xpath' | '/xpath' | '/xpath'
178 def 'Replace data node tree: #scenario.'() {
180 def jsonData = 'json data'
181 when: 'put request is performed'
182 def response = mvc.perform(
183 put(dataNodeEndpoint)
184 .contentType(MediaType.APPLICATION_JSON)
186 .param('xpath', xpath)
187 ).andReturn().response
188 then: 'the service method is invoked with expected parameters'
189 1 * mockCpsDataService.replaceNodeTree(dataspaceName, anchorName, xpathServiceParameter, jsonData)
190 and: 'response status indicates success'
191 response.status == HttpStatus.OK.value()
193 scenario | xpath | xpathServiceParameter
194 'root node by default' | '' | '/'
195 'node by parent xpath' | '/xpath' | '/xpath'