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.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
21 import org.testng.annotations.Test;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
29 import java.util.function.Function;
31 import static org.testng.Assert.assertTrue;
32 import static org.testng.Assert.assertFalse;
33 import static org.testng.Assert.assertEquals;
39 public class FileUtilsTest {
41 private static final String TEST_RESOURCE = FileUtilsTest.class.getPackage().getName()
42 .replace('.', '/') + "/test-resource.txt";
44 private static final Function<InputStream, Integer> TEST_FUNCTION = (s) -> {
48 } catch (IOException e) {
49 throw new RuntimeException(e);
54 public void testReadViaInputStreamWithSlash() throws Exception {
55 assertTrue(FileUtils.readViaInputStream(TEST_RESOURCE, TEST_FUNCTION) > 0);
59 public void testReadViaInputStreamWithoutSlash() throws Exception {
60 assertTrue(FileUtils.readViaInputStream(TEST_RESOURCE, TEST_FUNCTION) > 0);
63 @Test(expectedExceptions = NullPointerException.class)
64 public void testReadViaInputStreamNull() throws Exception {
65 FileUtils.readViaInputStream((String) null, TEST_FUNCTION);
68 @Test(expectedExceptions = IllegalArgumentException.class)
69 public void testReadViaInputStreamNotFound() throws Exception {
70 FileUtils.readViaInputStream("notfound.txt", TEST_FUNCTION);
74 public void testWriteFilesFromFileContentHandler() throws IOException {
75 Path dir = Files.createTempDirectory("CSAR_" + System.currentTimeMillis());
77 byte[] uploadedFileData = IOUtils.toByteArray(
78 FileUtilsTest.class.getResource("resource-Spgw-csar-ZTE" +
80 FileContentHandler contentMap = FileUtils.getFileContentMapFromZip(uploadedFileData);
81 Map<String, String> filePaths = FileUtils.writeFilesFromFileContentHandler(contentMap,
84 assertFalse(filePaths.isEmpty());
85 assertEquals(filePaths.size(), 18);
86 for (Map.Entry<String, String> fileEntry : filePaths.entrySet()) {
87 File f = new File(fileEntry.getValue());
88 assertTrue(f.exists());
92 org.apache.commons.io.FileUtils.deleteDirectory(dir.toFile());
97 public void testIsValidYamlExtension() throws IOException {
98 assertTrue(FileUtils.isValidYamlExtension("yaml"));
99 assertTrue(FileUtils.isValidYamlExtension("yml"));
100 assertFalse(FileUtils.isValidYamlExtension("yml1"));
101 assertFalse(FileUtils.isValidYamlExtension("zip"));