3c00d40a6b985946f7155909916808a516538cc8
[sdc.git] /
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.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
21 import org.testng.annotations.Test;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
28 import java.util.Map;
29 import java.util.function.Function;
30
31 import static org.testng.Assert.assertTrue;
32 import static org.testng.Assert.assertFalse;
33 import static org.testng.Assert.assertEquals;
34
35 /**
36  * @author EVITALIY
37  * @since 22 Oct 17
38  */
39 public class FileUtilsTest {
40
41     private static final String TEST_RESOURCE = FileUtilsTest.class.getPackage().getName()
42             .replace('.', '/') + "/test-resource.txt";
43
44     private static final Function<InputStream, Integer> TEST_FUNCTION = (s) -> {
45
46         try {
47             return s.available();
48         } catch (IOException e) {
49             throw new RuntimeException(e);
50         }
51     };
52
53     @Test
54     public void testReadViaInputStreamWithSlash() throws Exception {
55         assertTrue(FileUtils.readViaInputStream(TEST_RESOURCE, TEST_FUNCTION) > 0);
56     }
57
58     @Test
59     public void testReadViaInputStreamWithoutSlash() throws Exception {
60         assertTrue(FileUtils.readViaInputStream(TEST_RESOURCE, TEST_FUNCTION) > 0);
61     }
62
63     @Test(expectedExceptions = NullPointerException.class)
64     public void testReadViaInputStreamNull() throws Exception {
65         FileUtils.readViaInputStream((String) null, TEST_FUNCTION);
66     }
67
68     @Test(expectedExceptions = IllegalArgumentException.class)
69     public void testReadViaInputStreamNotFound() throws Exception {
70         FileUtils.readViaInputStream("notfound.txt", TEST_FUNCTION);
71     }
72
73     @Test
74     public void testWriteFilesFromFileContentHandler() throws IOException {
75         Path dir = Files.createTempDirectory("CSAR_" + System.currentTimeMillis());
76         try {
77             byte[] uploadedFileData = IOUtils.toByteArray(
78                 FileUtilsTest.class.getResource("resource-Spgw-csar-ZTE" +
79                     ".csar"));
80             FileContentHandler contentMap = FileUtils.getFileContentMapFromZip(uploadedFileData);
81             Map<String, String> filePaths = FileUtils.writeFilesFromFileContentHandler(contentMap,
82                 dir);
83
84             assertFalse(filePaths.isEmpty());
85             assertEquals(filePaths.size(), 18);
86             for (Map.Entry<String, String> fileEntry : filePaths.entrySet()) {
87                 File f = new File(fileEntry.getValue());
88                 assertTrue(f.exists());
89             }
90         }
91         finally {
92             org.apache.commons.io.FileUtils.deleteDirectory(dir.toFile());
93         }
94     }
95
96     @Test
97     public void testIsValidYamlExtension() throws IOException {
98         assertTrue(FileUtils.isValidYamlExtension("yaml"));
99         assertTrue(FileUtils.isValidYamlExtension("yml"));
100         assertFalse(FileUtils.isValidYamlExtension("yml1"));
101         assertFalse(FileUtils.isValidYamlExtension("zip"));
102     }
103 }