Replacing ModelMapper with MapStruct
[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-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
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 org.mapstruct.factory.Mappers
26
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
32
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
50
51 @WebMvcTest(AdminRestController)
52 class AdminRestControllerSpec extends Specification {
53
54     @SpringBean
55     CpsModuleService mockCpsModuleService = Mock()
56
57     @SpringBean
58     CpsAdminService mockCpsAdminService = Mock()
59
60     @SpringBean
61     CpsRestInputMapper cpsRestInputMapper = Mappers.getMapper(CpsRestInputMapper)
62
63     @Autowired
64     MockMvc mvc
65
66     @Value('${rest.api.cps-base-path}')
67     def basePath
68
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)
73
74     def 'Create new dataspace.'() {
75         given: 'an endpoint'
76             def createDataspaceEndpoint = "$basePath/v1/dataspaces";
77         when: 'post is invoked'
78             def response =
79                     mvc.perform(
80                             post(createDataspaceEndpoint)
81                                     .param('dataspace-name', dataspaceName))
82                             .andReturn().response
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()
87     }
88
89     def 'Create dataspace over existing with same name.'() {
90         given: 'an endpoint'
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'
96             def response =
97                     mvc.perform(
98                             post(createDataspaceEndpoint)
99                                     .param('dataspace-name', dataspaceName))
100                             .andReturn().response
101         then: 'dataspace creation fails'
102             response.status == HttpStatus.CONFLICT.value()
103     }
104
105     def 'Create schema set from yang file.'() {
106         def yangResourceMapCapture
107         given: 'single yang file'
108             def multipartFile = createMultipartFile("filename.yang", "content")
109         and: 'an endpoint'
110             def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
111         when: 'file uploaded with schema set create request'
112             def response =
113                     mvc.perform(
114                             multipart(schemaSetEndpoint)
115                                     .file(multipartFile)
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()
124     }
125
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")
130         and: 'an endpoint'
131             def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
132         when: 'file uploaded with schema set create request'
133             def response =
134                     mvc.perform(
135                             multipart(schemaSetEndpoint)
136                                     .file(multipartFile)
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()
146     }
147
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")
151         and: 'an endpoint'
152             def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
153         when: 'a file is uploaded to the create schema set endpoint'
154             def response =
155                     mvc.perform(
156                             multipart(schemaSetEndpoint)
157                                     .file(multipartFile)
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()
164     }
165
166     def 'Create schema set from zip archive having #caseDescriptor.'() {
167         given: 'an endpoint'
168             def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
169         when: 'zip archive having #caseDescriptor is uploaded with create schema set request'
170             def response =
171                     mvc.perform(
172                             multipart(schemaSetEndpoint)
173                                     .file(multipartFile)
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")
182     }
183
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")
187         and: 'an endpoint'
188             def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
189         when: 'file uploaded with schema set create request'
190             def response =
191                     mvc.perform(
192                             multipart(schemaSetEndpoint)
193                                     .file(multipartFile)
194                                     .param('schema-set-name', schemaSetName))
195                             .andReturn().response
196         then: 'create schema set rejected'
197             response.status == HttpStatus.BAD_REQUEST.value()
198     }
199
200     def 'Create schema set from #fileType file with IOException occurrence on processing.'() {
201         given: 'an endpoint'
202             def schemaSetEndpoint = "$basePath/v1/dataspaces/$dataspaceName/schema-sets"
203         when: 'file uploaded with schema set create request'
204             def multipartFile = createMultipartFileForIOException(fileType)
205             def response =
206                     mvc.perform(
207                             multipart(schemaSetEndpoint)
208                                     .file(multipartFile)
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']
215     }
216
217     def 'Delete schema set.'() {
218         given: 'an endpoint'
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()
226     }
227
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 }
233         and: 'an endpoint'
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()
239     }
240
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)
245         and: 'an endpoint'
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)
252     }
253
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)
259         and: 'an endpoint'
260             def anchorEndpoint = "$basePath/v1/dataspaces/$dataspaceName/anchors"
261         when: 'post is invoked'
262             def response =
263                     mvc.perform(
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)
271     }
272
273     def 'Get existing anchor.'() {
274         given: 'service method returns a list of anchors'
275             mockCpsAdminService.getAnchors(dataspaceName) >> [anchor]
276         and: 'an endpoint'
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)
283     }
284
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)
289         and: 'an endpoint'
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)
299     }
300
301     def 'Delete anchor.'() {
302         given: 'an endpoint'
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()
310     }
311
312     def 'Delete dataspace.'() {
313         given: 'an endpoint'
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()
323     }
324
325     def createMultipartFile(filename, content) {
326         return new MockMultipartFile("file", filename, "text/plain", content.getBytes())
327     }
328
329     def createZipMultipartFileFromResource(resourcePath) {
330         return new MockMultipartFile("file", "test.zip", "application/zip",
331                 getClass().getResource(resourcePath).getBytes())
332     }
333
334     def createMultipartFileFromResource(resourcePath) {
335         return new MockMultipartFile("file", "test.yang", "application/text",
336                 getClass().getResource(resourcePath).getBytes())
337     }
338
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() }
344         return multipartFile
345     }
346
347 }