From 0a1d82ac04a8ef78bfdcbcced4f5096c050edcfe Mon Sep 17 00:00:00 2001 From: "bilal.iqbal" Date: Sun, 10 Mar 2019 00:47:55 +0000 Subject: CSAR Package validation Change-Id: I11af8d93f5a2cd0566a5caf0dad0519d70bd57d7 Issue-ID: SDC-2147 Issue-ID: SDC-2148 Issue-ID: SDC-2149 Issue-ID: SDC-2150 Signed-off-by: bilal.iqbal --- .../csar/validation/ONAPCsarValidatorTest.java | 73 ++++ .../SOL004MetaDirectoryValidatorTest.java | 405 +++++++++++++++++++++ .../csar/validation/TestConstants.java | 25 ++ .../csar/validation/ValidatorFactoryTest.java | 88 +++++ .../csar/validation/ValidatorUtil.java | 22 ++ 5 files changed, 613 insertions(+) create mode 100644 openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ONAPCsarValidatorTest.java create mode 100644 openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/SOL004MetaDirectoryValidatorTest.java create mode 100644 openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/TestConstants.java create mode 100644 openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidatorFactoryTest.java create mode 100644 openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidatorUtil.java (limited to 'openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org') diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ONAPCsarValidatorTest.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ONAPCsarValidatorTest.java new file mode 100644 index 0000000000..bedf599cf5 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ONAPCsarValidatorTest.java @@ -0,0 +1,73 @@ +package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation; + +import org.junit.Before; +import org.junit.Test; +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.sdc.common.utils.SdcCommon; +import org.openecomp.sdc.datatypes.error.ErrorMessage; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertTrue; + + +public class ONAPCsarValidatorTest { + + ONAPCsarValidator onapCsarValidator; + private FileContentHandler contentHandler; + private List folderList; + + @Before + public void setUp() throws IOException{ + onapCsarValidator = new ONAPCsarValidator(); + contentHandler = new FileContentHandler(); + folderList = new ArrayList<>(); + + contentHandler.addFile("TOSCA-Metadata/TOSCA.meta", ValidatorUtil.getFileResource("/validation.files/metafile/nonSOL004WithMetaDirectoryCompliantMetaFile.meta")); + contentHandler.addFile("MainServiceTemplate.mf", ValidatorUtil.getFileResource("/validation.files/manifest/sampleManifest.mf")); + contentHandler.addFile(TestConstants.TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(TestConstants.SAMPLE_DEFINITION_FILE_PATH)); + } + + @Test + public void testGivenCSARPackage_withValidContent_thenNoErrorsReturned() { + assertExpectedErrors("Valid CSAR Package should have 0 errors", + onapCsarValidator.validateContent(contentHandler, folderList), 0); + } + + @Test + public void testGivenCSARPackage_withInvalidManifestFile_thenErrorsReturned() throws IOException{ + contentHandler = new FileContentHandler(); + contentHandler.addFile("TOSCA-Metadata/TOSCA.meta", ValidatorUtil.getFileResource("/validation.files/metafile/nonSOL004WithMetaDirectoryCompliantMetaFile.meta")); + contentHandler.addFile("MainServiceTemplate.mf", ValidatorUtil.getFileResource("/validation.files/manifest/invalidManifest.mf")); + contentHandler.addFile(TestConstants.TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(TestConstants.SAMPLE_DEFINITION_FILE_PATH)); + + assertExpectedErrors("CSAR package with invalid manifest file should have errors", onapCsarValidator.validateContent(contentHandler, folderList), 1); + + } + + @Test + public void testGivenCSARPackage_withUnwantedFolders_thenErrorsReturned(){ + + folderList.add("Files/"); + assertExpectedErrors("CSAR package with unwanted folders should fail with errors", onapCsarValidator.validateContent(contentHandler, folderList), 1); + } + + @Test + public void testGivenCSARPackage_withUnwantedFiles_thenErrorsReturned(){ + + contentHandler.addFile("ExtraFile.text", "".getBytes()); + assertExpectedErrors("CSAR package with unwanted files should fail with errors", + onapCsarValidator.validateContent(contentHandler, folderList), 1); + } + + private void assertExpectedErrors( String testCase, Map> errors, int expectedErrors){ + if(expectedErrors > 0){ + List errorMessages = errors.get(SdcCommon.UPLOAD_FILE); + assertTrue(testCase, errorMessages.size() == expectedErrors); + }else{ + assertTrue(testCase,errors.size() == expectedErrors); + } + } +} \ No newline at end of file diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/SOL004MetaDirectoryValidatorTest.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/SOL004MetaDirectoryValidatorTest.java new file mode 100644 index 0000000000..8584d482b4 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/SOL004MetaDirectoryValidatorTest.java @@ -0,0 +1,405 @@ +package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation; + +import org.junit.Before; +import org.junit.Test; +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.sdc.common.utils.SdcCommon; +import org.openecomp.sdc.datatypes.error.ErrorLevel; +import org.openecomp.sdc.datatypes.error.ErrorMessage; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertTrue; +import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.*; + +public class SOL004MetaDirectoryValidatorTest { + + private SOL004MetaDirectoryValidator sol004MetaDirectoryValidator; + private FileContentHandler handler; + private String metaFile; + + @Before + public void setUp(){ + sol004MetaDirectoryValidator = new SOL004MetaDirectoryValidator(); + handler = new FileContentHandler(); + metaFile = + "TOSCA-Meta-File-Version: 1.0\n"+ + "CSAR-Version: 1.1\n"+ + "Created-by: Vendor\n"+ + "Entry-Definitions: Definitions/MainServiceTemplate.yaml\n"+ + "Entry-Manifest: Definitions/MainServiceTemplate.mf\n"+ + "Entry-Change-Log: Artifacts/changeLog.text\n"; + } + + @Test + public void testGivenTOSCAMetaFile_whenEntryHasNoValue_thenErrorIsReturned() throws IOException{ + + String metaFileWithInvalidEntry = "TOSCA-Meta-File-Version: \n" + + "Entry-Definitions: Definitions/MainServiceTemplate.yaml"; + + handler.addFile(TOSCA_METADATA_FILEPATH, metaFileWithInvalidEntry.getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(TestConstants.SAMPLE_DEFINITION_FILE_PATH)); + + Map> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + List errorMessages = errors.get(SdcCommon.UPLOAD_FILE); + assertTrue(errors.size() == 1 && errorMessages.size() == 1); + } + + @Test + public void testGivenTOSCAMeta_withAllSupportedEntries_thenNoErrorsReturned() throws IOException{ + + String entryTestFilePath = "Files/Tests"; + String entryLicenseFilePath = "Files/Licenses"; + + List folderList = new ArrayList<>(); + folderList.add("Files/Tests/"); + folderList.add("Files/Licenses/"); + + metaFile = metaFile + + "Entry-Tests: "+ entryTestFilePath + "\n" + + "Entry-Licenses: "+ entryLicenseFilePath +"\n"; + + handler.addFile(TOSCA_METADATA_FILEPATH, metaFile.getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_MANIFEST_FILE_PATH)); + handler.addFile(SAMPLE_SOURCE, "".getBytes()); + handler.addFile(SAMPLE_DEFINITION_IMPORT_FILE_PATH, "".getBytes()); + handler.addFile(entryTestFilePath, "".getBytes()); + handler.addFile(entryLicenseFilePath, "".getBytes()); + + Map> errors = sol004MetaDirectoryValidator.validateContent(handler, folderList); + assertTrue(errors.size() == 0); + } + + @Test + public void testGivenTOSCAMeta_withUnsupportedEntry_thenWarningIsReturned(){ + + metaFile = "Entry-Events: Definitions/events.log"; + + handler.addFile(TOSCA_METADATA_FILEPATH, metaFile.getBytes(StandardCharsets.UTF_8)); + Map> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + List errorMessages = errors.get(SdcCommon.UPLOAD_FILE); + assertTrue(errors.size() == 1 && errorMessages.size() == 1); + assertTrue(errorMessages.get(0).getLevel() == ErrorLevel.ERROR); + + } + + @Test + public void testGivenTOSCAMetaFile_withInvalidTOSCAMetaFileVersionAndCSARVersion_thenErrorIsReturned() throws IOException{ + + String metaFile = + "TOSCA-Meta-File-Version: " + Integer.MAX_VALUE + + "\nCSAR-Version: " + Integer.MAX_VALUE + + "\nCreated-by: Bilal Iqbal" + + "\nEntry-Definitions: Definitions/MainServiceTemplate.yaml" + + "\nEntry-Manifest: Definitions/MainServiceTemplate.mf"+ + "\nEntry-Change-Log: Artifacts/changeLog.text"; + + handler.addFile(TOSCA_METADATA_FILEPATH, metaFile.getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(TestConstants.SAMPLE_DEFINITION_FILE_PATH)); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_MANIFEST_FILE_PATH)); + + Map> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + List errorMessages = errors.get(SdcCommon.UPLOAD_FILE); + assertTrue(errors.size() == 1 && errorMessages.size() == 2); + } + + @Test + public void testGivenTOSCAMetaFile_withNonExistentFileReferenced_thenErrorsReturned(){ + + metaFile = metaFile + + "Entry-Tests: Files/Tests\n" + + "Entry-License: Files/Licenses\n"; + + handler.addFile(TOSCA_METADATA_FILEPATH, metaFile.getBytes(StandardCharsets.UTF_8)); + + Map> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + List errorMessages = errors.get(SdcCommon.UPLOAD_FILE); + assertTrue(errors.size() == 1 && errorMessages.size() == 5); + } + + + @Test + public void testGivenDefinitionFile_whenValidImportStatementExist_thenNoErrorsReturned() throws IOException{ + + String definitionFileWithValidImports = "/validation.files/definition/definitionFileWithValidImports.yaml"; + + + handler.addFile(TOSCA_METADATA_FILEPATH, metaFile.getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_MANIFEST_FILE_PATH)); + handler.addFile(SAMPLE_SOURCE, "".getBytes()); + handler.addFile("Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.yaml", ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(definitionFileWithValidImports)); + + Map> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + assertTrue(errors.size() == 0); + } + + @Test + public void testGivenDefinitionFile_whenMultipleDefinitionsImportStatementExist_thenNoErrorsReturned() throws IOException{ + + byte [] sampleDefinitionFile1 = ValidatorUtil.getFileResource("/validation.files/definition/sampleDefinitionFile1.yaml"); + byte [] sampleDefinitionFile2 = ValidatorUtil.getFileResource("/validation.files/definition/sampleDefinitionFile2.yaml"); + byte [] sampleDefinitionFile3 = ValidatorUtil.getFileResource("/validation.files/definition/sampleDefinitionFile3.yaml"); + + handler.addFile(TOSCA_METADATA_FILEPATH, metaFile.getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_MANIFEST_FILE_PATH)); + handler.addFile(SAMPLE_SOURCE, "".getBytes()); + handler.addFile(TOSCA_DEFINITION_FILEPATH, sampleDefinitionFile1); + handler.addFile("Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.yaml", sampleDefinitionFile2); + handler.addFile("Definitions/etsi_nfv_sol001_pnfd_2_5_2_types.yaml", sampleDefinitionFile3); + + Map> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + assertTrue(errors.size() == 0); + } + + @Test + public void testGivenDefinitionFile_whenInvalidImportStatementExist_thenErrorIsReturned() throws IOException{ + + String definitionFileWithInvalidImports = "/validation.files/definition/definitionFileWithInvalidImport.yaml"; + + handler.addFile(TOSCA_METADATA_FILEPATH, metaFile.getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_MANIFEST_FILE_PATH)); + handler.addFile(SAMPLE_SOURCE, "".getBytes()); + handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(definitionFileWithInvalidImports)); + + Map> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + List errorMessages = errors.get(SdcCommon.UPLOAD_FILE); + assertTrue(errors.size() == 1 && errorMessages.size() == 1); + } + + @Test + public void testGivenDefinitionFile_whenReferencedImportDoesNotExist_thenErrorIsReturned() throws IOException{ + + handler.addFile(TOSCA_METADATA_FILEPATH, metaFile.getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_MANIFEST_FILE_PATH)); + handler.addFile(SAMPLE_SOURCE, "".getBytes()); + handler.addFile("Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.yaml", "".getBytes()); + handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource("/validation.files/definition/sampleDefinitionFile2.yaml")); + + Map> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + List errorMessages = errors.get(SdcCommon.UPLOAD_FILE); + assertTrue(errors.size() == 1 && errorMessages.size() == 1); + + } + + @Test + public void testGivenDefinitionFile_withInvalidYAML_thenErrorIsReturned() throws IOException{ + + String definitionFileWithInvalidYAML = "/validation.files/definition/invalidDefinitionFile.yaml"; + + handler.addFile(TOSCA_METADATA_FILEPATH, metaFile.getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_MANIFEST_FILE_PATH)); + handler.addFile(SAMPLE_SOURCE, "".getBytes()); + + handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(definitionFileWithInvalidYAML)); + + Map> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + List errorMessages = errors.get(SdcCommon.UPLOAD_FILE); + assertTrue(errors.size() == 1 && errorMessages.size() == 1); + } + + @Test + public void testGivenManifestFile_withValidSourceAndNonManoSources_thenNoErrorIsReturned() throws IOException{ + + String nonManoSource = "Artifacts/Deployment/Measurements/PM_Dictionary.yaml"; + + handler.addFile(TOSCA_METADATA_FILEPATH, metaFile.getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource("/validation.files/manifest/validManifest.mf")); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes()); + handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + handler.addFile(SAMPLE_SOURCE, "".getBytes()); + handler.addFile(SAMPLE_DEFINITION_IMPORT_FILE_PATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + handler.addFile(nonManoSource, "".getBytes()); + + Map> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + assertTrue(errors.size() == 0); + } + + @Test + public void testGivenManifestFile_withNonExistentSourceFile_thenErrorIsReturned() throws IOException{ + String nonManoSource = "Artifacts/Deployment/Measurements/PM_Dictionary.yaml"; + + handler.addFile(TOSCA_METADATA_FILEPATH, metaFile.getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource("/validation.files/manifest/validManifest.mf")); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes()); + handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + handler.addFile(SAMPLE_DEFINITION_IMPORT_FILE_PATH, "".getBytes()); + handler.addFile(nonManoSource, "".getBytes()); + + Map> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + List errorMessages = errors.get(SdcCommon.UPLOAD_FILE); + assertTrue(errors.size() == 1 && errorMessages.size() == 1); + } + + @Test + public void testGivenManifestFile_withInvalidData_thenErrorIsReturned() throws IOException{ + + handler.addFile(TOSCA_METADATA_FILEPATH, metaFile.getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource("/validation.files/manifest/invalidManifest.mf")); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes()); + handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + handler.addFile(SAMPLE_DEFINITION_IMPORT_FILE_PATH, "".getBytes()); + + Map> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + List errorMessages = errors.get(SdcCommon.UPLOAD_FILE); + assertTrue(errors.size() == 1 && errorMessages.size() == 1); + } + + @Test + public void testGivenManifestAndDefinitionFile_withSameNames_thenNoErrorReturned() throws IOException { + + handler.addFile(TOSCA_METADATA_FILEPATH, metaFile.getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource("/validation.files/manifest/sampleManifest.mf")); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes()); + handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + handler.addFile(SAMPLE_DEFINITION_IMPORT_FILE_PATH, "".getBytes()); + + Map> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + assertTrue(errors.size() == 0); + } + + @Test + public void testGivenManifestAndMainDefinitionFile_withDifferentNames_thenErrorIsReturned() throws IOException { + metaFile = + "TOSCA-Meta-File-Version: 1.0\n"+ + "CSAR-Version: 1.1\n"+ + "Created-by: Vendor\n"+ + "Entry-Definitions: Definitions/MainServiceTemplate.yaml\n"+ + "Entry-Manifest: Definitions/MainServiceTemplate2.mf\n"+ + "Entry-Change-Log: Artifacts/changeLog.text\n"; + + handler.addFile(TOSCA_METADATA_FILEPATH, metaFile.getBytes(StandardCharsets.UTF_8)); + handler.addFile("Definitions/MainServiceTemplate2.mf", ValidatorUtil.getFileResource("/validation.files/manifest/sampleManifest.mf")); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes()); + handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + handler.addFile(SAMPLE_DEFINITION_IMPORT_FILE_PATH, "".getBytes()); + + Map> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + assertExpectedErrors("Main TOSCA definitions file and Manifest file with different name should return error", + errors, 1); + } + + @Test + public void testGivenManifestFile_withDifferentExtension_thenErrorIsReturned() throws IOException { + metaFile = + "TOSCA-Meta-File-Version: 1.0\n"+ + "CSAR-Version: 1.1\n"+ + "Created-by: Vendor\n"+ + "Entry-Definitions: Definitions/MainServiceTemplate.yaml\n"+ + "Entry-Manifest: Definitions/MainServiceTemplate.txt\n"+ + "Entry-Change-Log: Artifacts/changeLog.text\n"; + + handler.addFile(TOSCA_METADATA_FILEPATH, metaFile.getBytes(StandardCharsets.UTF_8)); + handler.addFile("Definitions/MainServiceTemplate.txt", ValidatorUtil.getFileResource("/validation.files/manifest/sampleManifest.mf")); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes()); + handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + handler.addFile(SAMPLE_DEFINITION_IMPORT_FILE_PATH, "".getBytes()); + + Map> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + assertExpectedErrors("Manifest file with different extension than .mf should return error", + errors, 1); + } + + @Test + public void testGivenManifestFile_withValidVnfMetadata_thenNoErrorsReturned() throws IOException{ + handler.addFile(TOSCA_METADATA_FILEPATH, metaFile.getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource("/validation.files/manifest/sampleManifest.mf")); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes()); + handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + + Map> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + assertExpectedErrors("Manifest with valid vnf mandatory values should not return any errors", errors, 0); + } + + @Test + public void testGivenManifestFile_withValidPnfMetadata_thenNoErrorsReturned() throws IOException { + handler.addFile(TOSCA_METADATA_FILEPATH, metaFile.getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource("/validation.files/manifest/sampleManifest2.mf")); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes()); + handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + + Map> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + assertExpectedErrors("Manifest with valid pnf mandatory values should not return any errors", errors, 0); + } + + @Test + public void testGivenManifestFile_withMetadataContainingMixedPnfVnfMetadata_thenErrorIsReturned() throws IOException { + + handler.addFile(TOSCA_METADATA_FILEPATH, metaFile.getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource("/validation.files/manifest/manifestInvalidMetadata.mf")); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes()); + handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + + Map> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + assertExpectedErrors("Manifest with mixed metadata should return error", errors, 1); + } + + + @Test + public void testGivenManifestFile_withMetadataMissingPnfOrVnfMandatoryEntries_thenErrorIsReturned() throws IOException{ + + handler.addFile(TOSCA_METADATA_FILEPATH, metaFile.getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource("/validation.files/manifest/manifestInvalidMetadata2.mf")); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes()); + handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + + Map> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + assertExpectedErrors("Manifest with missing vnf or pnf mandatory entries should return error", errors, 1); + } + + @Test + public void testGivenManifestFile_withMetadataMissingMandatoryPnfEntries_thenErrorIsReturned() throws IOException{ + handler.addFile(TOSCA_METADATA_FILEPATH, metaFile.getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource("/validation.files/manifest/manifestInvalidMetadata4.mf")); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes()); + handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + + Map> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + assertExpectedErrors("Manifest with metadata missing pnf mandatory entries should return error", errors, 3); + + } + + @Test + public void testGivenManifestFile_withMetadataMissingMandatoryVnfEntries_thenErrorIsReturned() throws IOException{ + handler.addFile(TOSCA_METADATA_FILEPATH, metaFile.getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource("/validation.files/manifest/manifestInvalidMetadata5.mf")); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes()); + handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + + Map> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + assertExpectedErrors("Manifest with metadata missing vnf mandatory entries should return error", errors, 4); + + } + + @Test + public void testGivenManifestFile_withMetadataEntriesExceedingTheLimit_thenErrorIsReturned() throws IOException{ + handler.addFile(TOSCA_METADATA_FILEPATH, metaFile.getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource("/validation.files/manifest/manifestInvalidMetadata3.mf")); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes()); + handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + + Map> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + assertExpectedErrors("Manifest with more than 4 metadata entries should return error", errors, 2); + } + + private void assertExpectedErrors( String testCase, Map> errors, int expectedErrors){ + if(expectedErrors > 0){ + List errorMessages = errors.get(SdcCommon.UPLOAD_FILE); + assertTrue(testCase, errorMessages.size() == expectedErrors); + }else{ + assertTrue(testCase,errors.size() == expectedErrors); + } + } +} \ No newline at end of file diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/TestConstants.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/TestConstants.java new file mode 100644 index 0000000000..0a88ebc37d --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/TestConstants.java @@ -0,0 +1,25 @@ +package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation; + +class TestConstants { + + + public static final String SAMPLE_DEFINITION_IMPORT_FILE_PATH = "Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.yaml"; + public static final String SAMPLE_SOURCE = "Artifacts/Deployment/Events/RadioNode_pnf_v1.yaml"; + public static final String SAMPLE_DEFINITION_FILE_PATH = "/validation.files/definition/sampleDefinitionFile.yaml"; + public static final String SAMPLE_MANIFEST_FILE_PATH = "/validation.files/manifest/sampleManifest.mf"; + + public static final String ENTRY_DEFINITIONS = "Entry-Definitions"; + public static final String ENTRY_MANIFEST = "Entry-Manifest"; + public static final String ENTRY_CHANGE_LOG = "Entry-Change-Log"; + + public static final String ENTRY_SEPARATOR = ":"; + + public static final String TOSCA_DEFINITION_FILEPATH = "Definitions/MainServiceTemplate.yaml"; + public static final String TOSCA_MANIFEST_FILEPATH = "Definitions/MainServiceTemplate.mf"; + public static final String TOSCA_CHANGELOG_FILEPATH = "Artifacts/changeLog.text"; + public static final String TOSCA_METADATA_FILEPATH = "TOSCA-Metadata/TOSCA.meta"; + + private TestConstants(){ + + } +} diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidatorFactoryTest.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidatorFactoryTest.java new file mode 100644 index 0000000000..287662283c --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidatorFactoryTest.java @@ -0,0 +1,88 @@ +package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation; + +import org.junit.Before; +import org.junit.Test; +import org.openecomp.core.utilities.file.FileContentHandler; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +import static org.junit.Assert.assertEquals; +import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.ENTRY_CHANGE_LOG; +import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.ENTRY_DEFINITIONS; +import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.ENTRY_MANIFEST; +import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.ENTRY_SEPARATOR; +import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.TOSCA_CHANGELOG_FILEPATH; +import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.TOSCA_DEFINITION_FILEPATH; +import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.TOSCA_MANIFEST_FILEPATH; +import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.TOSCA_METADATA_FILEPATH; + +public class ValidatorFactoryTest { + + private String metaFile; + private FileContentHandler handler; + + @Before + public void setUp(){ + handler = new FileContentHandler(); + metaFile = + "TOSCA-Meta-File-Version: 1.0\n" + + "CSAR-Version: 1.1\n" + + "Created-by: Bilal Iqbal\n"; + } + + @Test(expected = IOException.class) + public void testGivenEmptyMetaFile_thenIOExceptionIsThrown() throws IOException{ + handler.addFile(TOSCA_METADATA_FILEPATH, "".getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_DEFINITION_FILEPATH, "".getBytes()); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_MANIFEST_FILEPATH, "".getBytes(StandardCharsets.UTF_8)); + + ValidatorFactory.getValidator(handler); + } + + @Test + public void testGivenEmptyBlock0_thenONAPCsarValidatorIsReturned() throws IOException{ + handler.addFile(TOSCA_METADATA_FILEPATH, " ".getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_DEFINITION_FILEPATH, "".getBytes()); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_MANIFEST_FILEPATH, "".getBytes(StandardCharsets.UTF_8)); + + assertEquals(ONAPCsarValidator.class, ValidatorFactory.getValidator(handler).getClass()); + } + + + @Test + public void testGivenNonSOL004MetaDirectoryCompliantMetaFile_thenONAPCSARValidatorIsReturned() throws IOException{ + metaFile = metaFile + + ENTRY_DEFINITIONS + ENTRY_SEPARATOR + TOSCA_DEFINITION_FILEPATH; + handler.addFile(TOSCA_METADATA_FILEPATH, metaFile.getBytes(StandardCharsets.UTF_8)); + + assertEquals(ONAPCsarValidator.class, ValidatorFactory.getValidator(handler).getClass()); + } + + @Test + public void testGivenSOL004MetaDirectoryCompliantMetafile_thenONAPCsarValidatorIsReturned() throws IOException{ + + metaFile = metaFile + + ENTRY_DEFINITIONS + ENTRY_SEPARATOR + TOSCA_DEFINITION_FILEPATH + "\n" + + ENTRY_MANIFEST + ENTRY_SEPARATOR + TOSCA_MANIFEST_FILEPATH + "\n" + + ENTRY_CHANGE_LOG + ENTRY_SEPARATOR + TOSCA_CHANGELOG_FILEPATH + "\n"; + handler.addFile(TOSCA_METADATA_FILEPATH, metaFile.getBytes(StandardCharsets.UTF_8)); + + assertEquals(SOL004MetaDirectoryValidator.class, ValidatorFactory.getValidator(handler).getClass()); + } + + @Test + public void testGivenMultiBlockMetadataWithSOL00CompliantMetaFile_thenSOL004MetaDirectoryValidatorReturned() throws IOException { + + handler.addFile(TOSCA_METADATA_FILEPATH, ValidatorUtil.getFileResource("/validation.files/metafile/metaFileWithMultipleBlocks.meta")); + handler.addFile(TOSCA_DEFINITION_FILEPATH, "".getBytes()); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_MANIFEST_FILEPATH, "".getBytes(StandardCharsets.UTF_8)); + + assertEquals(SOL004MetaDirectoryValidator.class, ValidatorFactory.getValidator(handler).getClass()); + + } + +} \ No newline at end of file diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidatorUtil.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidatorUtil.java new file mode 100644 index 0000000000..91e3807a79 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidatorUtil.java @@ -0,0 +1,22 @@ +package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation; + +import org.apache.commons.io.IOUtils; + +import java.io.IOException; +import java.io.InputStream; + +/** + * Provides util methods for Validation Test classes. + */ + +class ValidatorUtil { + + private ValidatorUtil(){ + + } + + public static byte[] getFileResource(String filePath) throws IOException{ + InputStream inputStream = ClassLoader.class.getClass().getResourceAsStream(filePath); + return IOUtils.toByteArray(inputStream); + } +} -- cgit 1.2.3-korg