More tests added 91/105091/3
authorDmitry Puzikov <d.puzikov2@partner.samsung.com>
Fri, 3 Apr 2020 15:40:28 +0000 (17:40 +0200)
committerOfir Sonsino <ofir.sonsino@intl.att.com>
Tue, 7 Apr 2020 09:52:41 +0000 (09:52 +0000)
Change-Id: I4b6ec51eb13d3553b308f794fb22b7761993f0ab
Issue-ID: SDC-2869
Signed-off-by: Dmitry Puzikov <d.puzikov2@partner.samsung.com>
openecomp-be/lib/openecomp-common-lib/src/test/java/org/openecomp/sdc/common/utils/CommonUtilTest.java
openecomp-be/lib/openecomp-common-lib/src/test/resources/valid.csar [new file with mode: 0644]
openecomp-be/lib/openecomp-common-lib/src/test/resources/valid.zip [new file with mode: 0644]
openecomp-be/lib/openecomp-common-lib/src/test/resources/valid_zip_with_dir.zip [new file with mode: 0644]
openecomp-be/lib/openecomp-common-lib/src/test/resources/valid_zip_with_not_yaml_file.zip [new file with mode: 0644]

index 119616a..e08238a 100644 (file)
 
 package org.openecomp.sdc.common.utils;
 
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.fail;
 import static org.onap.sdc.tosca.services.CommonUtil.DEFAULT;
 import static org.onap.sdc.tosca.services.CommonUtil.UNDERSCORE_DEFAULT;
 import static org.testng.Assert.assertTrue;
 
+import com.google.common.io.Files;
+import java.io.File;
+import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
+import org.openecomp.core.utilities.file.FileContentHandler;
+import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
+import org.openecomp.sdc.common.errors.CoreException;
+import org.openecomp.sdc.common.zip.exception.ZipException;
 import org.testng.annotations.Test;
 
 public class CommonUtilTest {
+    private static final String VALID_ZIP_FILE_PATH = "src/test/resources/valid.zip";
+    private static final String VALID_CSAR_FILE_PATH = "src/test/resources/valid.csar";
+    private static final String VALID_ZIP_WITH_NOT_YAML_FILE_PATH = "src/test/resources/valid_zip_with_not_yaml_file.zip";
+    private static final String VALID_ZIP_WITH_DIR_FILE_PATH = "src/test/resources/valid_zip_with_dir.zip";
 
     @Test
     public void testGetObjectAsMap() {
@@ -33,4 +48,76 @@ public class CommonUtilTest {
         assertTrue(CommonUtil.getObjectAsMap(obj).containsKey(UNDERSCORE_DEFAULT));
     }
 
+    @Test
+    public void testValidateAndUploadFileContentZip() throws IOException {
+        byte[] file = getFileAsBytes(VALID_ZIP_FILE_PATH);
+
+        FileContentHandler fch = CommonUtil.validateAndUploadFileContent(OnboardingTypesEnum.ZIP, file);
+
+        assertThat(fch, notNullValue(FileContentHandler.class));
+        assertThat(fch.containsFile("file.one.yaml"), is(true));
+        assertThat(fch.containsFile("file.two.yaml"), is(true));
+    }
+
+    @Test
+    public void testValidateAndUploadFileContentCsar() throws IOException {
+        byte[] file = getFileAsBytes(VALID_CSAR_FILE_PATH);
+
+        FileContentHandler fch = CommonUtil.validateAndUploadFileContent(OnboardingTypesEnum.CSAR, file);
+
+        assertThat(fch, notNullValue(FileContentHandler.class));
+        assertThat(fch.containsFile("file.one.yaml"), is(true));
+        assertThat(fch.containsFile("file.two.yaml"), is(true));
+    }
+
+    @Test(expectedExceptions={CoreException.class})
+    public void testValidateNoFolders() throws IOException {
+        byte[] file = getFileAsBytes(VALID_ZIP_WITH_DIR_FILE_PATH);
+
+        FileContentHandler fch = CommonUtil.validateAndUploadFileContent(OnboardingTypesEnum.ZIP, file);
+
+        fail("Should throw CoreException");
+    }
+
+    @Test
+    public void testGetZipContent() throws ZipException {
+        byte[] file = getFileAsBytes(VALID_ZIP_WITH_DIR_FILE_PATH);
+
+        FileContentHandler fch = CommonUtil.getZipContent(file);
+
+        assertThat(fch, notNullValue(FileContentHandler.class));
+        assertThat(fch.getFileList().size(), is(2));
+        assertThat(fch.getFolderList().size(), is(1));
+    }
+
+    @Test
+    public void testValidateAllFilesYaml() throws ZipException {
+        byte[] file = getFileAsBytes(VALID_ZIP_WITH_DIR_FILE_PATH);
+        FileContentHandler fch = CommonUtil.getZipContent(file);
+
+        boolean result = CommonUtil.validateAllFilesYml(fch);
+
+        assertThat(result, is(true));
+    }
+
+    @Test
+    public void testValidateNotAllFilesYaml() throws ZipException {
+        byte[] file = getFileAsBytes(VALID_ZIP_WITH_NOT_YAML_FILE_PATH);
+        FileContentHandler fch = CommonUtil.getZipContent(file);
+
+        boolean result = CommonUtil.validateAllFilesYml(fch);
+
+        assertThat(result, is(false));
+    }
+
+    private byte[] getFileAsBytes(String fileName) {
+        byte[] data = null;
+        try {
+            File file = new File(fileName);
+            data = Files.toByteArray(file);
+        } catch (IOException e) {
+            fail("Couldn't read test file");
+        }
+        return data;
+    }
 }
diff --git a/openecomp-be/lib/openecomp-common-lib/src/test/resources/valid.csar b/openecomp-be/lib/openecomp-common-lib/src/test/resources/valid.csar
new file mode 100644 (file)
index 0000000..b74c27b
Binary files /dev/null and b/openecomp-be/lib/openecomp-common-lib/src/test/resources/valid.csar differ
diff --git a/openecomp-be/lib/openecomp-common-lib/src/test/resources/valid.zip b/openecomp-be/lib/openecomp-common-lib/src/test/resources/valid.zip
new file mode 100644 (file)
index 0000000..b74c27b
Binary files /dev/null and b/openecomp-be/lib/openecomp-common-lib/src/test/resources/valid.zip differ
diff --git a/openecomp-be/lib/openecomp-common-lib/src/test/resources/valid_zip_with_dir.zip b/openecomp-be/lib/openecomp-common-lib/src/test/resources/valid_zip_with_dir.zip
new file mode 100644 (file)
index 0000000..4fcc17d
Binary files /dev/null and b/openecomp-be/lib/openecomp-common-lib/src/test/resources/valid_zip_with_dir.zip differ
diff --git a/openecomp-be/lib/openecomp-common-lib/src/test/resources/valid_zip_with_not_yaml_file.zip b/openecomp-be/lib/openecomp-common-lib/src/test/resources/valid_zip_with_not_yaml_file.zip
new file mode 100644 (file)
index 0000000..aded0bf
Binary files /dev/null and b/openecomp-be/lib/openecomp-common-lib/src/test/resources/valid_zip_with_not_yaml_file.zip differ