diff options
Diffstat (limited to 'catalog-be/src')
6 files changed, 194 insertions, 3 deletions
diff --git a/catalog-be/src/main/docker/backend/chef-repo/cookbooks/sdc-catalog-be/files/default/error-configuration.yaml b/catalog-be/src/main/docker/backend/chef-repo/cookbooks/sdc-catalog-be/files/default/error-configuration.yaml index a4491de352..d2a79909d6 100644 --- a/catalog-be/src/main/docker/backend/chef-repo/cookbooks/sdc-catalog-be/files/default/error-configuration.yaml +++ b/catalog-be/src/main/docker/backend/chef-repo/cookbooks/sdc-catalog-be/files/default/error-configuration.yaml @@ -2398,3 +2398,10 @@ errors: message: 'Error: Invalid Content. %1 has invalid format.', messageId: "SVC4723" } +#---------SVC4732------------------------------ + # %1 - list of validation errors + INVALID_PM_DICTIONARY_FILE: { + code: 400, + message: 'Error: Invalid PM Dictionary File. %1', + messageId: "SVC4732" + } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java index b6efd3ef0b..374e98e61f 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java @@ -16,7 +16,7 @@ * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= - * Modifications copyright (c) 2019 Nokia + * Modifications copyright (c) 2020 Nokia * ================================================================================ */ @@ -69,6 +69,7 @@ import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentEx import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException; import org.openecomp.sdc.be.components.impl.exceptions.ComponentException; import org.openecomp.sdc.be.components.impl.utils.ComponentUtils; +import org.openecomp.sdc.be.components.impl.validation.PMDictionaryValidator; import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic; import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction; import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction.LifecycleChanceActionEnum; @@ -2630,6 +2631,12 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic { String artifactType = artifactInfo.getArtifactType(); String fileExtension = GeneralUtility.getFilenameExtension(artifactInfo.getArtifactName()); PayloadTypeEnum payloadType = ArtifactTypeToPayloadTypeSelector.getPayloadType(artifactType, fileExtension); + + final Optional<ResponseFormat> pmDictionaryError = validateIfPmDictionary(artifactType, decodedPayload); + if (pmDictionaryError.isPresent()) { + return Either.right(pmDictionaryError.get()); + } + Either<Boolean, ActionStatus> isPayloadValid = payloadType.isValid(decodedPayload); if (isPayloadValid.isRight()) { ResponseFormat responseFormat = componentsUtils.getResponseFormat(isPayloadValid.right().value(), artifactType); @@ -2659,6 +2666,16 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic { return Either.left(decodedPayload); } + private Optional<ResponseFormat> validateIfPmDictionary(String artifactType, byte[] decodedPayload) { + return new PMDictionaryValidator() + .validateIfPmDictionary(artifactType, decodedPayload) + .map(this::preparePmDictionaryResponse); + } + + private ResponseFormat preparePmDictionaryResponse(String errorMessage) { + return componentsUtils.getResponseFormat(ActionStatus.INVALID_PM_DICTIONARY_FILE, errorMessage); + } + public Either<ArtifactDefinition, ResponseFormat> deleteArtifactByInterface( String resourceId, String userUserId, String artifactId, boolean inTransaction) { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/artifact/PayloadTypeEnum.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/artifact/PayloadTypeEnum.java index df6a552917..4a71d4bc86 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/artifact/PayloadTypeEnum.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/artifact/PayloadTypeEnum.java @@ -17,7 +17,7 @@ * * * * SPDX-License-Identifier: Apache-2.0 * * ============LICENSE_END========================================================= - * + * * Modifications copyright (c) 2020 Nokia */ package org.openecomp.sdc.be.components.impl.artifact; @@ -144,9 +144,9 @@ public enum PayloadTypeEnum { private static Either<Boolean, ActionStatus> isValidYaml(byte[] payload) { YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter(); if (yamlToObjectConverter.isValidYaml(payload)) { - log.debug("Invalid YAML format"); return Either.left(true); } + log.debug("Invalid YAML format"); return Either.right(ActionStatus.INVALID_YAML); } 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 new file mode 100644 index 0000000000..84bebe3bf6 --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/validation/PMDictionaryValidator.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.openecomp.sdc.be.components.impl.validation; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import org.onap.validation.yaml.YamlContentValidator; +import org.onap.validation.yaml.error.YamlDocumentValidationError; +import org.openecomp.sdc.common.api.ArtifactTypeEnum; + +public class PMDictionaryValidator { + + private final YamlContentValidator yamlContentValidator; + + public PMDictionaryValidator() { + this(new YamlContentValidator()); + } + + PMDictionaryValidator(YamlContentValidator yamlContentValidator) { + this.yamlContentValidator = yamlContentValidator; + } + + public Optional<String> validateIfPmDictionary(String artifactType, byte[] decodedPayload) { + if (isPmDictionary(artifactType)) { + return validate(decodedPayload).stream() + .reduce((a, b) -> a + "; " + b); + } + return Optional.empty(); + } + + private boolean isPmDictionary(String artifactType) { + return ArtifactTypeEnum.PM_DICTIONARY.name().equals(artifactType); + } + + private List<String> validate(byte[] fileContent) { + List<String> errors = new ArrayList<>(); + try { + List<YamlDocumentValidationError> validationErrors = yamlContentValidator.validate(fileContent); + validationErrors.stream() + .map(this::formatErrorMessage) + .forEach(errors::add); + } catch (Exception e) { + errors.add(e.getMessage()); + } + return errors; + } + + private String formatErrorMessage(YamlDocumentValidationError error) { + return String.format("Line number: %d, Path: %s, Message: %s", + error.getYamlDocumentNumber(), + error.getPath(), + error.getMessage()); + } + +} diff --git a/catalog-be/src/main/resources/config/error-configuration.yaml b/catalog-be/src/main/resources/config/error-configuration.yaml index a4491de352..14b4ece4b0 100644 --- a/catalog-be/src/main/resources/config/error-configuration.yaml +++ b/catalog-be/src/main/resources/config/error-configuration.yaml @@ -2398,3 +2398,10 @@ errors: message: 'Error: Invalid Content. %1 has invalid format.', messageId: "SVC4723" } +#---------SVC4732------------------------------ + # %1 - list of validation errors + INVALID_PM_DICTIONARY_FILE: { + code: 400, + message: 'Error: Invalid PM Dictionary File. %1', + messageId: "SVC4732" + }
\ No newline at end of file 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 new file mode 100644 index 0000000000..7e62e6dace --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/validation/PMDictionaryValidatorTest.java @@ -0,0 +1,87 @@ +/*- + * ============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.openecomp.sdc.be.components.impl.validation; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verifyNoInteractions; +import static org.mockito.Mockito.when; + +import java.util.List; +import java.util.Optional; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.onap.validation.yaml.YamlContentValidator; +import org.onap.validation.yaml.error.YamlDocumentValidationError; +import org.onap.validation.yaml.exception.YamlProcessingException; +import org.openecomp.sdc.common.api.ArtifactTypeEnum; + +class PMDictionaryValidatorTest { + + private YamlContentValidator yamlContentValidator; + + @BeforeEach + void setUp() { + yamlContentValidator = mock(YamlContentValidator.class); + } + + @Test + void shouldNotReturnErrors_whenArtifactTypeDoNotMatch() { + // when + Optional<String> errors = new PMDictionaryValidator(yamlContentValidator) + .validateIfPmDictionary(ArtifactTypeEnum.DCAE_INVENTORY_BLUEPRINT.name(), "".getBytes()); + + // then + assertTrue(errors.isEmpty()); + verifyNoInteractions(yamlContentValidator); + } + + @Test + void shouldReturnErrors_whenArtifactTypeIsPmDictionaryAndFileIsInvalid() throws YamlProcessingException { + // given + byte[] fileContent = "".getBytes(); + YamlDocumentValidationError validationError = new YamlDocumentValidationError(1, "/", "error"); + when(yamlContentValidator.validate(fileContent)).thenReturn(List.of(validationError)); + + // when + Optional<String> errors = new PMDictionaryValidator(yamlContentValidator) + .validateIfPmDictionary(ArtifactTypeEnum.PM_DICTIONARY.name(), fileContent); + + // then + assertTrue(errors.isPresent()); + assertThat(errors.get(), is("Line number: 1, Path: /, Message: error")); + } + + @Test + void shouldNotReturnErrors_whenArtifactTypeIsPmDictionaryAndFileIsValid() throws YamlProcessingException { + // given + byte[] fileContent = "".getBytes(); + when(yamlContentValidator.validate(fileContent)).thenReturn(List.of()); + + // when + Optional<String> errors = new PMDictionaryValidator(yamlContentValidator) + .validateIfPmDictionary(ArtifactTypeEnum.PM_DICTIONARY.name(), fileContent); + + // then + assertTrue(errors.isEmpty()); + } +} |