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
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.cps.rest.utils
22 import org.onap.cps.spi.exceptions.CpsException
23 import org.onap.cps.spi.exceptions.ModelValidationException
24 import org.onap.cps.spi.model.DataNodeBuilder
25 import org.onap.cps.utils.DataMapUtils
26 import org.springframework.mock.web.MockMultipartFile
27 import org.springframework.web.multipart.MultipartFile
28 import spock.lang.Specification
29 import spock.lang.Unroll
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"
65 def 'Extract resources from zip archive having #caseDescriptor.'() {
66 when: 'attempt to extract resources from zip file is performed'
67 MultipartFileUtil.extractYangResourcesMap(multipartFile)
68 then: 'the validation exception is thrown indicating invalid zip file content'
69 thrown(ModelValidationException)
70 where: 'following cases are tested'
71 caseDescriptor | multipartFile
72 'text files only' | multipartZipFileFromResource("/no-yang-files.zip")
73 'multiple yang file with same name' | multipartZipFileFromResource("/yang-files-multiple-sets.zip")
76 def 'Extract yang resource from a file with invalid filename extension.'() {
77 given: 'uploaded file with unsupported (.doc) exception'
78 def multipartFile = new MockMultipartFile("file", "filename.doc", "text/plain", "content".getBytes())
79 when: 'attempt to extract resources from the file is performed'
80 MultipartFileUtil.extractYangResourcesMap(multipartFile)
81 then: 'validation exception is thrown indicating the file type is not supported'
82 thrown(ModelValidationException)
86 def 'IOException thrown during yang resources extraction from #fileType file.'() {
87 when: 'attempt to extract resources from the file is performed'
88 MultipartFileUtil.extractYangResourcesMap(multipartFileForIOException(fileType))
89 then: 'CpsException is thrown indicating the internal error occurrence'
91 where: 'following file types are used'
92 fileType << ['YANG', 'ZIP']
95 def multipartZipFileFromResource(resourcePath) {
96 return new MockMultipartFile("file", "TEST.ZIP", "application/zip",
97 getClass().getResource(resourcePath).getBytes())
100 def multipartFileForIOException(extension) {
101 def multipartFile = Mock(MultipartFile)
102 multipartFile.getOriginalFilename() >> "TEST." + extension
103 multipartFile.getBytes() >> { throw new IOException() }
104 multipartFile.getInputStream() >> { throw new IOException() }