Centralize onboarding package validation
[sdc.git] / openecomp-be / lib / openecomp-core-lib / openecomp-utilities-lib / src / test / java / org / openecomp / core / utilities / file / FileContentHandlerTest.java
index 0c767a7..77ada19 100644 (file)
 
 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);
+        }
     }
 }