aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib
diff options
context:
space:
mode:
Diffstat (limited to 'openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib')
-rw-r--r--openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java12
-rw-r--r--openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file/FileUtilsTest.java48
-rw-r--r--openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/resources/org/openecomp/core/utilities/file/test-resource.txt1
3 files changed, 60 insertions, 1 deletions
diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java
index 6ff213c34c..6ab3f8b049 100644
--- a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java
+++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java
@@ -28,6 +28,7 @@ import org.openecomp.sdc.logging.api.LoggerFactory;
import org.openecomp.sdc.tosca.services.YamlUtil;
import java.io.ByteArrayInputStream;
+import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
@@ -54,7 +55,16 @@ public class FileUtils {
* @param function logic to be applied to the input stream
*/
public static <T> T readViaInputStream(String fileName, Function<InputStream, T> function) {
- return readViaInputStream(FileUtils.class.getClassLoader().getResource(fileName), function);
+
+ Objects.requireNonNull(fileName);
+
+ // the leading slash doesn't make sense and doesn't work when used with a class loader
+ URL resource = FileUtils.class.getClassLoader().getResource(fileName.startsWith("/") ? fileName.substring(1) : fileName);
+ if (resource == null) {
+ throw new IllegalArgumentException("Resource not found: " + fileName);
+ }
+
+ return readViaInputStream(resource, function);
}
/**
diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file/FileUtilsTest.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file/FileUtilsTest.java
new file mode 100644
index 0000000000..e32aa39904
--- /dev/null
+++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file/FileUtilsTest.java
@@ -0,0 +1,48 @@
+package org.openecomp.core.utilities.file;
+
+import org.testng.annotations.Test;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.function.Function;
+
+import static org.testng.Assert.assertTrue;
+
+/**
+ * @author EVITALIY
+ * @since 22 Oct 17
+ */
+public class FileUtilsTest {
+
+ private static final String TEST_RESOURCE = FileUtilsTest.class.getPackage().getName()
+ .replace('.', '/') + "/test-resource.txt";
+
+ private static final Function<InputStream, Integer> TEST_FUNCTION = (s) -> {
+
+ try {
+ return s.available();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ };
+
+ @Test
+ public void testReadViaInputStreamWithSlash() throws Exception {
+ assertTrue(FileUtils.readViaInputStream(TEST_RESOURCE, TEST_FUNCTION) > 0);
+ }
+
+ @Test
+ public void testReadViaInputStreamWithoutSlash() throws Exception {
+ assertTrue(FileUtils.readViaInputStream(TEST_RESOURCE, TEST_FUNCTION) > 0);
+ }
+
+ @Test(expectedExceptions = NullPointerException.class)
+ public void testReadViaInputStreamNull() throws Exception {
+ FileUtils.readViaInputStream((String) null, TEST_FUNCTION);
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testReadViaInputStreamNotFound() throws Exception {
+ FileUtils.readViaInputStream("notfound.txt", TEST_FUNCTION);
+ }
+} \ No newline at end of file
diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/resources/org/openecomp/core/utilities/file/test-resource.txt b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/resources/org/openecomp/core/utilities/file/test-resource.txt
new file mode 100644
index 0000000000..dce400c8a5
--- /dev/null
+++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/resources/org/openecomp/core/utilities/file/test-resource.txt
@@ -0,0 +1 @@
+CLASSPATH RESOURCE \ No newline at end of file