74c1b1424d15d85b476f7bf82353059c4f043bf3
[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 static org.testng.Assert.assertEquals;
20 import static org.testng.Assert.assertFalse;
21 import static org.testng.Assert.assertTrue;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.net.URL;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.function.Function;
32 import java.util.stream.Collectors;
33 import java.util.stream.Stream;
34
35 import org.apache.commons.io.IOUtils;
36 import org.testng.Assert;
37 import org.testng.annotations.Test;
38
39 /**
40  * @author EVITALIY
41  * @since 22 Oct 17
42  */
43 public class FileUtilsTest {
44
45     private static final String TEST_RESOURCE = FileUtilsTest.class.getPackage().getName()
46             .replace('.', '/') + "/test-resource.txt";
47
48     private static final Function<InputStream, Integer> TEST_FUNCTION = (s) -> {
49
50         try {
51             return s.available();
52         } catch (IOException e) {
53             throw new RuntimeException(e);
54         }
55     };
56
57     @Test
58     public void testReadViaInputStreamWithSlash() {
59         assertTrue(FileUtils.readViaInputStream(TEST_RESOURCE, TEST_FUNCTION) > 0);
60     }
61
62     @Test
63     public void testReadViaInputStreamWithoutSlash() {
64         assertTrue(FileUtils.readViaInputStream(TEST_RESOURCE, TEST_FUNCTION) > 0);
65     }
66
67     @Test(expectedExceptions = NullPointerException.class)
68     public void testReadViaInputStreamNull() {
69         FileUtils.readViaInputStream((String) null, TEST_FUNCTION);
70     }
71
72     @Test(expectedExceptions = IllegalArgumentException.class)
73     public void testReadViaInputStreamNotFound() {
74         FileUtils.readViaInputStream("notfound.txt", TEST_FUNCTION);
75     }
76
77     @Test
78     public void testWriteFilesFromFileContentHandler() throws IOException {
79         Path dir = Files.createTempDirectory("CSAR_" + System.currentTimeMillis());
80         try {
81             byte[] uploadedFileData = IOUtils.toByteArray(
82                 FileUtilsTest.class.getResource("resource-Spgw-csar-ZTE" +
83                     ".csar"));
84             FileContentHandler contentMap = FileUtils.getFileContentMapFromZip(uploadedFileData);
85             Map<String, String> filePaths = FileUtils.writeFilesFromFileContentHandler(contentMap,
86                 dir);
87
88             assertFalse(filePaths.isEmpty());
89             assertEquals(filePaths.size(), 18);
90             for (Map.Entry<String, String> fileEntry : filePaths.entrySet()) {
91                 File f = new File(fileEntry.getValue());
92                 assertTrue(f.exists());
93             }
94         }
95         finally {
96             org.apache.commons.io.FileUtils.deleteDirectory(dir.toFile());
97         }
98     }
99
100     @Test
101     public void testIsValidYamlExtension() {
102         assertTrue(FileUtils.isValidYamlExtension("yaml"));
103         assertTrue(FileUtils.isValidYamlExtension("yml"));
104         assertFalse(FileUtils.isValidYamlExtension("yml1"));
105         assertFalse(FileUtils.isValidYamlExtension("zip"));
106     }
107
108     @Test
109     public void testGetFileWithoutExtention() {
110         Assert.assertEquals(FileUtils.getFileWithoutExtention("test.txt"), "test");
111     }
112
113     @Test
114     public void testGetFileWithoutExtentionContainsNoExtension() {
115         Assert.assertEquals(FileUtils.getFileWithoutExtention("test"), "test");
116     }
117
118     @Test
119     public void testGetFileExtention() {
120         Assert.assertEquals(FileUtils.getFileExtension("test.txt"), "txt");
121     }
122
123     @Test
124     public void testGetNetworkPackageName() {
125         Assert.assertEquals(FileUtils.getNetworkPackageName("heat.zip"), "heat");
126     }
127
128     @Test
129     public void testGetNetworkPackageNameWithoutExtension() {
130         Assert.assertNull(FileUtils.getNetworkPackageName("heat"));
131     }
132
133     @Test
134     public void testToByteArrayNullStream() {
135         Assert.assertNotNull(FileUtils.toByteArray(null));
136     }
137
138     @Test
139     public void testGetAllLocations() {
140         List<URL> urlList = FileUtils.getAllLocations("org/openecomp/core/utilities/file/testFileUtils.txt");
141         Assert.assertNotNull(urlList);
142         Assert.assertEquals(urlList.size(), 1);
143     }
144
145     @Test
146     public void testConvertToBytesNullObject() {
147         Assert.assertNotNull(FileUtils.convertToBytes(null, null));
148     }
149
150     @Test
151     public void testConvertToBytes() {
152         byte[] bytesArray = FileUtils.convertToBytes(Stream.of("Json", "Util", "Test").collect(Collectors.toList()),
153                                 FileUtils.FileExtension.YAML);
154
155         Assert.assertNotNull(bytesArray);
156     }
157
158     @Test
159     public void testConvertToBytesNotYaml() {
160         byte[] bytesArray = FileUtils.convertToBytes(Stream.of("Json", "Util", "Test").collect(Collectors.toList()),
161                 FileUtils.FileExtension.JSON);
162
163         Assert.assertNotNull(bytesArray);
164     }
165
166     @Test
167     public void testConvertToInputStreamNullObject() {
168         Assert.assertNull(FileUtils.convertToInputStream(null, null));
169     }
170
171     @Test
172     public void testConvertToInputStream() {
173         InputStream inputStream = FileUtils.convertToInputStream(Stream.of("Json", "Util", "Test")
174                         .collect(Collectors.toList()), FileUtils.FileExtension.YAML);
175
176         Assert.assertNotNull(inputStream);
177     }
178
179     @Test(expectedExceptions = RuntimeException.class)
180     public void testLoadFileToInputStreamIncorrectFilePath() {
181         FileUtils.loadFileToInputStream("invalidfilepath");
182     }
183
184     @Test
185     public void testLoadFileToInputStream() throws IOException{
186         int i;
187         StringBuilder builder = new StringBuilder(20);
188         InputStream inputStream = FileUtils.loadFileToInputStream(
189                 "org/openecomp/core/utilities/file/testFileUtils.txt");
190         while((i = inputStream.read())!=-1) {
191             builder.append((char)i);
192         }
193
194         Assert.assertNotNull(inputStream);
195         Assert.assertEquals(builder.toString(), "hello-test");
196     }
197 }