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.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;
28 import java.io.IOException;
29 import java.io.InputStream;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
33 import java.util.List;
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;
47 public class FileUtilsTest {
49 private static final String TEST_RESOURCE = FileUtilsTest.class.getPackage().getName()
50 .replace('.', '/') + "/test-resource.txt";
52 private static final Function<InputStream, Integer> TEST_FUNCTION = (s) -> {
56 } catch (IOException e) {
57 throw new RuntimeException(e);
62 public void testReadViaInputStreamWithSlash() {
63 assertTrue(FileUtils.readViaInputStream(TEST_RESOURCE, TEST_FUNCTION) > 0);
67 public void testReadViaInputStreamWithoutSlash() {
68 assertTrue(FileUtils.readViaInputStream(TEST_RESOURCE, TEST_FUNCTION) > 0);
71 @Test(expected = NullPointerException.class)
72 public void testReadViaInputStreamNull() {
73 FileUtils.readViaInputStream((String) null, TEST_FUNCTION);
76 @Test(expected = IllegalArgumentException.class)
77 public void testReadViaInputStreamNotFound() {
78 FileUtils.readViaInputStream("notfound.txt", TEST_FUNCTION);
82 public void testWriteFilesFromFileContentHandler() throws IOException, ZipException {
83 final Path tempDirectory = Files.createTempDirectory("CSAR_" + System.currentTimeMillis());
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);
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));
97 org.apache.commons.io.FileUtils.deleteDirectory(tempDirectory.toFile());
102 public void testIsValidYamlExtension() {
103 assertTrue(FileUtils.isValidYamlExtension("yaml"));
104 assertTrue(FileUtils.isValidYamlExtension("yml"));
105 assertFalse(FileUtils.isValidYamlExtension("yml1"));
106 assertFalse(FileUtils.isValidYamlExtension("zip"));
110 public void testGetFileWithoutExtention() {
111 Assert.assertEquals("test", FileUtils.getFileWithoutExtention("test.txt"));
115 public void testGetFileWithoutExtentionContainsNoExtension() {
116 Assert.assertEquals("test", FileUtils.getFileWithoutExtention("test"));
120 public void testGetFileExtention() {
121 Assert.assertEquals("txt", FileUtils.getFileExtension("test.txt"));
125 public void testGetNetworkPackageName() {
126 Assert.assertEquals("heat", FileUtils.getNetworkPackageName("heat.zip"));
130 public void testGetNetworkPackageNameWithoutExtension() {
131 Assert.assertNull(FileUtils.getNetworkPackageName("heat"));
135 public void testToByteArrayNullStream() {
136 Assert.assertNotNull(FileUtils.toByteArray(null));
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);
147 public void testConvertToBytesNullObject() {
148 Assert.assertNotNull(FileUtils.convertToBytes(null, null));
152 public void testConvertToBytes() {
153 byte[] bytesArray = FileUtils.convertToBytes(Stream.of("Json", "Util", "Test").collect(Collectors.toList()),
154 FileUtils.FileExtension.YAML);
156 Assert.assertNotNull(bytesArray);
160 public void testConvertToBytesNotYaml() {
161 byte[] bytesArray = FileUtils.convertToBytes(Stream.of("Json", "Util", "Test").collect(Collectors.toList()),
162 FileUtils.FileExtension.JSON);
164 Assert.assertNotNull(bytesArray);
168 public void testConvertToInputStreamNullObject() {
169 Assert.assertNull(FileUtils.convertToInputStream(null, null));
173 public void testConvertToInputStream() {
174 InputStream inputStream = FileUtils.convertToInputStream(Stream.of("Json", "Util", "Test")
175 .collect(Collectors.toList()), FileUtils.FileExtension.YAML);
177 Assert.assertNotNull(inputStream);
180 @Test(expected = RuntimeException.class)
181 public void testLoadFileToInputStreamIncorrectFilePath() {
182 FileUtils.loadFileToInputStream("invalidfilepath");
186 public void testLoadFileToInputStream() throws IOException{
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);
195 Assert.assertNotNull(inputStream);
196 Assert.assertEquals("hello-test", builder.toString());