ZIP archive support for multiple YANG files delivery on Schema Set creation using...
[cps.git] / cps-rest / src / test / groovy / org / onap / cps / rest / utils / MultipartFileUtilSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Pantheon.tech
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.cps.rest.utils
21
22 import org.onap.cps.spi.exceptions.CpsException
23 import org.onap.cps.spi.exceptions.ModelValidationException
24 import org.springframework.mock.web.MockMultipartFile
25 import org.springframework.web.multipart.MultipartFile
26 import spock.lang.Specification
27 import spock.lang.Unroll
28
29 class MultipartFileUtilSpec extends Specification {
30
31     def 'Extract yang resource from yang file.'() {
32         given: 'uploaded yang file'
33             def multipartFile = new MockMultipartFile("file", "filename.yang", "text/plain", "content".getBytes())
34         when: 'resources are extracted from the file'
35             def result = MultipartFileUtil.extractYangResourcesMap(multipartFile)
36         then: 'the expected name and content are extracted as result'
37             assert result.size() == 1
38             assert result.get("filename.yang") == "content"
39     }
40
41     def 'Extract yang resources from zip archive.'() {
42         given: 'uploaded zip archive containing 2 yang files and 1 not yang (json) file'
43             def multipartFile = new MockMultipartFile("file", "TEST.ZIP", "application/zip",
44                     getClass().getResource("/yang-files-set.zip").getBytes())
45         when: 'resources are extracted from zip file'
46             def result = MultipartFileUtil.extractYangResourcesMap(multipartFile)
47         then: 'information from yang files is extracted, not yang file (json) is ignored'
48             assert result.size() == 2
49             assert result["assembly.yang"] == "fake assembly content 1\n"
50             assert result["component.yang"] == "fake component content 1\n"
51     }
52
53     @Unroll
54     def 'Extract resources from zip archive having #caseDescriptor.'() {
55         when: 'attempt to extract resources from zip file is performed'
56             MultipartFileUtil.extractYangResourcesMap(multipartFile)
57         then: 'the validation exception is thrown indicating invalid zip file content'
58             thrown(ModelValidationException)
59         where: 'following cases are tested'
60             caseDescriptor                      | multipartFile
61             'text files only'                   | multipartZipFileFromResource("/no-yang-files.zip")
62             'multiple yang file with same name' | multipartZipFileFromResource("/yang-files-multiple-sets.zip")
63     }
64
65     def 'Extract yang resource from a file with invalid filename extension.'() {
66         given: 'uploaded file with unsupported (.doc) exception'
67             def multipartFile = new MockMultipartFile("file", "filename.doc", "text/plain", "content".getBytes())
68         when: 'attempt to extract resources from the file is performed'
69             MultipartFileUtil.extractYangResourcesMap(multipartFile)
70         then: 'validation exception is thrown indicating the file type is not supported'
71             thrown(ModelValidationException)
72     }
73
74     @Unroll
75     def 'IOException thrown during yang resources extraction from #fileType file.'() {
76         when: 'attempt to extract resources from the file is performed'
77             MultipartFileUtil.extractYangResourcesMap(multipartFileForIOException(fileType))
78         then: 'CpsException is thrown indicating the internal error occurrence'
79             thrown(CpsException)
80         where: 'following file types are used'
81             fileType << ['YANG', 'ZIP']
82     }
83
84     def multipartZipFileFromResource(resourcePath) {
85         return new MockMultipartFile("file", "TEST.ZIP", "application/zip",
86                 getClass().getResource(resourcePath).getBytes())
87     }
88
89     def multipartFileForIOException(extension) {
90         def multipartFile = Mock(MultipartFile)
91         multipartFile.getOriginalFilename() >> "TEST." + extension
92         multipartFile.getBytes() >> { throw new IOException() }
93         multipartFile.getInputStream() >> { throw new IOException() }
94         return multipartFile
95     }
96
97 }