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 org.testng.Assert.assertEquals;
20 import static org.testng.Assert.assertFalse;
21 import static org.testng.Assert.assertTrue;
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;
35 import org.apache.commons.io.IOUtils;
36 import org.testng.Assert;
37 import org.testng.annotations.Test;
43 public class FileUtilsTest {
45 private static final String TEST_RESOURCE = FileUtilsTest.class.getPackage().getName()
46 .replace('.', '/') + "/test-resource.txt";
48 private static final Function<InputStream, Integer> TEST_FUNCTION = (s) -> {
52 } catch (IOException e) {
53 throw new RuntimeException(e);
58 public void testReadViaInputStreamWithSlash() {
59 assertTrue(FileUtils.readViaInputStream(TEST_RESOURCE, TEST_FUNCTION) > 0);
63 public void testReadViaInputStreamWithoutSlash() {
64 assertTrue(FileUtils.readViaInputStream(TEST_RESOURCE, TEST_FUNCTION) > 0);
67 @Test(expectedExceptions = NullPointerException.class)
68 public void testReadViaInputStreamNull() {
69 FileUtils.readViaInputStream((String) null, TEST_FUNCTION);
72 @Test(expectedExceptions = IllegalArgumentException.class)
73 public void testReadViaInputStreamNotFound() {
74 FileUtils.readViaInputStream("notfound.txt", TEST_FUNCTION);
78 public void testWriteFilesFromFileContentHandler() throws IOException {
79 Path dir = Files.createTempDirectory("CSAR_" + System.currentTimeMillis());
81 byte[] uploadedFileData = IOUtils.toByteArray(
82 FileUtilsTest.class.getResource("resource-Spgw-csar-ZTE" +
84 FileContentHandler contentMap = FileUtils.getFileContentMapFromZip(uploadedFileData);
85 Map<String, String> filePaths = FileUtils.writeFilesFromFileContentHandler(contentMap,
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());
96 org.apache.commons.io.FileUtils.deleteDirectory(dir.toFile());
101 public void testIsValidYamlExtension() {
102 assertTrue(FileUtils.isValidYamlExtension("yaml"));
103 assertTrue(FileUtils.isValidYamlExtension("yml"));
104 assertFalse(FileUtils.isValidYamlExtension("yml1"));
105 assertFalse(FileUtils.isValidYamlExtension("zip"));
109 public void testGetFileWithoutExtention() {
110 Assert.assertEquals(FileUtils.getFileWithoutExtention("test.txt"), "test");
114 public void testGetFileWithoutExtentionContainsNoExtension() {
115 Assert.assertEquals(FileUtils.getFileWithoutExtention("test"), "test");
119 public void testGetFileExtention() {
120 Assert.assertEquals(FileUtils.getFileExtension("test.txt"), "txt");
124 public void testGetNetworkPackageName() {
125 Assert.assertEquals(FileUtils.getNetworkPackageName("heat.zip"), "heat");
129 public void testGetNetworkPackageNameWithoutExtension() {
130 Assert.assertNull(FileUtils.getNetworkPackageName("heat"));
134 public void testToByteArrayNullStream() {
135 Assert.assertNotNull(FileUtils.toByteArray(null));
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);
146 public void testConvertToBytesNullObject() {
147 Assert.assertNotNull(FileUtils.convertToBytes(null, null));
151 public void testConvertToBytes() {
152 byte[] bytesArray = FileUtils.convertToBytes(Stream.of("Json", "Util", "Test").collect(Collectors.toList()),
153 FileUtils.FileExtension.YAML);
155 Assert.assertNotNull(bytesArray);
159 public void testConvertToBytesNotYaml() {
160 byte[] bytesArray = FileUtils.convertToBytes(Stream.of("Json", "Util", "Test").collect(Collectors.toList()),
161 FileUtils.FileExtension.JSON);
163 Assert.assertNotNull(bytesArray);
167 public void testConvertToInputStreamNullObject() {
168 Assert.assertNull(FileUtils.convertToInputStream(null, null));
172 public void testConvertToInputStream() {
173 InputStream inputStream = FileUtils.convertToInputStream(Stream.of("Json", "Util", "Test")
174 .collect(Collectors.toList()), FileUtils.FileExtension.YAML);
176 Assert.assertNotNull(inputStream);
179 @Test(expectedExceptions = RuntimeException.class)
180 public void testLoadFileToInputStreamIncorrectFilePath() {
181 FileUtils.loadFileToInputStream("invalidfilepath");
185 public void testLoadFileToInputStream() throws IOException{
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);
194 Assert.assertNotNull(inputStream);
195 Assert.assertEquals(builder.toString(), "hello-test");