e8cfcfb6f605cd9c4d66f42617e8e66cf5d14685
[cps.git] / cps-rest / src / test / groovy / org / onap / cps / rest / controller / AdminRestControllerSpec.groovy
1 /*
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
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  *  SPDX-License-Identifier: Apache-2.0
20  *  ============LICENSE_END=========================================================
21  */
22
23 package org.onap.cps.rest.controller
24
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
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.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
51
52 @WebMvcTest(AdminRestController)
53 class AdminRestControllerSpec extends Specification {
54
55     @SpringBean
56     CpsModuleService mockCpsModuleService = Mock()
57
58     @SpringBean
59     CpsAdminService mockCpsAdminService = Mock()
60
61     @SpringBean
62     ModelMapper modelMapper = Spy()
63
64     @Autowired
65     MockMvc mvc
66
67     @Value('${rest.api.cps-base-path}')
68     def basePath
69
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'
75
76     def 'Create new dataspace.'() {
77         given: 'an endpoint'
78             def createDataspaceEndpoint = "$basePath/v1/dataspaces";
79         when: 'post is invoked'
80             def response =
81                     mvc.perform(
82                             post(createDataspaceEndpoint)
83                                     .param('dataspace-name', dataspaceName))
84                             .andReturn().response
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()
89     }
90
91     def 'Create dataspace over existing with same name.'() {
92         given: 'an endpoint'
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'
98             def response =
99                     mvc.perform(
100                             post(createDataspaceEndpoint)
101                                     .param('dataspace-name', dataspaceName))
102                             .andReturn().response
103         then: 'dataspace creation fails'
104             response.status == HttpStatus.CONFLICT.value()
105     }
106
107     def 'Create schema set from yang file.'() {
108         def yangResourceMapCapture
109         given: 'single yang file'
110             def multipartFile = createMultipartFile("filename.yang", "content")
111         and: 'an endpoint'
112             def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
113         when: 'file uploaded with schema set create request'
114             def response =
115                     mvc.perform(
116                             multipart(schemaSetEndpoint)
117                                     .file(multipartFile)
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()
126     }
127
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")
132         and: 'an endpoint'
133             def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
134         when: 'file uploaded with schema set create request'
135             def response =
136                     mvc.perform(
137                             multipart(schemaSetEndpoint)
138                                     .file(multipartFile)
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()
148     }
149
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")
153         and: 'an endpoint'
154             def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
155         when: 'a file is uploaded to the create schema set endpoint'
156             def response =
157                     mvc.perform(
158                             multipart(schemaSetEndpoint)
159                                     .file(multipartFile)
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()
166     }
167
168     def 'Create schema set from zip archive having #caseDescriptor.'() {
169         given: 'an endpoint'
170             def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
171         when: 'zip archive having #caseDescriptor is uploaded with create schema set request'
172             def response =
173                     mvc.perform(
174                             multipart(schemaSetEndpoint)
175                                     .file(multipartFile)
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")
184     }
185
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")
189         and: 'an endpoint'
190             def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
191         when: 'file uploaded with schema set create request'
192             def response =
193                     mvc.perform(
194                             multipart(schemaSetEndpoint)
195                                     .file(multipartFile)
196                                     .param('schema-set-name', schemaSetName))
197                             .andReturn().response
198         then: 'create schema set rejected'
199             response.status == HttpStatus.BAD_REQUEST.value()
200     }
201
202     def 'Create schema set from #fileType file with IOException occurrence on processing.'() {
203         given: 'an endpoint'
204             def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
205         when: 'file uploaded with schema set create request'
206             def multipartFile = createMultipartFileForIOException(fileType)
207             def response =
208                     mvc.perform(
209                             multipart(schemaSetEndpoint)
210                                     .file(multipartFile)
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']
217     }
218
219     def 'Delete schema set.'() {
220         given: 'an endpoint'
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()
228     }
229
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 }
235         and: 'an endpoint'
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()
241     }
242
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)
247         and: 'an endpoint'
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)
254     }
255
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)
261         and: 'an endpoint'
262             def anchorEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors"
263         when: 'post is invoked'
264             def response =
265                     mvc.perform(
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)
273     }
274
275     def 'Get existing anchor.'() {
276         given: 'service method returns a list of anchors'
277             mockCpsAdminService.getAnchors(dataspaceName) >> anchorList
278         and: 'an endpoint'
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)
285     }
286
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)
291         and: 'an endpoint'
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)
301     }
302
303     def 'Delete anchor.'() {
304         given: 'an endpoint'
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()
312     }
313
314     def 'Delete dataspace.'() {
315         given: 'an endpoint'
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()
325     }
326
327     def createMultipartFile(filename, content) {
328         return new MockMultipartFile("file", filename, "text/plain", content.getBytes())
329     }
330
331     def createZipMultipartFileFromResource(resourcePath) {
332         return new MockMultipartFile("file", "test.zip", "application/zip",
333                 getClass().getResource(resourcePath).getBytes())
334     }
335
336     def createMultipartFileFromResource(resourcePath) {
337         return new MockMultipartFile("file", "test.yang", "application/text",
338                 getClass().getResource(resourcePath).getBytes())
339     }
340
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() }
346         return multipartFile
347     }
348
349 }