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.onap.cps.spi.model.DataNodeBuilder
26 import org.onap.cps.utils.DataMapUtils
27 import org.springframework.mock.web.MockMultipartFile
28 import org.springframework.web.multipart.MultipartFile
29 import spock.lang.Specification
31 class MultipartFileUtilSpec extends Specification {
33 def 'Data node without leaves and without children.'() {
34 given: 'a datanode with no leaves and no children'
35 def dataNodeWithoutData = new DataNodeBuilder().withXpath('some xpath').build()
36 when: 'it is converted to a map'
37 def result = DataMapUtils.toDataMap(dataNodeWithoutData)
38 then: 'an empty object map is returned'
42 def 'Extract yang resource from yang file.'() {
43 given: 'uploaded yang file'
44 def multipartFile = new MockMultipartFile("file", "filename.yang", "text/plain", "content".getBytes())
45 when: 'resources are extracted from the file'
46 def result = MultipartFileUtil.extractYangResourcesMap(multipartFile)
47 then: 'the expected name and content are extracted as result'
48 assert result.size() == 1
49 assert result.get("filename.yang") == "content"
52 def 'Extract yang resources from zip archive.'() {
53 given: 'uploaded zip archive containing 2 yang files and 1 not yang (json) file'
54 def multipartFile = new MockMultipartFile("file", "TEST.ZIP", "application/zip",
55 getClass().getResource("/yang-files-set.zip").getBytes())
56 when: 'resources are extracted from zip file'
57 def result = MultipartFileUtil.extractYangResourcesMap(multipartFile)
58 then: 'information from yang files is extracted, not yang file (json) is ignored'
59 assert result.size() == 2
60 assert result["assembly.yang"] == "fake assembly content 1\n"
61 assert result["component.yang"] == "fake component content 1\n"
64 def 'Yang file limits in zip archive: #scenario for the bug reported in CPS-1477'() {
65 given: 'a yang file size (uncompressed) limit of #threshold bytes'
66 ZipFileSizeValidator.thresholdSize = threshold
67 and: 'an archive with a yang file of 1083 bytes'
68 def multipartFile = multipartZipFileFromResource('/yang-files-set-total-1083-bytes.zip')
69 when: 'attempt to extract yang files'
70 def thrownException = null
72 MultipartFileUtil.extractYangResourcesMap(multipartFile)
73 } catch (Exception e) {
76 then: 'ModelValidationException indicating size limit is only thrown when threshold exceeded'
77 if (thresholdExceeded) {
78 assert thrownException instanceof ModelValidationException
79 assert thrownException.details.contains('limit of ' + threshold + ' bytes')
81 assert thrownException == null
84 scenario | threshold || thresholdExceeded
85 'exceed limit' | 1082 || true
86 'equals to limit' | 1083 || false
87 'within limit' | 1084 || false
90 def 'Extract resources from zip archive having #caseDescriptor.'() {
91 when: 'attempt to extract resources from zip file is performed'
92 MultipartFileUtil.extractYangResourcesMap(multipartFile)
93 then: 'the validation exception is thrown indicating invalid zip file content'
94 thrown(ModelValidationException)
95 where: 'following cases are tested'
96 caseDescriptor | multipartFile
97 'text files only' | multipartZipFileFromResource("/no-yang-files.zip")
98 'multiple yang file with same name' | multipartZipFileFromResource("/yang-files-multiple-sets.zip")
101 def 'Extract yang resource from a file with invalid filename extension.'() {
102 given: 'uploaded file with unsupported (.doc) exception'
103 def multipartFile = new MockMultipartFile("file", "filename.doc", "text/plain", "content".getBytes())
104 when: 'attempt to extract resources from the file is performed'
105 MultipartFileUtil.extractYangResourcesMap(multipartFile)
106 then: 'validation exception is thrown indicating the file type is not supported'
107 thrown(ModelValidationException)
110 def 'IOException thrown during yang resources extraction from #fileType file.'() {
111 when: 'attempt to extract resources from the file is performed'
112 MultipartFileUtil.extractYangResourcesMap(multipartFileForIOException(fileType))
113 then: 'CpsException is thrown indicating the internal error occurrence'
115 where: 'following file types are used'
116 fileType << ['YANG', 'ZIP']
119 def multipartZipFileFromResource(resourcePath) {
120 return new MockMultipartFile("file", "TEST.ZIP", "application/zip",
121 getClass().getResource(resourcePath).getBytes())
124 def multipartFileForIOException(extension) {
125 def multipartFile = Mock(MultipartFile)
126 multipartFile.getOriginalFilename() >> "TEST." + extension
127 multipartFile.getBytes() >> { throw new IOException() }
128 multipartFile.getInputStream() >> { throw new IOException() }