diff options
author | 2019-09-19 16:14:01 +0100 | |
---|---|---|
committer | 2019-11-03 15:41:37 +0000 | |
commit | 433947b5ab5e28fc29aee447de934de89a707419 (patch) | |
tree | a485b95b2ae7716ced4825fb7b9eb2b6eeb3433b /openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file | |
parent | ee64a64fb0705422c18608304e63a505d10d8ba1 (diff) |
Centralize onboarding package validation
Change-Id: I3cc58cf15f62008e83cfc7ddb095d07ab216b82a
Issue-ID: SDC-2583
Signed-off-by: andre.schmid <andre.schmid@est.tech>
Diffstat (limited to 'openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file')
-rw-r--r-- | openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file/FileContentHandlerTest.java | 107 |
1 files changed, 68 insertions, 39 deletions
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 index 0c767a7919..77ada193b3 100644 --- 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 @@ -16,16 +16,23 @@ package org.openecomp.core.utilities.file; +import static org.hamcrest.Matchers.aMapWithSize; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.hasSize; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.InputStream; import java.util.AbstractMap; import java.util.Arrays; import java.util.Map; import java.util.Optional; +import java.util.Set; +import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; import org.junit.Assert; @@ -48,7 +55,7 @@ public class FileContentHandlerTest { Arrays.fill(content, (byte) 44); contentHandler.addFile(FILE_NAME, content); - byte[] actualContent = contentHandler.processFileContent(FILE_NAME, optional -> { + byte[] actualContent = processFileContent(FILE_NAME, optional -> { try { byte[] buffer = new byte[size]; @@ -59,7 +66,7 @@ public class FileContentHandlerTest { throw new RuntimeException("Unexpected error", e); } - }); + }, contentHandler); Assert.assertTrue(Arrays.equals(actualContent, content)); } @@ -67,13 +74,13 @@ public class FileContentHandlerTest { public void testProcessEmptyFileContent() { FileContentHandler contentHandler = new FileContentHandler(); contentHandler.addFile(FILE_NAME, new byte[0]); - assertFalse(contentHandler.processFileContent(FILE_NAME, Optional::isPresent)); + assertFalse(processFileContent(FILE_NAME, Optional::isPresent, contentHandler)); } @Test public void testProcessNoFileContent() { FileContentHandler contentHandler = new FileContentHandler(); - assertFalse(contentHandler.processFileContent("filename", Optional::isPresent)); + assertFalse(processFileContent("filename", Optional::isPresent, contentHandler)); } @Test @@ -88,47 +95,69 @@ public class FileContentHandlerTest { @Test public void testSetFiles() { - FileContentHandler contentHandler = new FileContentHandler(); - Map<String, byte[]> fileMap = Stream.of(new AbstractMap.SimpleEntry<>("file1", new byte[0]), - new AbstractMap.SimpleEntry<>("file2", new byte[0])) - .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); - - contentHandler.setFiles(fileMap); - - Assert.assertEquals(contentHandler.getFiles().size(), 2); - Assert.assertEquals(contentHandler.getFileList().size(), 2); - assertFalse(contentHandler.isEmpty()); - contentHandler.remove("file1"); - assertFalse(contentHandler.containsFile("file1")); - } - - @Test - public void testAddAll() { - FileContentHandler contentHandler = new FileContentHandler(); - FileContentHandler contentHandler1 = createFileHandlerContent(); - - contentHandler.addAll(contentHandler1); - - Assert.assertTrue(contentHandler1.containsFile("file1")); - Assert.assertEquals(contentHandler.getFiles().size(), 2); + //given + final FileContentHandler expectedFileContentHandler = createFileContentHandler(); + //when + final FileContentHandler actualContentHandler = new FileContentHandler(); + actualContentHandler.setFiles(expectedFileContentHandler.getFiles()); + + //then + final Map<String, byte[]> actualFileMap = actualContentHandler.getFiles(); + assertThat("Should contain the expected number of folders", actualContentHandler.getFolderList(), hasSize(0)); + assertThat("Should contain the expected number of files", actualFileMap, aMapWithSize(2)); + expectedFileContentHandler.getFiles().keySet().forEach(filePath -> { + assertThat("Should contain the expected file", actualFileMap.keySet(), hasItem(filePath)); + }); } @Test - public void testSetFilesUsingFIleContentHandlerObject() { - FileContentHandler contentHandler1 = createFileHandlerContent(); - - FileContentHandler contentHandler = new FileContentHandler(); - contentHandler.setFiles(contentHandler1); - - Assert.assertEquals(contentHandler.getFiles().size(), 2); + public void testAddAllFromFileContentHandler() { + //given + final FileContentHandler expectedFileContentHandler = createFileContentHandler(); + //when + final FileContentHandler actualContentHandler = new FileContentHandler(); + actualContentHandler.addAll(expectedFileContentHandler); + //then + final Map<String, byte[]> actualFileMap = actualContentHandler.getFiles(); + assertThat("Should contain the expected number of files", actualFileMap, aMapWithSize(2)); + final Set<String> actualFolderList = actualContentHandler.getFolderList(); + assertThat("Should contain the expected number of folders", actualFolderList, hasSize(3)); + expectedFileContentHandler.getFiles().keySet().forEach(filePath -> { + assertThat("Should contain the expected file", actualFileMap.keySet(), hasItem(filePath)); + }); + expectedFileContentHandler.getFolderList().forEach(folderPath -> { + assertThat("Should contain the expected file", actualFolderList, hasItem(folderPath)); + }); } - private FileContentHandler createFileHandlerContent() { - FileContentHandler contentHandler1 = new FileContentHandler(); - Map<String, byte[]> fileMap = Stream.of(new AbstractMap.SimpleEntry<>("file1", new byte[0]), + private FileContentHandler createFileContentHandler() { + final FileContentHandler contentHandler = new FileContentHandler(); + final Map<String, byte[]> fileMap = Stream.of(new AbstractMap.SimpleEntry<>("file1", new byte[0]), new AbstractMap.SimpleEntry<>("file2", new byte[0])) .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); - contentHandler1.putAll(fileMap); - return contentHandler1; + contentHandler.setFiles(fileMap); + contentHandler.addFolder("folder1"); + contentHandler.addFolder("folder1/folder2"); + contentHandler.addFolder("folder3"); + return contentHandler; + } + + /** + * 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, FileContentHandler contentHandler) { + + // do not throw IOException to mimic the existing uses of getFileContent() + try (InputStream contentInputStream = contentHandler.getFileContentAsStream(fileName)) { + return processor.apply(Optional.ofNullable(contentInputStream)); + } catch (IOException e) { + throw new RuntimeException("Failed to process file: " + fileName, e); + } } } |