diff options
Diffstat (limited to 'openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test')
4 files changed, 611 insertions, 167 deletions
diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ManifestBuilderTest.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ManifestBuilderTest.java new file mode 100644 index 0000000000..72f235e287 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ManifestBuilderTest.java @@ -0,0 +1,157 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasItems; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.isEmptyString; + +import java.io.ByteArrayInputStream; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import org.junit.Before; +import org.junit.Test; +import org.openecomp.sdc.tosca.csar.CSARConstants; +import org.openecomp.sdc.tosca.csar.Manifest; +import org.openecomp.sdc.tosca.csar.SOL004ManifestOnboarding; + +public class ManifestBuilderTest { + + private ManifestBuilder manifestBuilder; + + @Before + public void setUp() { + manifestBuilder = new ManifestBuilder(); + } + + @Test + public void givenNoManifestInformation_whenBuildingManifest_thenEmptyStringShouldBeReturned() { + final String manifest = manifestBuilder.build(); + assertThat("Manifest should be empty", manifest, isEmptyString()); + } + + @Test + public void givenSourceFiles_whenBuildingManifestWithSources_thenManifestSourceListShouldBeTheSame() { + final List<String> expectedSourceList = mockSourceList(); + mockManifestMetadata(); + expectedSourceList.forEach(source -> manifestBuilder.withSource(source)); + + final Manifest onboardingManifest = parseManifest(); + final List<String> actualSourceList = onboardingManifest.getSources(); + + assertThat("Source list should have the same size as expected source items", actualSourceList, + hasSize(expectedSourceList.size())); + assertThat("Source list should contain all expected source items", actualSourceList, + hasItems(expectedSourceList.toArray(new String[0]))); + assertThat("Source list should contain only expected sources items", expectedSourceList, + hasItems(actualSourceList.toArray(new String[expectedSourceList.size()]))); + } + + @Test + public void givenSourceFiles_whenBuildingManifestWithSignedSources_thenManifestSourceListShouldBeTheSame() { + final List<String> expectedSourceList = mockSourceList(); + mockManifestMetadata(); + expectedSourceList.forEach(sourceArtifact -> + manifestBuilder.withSignedSource(sourceArtifact, "anyAlgorithm", "anyHash") + ); + + final Manifest onboardingManifest = parseManifest(); + final List<String> sources = onboardingManifest.getSources(); + + assertThat("Source list should have the same size as expected source items", sources, + hasSize(expectedSourceList.size())); + assertThat("Source list should contain all expected source items", sources, + hasItems(expectedSourceList.toArray(new String[0]))); + assertThat("Source list should contain only expected sources items", expectedSourceList, + hasItems(sources.toArray(new String[expectedSourceList.size()]))); + } + + @Test + public void givenMetadata_whenBuildingManifestWithMetadata_thenParsedManifestMetadataShouldBeTheSame() { + final Map<String, String> expectedMetadataMap = new TreeMap<>(); + expectedMetadataMap.put(CSARConstants.PNFD_NAME, "myPnf"); + expectedMetadataMap.put(CSARConstants.PNFD_PROVIDER, "Acme"); + expectedMetadataMap.put(CSARConstants.PNFD_ARCHIVE_VERSION, "1.0"); + expectedMetadataMap.put(CSARConstants.PNFD_RELEASE_DATE_TIME, "2019-03-11T11:25:00+00:00"); + + expectedMetadataMap.forEach((key, value) -> manifestBuilder.withMetaData(key, value)); + + final Manifest onboardingManifest = parseManifest(); + final Map<String, String> actualMetadataMap = onboardingManifest.getMetadata(); + + assertThat("Metadata should be as expected", actualMetadataMap, is(expectedMetadataMap)); + } + + @Test + public void givenNonManoArtifacts_whenBuildingManifestWithArtifacts_thenParsedManifestNonManoArtifactsShouldBeTheSame() { + mockManifestMetadata(); + mockManifestSource(); + + final Map<String, List<String>> expectedNonManoArtifactMap = new TreeMap<>(); + expectedNonManoArtifactMap.put("onap_ves_events", Arrays.asList("Files/Events/MyPnf_Pnf_v1.yaml")); + expectedNonManoArtifactMap.put("onap_pm_dictionary", Arrays.asList("Files/Measurements/PM_Dictionary.yaml")); + expectedNonManoArtifactMap.put("onap_yang_modules", + Arrays.asList("Files/Yang_module/mynetconf.yang", "Files/Yang_module/mynetconf2.yang")); + expectedNonManoArtifactMap + .put("onap_others", Arrays.asList("Files/Guides/user_guide.txt", "Files/Test/test.txt")); + + expectedNonManoArtifactMap.forEach((key, artifacts) -> + artifacts.forEach(s -> manifestBuilder.withNonManoArtifact(key, s)) + ); + + final Manifest onboardingManifest = parseManifest(); + final Map<String, List<String>> actualNonManoArtifactMap = onboardingManifest.getNonManoSources(); + + assertThat("Non Mano Sources should be as expected", actualNonManoArtifactMap, is(expectedNonManoArtifactMap)); + } + + private Manifest parseManifest() { + final Manifest onboardingManifest = new SOL004ManifestOnboarding(); + onboardingManifest.parse(new ByteArrayInputStream(manifestBuilder.build().getBytes())); + return onboardingManifest; + } + + private List<String> mockSourceList() { + return Arrays.asList("pnf_main_descriptor.mf" + , "Definitions/pnf_main_descriptor.yaml" + , "Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.yaml" + , "Definitions/etsi_nfv_sol001_vnfd_2_5_1_types.yaml" + , "Files/ChangeLog.txt" + , "Files/Events/MyPnf_Pnf_v1.yaml" + , "Files/Guides/user_guide.txt" + , "Files/Measurements/PM_Dictionary.yaml" + , "Files/Scripts/my_script.sh" + , "Files/Yang_module/mynetconf.yang" + , "TOSCA-Metadata/TOSCA.meta" + ); + } + + private void mockManifestMetadata() { + manifestBuilder.withMetaData(CSARConstants.PNFD_PROVIDER, "test"); + } + + private void mockManifestSource() { + manifestBuilder.withSource("/test.yaml"); + } +}
\ 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 index 93c8b23268..17017f5b95 100644 --- 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 @@ -1,5 +1,6 @@ package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation; +import org.apache.commons.collections.CollectionUtils; import org.junit.Before; import org.junit.Test; import org.openecomp.core.utilities.file.FileContentHandler; @@ -13,8 +14,10 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import static org.openecomp.sdc.tosca.csar.CSARConstants.SEPERATOR_MF_ATTRIBUTE; +import static org.junit.Assert.fail; +import static org.openecomp.sdc.tosca.csar.CSARConstants.SEPARATOR_MF_ATTRIBUTE; import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ENTRY_DEFINITIONS; import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ETSI_ENTRY_CERTIFICATE; import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ETSI_ENTRY_CHANGE_LOG; @@ -22,6 +25,15 @@ import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ETSI_ENTRY_L import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ETSI_ENTRY_MANIFEST; import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ETSI_ENTRY_TESTS; import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_PATH_FILE_NAME; +import static org.openecomp.sdc.tosca.csar.CSARConstants.PNFD_NAME; +import static org.openecomp.sdc.tosca.csar.CSARConstants.PNFD_PROVIDER; +import static org.openecomp.sdc.tosca.csar.CSARConstants.PNFD_ARCHIVE_VERSION; +import static org.openecomp.sdc.tosca.csar.CSARConstants.PNFD_RELEASE_DATE_TIME; +import static org.openecomp.sdc.tosca.csar.CSARConstants.VNF_PRODUCT_NAME; +import static org.openecomp.sdc.tosca.csar.CSARConstants.VNF_PROVIDER_ID; +import static org.openecomp.sdc.tosca.csar.CSARConstants.VNF_PACKAGE_VERSION; +import static org.openecomp.sdc.tosca.csar.CSARConstants.VNF_RELEASE_DATE_TIME; + import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.*; public class SOL004MetaDirectoryValidatorTest { @@ -31,400 +43,660 @@ public class SOL004MetaDirectoryValidatorTest { private String metaFile; @Before - public void setUp(){ + 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"+ - TOSCA_META_ENTRY_DEFINITIONS + SEPERATOR_MF_ATTRIBUTE + "Definitions/MainServiceTemplate.yaml\n"+ - TOSCA_META_ETSI_ENTRY_MANIFEST + SEPERATOR_MF_ATTRIBUTE + "Definitions/MainServiceTemplate.mf\n"+ - TOSCA_META_ETSI_ENTRY_CHANGE_LOG + SEPERATOR_MF_ATTRIBUTE + "Artifacts/changeLog.text\n"; + TOSCA_META_ENTRY_DEFINITIONS + SEPARATOR_MF_ATTRIBUTE + "Definitions/MainServiceTemplate.yaml\n"+ + TOSCA_META_ETSI_ENTRY_MANIFEST + SEPARATOR_MF_ATTRIBUTE + "Definitions/MainServiceTemplate.mf\n"+ + TOSCA_META_ETSI_ENTRY_CHANGE_LOG + SEPARATOR_MF_ATTRIBUTE + "Artifacts/changeLog.text\n"; } @Test - public void testGivenTOSCAMetaFile_whenEntryHasNoValue_thenErrorIsReturned() throws IOException{ - - String metaFileWithInvalidEntry = "TOSCA-Meta-File-Version: \n" + + public void testGivenTOSCAMetaFile_whenEntryHasNoValue_thenErrorIsReturned() { + final String metaFileWithInvalidEntry = "TOSCA-Meta-File-Version: \n" + "Entry-Definitions: Definitions/MainServiceTemplate.yaml"; handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFileWithInvalidEntry.getBytes(StandardCharsets.UTF_8)); - handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(TestConstants.SAMPLE_DEFINITION_FILE_PATH)); + handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytes(TestConstants.SAMPLE_DEFINITION_FILE_PATH)); - Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); - List<ErrorMessage> errorMessages = errors.get(SdcCommon.UPLOAD_FILE); - assertTrue(errors.size() == 1 && errorMessages.size() == 1); + final Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + assertExpectedErrors("TOSCA Meta file with no entries", errors, 1); } @Test - public void testGivenTOSCAMeta_withAllSupportedEntries_thenNoErrorsReturned() throws IOException{ + public void testGivenTOSCAMeta_withAllSupportedEntries_thenNoErrorsReturned() { - String entryTestFilePath = "Files/Tests"; - String entryLicenseFilePath = "Files/Licenses"; + final String entryTestFilePath = "Files/Tests"; + final String entryLicenseFilePath = "Files/Licenses"; - List<String> folderList = new ArrayList<>(); + final List<String> folderList = new ArrayList<>(); folderList.add("Files/Tests/"); folderList.add("Files/Licenses/"); metaFile = metaFile + - TOSCA_META_ETSI_ENTRY_TESTS + SEPERATOR_MF_ATTRIBUTE + entryTestFilePath + "\n" + - TOSCA_META_ETSI_ENTRY_LICENSES + SEPERATOR_MF_ATTRIBUTE + entryLicenseFilePath +"\n"; + TOSCA_META_ETSI_ENTRY_TESTS + SEPARATOR_MF_ATTRIBUTE + entryTestFilePath + "\n" + + TOSCA_META_ETSI_ENTRY_LICENSES + SEPARATOR_MF_ATTRIBUTE + entryLicenseFilePath +"\n"; handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8)); - handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytes(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<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, folderList); + final ManifestBuilder manifestBuilder = getVnfManifestSampleBuilder() + .withSource(TOSCA_META_PATH_FILE_NAME) + .withSource(TOSCA_DEFINITION_FILEPATH) + .withSource(TOSCA_CHANGELOG_FILEPATH) + .withSource(TOSCA_MANIFEST_FILEPATH).withSource(SAMPLE_SOURCE) + .withSource(SAMPLE_DEFINITION_IMPORT_FILE_PATH) + .withSource(entryTestFilePath) + .withSource(entryLicenseFilePath); + + handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); + + final Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, folderList); assertTrue(errors.size() == 0); } @Test - public void testGivenTOSCAMeta_withUnsupportedEntry_thenWarningIsReturned(){ - + public void testGivenTOSCAMeta_withUnsupportedEntry_thenWarningIsReturned() { metaFile = "Entry-Events: Definitions/events.log"; handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8)); - Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + final Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); List<ErrorMessage> errorMessages = errors.get(SdcCommon.UPLOAD_FILE); assertTrue(errors.size() == 1 && errorMessages.size() == 1); assertTrue(errorMessages.get(0).getLevel() == ErrorLevel.ERROR); - } + /** + * Tests if the meta file contains invalid versions in TOSCA-Meta-File-Version and CSAR-Version attributes. + */ @Test - public void testGivenTOSCAMetaFile_withInvalidTOSCAMetaFileVersionAndCSARVersion_thenErrorIsReturned() throws IOException{ - - String metaFile = + public void testGivenTOSCAMetaFile_withInvalidTOSCAMetaFileVersionAndCSARVersion_thenErrorIsReturned() { + final String metaFile = "TOSCA-Meta-File-Version: " + Integer.MAX_VALUE + "\nCSAR-Version: " + Integer.MAX_VALUE + "\nCreated-by: Bilal Iqbal\n" + - TOSCA_META_ENTRY_DEFINITIONS+ SEPERATOR_MF_ATTRIBUTE + "Definitions/MainServiceTemplate.yaml\n" + - TOSCA_META_ETSI_ENTRY_MANIFEST + SEPERATOR_MF_ATTRIBUTE + "Definitions/MainServiceTemplate.mf\n"+ - TOSCA_META_ETSI_ENTRY_CHANGE_LOG + SEPERATOR_MF_ATTRIBUTE + "Artifacts/changeLog.text"; + TOSCA_META_ENTRY_DEFINITIONS+ SEPARATOR_MF_ATTRIBUTE + "Definitions/MainServiceTemplate.yaml\n" + + TOSCA_META_ETSI_ENTRY_MANIFEST + SEPARATOR_MF_ATTRIBUTE + "Definitions/MainServiceTemplate.mf\n"+ + TOSCA_META_ETSI_ENTRY_CHANGE_LOG + SEPARATOR_MF_ATTRIBUTE + "Artifacts/changeLog.text"; + + final ManifestBuilder manifestBuilder = getVnfManifestSampleBuilder(); handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8)); - handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(TestConstants.SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME); + + handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytes(TestConstants.SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(TOSCA_DEFINITION_FILEPATH); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8)); - handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_MANIFEST_FILE_PATH)); + manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH); - Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); - List<ErrorMessage> errorMessages = errors.get(SdcCommon.UPLOAD_FILE); - assertTrue(errors.size() == 1 && errorMessages.size() == 2); + manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH); + handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); + + final Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + assertExpectedErrors("Invalid TOSCA-Meta-File-Version and CSAR-Version attributes", errors, 2); } @Test - public void testGivenTOSCAMetaFile_withNonExistentFileReferenced_thenErrorsReturned(){ - + public void testGivenTOSCAMetaFile_withNonExistentFileReferenced_thenErrorsReturned() { handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8)); - Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + final Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); List<ErrorMessage> errorMessages = errors.get(SdcCommon.UPLOAD_FILE); assertTrue(errors.size() == 1 && errorMessages.size() == 3); } @Test - public void testGivenDefinitionFile_whenValidImportStatementExist_thenNoErrorsReturned() throws IOException{ - - String definitionFileWithValidImports = "/validation.files/definition/definitionFileWithValidImports.yaml"; - + public void testGivenDefinitionFile_whenValidImportStatementExist_thenNoErrorsReturned() { + final ManifestBuilder manifestBuilder = getPnfManifestSampleBuilder(); handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8)); + manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8)); - handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_MANIFEST_FILE_PATH)); + manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH); + 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)); + manifestBuilder.withSource(SAMPLE_SOURCE); - Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + handler.addFile("Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.yaml", getResourceBytes(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource("Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.yaml"); + + final String definitionFileWithValidImports = "/validation.files/definition/definitionFileWithValidImports.yaml"; + handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytes(definitionFileWithValidImports)); + manifestBuilder.withSource(TOSCA_DEFINITION_FILEPATH); + + manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH); + handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); + + final Map<String, List<ErrorMessage>> 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"); + public void testGivenDefinitionFile_whenMultipleDefinitionsImportStatementExist_thenNoErrorsReturned() { + final ManifestBuilder manifestBuilder = getVnfManifestSampleBuilder(); handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8)); + manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8)); - handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_MANIFEST_FILE_PATH)); + manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH); + handler.addFile(SAMPLE_SOURCE, "".getBytes()); + manifestBuilder.withSource(SAMPLE_SOURCE); + + final byte [] sampleDefinitionFile1 = getResourceBytes("/validation.files/definition/sampleDefinitionFile1.yaml"); handler.addFile(TOSCA_DEFINITION_FILEPATH, sampleDefinitionFile1); + manifestBuilder.withSource(TOSCA_DEFINITION_FILEPATH); + + final byte [] sampleDefinitionFile2 = getResourceBytes("/validation.files/definition/sampleDefinitionFile2.yaml"); handler.addFile("Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.yaml", sampleDefinitionFile2); + manifestBuilder.withSource("Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.yaml"); + + final byte [] sampleDefinitionFile3 = getResourceBytes("/validation.files/definition/sampleDefinitionFile3.yaml"); handler.addFile("Definitions/etsi_nfv_sol001_pnfd_2_5_2_types.yaml", sampleDefinitionFile3); + manifestBuilder.withSource("Definitions/etsi_nfv_sol001_pnfd_2_5_2_types.yaml"); + + manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH); + handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); - Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + final Map<String, List<ErrorMessage>> 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"; + public void testGivenDefinitionFile_whenInvalidImportStatementExist_thenErrorIsReturned() { + final ManifestBuilder manifestBuilder = getVnfManifestSampleBuilder(); handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8)); + manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8)); - handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_MANIFEST_FILE_PATH)); + manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH); + handler.addFile(SAMPLE_SOURCE, "".getBytes()); - handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(definitionFileWithInvalidImports)); + manifestBuilder.withSource(SAMPLE_SOURCE); - Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); - List<ErrorMessage> errorMessages = errors.get(SdcCommon.UPLOAD_FILE); - assertTrue(errors.size() == 1 && errorMessages.size() == 1); + final String definitionFileWithInvalidImports = "/validation.files/definition/definitionFileWithInvalidImport.yaml"; + handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytes(definitionFileWithInvalidImports)); + manifestBuilder.withSource(TOSCA_DEFINITION_FILEPATH); + + manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH); + String manifest = manifestBuilder.build(); + handler.addFile(TOSCA_MANIFEST_FILEPATH, manifest.getBytes(StandardCharsets.UTF_8)); + + final Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + assertExpectedErrors("", errors, 1); } + /** + * Manifest referenced import file missing + */ @Test - public void testGivenDefinitionFile_whenReferencedImportDoesNotExist_thenErrorIsReturned() throws IOException{ + public void testGivenDefinitionFile_whenReferencedImportDoesNotExist_thenErrorIsReturned() { + final ManifestBuilder manifestBuilder = getVnfManifestSampleBuilder(); handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8)); + manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8)); - handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_MANIFEST_FILE_PATH)); + manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH); + handler.addFile(SAMPLE_SOURCE, "".getBytes()); + manifestBuilder.withSource(SAMPLE_SOURCE); + 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")); + manifestBuilder.withSource("Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.yaml"); - Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); - List<ErrorMessage> errorMessages = errors.get(SdcCommon.UPLOAD_FILE); - assertTrue(errors.size() == 1 && errorMessages.size() == 1); + manifestBuilder.withSource(TOSCA_DEFINITION_FILEPATH); + handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytes("/validation.files/definition/sampleDefinitionFile2.yaml")); + + manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH); + handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); + final Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + assertExpectedErrors("Manifest referenced import file missing", errors, 1); } + /** + * Reference with invalid YAML format. + */ @Test - public void testGivenDefinitionFile_withInvalidYAML_thenErrorIsReturned() throws IOException{ - - String definitionFileWithInvalidYAML = "/validation.files/definition/invalidDefinitionFile.yaml"; + public void testGivenDefinitionFile_withInvalidYAML_thenErrorIsReturned() { + final ManifestBuilder manifestBuilder = getVnfManifestSampleBuilder(); handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8)); + manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8)); - handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_MANIFEST_FILE_PATH)); + manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH); + handler.addFile(SAMPLE_SOURCE, "".getBytes()); + manifestBuilder.withSource(SAMPLE_SOURCE); - handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(definitionFileWithInvalidYAML)); + final String definitionFileWithInvalidYAML = "/validation.files/definition/invalidDefinitionFile.yaml"; + handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytes(definitionFileWithInvalidYAML)); + manifestBuilder.withSource(TOSCA_DEFINITION_FILEPATH); - Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); - List<ErrorMessage> errorMessages = errors.get(SdcCommon.UPLOAD_FILE); - assertTrue(errors.size() == 1 && errorMessages.size() == 1); + manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH); + handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); + + final Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + assertExpectedErrors("Reference with invalid YAML format", errors, 1); } @Test - public void testGivenManifestFile_withValidSourceAndNonManoSources_thenNoErrorIsReturned() throws IOException{ - - String nonManoSource = "Artifacts/Deployment/Measurements/PM_Dictionary.yaml"; + public void testGivenManifestFile_withValidSourceAndNonManoSources_thenNoErrorIsReturned() { + final ManifestBuilder manifestBuilder = getPnfManifestSampleBuilder(); handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8)); - handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource("/validation.files/manifest/validManifest.mf")); + manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes()); - handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH); + + handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytes(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(TOSCA_DEFINITION_FILEPATH); + handler.addFile(SAMPLE_SOURCE, "".getBytes()); - handler.addFile(SAMPLE_DEFINITION_IMPORT_FILE_PATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(SAMPLE_SOURCE); + + handler.addFile(SAMPLE_DEFINITION_IMPORT_FILE_PATH, getResourceBytes(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(SAMPLE_DEFINITION_IMPORT_FILE_PATH); + + final String nonManoSource = "Artifacts/Deployment/Measurements/PM_Dictionary.yaml"; handler.addFile(nonManoSource, "".getBytes()); + manifestBuilder.withNonManoArtifact("onap_pm_events", nonManoSource); + + manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH); + handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); - Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + final Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); assertTrue(errors.size() == 0); } + /** + * Manifest with non existent source files should return error. + */ @Test - public void testGivenManifestFile_withNonExistentSourceFile_thenErrorIsReturned() throws IOException{ - String nonManoSource = "Artifacts/Deployment/Measurements/PM_Dictionary.yaml"; + public void testGivenManifestFile_withNonExistentSourceFile_thenErrorIsReturned() { + final ManifestBuilder manifestBuilder = getPnfManifestSampleBuilder(); + //non existent reference + manifestBuilder.withSource("Artifacts/Deployment/Events/RadioNode_pnf_v1.yaml"); handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8)); - handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource("/validation.files/manifest/validManifest.mf")); + manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes()); - handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH); + + handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytes(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(TOSCA_DEFINITION_FILEPATH); + handler.addFile(SAMPLE_DEFINITION_IMPORT_FILE_PATH, "".getBytes()); + manifestBuilder.withSource(SAMPLE_DEFINITION_IMPORT_FILE_PATH); + + String nonManoSource = "Artifacts/Deployment/Measurements/PM_Dictionary.yaml"; handler.addFile(nonManoSource, "".getBytes()); + manifestBuilder.withNonManoArtifact("onap_pm_events", nonManoSource); - Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); - List<ErrorMessage> errorMessages = errors.get(SdcCommon.UPLOAD_FILE); - assertTrue(errors.size() == 1 && errorMessages.size() == 1); + manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH); + handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); + + final Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + assertExpectedErrors("Manifest with non existent source files", errors, 1); } + /** + * Tests the validation for a TOSCA Manifest with invalid data. + */ @Test - public void testGivenManifestFile_withInvalidData_thenErrorIsReturned() throws IOException{ - + public void testGivenManifestFile_withInvalidData_thenErrorIsReturned() { handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8)); - handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource("/validation.files/manifest/invalidManifest.mf")); + handler.addFile(TOSCA_MANIFEST_FILEPATH, getResourceBytes("/validation.files/manifest/invalidManifest.mf")); handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes()); - handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytes(SAMPLE_DEFINITION_FILE_PATH)); handler.addFile(SAMPLE_DEFINITION_IMPORT_FILE_PATH, "".getBytes()); - Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); - List<ErrorMessage> errorMessages = errors.get(SdcCommon.UPLOAD_FILE); - assertTrue(errors.size() == 1 && errorMessages.size() == 1); + final Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + assertExpectedErrors("TOSCA manifest with invalid data", errors, 1); } @Test - public void testGivenManifestAndDefinitionFile_withSameNames_thenNoErrorReturned() throws IOException { + public void testGivenManifestAndDefinitionFile_withSameNames_thenNoErrorReturned() { + final ManifestBuilder manifestBuilder = getVnfManifestSampleBuilder(); handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8)); - handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource("/validation.files/manifest/sampleManifest.mf")); + manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes()); - handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH); + + handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytes(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(TOSCA_DEFINITION_FILEPATH); + handler.addFile(SAMPLE_DEFINITION_IMPORT_FILE_PATH, "".getBytes()); + manifestBuilder.withSource(SAMPLE_DEFINITION_IMPORT_FILE_PATH); - Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH); + handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); + + final Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); assertTrue(errors.size() == 0); } + /** + * Main TOSCA definitions file and Manifest file with different name should return error. + */ @Test - public void testGivenManifestAndMainDefinitionFile_withDifferentNames_thenErrorIsReturned() throws IOException { + public void testGivenManifestAndMainDefinitionFile_withDifferentNames_thenErrorIsReturned() { metaFile = "TOSCA-Meta-File-Version: 1.0\n"+ "CSAR-Version: 1.1\n"+ "Created-by: Vendor\n"+ - TOSCA_META_ENTRY_DEFINITIONS + SEPERATOR_MF_ATTRIBUTE + "Definitions/MainServiceTemplate.yaml\n"+ - TOSCA_META_ETSI_ENTRY_MANIFEST + SEPERATOR_MF_ATTRIBUTE +"Definitions/MainServiceTemplate2.mf\n"+ - TOSCA_META_ETSI_ENTRY_CHANGE_LOG + SEPERATOR_MF_ATTRIBUTE +"Artifacts/changeLog.text\n"; + TOSCA_META_ENTRY_DEFINITIONS + SEPARATOR_MF_ATTRIBUTE + "Definitions/MainServiceTemplate.yaml\n"+ + TOSCA_META_ETSI_ENTRY_MANIFEST + SEPARATOR_MF_ATTRIBUTE +"Definitions/MainServiceTemplate2.mf\n"+ + TOSCA_META_ETSI_ENTRY_CHANGE_LOG + SEPARATOR_MF_ATTRIBUTE +"Artifacts/changeLog.text\n"; + + final ManifestBuilder manifestBuilder = getVnfManifestSampleBuilder(); handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8)); - handler.addFile("Definitions/MainServiceTemplate2.mf", ValidatorUtil.getFileResource("/validation.files/manifest/sampleManifest.mf")); + manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes()); - handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH); + + handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytes(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(TOSCA_DEFINITION_FILEPATH); + handler.addFile(SAMPLE_DEFINITION_IMPORT_FILE_PATH, "".getBytes()); + manifestBuilder.withSource(SAMPLE_DEFINITION_IMPORT_FILE_PATH); + + manifestBuilder.withSource("Definitions/MainServiceTemplate2.mf"); + handler.addFile("Definitions/MainServiceTemplate2.mf", manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); - Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + final Map<String, List<ErrorMessage>> 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 { + public void testGivenManifestFile_withDifferentExtension_thenErrorIsReturned() { metaFile = "TOSCA-Meta-File-Version: 1.0\n"+ "CSAR-Version: 1.1\n"+ "Created-by: Vendor\n"+ "Entry-Definitions: Definitions/MainServiceTemplate.yaml\n"+ - TOSCA_META_ETSI_ENTRY_MANIFEST + SEPERATOR_MF_ATTRIBUTE + "Definitions/MainServiceTemplate.txt\n"+ - TOSCA_META_ETSI_ENTRY_CHANGE_LOG + SEPERATOR_MF_ATTRIBUTE + "Artifacts/changeLog.text\n"; + TOSCA_META_ETSI_ENTRY_MANIFEST + SEPARATOR_MF_ATTRIBUTE + "Definitions/MainServiceTemplate.txt\n"+ + TOSCA_META_ETSI_ENTRY_CHANGE_LOG + SEPARATOR_MF_ATTRIBUTE + "Artifacts/changeLog.text\n"; + + final ManifestBuilder manifestBuilder = getVnfManifestSampleBuilder(); handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8)); - handler.addFile("Definitions/MainServiceTemplate.txt", ValidatorUtil.getFileResource("/validation.files/manifest/sampleManifest.mf")); + manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes()); - handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH); + + handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytes(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(TOSCA_DEFINITION_FILEPATH); + handler.addFile(SAMPLE_DEFINITION_IMPORT_FILE_PATH, "".getBytes()); + manifestBuilder.withSource(SAMPLE_DEFINITION_IMPORT_FILE_PATH); - Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + manifestBuilder.withSource("Definitions/MainServiceTemplate.txt"); + handler.addFile("Definitions/MainServiceTemplate.txt", manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); + + final Map<String, List<ErrorMessage>> 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{ + public void testGivenManifestFile_withValidVnfMetadata_thenNoErrorsReturned() { + final ManifestBuilder manifestBuilder = getVnfManifestSampleBuilder(); + handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8)); - handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource("/validation.files/manifest/sampleManifest.mf")); + manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME); handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes()); - handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH); + handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytes(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(TOSCA_DEFINITION_FILEPATH); + + manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH); + handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); - Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + final Map<String, List<ErrorMessage>> 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 { + public void testGivenManifestFile_withValidPnfMetadata_thenNoErrorsReturned() { + final ManifestBuilder manifestBuilder = getPnfManifestSampleBuilder(); + handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8)); - handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource("/validation.files/manifest/sampleManifest2.mf")); + manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes()); - handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH); - Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + manifestBuilder.withSignedSource(TOSCA_DEFINITION_FILEPATH + , "SHA-abc", "09e5a788acb180162c51679ae4c998039fa6644505db2415e35107d1ee213943"); + handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytes(SAMPLE_DEFINITION_FILE_PATH)); + + manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH); + handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); + + final Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); assertExpectedErrors("Manifest with valid pnf mandatory values should not return any errors", errors, 0); } + /** + * Manifest with mixed metadata should return error. + */ @Test - public void testGivenManifestFile_withMetadataContainingMixedPnfVnfMetadata_thenErrorIsReturned() throws IOException { + public void testGivenManifestFile_withMetadataContainingMixedPnfVnfMetadata_thenErrorIsReturned() { + final ManifestBuilder manifestBuilder = new ManifestBuilder() + .withMetaData(PNFD_NAME, "RadioNode") + .withMetaData(VNF_PROVIDER_ID, "Bilal Iqbal") + .withMetaData(PNFD_ARCHIVE_VERSION, "1.0") + .withMetaData(VNF_RELEASE_DATE_TIME, "2019-12-14T11:25:00+00:00"); handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8)); - handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource("/validation.files/manifest/manifestInvalidMetadata.mf")); + manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME); handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes()); - handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH); + handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytes(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(TOSCA_DEFINITION_FILEPATH); - Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH); + handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); + + final Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); assertExpectedErrors("Manifest with mixed metadata should return error", errors, 1); } @Test - public void testGivenManifestFile_withMetadataMissingPnfOrVnfMandatoryEntries_thenErrorIsReturned() throws IOException{ + public void testGivenManifestFile_withMetadataMissingPnfOrVnfMandatoryEntries_thenErrorIsReturned() { + final ManifestBuilder manifestBuilder = new ManifestBuilder() + .withMetaData("invalid_product_name", "RadioNode") + .withMetaData("invalid_provider_id", "Bilal Iqbal") + .withMetaData("invalid_package_version", "1.0") + .withMetaData("invalid_release_date_time", "2019-12-14T11:25:00+00:00"); handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8)); - handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource("/validation.files/manifest/manifestInvalidMetadata2.mf")); + manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes()); - handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH); - Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytes(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(TOSCA_DEFINITION_FILEPATH); + + manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH); + handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); + + final Map<String, List<ErrorMessage>> 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{ + public void testGivenManifestFile_withMetadataMissingMandatoryPnfEntries_thenErrorIsReturned() { + final ManifestBuilder manifestBuilder = new ManifestBuilder(); + + manifestBuilder.withMetaData(PNFD_NAME, "RadioNode"); + manifestBuilder.withMetaData(PNFD_RELEASE_DATE_TIME, "2019-12-14T11:25:00+00:00"); + handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8)); - handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource("/validation.files/manifest/manifestInvalidMetadata4.mf")); + manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes()); - handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH); + + handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytes(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(TOSCA_DEFINITION_FILEPATH); + + manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH); + handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); - Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + final Map<String, List<ErrorMessage>> 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{ + public void testGivenManifestFile_withMetadataMissingMandatoryVnfEntries_thenErrorIsReturned() { + final ManifestBuilder manifestBuilder = new ManifestBuilder(); + + manifestBuilder.withMetaData(VNF_PRODUCT_NAME, "RadioNode"); + handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8)); - handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource("/validation.files/manifest/manifestInvalidMetadata5.mf")); + manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes()); - handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH); + + handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytes(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(TOSCA_DEFINITION_FILEPATH); - Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH); + handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); + + final Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); assertExpectedErrors("Manifest with metadata missing vnf mandatory entries should return error", errors, 4); } + /** + * Manifest with more than 4 metadata entries should return error. + */ @Test - public void testGivenManifestFile_withMetadataEntriesExceedingTheLimit_thenErrorIsReturned() throws IOException{ + public void testGivenManifestFile_withMetadataEntriesExceedingTheLimit_thenErrorIsReturned() { + final ManifestBuilder manifestBuilder = getVnfManifestSampleBuilder() + .withMetaData(PNFD_NAME, "RadioNode") + .withMetaData(PNFD_PROVIDER, "Bilal Iqbal") + .withMetaData(PNFD_ARCHIVE_VERSION, "1.0") + .withMetaData(PNFD_RELEASE_DATE_TIME, "2019-03-11T11:25:00+00:00"); + handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8)); - handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource("/validation.files/manifest/manifestInvalidMetadata3.mf")); + manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes()); - handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH); + + handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytes(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(TOSCA_DEFINITION_FILEPATH); - Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); + manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH); + handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); + + final Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, Collections.emptyList()); assertExpectedErrors("Manifest with more than 4 metadata entries should return error", errors, 2); } @Test - public void testGivenManifestFile_withPnfMetadataAndVfEntries_thenErrorIsReturned() throws IOException { - - List<String> folderList = new ArrayList<>(); - folderList.add("Files/Certificates/"); + public void testGivenManifestFile_withPnfMetadataAndVfEntries_thenErrorIsReturned() { + final ManifestBuilder manifestBuilder = getPnfManifestSampleBuilder(); metaFile = metaFile + - TOSCA_META_ETSI_ENTRY_TESTS + SEPERATOR_MF_ATTRIBUTE + "Files/Tests\n" + - TOSCA_META_ETSI_ENTRY_LICENSES + SEPERATOR_MF_ATTRIBUTE + "Files/Licenses\n" + - TOSCA_META_ETSI_ENTRY_CERTIFICATE + SEPERATOR_MF_ATTRIBUTE + "Files/Certificates"; + TOSCA_META_ETSI_ENTRY_TESTS + SEPARATOR_MF_ATTRIBUTE + "Files/Tests\n" + + TOSCA_META_ETSI_ENTRY_LICENSES + SEPARATOR_MF_ATTRIBUTE + "Files/Licenses\n" + + TOSCA_META_ETSI_ENTRY_CERTIFICATE + SEPARATOR_MF_ATTRIBUTE + "Files/Certificates"; handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8)); - handler.addFile(TOSCA_MANIFEST_FILEPATH, ValidatorUtil.getFileResource("/validation.files/manifest/sampleManifest2.mf")); + manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes()); - handler.addFile(TOSCA_DEFINITION_FILEPATH, ValidatorUtil.getFileResource(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH); + + handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytes(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(TOSCA_DEFINITION_FILEPATH); - Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, folderList); + manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH); + handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); + + final List<String> folderList = new ArrayList<>(); + folderList.add("Files/Certificates/"); + final Map<String, List<ErrorMessage>> errors = sol004MetaDirectoryValidator.validateContent(handler, folderList); assertExpectedErrors("Tosca.meta should not have entries applicable only to VF", errors, 2); } - private void assertExpectedErrors( String testCase, Map<String, List<ErrorMessage>> errors, int expectedErrors){ - if(expectedErrors > 0){ - List<ErrorMessage> errorMessages = errors.get(SdcCommon.UPLOAD_FILE); - assertTrue(testCase, errorMessages.size() == expectedErrors); - }else{ - assertTrue(testCase,errors.size() == expectedErrors); + private void assertExpectedErrors(final String testCase, final Map<String, List<ErrorMessage>> errors, final int expectedErrors){ + final List<ErrorMessage> errorMessages = errors.get(SdcCommon.UPLOAD_FILE); + printErrorMessages(errorMessages); + if (expectedErrors > 0) { + assertEquals(testCase, errorMessages.size(), expectedErrors); + } else { + assertEquals(testCase, errors.size(), expectedErrors); } } + + private void printErrorMessages(final List<ErrorMessage> errorMessages) { + if (CollectionUtils.isNotEmpty(errorMessages)) { + errorMessages.forEach(errorMessage -> { + System.out.println(String.format("%s: %s", errorMessage.getLevel(), errorMessage.getMessage())); + }); + } + } + + private byte[] getResourceBytes(final String resourcePath) { + try { + return ValidatorUtil.getFileResource(resourcePath); + } catch (final IOException e) { + fail(String.format("Could not load resource '%s'", resourcePath)); + e.printStackTrace(); + } + + return null; + } + + private ManifestBuilder getPnfManifestSampleBuilder() { + return new ManifestBuilder() + .withMetaData(PNFD_NAME, "myPnf") + .withMetaData(PNFD_PROVIDER, "ACME") + .withMetaData(PNFD_ARCHIVE_VERSION, "1.0") + .withMetaData(PNFD_RELEASE_DATE_TIME, "2019-03-11T11:25:00+00:00"); + } + + private ManifestBuilder getVnfManifestSampleBuilder() { + return new ManifestBuilder() + .withMetaData(VNF_PRODUCT_NAME, "RadioNode") + .withMetaData(VNF_PROVIDER_ID, "ACME") + .withMetaData(VNF_PACKAGE_VERSION, "1.0") + .withMetaData(VNF_RELEASE_DATE_TIME, "2019-03-11T11:25:00+00:00"); + } }
\ 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/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 index 3f42c5715c..5a43ab5e71 100644 --- 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 @@ -7,7 +7,7 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import static org.junit.Assert.assertEquals; -import static org.openecomp.sdc.tosca.csar.CSARConstants.SEPERATOR_MF_ATTRIBUTE; +import static org.openecomp.sdc.tosca.csar.CSARConstants.SEPARATOR_MF_ATTRIBUTE; import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ENTRY_DEFINITIONS; import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ETSI_ENTRY_CHANGE_LOG; import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ETSI_ENTRY_MANIFEST; @@ -54,7 +54,7 @@ public class ValidatorFactoryTest { @Test public void testGivenNonSOL004MetaDirectoryCompliantMetaFile_thenONAPCSARValidatorIsReturned() throws IOException{ metaFile = metaFile + - TOSCA_META_ENTRY_DEFINITIONS + SEPERATOR_MF_ATTRIBUTE + TOSCA_DEFINITION_FILEPATH; + TOSCA_META_ENTRY_DEFINITIONS + SEPARATOR_MF_ATTRIBUTE + TOSCA_DEFINITION_FILEPATH; handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8)); assertEquals(ONAPCsarValidator.class, ValidatorFactory.getValidator(handler).getClass()); @@ -64,9 +64,9 @@ public class ValidatorFactoryTest { public void testGivenSOL004MetaDirectoryCompliantMetafile_thenONAPCsarValidatorIsReturned() throws IOException{ metaFile = metaFile + - TOSCA_META_ENTRY_DEFINITIONS + SEPERATOR_MF_ATTRIBUTE + TOSCA_DEFINITION_FILEPATH + "\n" - + TOSCA_META_ETSI_ENTRY_MANIFEST + SEPERATOR_MF_ATTRIBUTE + TOSCA_MANIFEST_FILEPATH + "\n" - + TOSCA_META_ETSI_ENTRY_CHANGE_LOG + SEPERATOR_MF_ATTRIBUTE + TOSCA_CHANGELOG_FILEPATH + "\n"; + TOSCA_META_ENTRY_DEFINITIONS + SEPARATOR_MF_ATTRIBUTE + TOSCA_DEFINITION_FILEPATH + "\n" + + TOSCA_META_ETSI_ENTRY_MANIFEST + SEPARATOR_MF_ATTRIBUTE + TOSCA_MANIFEST_FILEPATH + "\n" + + TOSCA_META_ETSI_ENTRY_CHANGE_LOG + SEPARATOR_MF_ATTRIBUTE + TOSCA_CHANGELOG_FILEPATH + "\n"; handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8)); assertEquals(SOL004MetaDirectoryValidator.class, ValidatorFactory.getValidator(handler).getClass()); 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 index 91e3807a79..fdde36c935 100644 --- 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 @@ -11,12 +11,27 @@ import java.io.InputStream; class ValidatorUtil { - private ValidatorUtil(){ + private ValidatorUtil() { } - public static byte[] getFileResource(String filePath) throws IOException{ - InputStream inputStream = ClassLoader.class.getClass().getResourceAsStream(filePath); - return IOUtils.toByteArray(inputStream); + /** + * Reads a file and coverts it to a byte array. + * + * @param filePath The file path + * @return + * The file byte array + * @throws IOException + * When the file was not found or the input stream could not be opened + */ + public static byte[] getFileResource(final String filePath) throws IOException { + try(final InputStream inputStream = ClassLoader.class.getResourceAsStream(filePath)) { + if (inputStream == null) { + throw new IOException(String.format("Could not find the resource on path \"%s\"", filePath)); + } + return IOUtils.toByteArray(inputStream); + } catch (final IOException ex) { + throw new IOException(String.format("Could not open the input stream for resource on path \"%s\"", filePath), ex); + } } } |