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 org.apache.commons.io.IOUtils;
20 import org.testng.annotations.Test;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
28 import java.util.function.Function;
30 import static org.testng.Assert.*;
36 public class FileUtilsTest {
38 private static final String TEST_RESOURCE = FileUtilsTest.class.getPackage().getName()
39 .replace('.', '/') + "/test-resource.txt";
41 private static final Function<InputStream, Integer> TEST_FUNCTION = (s) -> {
45 } catch (IOException e) {
46 throw new RuntimeException(e);
51 public void testReadViaInputStreamWithSlash() throws Exception {
52 assertTrue(FileUtils.readViaInputStream(TEST_RESOURCE, TEST_FUNCTION) > 0);
56 public void testReadViaInputStreamWithoutSlash() throws Exception {
57 assertTrue(FileUtils.readViaInputStream(TEST_RESOURCE, TEST_FUNCTION) > 0);
60 @Test(expectedExceptions = NullPointerException.class)
61 public void testReadViaInputStreamNull() throws Exception {
62 FileUtils.readViaInputStream((String) null, TEST_FUNCTION);
65 @Test(expectedExceptions = IllegalArgumentException.class)
66 public void testReadViaInputStreamNotFound() throws Exception {
67 FileUtils.readViaInputStream("notfound.txt", TEST_FUNCTION);
71 public void testWriteFilesFromFileContentHandler() throws IOException {
72 Path dir = Files.createTempDirectory("CSAR_" + System.currentTimeMillis());
74 byte[] uploadedFileData = IOUtils.toByteArray(
75 FileUtilsTest.class.getResource("resource-Spgw-csar-ZTE" +
77 FileContentHandler contentMap = FileUtils.getFileContentMapFromZip(uploadedFileData);
78 Map<String, String> filePaths = FileUtils.writeFilesFromFileContentHandler(contentMap,
81 assertFalse(filePaths.isEmpty());
82 assertEquals(filePaths.size(), 18);
83 for (Map.Entry<String, String> fileEntry : filePaths.entrySet()) {
84 File f = new File(fileEntry.getValue());
85 assertTrue(f.exists());
89 org.apache.commons.io.FileUtils.deleteDirectory(dir.toFile());
94 public void testIsValidYamlExtension() throws IOException {
95 assertTrue(FileUtils.isValidYamlExtension("yaml"));
96 assertTrue(FileUtils.isValidYamlExtension("yml"));
97 assertFalse(FileUtils.isValidYamlExtension("yml1"));
98 assertFalse(FileUtils.isValidYamlExtension("zip"));