2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020-2021 Pantheon.tech
4 * Modifications Copyright (C) 2020-2021 Bell Canada.
5 * Modifications Copyright (C) 2021-2022 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 org.mapstruct.factory.Mappers
27 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED
28 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete
29 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
30 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart
31 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
33 import org.onap.cps.api.CpsAdminService
34 import org.onap.cps.api.CpsModuleService
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
51 @WebMvcTest(AdminRestController)
52 class AdminRestControllerSpec extends Specification {
55 CpsModuleService mockCpsModuleService = Mock()
58 CpsAdminService mockCpsAdminService = Mock()
61 CpsRestInputMapper cpsRestInputMapper = Mappers.getMapper(CpsRestInputMapper)
66 @Value('${rest.api.cps-base-path}')
69 def dataspaceName = 'my_dataspace'
70 def anchorName = 'my_anchor'
71 def schemaSetName = 'my_schema_set'
72 def anchor = new Anchor(name: anchorName, dataspaceName: dataspaceName, schemaSetName: schemaSetName)
74 def 'Create new dataspace.'() {
76 def createDataspaceEndpoint = "$basePath/v1/dataspaces";
77 when: 'post is invoked'
80 post(createDataspaceEndpoint)
81 .param('dataspace-name', dataspaceName))
83 then: 'service method is invoked with expected parameters'
84 1 * mockCpsAdminService.createDataspace(dataspaceName)
85 and: 'dataspace is create successfully'
86 response.status == HttpStatus.CREATED.value()
89 def 'Create dataspace over existing with same name.'() {
91 def createDataspaceEndpoint = "$basePath/v1/dataspaces";
92 and: 'the service method throws an exception indicating the dataspace is already defined'
93 def thrownException = new AlreadyDefinedException(dataspaceName, new RuntimeException())
94 mockCpsAdminService.createDataspace(dataspaceName) >> { throw thrownException }
95 when: 'post is invoked'
98 post(createDataspaceEndpoint)
99 .param('dataspace-name', dataspaceName))
100 .andReturn().response
101 then: 'dataspace creation fails'
102 response.status == HttpStatus.CONFLICT.value()
105 def 'Create schema set from yang file.'() {
106 def yangResourceMapCapture
107 given: 'single yang file'
108 def multipartFile = createMultipartFile("filename.yang", "content")
110 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
111 when: 'file uploaded with schema set create request'
114 multipart(schemaSetEndpoint)
116 .param('schema-set-name', schemaSetName))
117 .andReturn().response
118 then: 'associated service method is invoked with expected parameters'
119 1 * mockCpsModuleService.createSchemaSet(dataspaceName, schemaSetName, _) >>
120 { args -> yangResourceMapCapture = args[2] }
121 yangResourceMapCapture['filename.yang'] == 'content'
122 and: 'response code indicates success'
123 response.status == HttpStatus.CREATED.value()
126 def 'Create schema set from zip archive.'() {
127 def yangResourceMapCapture
128 given: 'zip archive with multiple .yang files inside'
129 def multipartFile = createZipMultipartFileFromResource("/yang-files-set.zip")
131 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
132 when: 'file uploaded with schema set create request'
135 multipart(schemaSetEndpoint)
137 .param('schema-set-name', schemaSetName))
138 .andReturn().response
139 then: 'associated service method is invoked with expected parameters'
140 1 * mockCpsModuleService.createSchemaSet(dataspaceName, schemaSetName, _) >>
141 { args -> yangResourceMapCapture = args[2] }
142 yangResourceMapCapture['assembly.yang'] == "fake assembly content 1\n"
143 yangResourceMapCapture['component.yang'] == "fake component content 1\n"
144 and: 'response code indicates success'
145 response.status == HttpStatus.CREATED.value()
148 def 'Create a schema set from a yang file that is greater than 1MB.'() {
149 given: 'a yang file greater than 1MB'
150 def multipartFile = createMultipartFileFromResource("/model-over-1mb.yang")
152 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
153 when: 'a file is uploaded to the create schema set endpoint'
156 multipart(schemaSetEndpoint)
158 .param('schema-set-name', schemaSetName))
159 .andReturn().response
160 then: 'the associated service method is invoked'
161 1 * mockCpsModuleService.createSchemaSet(dataspaceName, schemaSetName, _)
162 and: 'the response code indicates success'
163 response.status == HttpStatus.CREATED.value()
166 def 'Create schema set from zip archive having #caseDescriptor.'() {
168 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
169 when: 'zip archive having #caseDescriptor is uploaded with create schema set request'
172 multipart(schemaSetEndpoint)
174 .param('schema-set-name', schemaSetName))
175 .andReturn().response
176 then: 'create schema set rejected'
177 response.status == HttpStatus.BAD_REQUEST.value()
178 where: 'following cases are tested'
179 caseDescriptor | multipartFile
180 'no .yang files inside' | createZipMultipartFileFromResource("/no-yang-files.zip")
181 'multiple .yang files with same name' | createZipMultipartFileFromResource("/yang-files-multiple-sets.zip")
184 def 'Create schema set from file with unsupported filename extension.'() {
185 given: 'file with unsupported filename extension (.doc)'
186 def multipartFile = createMultipartFile("filename.doc", "content")
188 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
189 when: 'file uploaded with schema set create request'
192 multipart(schemaSetEndpoint)
194 .param('schema-set-name', schemaSetName))
195 .andReturn().response
196 then: 'create schema set rejected'
197 response.status == HttpStatus.BAD_REQUEST.value()
200 def 'Create schema set from #fileType file with IOException occurrence on processing.'() {
202 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
203 when: 'file uploaded with schema set create request'
204 def multipartFile = createMultipartFileForIOException(fileType)
207 multipart(schemaSetEndpoint)
209 .param('schema-set-name', schemaSetName))
210 .andReturn().response
211 then: 'the error response returned indicating internal server error occurrence'
212 response.status == HttpStatus.INTERNAL_SERVER_ERROR.value()
213 where: 'following file types are used'
214 fileType << ['YANG', 'ZIP']
217 def 'Delete schema set.'() {
219 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets/$schemaSetName"
220 when: 'delete schema set endpoint is invoked'
221 def response = mvc.perform(delete(schemaSetEndpoint)).andReturn().response
222 then: 'associated service method is invoked with expected parameters'
223 1 * mockCpsModuleService.deleteSchemaSet(dataspaceName, schemaSetName, CASCADE_DELETE_PROHIBITED)
224 and: 'response code indicates success'
225 response.status == HttpStatus.NO_CONTENT.value()
228 def 'Delete schema set which is in use.'() {
229 given: 'service method throws an exception indicating the schema set is in use'
230 def thrownException = new SchemaSetInUseException(dataspaceName, schemaSetName)
231 mockCpsModuleService.deleteSchemaSet(dataspaceName, schemaSetName, CASCADE_DELETE_PROHIBITED) >>
232 { throw thrownException }
234 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets/$schemaSetName"
235 when: 'delete schema set endpoint is invoked'
236 def response = mvc.perform(delete(schemaSetEndpoint)).andReturn().response
237 then: 'schema set deletion fails with conflict response code'
238 response.status == HttpStatus.CONFLICT.value()
241 def 'Get existing schema set.'() {
242 given: 'service method returns a new schema set'
243 mockCpsModuleService.getSchemaSet(dataspaceName, schemaSetName) >>
244 new SchemaSet(name: schemaSetName, dataspaceName: dataspaceName)
246 def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets/$schemaSetName"
247 when: 'get schema set API is invoked'
248 def response = mvc.perform(get(schemaSetEndpoint)).andReturn().response
249 then: 'the correct schema set is returned'
250 response.status == HttpStatus.OK.value()
251 response.getContentAsString().contains(schemaSetName)
254 def 'Create Anchor.'() {
255 given: 'request parameters'
256 def requestParams = new LinkedMultiValueMap<>()
257 requestParams.add('schema-set-name', schemaSetName)
258 requestParams.add('anchor-name', anchorName)
260 def anchorEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors"
261 when: 'post is invoked'
264 post(anchorEndpoint).contentType(MediaType.APPLICATION_JSON)
265 .params(requestParams as MultiValueMap))
266 .andReturn().response
267 then: 'anchor is created successfully'
268 1 * mockCpsAdminService.createAnchor(dataspaceName, schemaSetName, anchorName)
269 response.status == HttpStatus.CREATED.value()
270 response.getContentAsString().contains(anchorName)
273 def 'Get existing anchor.'() {
274 given: 'service method returns a list of anchors'
275 mockCpsAdminService.getAnchors(dataspaceName) >> [anchor]
277 def anchorEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors"
278 when: 'get all anchors API is invoked'
279 def response = mvc.perform(get(anchorEndpoint)).andReturn().response
280 then: 'the correct anchor is returned'
281 response.status == HttpStatus.OK.value()
282 response.getContentAsString().contains(anchorName)
285 def 'Get existing anchor by dataspace and anchor name.'() {
286 given: 'service method returns an anchor'
287 mockCpsAdminService.getAnchor(dataspaceName, anchorName) >>
288 new Anchor(name: anchorName, dataspaceName: dataspaceName, schemaSetName: schemaSetName)
290 def anchorEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors/$anchorName"
291 when: 'get anchor API is invoked'
292 def response = mvc.perform(get(anchorEndpoint)).andReturn().response
293 def responseContent = response.getContentAsString()
294 then: 'the correct anchor is returned'
295 response.status == HttpStatus.OK.value()
296 responseContent.contains(anchorName)
297 responseContent.contains(dataspaceName)
298 responseContent.contains(schemaSetName)
301 def 'Delete anchor.'() {
303 def anchorEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors/$anchorName"
304 when: 'delete method is invoked on anchor endpoint'
305 def response = mvc.perform(delete(anchorEndpoint)).andReturn().response
306 then: 'associated service method is invoked with expected parameters'
307 1 * mockCpsAdminService.deleteAnchor(dataspaceName, anchorName)
308 and: 'response code indicates success'
309 response.status == HttpStatus.NO_CONTENT.value()
312 def 'Delete dataspace.'() {
314 def dataspaceEndpoint = "$basePath/v1/dataspaces"
315 when: 'delete dataspace endpoint is invoked'
316 def response = mvc.perform(delete(dataspaceEndpoint)
317 .param('dataspace-name', dataspaceName))
318 .andReturn().response
319 then: 'associated service method is invoked with expected parameter'
320 1 * mockCpsAdminService.deleteDataspace(dataspaceName)
321 and: 'response code indicates success'
322 response.status == HttpStatus.NO_CONTENT.value()
325 def createMultipartFile(filename, content) {
326 return new MockMultipartFile("file", filename, "text/plain", content.getBytes())
329 def createZipMultipartFileFromResource(resourcePath) {
330 return new MockMultipartFile("file", "test.zip", "application/zip",
331 getClass().getResource(resourcePath).getBytes())
334 def createMultipartFileFromResource(resourcePath) {
335 return new MockMultipartFile("file", "test.yang", "application/text",
336 getClass().getResource(resourcePath).getBytes())
339 def createMultipartFileForIOException(extension) {
340 def multipartFile = Mock(MockMultipartFile)
341 multipartFile.getOriginalFilename() >> "TEST." + extension
342 multipartFile.getBytes() >> { throw new IOException() }
343 multipartFile.getInputStream() >> { throw new IOException() }