From 43645dccbaced7a45b37f9e05221186231c39d74 Mon Sep 17 00:00:00 2001 From: vempo Date: Tue, 24 Oct 2017 11:20:03 +0300 Subject: 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 --- .../utilities/file/FileContentHandlerTest.java | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file/FileContentHandlerTest.java (limited to 'openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities') 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 -- cgit 1.2.3-korg