aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-core-lib
diff options
context:
space:
mode:
authorvempo <vitaliy.emporopulo@amdocs.com>2017-10-24 11:20:03 +0300
committervempo <vitaliy.emporopulo@amdocs.com>2017-10-24 11:24:03 +0300
commit43645dccbaced7a45b37f9e05221186231c39d74 (patch)
tree4e503cc762adf04d95366e08955c5ba5bd4b8c59 /openecomp-be/lib/openecomp-core-lib
parentaa61644704edc4b2ef231d75b537b38d0ae6d7c6 (diff)
Fixed resources not being closed in tests
Fixed static analysis violations in a few modules of SDC onboarding - high-severity issues like not releasing resources (e.g. FileInputStream). Did some minor code cleanup. Change-Id: I89a229ad6bc150951f1f3cc437b3a175a663e203 Issue-ID: SDC-291 Signed-off-by: vempo <vitaliy.emporopulo@amdocs.com>
Diffstat (limited to 'openecomp-be/lib/openecomp-core-lib')
-rw-r--r--openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileContentHandler.java52
-rw-r--r--openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file/FileContentHandlerTest.java53
2 files changed, 101 insertions, 4 deletions
diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileContentHandler.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileContentHandler.java
index 8de222e688..d5cd56e058 100644
--- a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileContentHandler.java
+++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileContentHandler.java
@@ -23,10 +23,13 @@ package org.openecomp.core.utilities.file;
import org.apache.commons.collections4.MapUtils;
import java.io.ByteArrayInputStream;
+import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
+import java.util.Optional;
import java.util.Set;
+import java.util.function.Function;
public class FileContentHandler {
@@ -45,12 +48,31 @@ public class FileContentHandler {
return null;
}
- ByteArrayInputStream is = new ByteArrayInputStream(content);
- return is;
+ return new ByteArrayInputStream(content);
}
- public void addFile(String fileName, byte[] contect) {
- files.put(fileName, contect);
+ /**
+ * Applies a business logic to a file's content while taking care of all retrieval logic.
+ *
+ * @param fileName name of a file inside this content handler.
+ * @param processor the business logic to work on the file's input stream, which may not be set
+ * (check the {@link Optional} if no such file can be found
+ * @param <T> return type, may be {@link java.lang.Void}
+ *
+ * @return result produced by the processor
+ */
+ public <T> T processFileContent(String fileName, Function<Optional<InputStream>, T> processor) {
+
+ // do not throw IOException to mimic the existing uses of getFileContent()
+ try (InputStream contentInputStream = getFileContent(fileName)) {
+ return processor.apply(Optional.ofNullable(contentInputStream));
+ } catch (IOException e) {
+ throw new ProcessingException("Failed to process file: " + fileName, e);
+ }
+ }
+
+ public void addFile(String fileName, byte[] content) {
+ files.put(fileName, content);
}
public void addFile(String fileName, InputStream is) {
@@ -94,4 +116,26 @@ public class FileContentHandler {
public boolean containsFile(String fileName) {
return files.containsKey(fileName);
}
+
+ /**
+ * An application-specific runtime exception
+ */
+ private static class ProcessingException extends RuntimeException {
+
+ public ProcessingException() {
+ super();
+ }
+
+ public ProcessingException(String message) {
+ super(message);
+ }
+
+ public ProcessingException(Throwable cause) {
+ super(cause);
+ }
+
+ public ProcessingException(String msg, Throwable cause) {
+ super(msg, cause);
+ }
+ }
}
diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file/FileContentHandlerTest.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file/FileContentHandlerTest.java
new file mode 100644
index 0000000000..527ba22c1d
--- /dev/null
+++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file/FileContentHandlerTest.java
@@ -0,0 +1,53 @@
+package org.openecomp.core.utilities.file;
+
+import org.testng.annotations.Test;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Optional;
+
+import static org.testng.Assert.*;
+
+/**
+ * @author EVITALIY
+ * @since 24 Oct 17
+ */
+public class FileContentHandlerTest {
+
+ private static final String FILE_NAME = "test-file.txt";
+
+ @Test
+ public void testProcessFileContent() throws Exception {
+
+ final int size = 13;
+ FileContentHandler contentHandler = new FileContentHandler();
+ final byte[] content = new byte[size];
+ Arrays.fill(content, (byte) 44);
+ contentHandler.addFile(FILE_NAME, content);
+ assertEquals(contentHandler.processFileContent(FILE_NAME, optional -> {
+
+ try {
+ byte[] buffer = new byte[size];
+ assertTrue(optional.isPresent());
+ assertEquals(size, optional.get().read(buffer));
+ return buffer;
+ } catch (IOException e) {
+ throw new RuntimeException("Unexpected error", e);
+ }
+
+ }), content);
+ }
+
+ @Test
+ public void testProcessEmptyFileContent() throws Exception {
+ FileContentHandler contentHandler = new FileContentHandler();
+ contentHandler.addFile(FILE_NAME, new byte[0]);
+ assertFalse(contentHandler.processFileContent(FILE_NAME, Optional::isPresent));
+ }
+
+ @Test
+ public void testProcessNoFileContent() throws Exception {
+ FileContentHandler contentHandler = new FileContentHandler();
+ assertFalse(contentHandler.processFileContent("filename", Optional::isPresent));
+ }
+} \ No newline at end of file