2 * Copyright © 2016-2018 European Support Limited
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
8 * 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.
17 package org.openecomp.core.utilities.file;
19 import static junit.framework.TestCase.assertTrue;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
24 import java.io.IOException;
25 import java.io.InputStream;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.util.List;
31 import java.util.function.Function;
32 import java.util.stream.Collectors;
33 import java.util.stream.Stream;
34 import org.apache.commons.io.IOUtils;
35 import org.junit.Assert;
36 import org.junit.Test;
42 public class FileUtilsTest {
44 private static final String TEST_RESOURCE = FileUtilsTest.class.getPackage().getName()
45 .replace('.', '/') + "/test-resource.txt";
47 private static final Function<InputStream, Integer> TEST_FUNCTION = (s) -> {
51 } catch (IOException e) {
52 throw new RuntimeException(e);
57 public void testReadViaInputStreamWithSlash() {
58 assertTrue(FileUtils.readViaInputStream(TEST_RESOURCE, TEST_FUNCTION) > 0);
62 public void testReadViaInputStreamWithoutSlash() {
63 assertTrue(FileUtils.readViaInputStream(TEST_RESOURCE, TEST_FUNCTION) > 0);
66 @Test(expected = NullPointerException.class)
67 public void testReadViaInputStreamNull() {
68 FileUtils.readViaInputStream((String) null, TEST_FUNCTION);
71 @Test(expected = IllegalArgumentException.class)
72 public void testReadViaInputStreamNotFound() {
73 FileUtils.readViaInputStream("notfound.txt", TEST_FUNCTION);
77 public void testWriteFilesFromFileContentHandler() throws IOException {
78 Path dir = Files.createTempDirectory("CSAR_" + System.currentTimeMillis());
80 byte[] uploadedFileData = IOUtils.toByteArray(
81 FileUtilsTest.class.getResource("resource-Spgw-csar-ZTE" +
83 FileContentHandler contentMap = FileUtils.getFileContentMapFromZip(uploadedFileData);
84 Map<String, String> filePaths = FileUtils.writeFilesFromFileContentHandler(contentMap,
87 assertFalse(filePaths.isEmpty());
88 assertEquals(filePaths.size(), 18);
89 for (Map.Entry<String, String> fileEntry : filePaths.entrySet()) {
90 File f = new File(fileEntry.getValue());
91 assertTrue(f.exists());
95 org.apache.commons.io.FileUtils.deleteDirectory(dir.toFile());
100 public void testIsValidYamlExtension() {
101 assertTrue(FileUtils.isValidYamlExtension("yaml"));
102 assertTrue(FileUtils.isValidYamlExtension("yml"));
103 assertFalse(FileUtils.isValidYamlExtension("yml1"));
104 assertFalse(FileUtils.isValidYamlExtension("zip"));
108 public void testGetFileWithoutExtention() {
109 Assert.assertEquals(FileUtils.getFileWithoutExtention("test.txt"), "test");
113 public void testGetFileWithoutExtentionContainsNoExtension() {
114 Assert.assertEquals(FileUtils.getFileWithoutExtention("test"), "test");
118 public void testGetFileExtention() {
119 Assert.assertEquals(FileUtils.getFileExtension("test.txt"), "txt");
123 public void testGetNetworkPackageName() {
124 Assert.assertEquals(FileUtils.getNetworkPackageName("heat.zip"), "heat");
128 public void testGetNetworkPackageNameWithoutExtension() {
129 Assert.assertNull(FileUtils.getNetworkPackageName("heat"));
133 public void testToByteArrayNullStream() {
134 Assert.assertNotNull(FileUtils.toByteArray(null));
138 public void testGetAllLocations() {
139 List<URL> urlList = FileUtils.getAllLocations("org/openecomp/core/utilities/file/testFileUtils.txt");
140 Assert.assertNotNull(urlList);
141 Assert.assertEquals(urlList.size(), 1);
145 public void testConvertToBytesNullObject() {
146 Assert.assertNotNull(FileUtils.convertToBytes(null, null));
150 public void testConvertToBytes() {
151 byte[] bytesArray = FileUtils.convertToBytes(Stream.of("Json", "Util", "Test").collect(Collectors.toList()),
152 FileUtils.FileExtension.YAML);
154 Assert.assertNotNull(bytesArray);
158 public void testConvertToBytesNotYaml() {
159 byte[] bytesArray = FileUtils.convertToBytes(Stream.of("Json", "Util", "Test").collect(Collectors.toList()),
160 FileUtils.FileExtension.JSON);
162 Assert.assertNotNull(bytesArray);
166 public void testConvertToInputStreamNullObject() {
167 Assert.assertNull(FileUtils.convertToInputStream(null, null));
171 public void testConvertToInputStream() {
172 InputStream inputStream = FileUtils.convertToInputStream(Stream.of("Json", "Util", "Test")
173 .collect(Collectors.toList()), FileUtils.FileExtension.YAML);
175 Assert.assertNotNull(inputStream);
178 @Test(expected = RuntimeException.class)
179 public void testLoadFileToInputStreamIncorrectFilePath() {
180 FileUtils.loadFileToInputStream("invalidfilepath");
184 public void testLoadFileToInputStream() throws IOException{
186 StringBuilder builder = new StringBuilder(20);
187 InputStream inputStream = FileUtils.loadFileToInputStream(
188 "org/openecomp/core/utilities/file/testFileUtils.txt");
189 while((i = inputStream.read())!=-1) {
190 builder.append((char)i);
193 Assert.assertNotNull(inputStream);
194 Assert.assertEquals(builder.toString(), "hello-test");