2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020-2021 Pantheon.tech
4 * Modifications Copyright (C) 2020, 2021 Bell Canada. All rights reserved.
5 * Copyright (C) 2021 Nordix Foundation
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.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED
25 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete
26 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
27 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart
28 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
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.api.CpsQueryService
35 import org.onap.cps.spi.exceptions.AlreadyDefinedException
36 import org.onap.cps.spi.exceptions.SchemaSetInUseException
37 import org.onap.cps.spi.model.Anchor
38 import org.onap.cps.spi.model.SchemaSet
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.mock.web.MockMultipartFile
46 import org.springframework.test.web.servlet.MockMvc
47 import org.springframework.util.LinkedMultiValueMap
48 import org.springframework.util.MultiValueMap
49 import spock.lang.Specification
50 import spock.lang.Unroll
53 class AdminRestControllerSpec extends Specification {
56 CpsModuleService mockCpsModuleService = Mock()
59 CpsAdminService mockCpsAdminService = Mock()
62 CpsDataService mockCpsDataService = Mock()
65 CpsQueryService mockCpsQueryService = Mock()
68 ModelMapper modelMapper = Mock()
73 @Value('${rest.api.cps-base-path}')
76 def dataspaceName = 'my_dataspace'
77 def anchor = new Anchor(name: 'my_anchor')
78 def anchorList = [anchor]
79 def anchorName = 'my_anchor'
80 def schemaSetName = 'my_schema_set'
82 def 'Create new dataspace.'() {
84 def createDataspaceEndpoint = "$basePath/v1/dataspaces";
85 when: 'post is invoked'
88 post(createDataspaceEndpoint)
89 .param('dataspace-name', dataspaceName))
91 then: 'service method is invoked with expected parameters'
92 1 * mockCpsAdminService.createDataspace(dataspaceName)
93 and: 'dataspace is create successfully'
94 response.status == HttpStatus.CREATED.value()
97 def 'Create dataspace over existing with same name.'() {
99 def createDataspaceEndpoint = "$basePath/v1/dataspaces";
100 and: 'the service method throws an exception indicating the dataspace is already defined'
101 def thrownException = new AlreadyDefinedException(dataspaceName, new RuntimeException())
102 mockCpsAdminService.createDataspace(dataspaceName) >> { throw thrownException }
103 when: 'post is invoked'
106 post(createDataspaceEndpoint)
107 .param('dataspace-name', dataspaceName))
108 .andReturn().response
109 then: 'dataspace creation fails'
110 response.status == HttpStatus.CONFLICT.value()
113 def 'Create schema set from yang file.'() {
114 def yangResourceMapCapture
115 given: 'single yang file'
116 def multipartFile = createMultipartFile("filename.yang", "content")
118 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
119 when: 'file uploaded with schema set create request'
122 multipart(schemaSetEndpoint)
124 .param('schema-set-name', schemaSetName))
125 .andReturn().response
126 then: 'associated service method is invoked with expected parameters'
127 1 * mockCpsModuleService.createSchemaSet(dataspaceName, schemaSetName, _) >>
128 { args -> yangResourceMapCapture = args[2] }
129 yangResourceMapCapture['filename.yang'] == 'content'
130 and: 'response code indicates success'
131 response.status == HttpStatus.CREATED.value()
134 def 'Create schema set from zip archive.'() {
135 def yangResourceMapCapture
136 given: 'zip archive with multiple .yang files inside'
137 def multipartFile = createZipMultipartFileFromResource("/yang-files-set.zip")
139 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
140 when: 'file uploaded with schema set create request'
143 multipart(schemaSetEndpoint)
145 .param('schema-set-name', schemaSetName))
146 .andReturn().response
147 then: 'associated service method is invoked with expected parameters'
148 1 * mockCpsModuleService.createSchemaSet(dataspaceName, schemaSetName, _) >>
149 { args -> yangResourceMapCapture = args[2] }
150 yangResourceMapCapture['assembly.yang'] == "fake assembly content 1\n"
151 yangResourceMapCapture['component.yang'] == "fake component content 1\n"
152 and: 'response code indicates success'
153 response.status == HttpStatus.CREATED.value()
156 def 'Create a schema set from a yang file that is greater than 1MB.'() {
157 given: 'a yang file greater than 1MB'
158 def multipartFile = createMultipartFileFromResource("/model-over-1mb.yang")
160 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
161 when: 'a file is uploaded to the create schema set endpoint'
164 multipart(schemaSetEndpoint)
166 .param('schema-set-name', schemaSetName))
167 .andReturn().response
168 then: 'the associated service method is invoked'
169 1 * mockCpsModuleService.createSchemaSet(dataspaceName, schemaSetName, _)
170 and: 'the response code indicates success'
171 response.status == HttpStatus.CREATED.value()
175 def 'Create schema set from zip archive having #caseDescriptor.'() {
177 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
178 when: 'zip archive having #caseDescriptor is uploaded with create schema set request'
181 multipart(schemaSetEndpoint)
183 .param('schema-set-name', schemaSetName))
184 .andReturn().response
185 then: 'create schema set rejected'
186 response.status == HttpStatus.BAD_REQUEST.value()
187 where: 'following cases are tested'
188 caseDescriptor | multipartFile
189 'no .yang files inside' | createZipMultipartFileFromResource("/no-yang-files.zip")
190 'multiple .yang files with same name' | createZipMultipartFileFromResource("/yang-files-multiple-sets.zip")
193 def 'Create schema set from file with unsupported filename extension.'() {
194 given: 'file with unsupported filename extension (.doc)'
195 def multipartFile = createMultipartFile("filename.doc", "content")
197 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
198 when: 'file uploaded with schema set create request'
201 multipart(schemaSetEndpoint)
203 .param('schema-set-name', schemaSetName))
204 .andReturn().response
205 then: 'create schema set rejected'
206 response.status == HttpStatus.BAD_REQUEST.value()
210 def 'Create schema set from #fileType file with IOException occurrence on processing.'() {
212 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
213 when: 'file uploaded with schema set create request'
214 def multipartFile = createMultipartFileForIOException(fileType)
217 multipart(schemaSetEndpoint)
219 .param('schema-set-name', schemaSetName))
220 .andReturn().response
221 then: 'the error response returned indicating internal server error occurrence'
222 response.status == HttpStatus.INTERNAL_SERVER_ERROR.value()
223 where: 'following file types are used'
224 fileType << ['YANG', 'ZIP']
227 def 'Delete schema set.'() {
229 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets/$schemaSetName"
230 when: 'delete schema set endpoint is invoked'
231 def response = mvc.perform(delete(schemaSetEndpoint)).andReturn().response
232 then: 'associated service method is invoked with expected parameters'
233 1 * mockCpsModuleService.deleteSchemaSet(dataspaceName, schemaSetName, CASCADE_DELETE_PROHIBITED)
234 and: 'response code indicates success'
235 response.status == HttpStatus.NO_CONTENT.value()
238 def 'Delete schema set which is in use.'() {
239 given: 'service method throws an exception indicating the schema set is in use'
240 def thrownException = new SchemaSetInUseException(dataspaceName, schemaSetName)
241 mockCpsModuleService.deleteSchemaSet(dataspaceName, schemaSetName, CASCADE_DELETE_PROHIBITED) >>
242 { throw thrownException }
244 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets/$schemaSetName"
245 when: 'delete schema set endpoint is invoked'
246 def response = mvc.perform(delete(schemaSetEndpoint)).andReturn().response
247 then: 'schema set deletion fails with conflict response code'
248 response.status == HttpStatus.CONFLICT.value()
251 def 'Get existing schema set.'() {
252 given: 'service method returns a new schema set'
253 mockCpsModuleService.getSchemaSet(dataspaceName, schemaSetName) >>
254 new SchemaSet(name: schemaSetName, dataspaceName: dataspaceName)
256 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets/$schemaSetName"
257 when: 'get schema set API is invoked'
258 def response = mvc.perform(get(schemaSetEndpoint)).andReturn().response
259 then: 'the correct schema set is returned'
260 response.status == HttpStatus.OK.value()
261 response.getContentAsString().contains(schemaSetName)
264 def 'Create Anchor.'() {
265 given: 'request parameters'
266 def requestParams = new LinkedMultiValueMap<>()
267 requestParams.add('schema-set-name', schemaSetName)
268 requestParams.add('anchor-name', anchorName)
270 def anchorEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors"
271 when: 'post is invoked'
274 post(anchorEndpoint).contentType(MediaType.APPLICATION_JSON)
275 .params(requestParams as MultiValueMap))
276 .andReturn().response
277 then: 'anchor is created successfully'
278 1 * mockCpsAdminService.createAnchor(dataspaceName, schemaSetName, anchorName)
279 response.status == HttpStatus.CREATED.value()
280 response.getContentAsString().contains(anchorName)
283 def 'Get existing anchor.'() {
284 given: 'service method returns a list of anchors'
285 mockCpsAdminService.getAnchors(dataspaceName) >> anchorList
287 def anchorEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors"
288 when: 'get all anchors API is invoked'
289 def response = mvc.perform(get(anchorEndpoint)).andReturn().response
290 then: 'the correct anchor is returned'
291 response.status == HttpStatus.OK.value()
292 response.getContentAsString().contains(anchorName)
295 def 'Get existing anchor by dataspace and anchor name.'() {
296 given: 'service method returns an anchor'
297 mockCpsAdminService.getAnchor(dataspaceName, anchorName) >>
298 new Anchor(name: anchorName, dataspaceName: dataspaceName, schemaSetName: schemaSetName)
300 def anchorEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors/$anchorName"
301 when: 'get anchor API is invoked'
302 def response = mvc.perform(get(anchorEndpoint)).andReturn().response
303 def responseContent = response.getContentAsString()
304 then: 'the correct anchor is returned'
305 response.status == HttpStatus.OK.value()
306 responseContent.contains(anchorName)
307 responseContent.contains(dataspaceName)
308 responseContent.contains(schemaSetName)
311 def 'Delete anchor.'() {
313 def anchorEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors/$anchorName"
314 when: 'delete method is invoked on anchor endpoint'
315 def response = mvc.perform(delete(anchorEndpoint)).andReturn().response
316 then: 'associated service method is invoked with expected parameters'
317 1 * mockCpsAdminService.deleteAnchor(dataspaceName, anchorName)
318 and: 'response code indicates success'
319 response.status == HttpStatus.NO_CONTENT.value()
323 def createMultipartFile(filename, content) {
324 return new MockMultipartFile("file", filename, "text/plain", content.getBytes())
327 def createZipMultipartFileFromResource(resourcePath) {
328 return new MockMultipartFile("file", "test.zip", "application/zip",
329 getClass().getResource(resourcePath).getBytes())
332 def createMultipartFileFromResource(resourcePath) {
333 return new MockMultipartFile("file", "test.yang", "application/text",
334 getClass().getResource(resourcePath).getBytes())
337 def createMultipartFileForIOException(extension) {
338 def multipartFile = Mock(MockMultipartFile)
339 multipartFile.getOriginalFilename() >> "TEST." + extension
340 multipartFile.getBytes() >> { throw new IOException() }
341 multipartFile.getInputStream() >> { throw new IOException() }