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