2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020-2021 Pantheon.tech
4 * Modifications Copyright (C) 2020-2021 Bell Canada.
5 * Modifications 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
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.cps.rest.controller
25 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED
26 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete
27 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
28 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart
29 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
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.SchemaSetInUseException
38 import org.onap.cps.spi.model.Anchor
39 import org.onap.cps.spi.model.SchemaSet
40 import org.spockframework.spring.SpringBean
41 import org.springframework.beans.factory.annotation.Autowired
42 import org.springframework.beans.factory.annotation.Value
43 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
44 import org.springframework.http.HttpStatus
45 import org.springframework.http.MediaType
46 import org.springframework.mock.web.MockMultipartFile
47 import org.springframework.test.web.servlet.MockMvc
48 import org.springframework.util.LinkedMultiValueMap
49 import org.springframework.util.MultiValueMap
50 import spock.lang.Specification
52 @WebMvcTest(AdminRestController)
53 class AdminRestControllerSpec extends Specification {
56 CpsModuleService mockCpsModuleService = Mock()
59 CpsAdminService mockCpsAdminService = Mock()
62 ModelMapper modelMapper = Spy()
67 @Value('${rest.api.cps-base-path}')
70 def dataspaceName = 'my_dataspace'
71 def anchor = new Anchor(name: 'my_anchor')
72 def anchorList = [anchor]
73 def anchorName = 'my_anchor'
74 def schemaSetName = 'my_schema_set'
76 def 'Create new dataspace.'() {
78 def createDataspaceEndpoint = "$basePath/v1/dataspaces";
79 when: 'post is invoked'
82 post(createDataspaceEndpoint)
83 .param('dataspace-name', dataspaceName))
85 then: 'service method is invoked with expected parameters'
86 1 * mockCpsAdminService.createDataspace(dataspaceName)
87 and: 'dataspace is create successfully'
88 response.status == HttpStatus.CREATED.value()
91 def 'Create dataspace over existing with same name.'() {
93 def createDataspaceEndpoint = "$basePath/v1/dataspaces";
94 and: 'the service method throws an exception indicating the dataspace is already defined'
95 def thrownException = new AlreadyDefinedException(dataspaceName, new RuntimeException())
96 mockCpsAdminService.createDataspace(dataspaceName) >> { throw thrownException }
97 when: 'post is invoked'
100 post(createDataspaceEndpoint)
101 .param('dataspace-name', dataspaceName))
102 .andReturn().response
103 then: 'dataspace creation fails'
104 response.status == HttpStatus.CONFLICT.value()
107 def 'Create schema set from yang file.'() {
108 def yangResourceMapCapture
109 given: 'single yang file'
110 def multipartFile = createMultipartFile("filename.yang", "content")
112 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
113 when: 'file uploaded with schema set create request'
116 multipart(schemaSetEndpoint)
118 .param('schema-set-name', schemaSetName))
119 .andReturn().response
120 then: 'associated service method is invoked with expected parameters'
121 1 * mockCpsModuleService.createSchemaSet(dataspaceName, schemaSetName, _) >>
122 { args -> yangResourceMapCapture = args[2] }
123 yangResourceMapCapture['filename.yang'] == 'content'
124 and: 'response code indicates success'
125 response.status == HttpStatus.CREATED.value()
128 def 'Create schema set from zip archive.'() {
129 def yangResourceMapCapture
130 given: 'zip archive with multiple .yang files inside'
131 def multipartFile = createZipMultipartFileFromResource("/yang-files-set.zip")
133 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
134 when: 'file uploaded with schema set create request'
137 multipart(schemaSetEndpoint)
139 .param('schema-set-name', schemaSetName))
140 .andReturn().response
141 then: 'associated service method is invoked with expected parameters'
142 1 * mockCpsModuleService.createSchemaSet(dataspaceName, schemaSetName, _) >>
143 { args -> yangResourceMapCapture = args[2] }
144 yangResourceMapCapture['assembly.yang'] == "fake assembly content 1\n"
145 yangResourceMapCapture['component.yang'] == "fake component content 1\n"
146 and: 'response code indicates success'
147 response.status == HttpStatus.CREATED.value()
150 def 'Create a schema set from a yang file that is greater than 1MB.'() {
151 given: 'a yang file greater than 1MB'
152 def multipartFile = createMultipartFileFromResource("/model-over-1mb.yang")
154 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
155 when: 'a file is uploaded to the create schema set endpoint'
158 multipart(schemaSetEndpoint)
160 .param('schema-set-name', schemaSetName))
161 .andReturn().response
162 then: 'the associated service method is invoked'
163 1 * mockCpsModuleService.createSchemaSet(dataspaceName, schemaSetName, _)
164 and: 'the response code indicates success'
165 response.status == HttpStatus.CREATED.value()
168 def 'Create schema set from zip archive having #caseDescriptor.'() {
170 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
171 when: 'zip archive having #caseDescriptor is uploaded with create schema set request'
174 multipart(schemaSetEndpoint)
176 .param('schema-set-name', schemaSetName))
177 .andReturn().response
178 then: 'create schema set rejected'
179 response.status == HttpStatus.BAD_REQUEST.value()
180 where: 'following cases are tested'
181 caseDescriptor | multipartFile
182 'no .yang files inside' | createZipMultipartFileFromResource("/no-yang-files.zip")
183 'multiple .yang files with same name' | createZipMultipartFileFromResource("/yang-files-multiple-sets.zip")
186 def 'Create schema set from file with unsupported filename extension.'() {
187 given: 'file with unsupported filename extension (.doc)'
188 def multipartFile = createMultipartFile("filename.doc", "content")
190 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
191 when: 'file uploaded with schema set create request'
194 multipart(schemaSetEndpoint)
196 .param('schema-set-name', schemaSetName))
197 .andReturn().response
198 then: 'create schema set rejected'
199 response.status == HttpStatus.BAD_REQUEST.value()
202 def 'Create schema set from #fileType file with IOException occurrence on processing.'() {
204 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
205 when: 'file uploaded with schema set create request'
206 def multipartFile = createMultipartFileForIOException(fileType)
209 multipart(schemaSetEndpoint)
211 .param('schema-set-name', schemaSetName))
212 .andReturn().response
213 then: 'the error response returned indicating internal server error occurrence'
214 response.status == HttpStatus.INTERNAL_SERVER_ERROR.value()
215 where: 'following file types are used'
216 fileType << ['YANG', 'ZIP']
219 def 'Delete schema set.'() {
221 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets/$schemaSetName"
222 when: 'delete schema set endpoint is invoked'
223 def response = mvc.perform(delete(schemaSetEndpoint)).andReturn().response
224 then: 'associated service method is invoked with expected parameters'
225 1 * mockCpsModuleService.deleteSchemaSet(dataspaceName, schemaSetName, CASCADE_DELETE_PROHIBITED)
226 and: 'response code indicates success'
227 response.status == HttpStatus.NO_CONTENT.value()
230 def 'Delete schema set which is in use.'() {
231 given: 'service method throws an exception indicating the schema set is in use'
232 def thrownException = new SchemaSetInUseException(dataspaceName, schemaSetName)
233 mockCpsModuleService.deleteSchemaSet(dataspaceName, schemaSetName, CASCADE_DELETE_PROHIBITED) >>
234 { throw thrownException }
236 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets/$schemaSetName"
237 when: 'delete schema set endpoint is invoked'
238 def response = mvc.perform(delete(schemaSetEndpoint)).andReturn().response
239 then: 'schema set deletion fails with conflict response code'
240 response.status == HttpStatus.CONFLICT.value()
243 def 'Get existing schema set.'() {
244 given: 'service method returns a new schema set'
245 mockCpsModuleService.getSchemaSet(dataspaceName, schemaSetName) >>
246 new SchemaSet(name: schemaSetName, dataspaceName: dataspaceName)
248 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets/$schemaSetName"
249 when: 'get schema set API is invoked'
250 def response = mvc.perform(get(schemaSetEndpoint)).andReturn().response
251 then: 'the correct schema set is returned'
252 response.status == HttpStatus.OK.value()
253 response.getContentAsString().contains(schemaSetName)
256 def 'Create Anchor.'() {
257 given: 'request parameters'
258 def requestParams = new LinkedMultiValueMap<>()
259 requestParams.add('schema-set-name', schemaSetName)
260 requestParams.add('anchor-name', anchorName)
262 def anchorEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors"
263 when: 'post is invoked'
266 post(anchorEndpoint).contentType(MediaType.APPLICATION_JSON)
267 .params(requestParams as MultiValueMap))
268 .andReturn().response
269 then: 'anchor is created successfully'
270 1 * mockCpsAdminService.createAnchor(dataspaceName, schemaSetName, anchorName)
271 response.status == HttpStatus.CREATED.value()
272 response.getContentAsString().contains(anchorName)
275 def 'Get existing anchor.'() {
276 given: 'service method returns a list of anchors'
277 mockCpsAdminService.getAnchors(dataspaceName) >> anchorList
279 def anchorEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors"
280 when: 'get all anchors API is invoked'
281 def response = mvc.perform(get(anchorEndpoint)).andReturn().response
282 then: 'the correct anchor is returned'
283 response.status == HttpStatus.OK.value()
284 response.getContentAsString().contains(anchorName)
287 def 'Get existing anchor by dataspace and anchor name.'() {
288 given: 'service method returns an anchor'
289 mockCpsAdminService.getAnchor(dataspaceName, anchorName) >>
290 new Anchor(name: anchorName, dataspaceName: dataspaceName, schemaSetName: schemaSetName)
292 def anchorEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors/$anchorName"
293 when: 'get anchor API is invoked'
294 def response = mvc.perform(get(anchorEndpoint)).andReturn().response
295 def responseContent = response.getContentAsString()
296 then: 'the correct anchor is returned'
297 response.status == HttpStatus.OK.value()
298 responseContent.contains(anchorName)
299 responseContent.contains(dataspaceName)
300 responseContent.contains(schemaSetName)
303 def 'Delete anchor.'() {
305 def anchorEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors/$anchorName"
306 when: 'delete method is invoked on anchor endpoint'
307 def response = mvc.perform(delete(anchorEndpoint)).andReturn().response
308 then: 'associated service method is invoked with expected parameters'
309 1 * mockCpsAdminService.deleteAnchor(dataspaceName, anchorName)
310 and: 'response code indicates success'
311 response.status == HttpStatus.NO_CONTENT.value()
314 def 'Delete dataspace.'() {
316 def dataspaceEndpoint = "$basePath/v1/dataspaces"
317 when: 'delete dataspace endpoint is invoked'
318 def response = mvc.perform(delete(dataspaceEndpoint)
319 .param('dataspace-name', dataspaceName))
320 .andReturn().response
321 then: 'associated service method is invoked with expected parameter'
322 1 * mockCpsAdminService.deleteDataspace(dataspaceName)
323 and: 'response code indicates success'
324 response.status == HttpStatus.NO_CONTENT.value()
327 def createMultipartFile(filename, content) {
328 return new MockMultipartFile("file", filename, "text/plain", content.getBytes())
331 def createZipMultipartFileFromResource(resourcePath) {
332 return new MockMultipartFile("file", "test.zip", "application/zip",
333 getClass().getResource(resourcePath).getBytes())
336 def createMultipartFileFromResource(resourcePath) {
337 return new MockMultipartFile("file", "test.yang", "application/text",
338 getClass().getResource(resourcePath).getBytes())
341 def createMultipartFileForIOException(extension) {
342 def multipartFile = Mock(MockMultipartFile)
343 multipartFile.getOriginalFilename() >> "TEST." + extension
344 multipartFile.getBytes() >> { throw new IOException() }
345 multipartFile.getInputStream() >> { throw new IOException() }