From 2ea2a56aed979ea94cf65f7fefdae09d1f95506b Mon Sep 17 00:00:00 2001 From: Dmitry Puzikov Date: Fri, 3 Apr 2020 17:40:28 +0200 Subject: More tests added Change-Id: I4b6ec51eb13d3553b308f794fb22b7761993f0ab Issue-ID: SDC-2869 Signed-off-by: Dmitry Puzikov --- .../openecomp/sdc/common/utils/CommonUtilTest.java | 87 +++++++++++++++++++++ .../src/test/resources/valid.csar | Bin 0 -> 330 bytes .../src/test/resources/valid.zip | Bin 0 -> 330 bytes .../src/test/resources/valid_zip_with_dir.zip | Bin 0 -> 474 bytes .../resources/valid_zip_with_not_yaml_file.zip | Bin 0 -> 490 bytes 5 files changed, 87 insertions(+) create mode 100644 openecomp-be/lib/openecomp-common-lib/src/test/resources/valid.csar create mode 100644 openecomp-be/lib/openecomp-common-lib/src/test/resources/valid.zip create mode 100644 openecomp-be/lib/openecomp-common-lib/src/test/resources/valid_zip_with_dir.zip create mode 100644 openecomp-be/lib/openecomp-common-lib/src/test/resources/valid_zip_with_not_yaml_file.zip (limited to 'openecomp-be/lib') diff --git a/openecomp-be/lib/openecomp-common-lib/src/test/java/org/openecomp/sdc/common/utils/CommonUtilTest.java b/openecomp-be/lib/openecomp-common-lib/src/test/java/org/openecomp/sdc/common/utils/CommonUtilTest.java index 119616a9be..e08238aa1f 100644 --- a/openecomp-be/lib/openecomp-common-lib/src/test/java/org/openecomp/sdc/common/utils/CommonUtilTest.java +++ b/openecomp-be/lib/openecomp-common-lib/src/test/java/org/openecomp/sdc/common/utils/CommonUtilTest.java @@ -16,15 +16,30 @@ 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 index 0000000000..b74c27b3ef 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 index 0000000000..b74c27b3ef 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 index 0000000000..4fcc17d3ce 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 index 0000000000..aded0bfba6 Binary files /dev/null and b/openecomp-be/lib/openecomp-common-lib/src/test/resources/valid_zip_with_not_yaml_file.zip differ -- cgit 1.2.3-korg