re base code
[sdc.git] / openecomp-be / lib / openecomp-core-lib / openecomp-utilities-lib / src / test / java / org / openecomp / core / utilities / file / FileUtilsTest.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
17 package org.openecomp.core.utilities.file;
18
19 import org.apache.commons.io.IOUtils;
20 import org.testng.annotations.Test;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.util.Map;
28 import java.util.function.Function;
29
30 import static org.testng.Assert.*;
31
32 /**
33  * @author EVITALIY
34  * @since 22 Oct 17
35  */
36 public class FileUtilsTest {
37
38     private static final String TEST_RESOURCE = FileUtilsTest.class.getPackage().getName()
39             .replace('.', '/') + "/test-resource.txt";
40
41     private static final Function<InputStream, Integer> TEST_FUNCTION = (s) -> {
42
43         try {
44             return s.available();
45         } catch (IOException e) {
46             throw new RuntimeException(e);
47         }
48     };
49
50     @Test
51     public void testReadViaInputStreamWithSlash() throws Exception {
52         assertTrue(FileUtils.readViaInputStream(TEST_RESOURCE, TEST_FUNCTION) > 0);
53     }
54
55     @Test
56     public void testReadViaInputStreamWithoutSlash() throws Exception {
57         assertTrue(FileUtils.readViaInputStream(TEST_RESOURCE, TEST_FUNCTION) > 0);
58     }
59
60     @Test(expectedExceptions = NullPointerException.class)
61     public void testReadViaInputStreamNull() throws Exception {
62         FileUtils.readViaInputStream((String) null, TEST_FUNCTION);
63     }
64
65     @Test(expectedExceptions = IllegalArgumentException.class)
66     public void testReadViaInputStreamNotFound() throws Exception {
67         FileUtils.readViaInputStream("notfound.txt", TEST_FUNCTION);
68     }
69
70     @Test
71     public void testWriteFilesFromFileContentHandler() throws IOException {
72         Path dir = Files.createTempDirectory("CSAR_" + System.currentTimeMillis());
73         try {
74             byte[] uploadedFileData = IOUtils.toByteArray(
75                 FileUtilsTest.class.getResource("resource-Spgw-csar-ZTE" +
76                     ".csar"));
77             FileContentHandler contentMap = FileUtils.getFileContentMapFromZip(uploadedFileData);
78             Map<String, String> filePaths = FileUtils.writeFilesFromFileContentHandler(contentMap,
79                 dir);
80
81             assertFalse(filePaths.isEmpty());
82             assertEquals(filePaths.size(), 18);
83             for (Map.Entry<String, String> fileEntry : filePaths.entrySet()) {
84                 File f = new File(fileEntry.getValue());
85                 assertTrue(f.exists());
86             }
87         }
88         finally {
89             org.apache.commons.io.FileUtils.deleteDirectory(dir.toFile());
90         }
91     }
92
93     @Test
94     public void testIsValidYamlExtension() throws IOException {
95         assertTrue(FileUtils.isValidYamlExtension("yaml"));
96         assertTrue(FileUtils.isValidYamlExtension("yml"));
97         assertFalse(FileUtils.isValidYamlExtension("yml1"));
98         assertFalse(FileUtils.isValidYamlExtension("zip"));
99     }
100 }