From 1b44134b16ac74e8326ba14e1764a9023cc6f0cf Mon Sep 17 00:00:00 2001 From: Maciej Malewski Date: Thu, 10 Dec 2020 14:38:00 +0100 Subject: Integration tests for validating PM_Dictionary. Checking compliance content of the PM_Dictionary file against it's schema. Issue-ID: SDC-3390 Change-Id: I8e20f977c7d6838005bc84fc6c7c8ab197152a53 Signed-off-by: Maciej Malewski --- .../impl/validation/PMDictionaryValidator.java | 2 +- .../impl/validation/PMDictionaryValidatorTest.java | 2 +- integration-tests/pom.xml | 7 ++ .../tests/validation/pmdictionary/CsarLoader.java | 54 +++++++++++++ .../pmdictionary/CsarValidationTest.java | 73 +++++++++++++++++ .../validation/pmdictionary/ZipValidationTest.java | 90 +++++++++++++++++++++ .../invalidPnfCompliantWithSOL004.csar | Bin 0 -> 104109 bytes .../pmdictionary/validPnfCompliantWithSOL004.csar | Bin 0 -> 104106 bytes .../VNFs/validation/pmdictionary/invalidPmDict.zip | Bin 0 -> 3234 bytes .../VNFs/validation/pmdictionary/validPmDict.zip | Bin 0 -> 3233 bytes .../validation/util/ValidationManagerUtilTest.java | 2 +- .../csar/validation/PMDictionaryValidator.java | 9 ++- .../csar/validation/PMDictionaryValidatorTest.java | 4 +- 13 files changed, 237 insertions(+), 6 deletions(-) create mode 100644 integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/validation/pmdictionary/CsarLoader.java create mode 100644 integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/validation/pmdictionary/CsarValidationTest.java create mode 100644 integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/validation/pmdictionary/ZipValidationTest.java create mode 100644 integration-tests/src/test/resources/Files/PNFs/validation/pmdictionary/invalidPnfCompliantWithSOL004.csar create mode 100644 integration-tests/src/test/resources/Files/PNFs/validation/pmdictionary/validPnfCompliantWithSOL004.csar create mode 100644 integration-tests/src/test/resources/Files/VNFs/validation/pmdictionary/invalidPmDict.zip create mode 100644 integration-tests/src/test/resources/Files/VNFs/validation/pmdictionary/validPmDict.zip diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/validation/PMDictionaryValidator.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/validation/PMDictionaryValidator.java index 84bebe3bf6..eee84bb14b 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/validation/PMDictionaryValidator.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/validation/PMDictionaryValidator.java @@ -64,7 +64,7 @@ public class PMDictionaryValidator { } private String formatErrorMessage(YamlDocumentValidationError error) { - return String.format("Line number: %d, Path: %s, Message: %s", + return String.format("Document number: %d, Path: %s, Message: %s", error.getYamlDocumentNumber(), error.getPath(), error.getMessage()); diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/validation/PMDictionaryValidatorTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/validation/PMDictionaryValidatorTest.java index 7e62e6dace..67c310e5bf 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/validation/PMDictionaryValidatorTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/validation/PMDictionaryValidatorTest.java @@ -68,7 +68,7 @@ class PMDictionaryValidatorTest { // then assertTrue(errors.isPresent()); - assertThat(errors.get(), is("Line number: 1, Path: /, Message: error")); + assertThat(errors.get(), is("Document number: 1, Path: /, Message: error")); } @Test diff --git a/integration-tests/pom.xml b/integration-tests/pom.xml index c990ef2836..76b6b039d4 100644 --- a/integration-tests/pom.xml +++ b/integration-tests/pom.xml @@ -3,6 +3,7 @@ SDC ================================================================================ Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. +Modifications copyright (c) 2020 Nokia ================================================================================ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -104,6 +105,12 @@ limitations under the License. ${project.version} test + + org.openecomp.sdc + openecomp-sdc-vendor-software-product-manager + ${project.version} + test + com.aventstack extentreports diff --git a/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/validation/pmdictionary/CsarLoader.java b/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/validation/pmdictionary/CsarLoader.java new file mode 100644 index 0000000000..ca80ecee4c --- /dev/null +++ b/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/validation/pmdictionary/CsarLoader.java @@ -0,0 +1,54 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ +package org.onap.sdc.backend.ci.tests.validation.pmdictionary; + +import org.apache.commons.io.IOUtils; +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum; +import org.openecomp.sdc.vendorsoftwareproduct.exception.OnboardPackageException; +import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackage; +import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackageInfo; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; + +class CsarLoader { + + private CsarLoader() { + } + + static FileContentHandler load(String csarFileName, String csarFilePath) throws IOException, OnboardPackageException { + InputStream inputStream = CsarLoader.class.getResourceAsStream(csarFilePath); + OnboardPackage onboardPackage = getOnboardPackage(csarFileName, inputStream); + return onboardPackage.getFileContentHandler(); + } + + private static OnboardPackage getOnboardPackage(String csarFileName, InputStream inputStream) throws OnboardPackageException, IOException { + OnboardPackageInfo onboardPackageInfo = new OnboardPackageInfo(csarFileName, OnboardingTypesEnum.CSAR.name(), + convertFileInputStream(inputStream), OnboardingTypesEnum.CSAR); + return onboardPackageInfo.getOnboardPackage(); + } + + private static ByteBuffer convertFileInputStream(final InputStream fileInputStream) throws IOException { + byte[] fileContent = IOUtils.toByteArray(fileInputStream); + return ByteBuffer.wrap(fileContent); + } +} diff --git a/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/validation/pmdictionary/CsarValidationTest.java b/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/validation/pmdictionary/CsarValidationTest.java new file mode 100644 index 0000000000..e12e058d6c --- /dev/null +++ b/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/validation/pmdictionary/CsarValidationTest.java @@ -0,0 +1,73 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ +package org.onap.sdc.backend.ci.tests.validation.pmdictionary; + +import org.junit.jupiter.api.Test; +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.sdc.datatypes.error.ErrorLevel; +import org.openecomp.sdc.datatypes.error.ErrorMessage; +import org.openecomp.sdc.vendorsoftwareproduct.exception.OnboardPackageException; +import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.ValidatorFactory; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +class CsarValidationTest { + + @Test + void shouldNotReturnErrors_whenPnfCsarIsValid() throws OnboardPackageException, IOException { + //given + FileContentHandler pnfFileContent = CsarLoader.load("validPnfCompliantWithSOL004.csar", "/Files/PNFs/validation/pmdictionary/validPnfCompliantWithSOL004.csar"); + + //when + Map> errorsMap = ValidatorFactory.getValidator(pnfFileContent).validateContent(pnfFileContent); + + //then + assertThat(errorsMap, is(anEmptyMap())); + } + + @Test + void shouldReturnError_whenPMDictionaryContentIsNotCompliantWithSchema() throws OnboardPackageException, IOException { + //given + String expectedErrorMessage = "Document number: 1, Path: /pmMetaData/, Message: Key not found: pmHeader"; + FileContentHandler pnfFileContent = CsarLoader.load("invalidPnfCompliantWithSOL004.csar", "/Files/PNFs/validation/pmdictionary/invalidPnfCompliantWithSOL004.csar"); + + //when + Map> errorMap = ValidatorFactory.getValidator(pnfFileContent).validateContent(pnfFileContent); + List errorList = errorMap.get("uploadFile"); + + //then + assertThat(errorList, is(not(empty()))); + assertThat(getActualErrorMessage(errorList), is(equalTo(expectedErrorMessage))); + assertThat(getActualErrorLevel(errorList), is(ErrorLevel.ERROR)); + } + + private String getActualErrorMessage(List errorList) { + return errorList.get(0).getMessage(); + } + + private ErrorLevel getActualErrorLevel(List errorList) { + return errorList.get(0).getLevel(); + } +} diff --git a/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/validation/pmdictionary/ZipValidationTest.java b/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/validation/pmdictionary/ZipValidationTest.java new file mode 100644 index 0000000000..d3287d29b6 --- /dev/null +++ b/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/validation/pmdictionary/ZipValidationTest.java @@ -0,0 +1,90 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ +package org.onap.sdc.backend.ci.tests.validation.pmdictionary; + +import org.junit.jupiter.api.Test; +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum; +import org.openecomp.sdc.common.utils.CommonUtil; +import org.openecomp.sdc.datatypes.error.ErrorLevel; +import org.openecomp.sdc.datatypes.error.ErrorMessage; +import org.openecomp.sdc.validation.util.ValidationManagerUtil; + +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.assertThat; + +class ZipValidationTest { + + private static final String PM_DICTIONARY_FILE = "pmdict.yaml"; + private static final String ZIPS_PATH = "Files/VNFs/validation/pmdictionary"; + + @Test + void shouldReportOnlyFileNotReferenced_whenValidPmDictionaryZip() throws IOException { + // given + FileContentHandler fileContentHandler = getFileContentHandler("validPmDict.zip"); + + // when + Map> errors = ValidationManagerUtil.initValidationManager(fileContentHandler) + .validate(); + List pmDictValidationErrors = extractPmDictValidationErrors(errors); + + // then + assertThat(pmDictValidationErrors).isEmpty(); + } + + @Test + void shouldReportPmDictionaryErrors_whenInvalidPmDictionaryZip() throws IOException { + // given + FileContentHandler fileContentHandler = getFileContentHandler("invalidPmDict.zip"); + ErrorMessage errorMessage = new ErrorMessage(ErrorLevel.ERROR, + "ERROR: [PM_DICT]: Document Number: 1, Path: /pmMetaData/, Problem: Key not found: pmHeader"); + + + // when + Map> errors = ValidationManagerUtil.initValidationManager(fileContentHandler) + .validate(); + List pmDictValidationErrors = extractPmDictValidationErrors(errors); + + // then + assertThat(pmDictValidationErrors) + .hasSize(1) + .containsExactly(errorMessage); + } + + private FileContentHandler getFileContentHandler(String filename) throws IOException { + return CommonUtil.validateAndUploadFileContent(OnboardingTypesEnum.ZIP, + Objects.requireNonNull(this.getClass() + .getClassLoader() + .getResourceAsStream(ZIPS_PATH + "/" + filename)) + .readAllBytes()); + } + + private List extractPmDictValidationErrors(Map> errors) { + return errors.getOrDefault(PM_DICTIONARY_FILE, List.of()) + .stream() + .filter(error -> error.getMessage().contains("[PM_DICT]")) + .collect(Collectors.toList()); + } +} diff --git a/integration-tests/src/test/resources/Files/PNFs/validation/pmdictionary/invalidPnfCompliantWithSOL004.csar b/integration-tests/src/test/resources/Files/PNFs/validation/pmdictionary/invalidPnfCompliantWithSOL004.csar new file mode 100644 index 0000000000..2929843825 Binary files /dev/null and b/integration-tests/src/test/resources/Files/PNFs/validation/pmdictionary/invalidPnfCompliantWithSOL004.csar differ diff --git a/integration-tests/src/test/resources/Files/PNFs/validation/pmdictionary/validPnfCompliantWithSOL004.csar b/integration-tests/src/test/resources/Files/PNFs/validation/pmdictionary/validPnfCompliantWithSOL004.csar new file mode 100644 index 0000000000..3574b72393 Binary files /dev/null and b/integration-tests/src/test/resources/Files/PNFs/validation/pmdictionary/validPnfCompliantWithSOL004.csar differ diff --git a/integration-tests/src/test/resources/Files/VNFs/validation/pmdictionary/invalidPmDict.zip b/integration-tests/src/test/resources/Files/VNFs/validation/pmdictionary/invalidPmDict.zip new file mode 100644 index 0000000000..2c8c3691ec Binary files /dev/null and b/integration-tests/src/test/resources/Files/VNFs/validation/pmdictionary/invalidPmDict.zip differ diff --git a/integration-tests/src/test/resources/Files/VNFs/validation/pmdictionary/validPmDict.zip b/integration-tests/src/test/resources/Files/VNFs/validation/pmdictionary/validPmDict.zip new file mode 100644 index 0000000000..b1a49feea8 Binary files /dev/null and b/integration-tests/src/test/resources/Files/VNFs/validation/pmdictionary/validPmDict.zip differ diff --git a/openecomp-be/backend/openecomp-sdc-validation-manager/src/test/java/org/openecomp/sdc/validation/util/ValidationManagerUtilTest.java b/openecomp-be/backend/openecomp-sdc-validation-manager/src/test/java/org/openecomp/sdc/validation/util/ValidationManagerUtilTest.java index b151ea2e8b..a5fa97566b 100644 --- a/openecomp-be/backend/openecomp-sdc-validation-manager/src/test/java/org/openecomp/sdc/validation/util/ValidationManagerUtilTest.java +++ b/openecomp-be/backend/openecomp-sdc-validation-manager/src/test/java/org/openecomp/sdc/validation/util/ValidationManagerUtilTest.java @@ -58,4 +58,4 @@ public class ValidationManagerUtilTest { assertEquals(errors.get(SdcCommon.MANIFEST_NAME).get(0).getMessage(), "Manifest doesn't exist"); } -} \ No newline at end of file +} diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/PMDictionaryValidator.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/PMDictionaryValidator.java index a91dd9f445..90270c2f7e 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/PMDictionaryValidator.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/PMDictionaryValidator.java @@ -41,11 +41,18 @@ public class PMDictionaryValidator { try { List validationErrors = new YamlContentValidator().validate(fileContent); validationErrors.stream() - .map(YamlDocumentValidationError::getMessage) + .map(this::formatErrorMessage) .forEach(errors::add); } catch (Exception e) { errors.add(e.getMessage()); } return errors; } + + private String formatErrorMessage(YamlDocumentValidationError error) { + return String.format("Document number: %d, Path: %s, Message: %s", + error.getYamlDocumentNumber(), + error.getPath(), + error.getMessage()); + } } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/PMDictionaryValidatorTest.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/PMDictionaryValidatorTest.java index 22ef7720dc..1aec5570a2 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/PMDictionaryValidatorTest.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/PMDictionaryValidatorTest.java @@ -58,7 +58,7 @@ class PMDictionaryValidatorTest { // then assertThat(errors.size(), is(1)); - assertThat(errors.get(0), is("Key not found: pmDictionaryHeader")); + assertThat(errors.get(0), is("Document number: 1, Path: /pmDictionary/, Message: Key not found: pmDictionaryHeader")); } @Test @@ -74,4 +74,4 @@ class PMDictionaryValidatorTest { assertThat(errors.size(), is(1)); assertThat(errors.get(0), is("PM_Dictionary YAML file is empty")); } -} \ No newline at end of file +} -- cgit 1.2.3-korg