diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-common-lib/src/test/java/org/openecomp')
-rw-r--r-- | openecomp-be/lib/openecomp-common-lib/src/test/java/org/openecomp/sdc/common/utils/CommonUtilTest.java | 87 |
1 files changed, 87 insertions, 0 deletions
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; + } } |