2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Pantheon.tech
4 * Modifications Copyright (C) 2023 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.rest.utils
23 import org.onap.cps.spi.exceptions.CpsException
24 import org.onap.cps.spi.exceptions.ModelValidationException
25 import org.springframework.mock.web.MockMultipartFile
26 import org.springframework.web.multipart.MultipartFile
27 import spock.lang.Specification
29 class MultipartFileUtilSpec extends Specification {
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"
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"
53 def 'Yang file limits in zip archive: #scenario for the bug reported in CPS-1477'() {
54 given: 'a yang file size (uncompressed) limit of #threshold bytes'
55 ZipFileSizeValidator.thresholdSize = threshold
56 and: 'an archive with a yang file of 1083 bytes'
57 def multipartFile = multipartZipFileFromResource('/yang-files-set-total-1083-bytes.zip')
58 when: 'attempt to extract yang files'
59 def thrownException = null
61 MultipartFileUtil.extractYangResourcesMap(multipartFile)
62 } catch (Exception e) {
65 then: 'ModelValidationException indicating size limit is only thrown when threshold exceeded'
66 if (thresholdExceeded) {
67 assert thrownException instanceof ModelValidationException
68 assert thrownException.details.contains('limit of ' + threshold + ' bytes')
70 assert thrownException == null
73 scenario | threshold || thresholdExceeded
74 'exceed limit' | 1082 || true
75 'equals to limit' | 1083 || false
76 'within limit' | 1084 || false
79 def 'Extract resources from zip archive having #caseDescriptor.'() {
80 when: 'attempt to extract resources from zip file is performed'
81 MultipartFileUtil.extractYangResourcesMap(multipartFile)
82 then: 'the validation exception is thrown indicating invalid zip file content'
83 thrown(ModelValidationException)
84 where: 'following cases are tested'
85 caseDescriptor | multipartFile
86 'text files only' | multipartZipFileFromResource("/no-yang-files.zip")
87 'multiple yang file with same name' | multipartZipFileFromResource("/yang-files-multiple-sets.zip")
90 def 'Extract yang resource from a file with invalid filename extension.'() {
91 given: 'uploaded file with unsupported (.doc) exception'
92 def multipartFile = new MockMultipartFile("file", "filename.doc", "text/plain", "content".getBytes())
93 when: 'attempt to extract resources from the file is performed'
94 MultipartFileUtil.extractYangResourcesMap(multipartFile)
95 then: 'validation exception is thrown indicating the file type is not supported'
96 thrown(ModelValidationException)
99 def 'IOException thrown during yang resources extraction from #fileType file.'() {
100 when: 'attempt to extract resources from the file is performed'
101 MultipartFileUtil.extractYangResourcesMap(multipartFileForIOException(fileType))
102 then: 'CpsException is thrown indicating the internal error occurrence'
104 where: 'following file types are used'
105 fileType << ['YANG', 'ZIP']
108 def 'Resource name extension checks, with #scenario.'() {
109 expect: 'extension check returns expected result'
110 assert MultipartFileUtil.resourceNameEndsWithExtension(resourceName, '.test') == expectedResult
111 where: 'following resource names are tested'
112 scenario | resourceName || expectedResult
113 'correct extension'| 'file.test' || true
114 'mixed case' | 'file.TesT' || true
115 'other extension' | 'file.other' || false
116 'no extension' | 'file' || false
117 'null' | null || false
120 def 'Extract resourcename, with #scenario.'() {
121 expect: 'extension check returns expected result'
122 assert MultipartFileUtil.extractResourceNameFromPath(path) == expectedResoureName
123 where: 'following resource names are tested'
124 scenario | path || expectedResoureName
125 'no folder' | 'file.test' || 'file.test'
126 'single folder' | 'folder/file.test' || 'file.test'
127 'multiple folders' | 'f1/f2/file.test' || 'file.test'
128 'with root' | '/f1/f2/file.test' || 'file.test'
129 'windows notation' | 'c:\\f2\\file.test' || 'file.test'
130 'empty path' | '' || ''
131 'null path' | null || ''
134 def multipartZipFileFromResource(resourcePath) {
135 return new MockMultipartFile("file", "TEST.ZIP", "application/zip",
136 getClass().getResource(resourcePath).getBytes())
139 def multipartFileForIOException(extension) {
140 def multipartFile = Mock(MultipartFile)
141 multipartFile.getOriginalFilename() >> "TEST." + extension
142 multipartFile.getBytes() >> { throw new IOException() }
143 multipartFile.getInputStream() >> { throw new IOException() }