From 2dec83af8abdb08d1db06afbcaec7325e098276a Mon Sep 17 00:00:00 2001 From: Bartosz Gardziejewski Date: Thu, 27 Aug 2020 13:35:16 +0200 Subject: Fix searching for path to PM_Dictionary in manifest file. Signed-off-by: Bartosz Gardziejewski Change-Id: I3bf22f9095c560fa6c98e37b6d28b306e9af4d95 Issue-ID: VNFSDK-645 --- .../cvc/csar/cc/sol004/VTPValidateCSARR816745.java | 37 +++++++++++++++++---- .../VTPValidateCSARR816745IntegrationTest.java | 14 ++++++++ ...source-value-for-pm-dictionary-in-manifest.csar | Bin 0 -> 8793 bytes .../pnf/r816745/csar-with-valid-pm-dictionary.csar | Bin 8792 -> 8792 bytes 4 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 csarvalidation/src/test/resources/pnf/r816745/csar-with-missing-source-value-for-pm-dictionary-in-manifest.csar diff --git a/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR816745.java b/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR816745.java index a929418..b43dbba 100644 --- a/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR816745.java +++ b/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR816745.java @@ -29,6 +29,7 @@ import org.yaml.snakeyaml.error.YAMLException; import java.nio.file.Path; import java.util.List; import java.util.Map; +import java.util.Optional; @OnapCommandSchema(schema = "vtp-validate-csar-r816745.yaml") public class VTPValidateCSARR816745 extends VTPValidateCSARBase { @@ -65,18 +66,32 @@ public class VTPValidateCSARR816745 extends VTPValidateCSARBase { } private static final String PM_DICTIONARY = "onap_pm_dictionary"; + private static final String SOURCE_ELEMENT_TAG = "Source"; @Override protected void validateCSAR(CSARArchive csar) { Map>> nonManoFields = csar.getManifest().getNonMano(); String rootPath = csar.getWorkspace().getPathToCsarFolder().map(Path::toString).orElse("/"); if (nonManoFields.containsKey(PM_DICTIONARY)) { - validateYamlFile(rootPath+"/",getLocationOfPmDictionaryFile(nonManoFields)); + getLocationOfPmDictionaryFile(nonManoFields, csar.getManifestMfFile().getName()).ifPresent(pmDictionary -> + validateYamlFile(rootPath+"/",pmDictionary) + ); } } - private String getLocationOfPmDictionaryFile(Map>> nonManoFields) { - return nonManoFields.get(PM_DICTIONARY).get("source").get(0); + private Optional getLocationOfPmDictionaryFile(Map>> nonManoFields, String manifestFileName) { + if(nonManoFields.get(PM_DICTIONARY).containsKey(SOURCE_ELEMENT_TAG)) { + return getPathToPmDictionary(nonManoFields, SOURCE_ELEMENT_TAG); + } else if(nonManoFields.get(PM_DICTIONARY).containsKey(SOURCE_ELEMENT_TAG.toLowerCase())) { + return getPathToPmDictionary(nonManoFields, SOURCE_ELEMENT_TAG.toLowerCase()); + } else { + addPmDictionaryLoadingError(manifestFileName, PM_DICTIONARY +" in manifest does not contains key 'Source'"); + return Optional.empty(); + } + } + + private Optional getPathToPmDictionary(Map>> nonManoFields, String sourceElementTag) { + return Optional.ofNullable(nonManoFields.get(PM_DICTIONARY).get(sourceElementTag).get(0)); } private void validateYamlFile(String rootPath, String artifactPath) { @@ -86,14 +101,22 @@ public class VTPValidateCSARR816745 extends VTPValidateCSARBase { addAllErrorsReportedByVaidator(artifactPath, validationErrors); } catch (YamlProcessingException | YAMLException e) { LOGGER.error("Failed to load PM_Dictionary file.", e); - errors.add(new CSARPmDictionaryLoadingError( - artifactPath, - e.getMessage() - )); + addPmDictionaryLoadingError(artifactPath, e); } } + private void addPmDictionaryLoadingError(String artifactPath, Exception e) { + addPmDictionaryLoadingError(artifactPath,e.getMessage()); + } + + private void addPmDictionaryLoadingError(String artifactPath, String message) { + errors.add(new CSARPmDictionaryLoadingError( + artifactPath, + message + )); + } + private void addAllErrorsReportedByVaidator(String artifactPath, List validationErrors) { for(YamlDocumentValidationError validationError: validationErrors) { addPmDictionaryValidationError(artifactPath, validationError); diff --git a/csarvalidation/src/test/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR816745IntegrationTest.java b/csarvalidation/src/test/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR816745IntegrationTest.java index f171b4d..17e9de3 100644 --- a/csarvalidation/src/test/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR816745IntegrationTest.java +++ b/csarvalidation/src/test/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR816745IntegrationTest.java @@ -51,6 +51,20 @@ public class VTPValidateCSARR816745IntegrationTest { testCase = new VTPValidateCSARR816745(); } + @Test + public void shouldAddPmDictionaryLoadingErrorWhenPmDictionaryHaveNoSourceInManifest() throws Exception { + // given + configureTestCase(testCase, TEST_CSAR_DIRECTORY + "csar-with-missing-source-value-for-pm-dictionary-in-manifest.csar", "vtp-validate-csar-r816745.yaml", IS_PNF); + + // when + testCase.execute(); + + // then + List errors = testCase.getErrors(); + assertThat(errors.size()).isEqualTo(1); + assertThat(convertToMessagesList(errors)).contains("Fail to load PM_Dictionary With error: onap_pm_dictionary in manifest does not contains key 'Source'"); + } + @Test public void shouldNotReportAnyErrorWhenCsarIsNotContainingPmDictionary() throws Exception { // given diff --git a/csarvalidation/src/test/resources/pnf/r816745/csar-with-missing-source-value-for-pm-dictionary-in-manifest.csar b/csarvalidation/src/test/resources/pnf/r816745/csar-with-missing-source-value-for-pm-dictionary-in-manifest.csar new file mode 100644 index 0000000..cd35d37 Binary files /dev/null and b/csarvalidation/src/test/resources/pnf/r816745/csar-with-missing-source-value-for-pm-dictionary-in-manifest.csar differ diff --git a/csarvalidation/src/test/resources/pnf/r816745/csar-with-valid-pm-dictionary.csar b/csarvalidation/src/test/resources/pnf/r816745/csar-with-valid-pm-dictionary.csar index cc7db55..528a002 100644 Binary files a/csarvalidation/src/test/resources/pnf/r816745/csar-with-valid-pm-dictionary.csar and b/csarvalidation/src/test/resources/pnf/r816745/csar-with-valid-pm-dictionary.csar differ -- cgit 1.2.3-korg