Investigate and update Spock version
[cps.git] / cps-rest / src / test / groovy / org / onap / cps / rest / controller / DataRestControllerSpec.groovy
1 /*
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
10  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.rest.controller
23
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
30
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.AlreadyDefinedException
37 import org.onap.cps.spi.exceptions.AnchorNotFoundException
38 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
39 import org.onap.cps.spi.exceptions.DataspaceNotFoundException
40 import org.onap.cps.spi.model.DataNode
41 import org.onap.cps.spi.model.DataNodeBuilder
42 import org.spockframework.spring.SpringBean
43 import org.springframework.beans.factory.annotation.Autowired
44 import org.springframework.beans.factory.annotation.Value
45 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
46 import org.springframework.http.HttpStatus
47 import org.springframework.http.MediaType
48 import org.springframework.test.web.servlet.MockMvc
49 import spock.lang.Shared
50 import spock.lang.Specification
51
52 @WebMvcTest
53 class DataRestControllerSpec extends Specification {
54
55     @SpringBean
56     CpsDataService mockCpsDataService = Mock()
57
58     @SpringBean
59     CpsModuleService mockCpsModuleService = Mock()
60
61     @SpringBean
62     CpsAdminService mockCpsAdminService = Mock()
63
64     @SpringBean
65     CpsQueryService mockCpsQueryService = Mock()
66
67     @SpringBean
68     ModelMapper modelMapper = Mock()
69
70     @Autowired
71     MockMvc mvc
72
73     @Value('${rest.api.cps-base-path}')
74     def basePath
75
76     def dataNodeBaseEndpoint
77     def dataspaceName = 'my_dataspace'
78     def anchorName = 'my_anchor'
79
80     @Shared
81     static DataNode dataNodeWithLeavesNoChildren = new DataNodeBuilder().withXpath('/xpath')
82             .withLeaves([leaf: 'value', leafList: ['leaveListElement1', 'leaveListElement2']]).build()
83
84     @Shared
85     static DataNode dataNodeWithChild = new DataNodeBuilder().withXpath('/parent')
86             .withChildDataNodes([new DataNodeBuilder().withXpath("/parent/child").build()]).build()
87
88     def setup() {
89         dataNodeBaseEndpoint = "$basePath/v1/dataspaces/$dataspaceName"
90     }
91
92     def 'Create a node: #scenario.'() {
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'
97             def response =
98                     mvc.perform(
99                             post(endpoint)
100                                     .contentType(MediaType.APPLICATION_JSON)
101                                     .param('xpath', parentNodeXpath)
102                                     .content(json)
103                     ).andReturn().response
104         then: 'a created response is returned'
105             response.status == HttpStatus.CREATED.value()
106         then: 'the java API was called with the correct parameters'
107             1 * mockCpsDataService.saveData(dataspaceName, anchorName, json)
108         where: 'following xpath parameters are are used'
109             scenario                     | parentNodeXpath
110             'no xpath parameter'         | ''
111             'xpath parameter point root' | '/'
112     }
113
114     def 'Create a child node'() {
115         given: 'some json to create a data node'
116             def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
117             def json = 'some json (this is not validated)'
118         and: 'parent node xpath'
119             def parentNodeXpath = 'some xpath'
120         when: 'post is invoked with datanode endpoint and json'
121             def response =
122                     mvc.perform(
123                             post(endpoint)
124                                     .contentType(MediaType.APPLICATION_JSON)
125                                     .param('xpath', parentNodeXpath)
126                                     .content(json)
127                     ).andReturn().response
128         then: 'a created response is returned'
129             response.status == HttpStatus.CREATED.value()
130         then: 'the java API was called with the correct parameters'
131             1 * mockCpsDataService.saveData(dataspaceName, anchorName, parentNodeXpath, json)
132     }
133
134     def 'Get data node with leaves'() {
135         given: 'the service returns data node leaves'
136             def xpath = 'some xPath'
137             def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/node"
138             mockCpsDataService.getDataNode(dataspaceName, anchorName, xpath, OMIT_DESCENDANTS) >> dataNodeWithLeavesNoChildren
139         when: 'get request is performed through REST API'
140             def response =
141                     mvc.perform(get(endpoint).param('xpath', xpath))
142                             .andReturn().response
143         then: 'a success response is returned'
144             response.status == HttpStatus.OK.value()
145         and: 'response contains expected leaf and value'
146             response.contentAsString.contains('"leaf":"value"')
147         and: 'response contains expected leaf-list and values'
148             response.contentAsString.contains('"leafList":["leaveListElement1","leaveListElement2"]')
149     }
150
151     def 'Get data node with #scenario.'() {
152         given: 'the service returns data node with #scenario'
153             def xpath = 'some xPath'
154             def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/node"
155             mockCpsDataService.getDataNode(dataspaceName, anchorName, xpath, expectedCpsDataServiceOption) >> dataNode
156         when: 'get request is performed through REST API'
157             def response =
158                     mvc.perform(
159                             get(endpoint)
160                                     .param('xpath', xpath)
161                                     .param('include-descendants', includeDescendantsOption))
162                             .andReturn().response
163         then: 'a success response is returned'
164             response.status == HttpStatus.OK.value()
165         and: 'the response contains child is #expectChildInResponse'
166             response.contentAsString.contains('"child"') == expectChildInResponse
167         where:
168             scenario                    | dataNode                     | includeDescendantsOption || expectedCpsDataServiceOption | expectChildInResponse
169             'no descendants by default' | dataNodeWithLeavesNoChildren | ''                       || OMIT_DESCENDANTS             | false
170             'no descendant explicitly'  | dataNodeWithLeavesNoChildren | 'false'                  || OMIT_DESCENDANTS             | false
171             'with descendants'          | dataNodeWithChild            | 'true'                   || INCLUDE_ALL_DESCENDANTS      | true
172     }
173
174     def 'Update data node leaves: #scenario.'() {
175         given: 'json data'
176             def jsonData = 'json data'
177             def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
178         when: 'patch request is performed'
179             def response =
180                     mvc.perform(
181                             patch(endpoint)
182                                     .contentType(MediaType.APPLICATION_JSON)
183                                     .content(jsonData)
184                                     .param('xpath', inputXpath)
185                     ).andReturn().response
186         then: 'the service method is invoked with expected parameters'
187             1 * mockCpsDataService.updateNodeLeaves(dataspaceName, anchorName, xpathServiceParameter, jsonData)
188         and: 'response status indicates success'
189             response.status == HttpStatus.OK.value()
190         where:
191             scenario               | inputXpath    || xpathServiceParameter
192             'root node by default' | ''            || '/'
193             'root node by choice'  | '/'           || '/'
194             'some xpath by parent' | '/some/xpath' || '/some/xpath'
195     }
196
197     def 'Replace data node tree: #scenario.'() {
198         given: 'json data'
199             def jsonData = 'json data'
200             def endpoint = "$dataNodeBaseEndpoint/anchors/$anchorName/nodes"
201         when: 'put request is performed'
202             def response =
203                     mvc.perform(
204                             put(endpoint)
205                                     .contentType(MediaType.APPLICATION_JSON)
206                                     .content(jsonData)
207                                     .param('xpath', inputXpath))
208                             .andReturn().response
209         then: 'the service method is invoked with expected parameters'
210             1 * mockCpsDataService.replaceNodeTree(dataspaceName, anchorName, xpathServiceParameter, jsonData)
211         and: 'response status indicates success'
212             response.status == HttpStatus.OK.value()
213         where:
214             scenario               | inputXpath    || xpathServiceParameter
215             'root node by default' | ''            || '/'
216             'root node by choice'  | '/'           || '/'
217             'some xpath by parent' | '/some/xpath' || '/some/xpath'
218     }
219 }