From e0a0f45ecaf19e0122e745e8301b79ca4baf2e42 Mon Sep 17 00:00:00 2001 From: Bogumil Zebek Date: Wed, 18 Sep 2019 11:37:56 +0200 Subject: Sonar fix Change-Id: I7c39a2d83d067382ffb91ed136ad840dab85ccd8 Issue-ID: VNFSDK-477 Signed-off-by: Zebek Bogumil --- .../cvc/csar/DefinitionMetadataParserTest.java | 188 +++++++++++++++++++++ .../cvc/csar/cc/sol004/IntegrationTestUtils.java | 2 +- 2 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 csarvalidation/src/test/java/org/onap/cvc/csar/DefinitionMetadataParserTest.java (limited to 'csarvalidation/src/test/java/org') diff --git a/csarvalidation/src/test/java/org/onap/cvc/csar/DefinitionMetadataParserTest.java b/csarvalidation/src/test/java/org/onap/cvc/csar/DefinitionMetadataParserTest.java new file mode 100644 index 0000000..e8adde8 --- /dev/null +++ b/csarvalidation/src/test/java/org/onap/cvc/csar/DefinitionMetadataParserTest.java @@ -0,0 +1,188 @@ +/* + * Copyright 2019 Nokia + *

+ * 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. + * + */ + +package org.onap.cvc.csar; + +import org.apache.commons.lang3.tuple.Pair; +import org.junit.Test; +import org.onap.cvc.csar.parser.vnf.DefinitionMetadataParser; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.onap.cvc.csar.CSARArchive.Definition; +import static org.onap.cvc.csar.CSARArchive.ENTRY_DEFINITION_TEMPLATE_AUTHOR; +import static org.onap.cvc.csar.CSARArchive.ENTRY_DEFINITION_TEMPLATE_NAME; +import static org.onap.cvc.csar.CSARArchive.ENTRY_DEFINITION_TEMPLATE_VERSION; +import static org.onap.cvc.csar.CSARArchive.ENTRY_DEFINITION_TOSCA_DEFINITIONS_VERSION; +import static org.onap.cvc.csar.cc.sol004.IntegrationTestUtils.convertToMessagesList; + +public class DefinitionMetadataParserTest { + + private static final String DEFINITION_YAML = "definition.yaml"; + + @Test + public void shouldReportThatDefinitionYamlDataAreEmpty(){ + // given + DefinitionMetadataParser definitionMetadataParser = new DefinitionMetadataParser( + new HashMap<>(), DEFINITION_YAML, CSARArchive.Mode.WITHOUT_TOSCA_META_DIR); + + // when + final Pair> data = definitionMetadataParser.parse(); + + // then + assertThat(convertToMessagesList(data.getRight())).contains( + "Missing. Entry [tosca_definitions_version]" + ); + } + + @Test + public void shouldReportThatDefinitionYamlDataIsInUnsupportedVersion(){ + // given + final HashMap yaml = givenEmptyYamlData("tosca_simple_yaml_0_0"); + DefinitionMetadataParser definitionMetadataParser = new DefinitionMetadataParser( + yaml, DEFINITION_YAML, CSARArchive.Mode.WITHOUT_TOSCA_META_DIR); + + // when + final Pair> data = definitionMetadataParser.parse(); + + // then + assertThat(convertToMessagesList(data.getRight())).contains( + "Invalid. Entry [tosca_definitions_version]. Should be tosca_simple_yaml_1_1" + ); + } + + @Test + public void shouldSetOnlyAVersionWhenMetaIsForToscaDirMode(){ + // given + final HashMap yaml = givenEmptyYamlData("tosca_simple_yaml_1_1"); + DefinitionMetadataParser definitionMetadataParser = new DefinitionMetadataParser( + yaml, DEFINITION_YAML, CSARArchive.Mode.WITH_TOSCA_META_DIR); + + // when + final Pair> data = definitionMetadataParser.parse(); + + // then + final CSARArchive.Definition definition = data.getLeft(); + final List errors = data.getRight(); + assertThat(definition.getToscaDefinitionVersion()).isEqualTo("tosca_simple_yaml_1_1"); + assertThat(errors).isEmpty(); + } + + @Test + public void shouldReportAnErrorWhenMetaIsNotSet(){ + // given + final HashMap yaml = givenEmptyYamlData("tosca_simple_yaml_1_1"); + DefinitionMetadataParser definitionMetadataParser = new DefinitionMetadataParser( + yaml, DEFINITION_YAML, CSARArchive.Mode.WITHOUT_TOSCA_META_DIR); + + // when + final Pair> data = definitionMetadataParser.parse(); + + // then + final CSARArchive.Definition definition = data.getLeft(); + final List errors = data.getRight(); + assertThat(definition.getToscaDefinitionVersion()).isEqualTo("tosca_simple_yaml_1_1"); + assertThat(convertToMessagesList(errors)).contains( + "Invalid value. Entry [tosca_definitions_version]. Valid values are [tosca_simple_yaml_1_1]" + ); + } + + @Test + public void shouldPopulateMetadataModel(){ + // given + final Map metadata = givenPopulateMetadataData(); + final HashMap yaml = givenEmptyYamlData("tosca_simple_yaml_1_1"); + yaml.put("metadata", metadata); + + DefinitionMetadataParser definitionMetadataParser = new DefinitionMetadataParser( + yaml, DEFINITION_YAML, CSARArchive.Mode.WITHOUT_TOSCA_META_DIR); + + // when + final Pair> data = definitionMetadataParser.parse(); + + //then + final CSARArchive.Definition definition = data.getLeft(); + final List errors = data.getRight(); + assertThat(definition.getToscaDefinitionVersion()).isEqualTo("tosca_simple_yaml_1_1"); + assertThat(convertToMessagesList(errors)).isEmpty(); + final Definition.Metadata metadataModel = definition.getMetadata(); + assertThat(metadataModel.getTemplateAuthor()).isEqualTo("author"); + assertThat(metadataModel.getTemplateName()).isEqualTo("template name"); + assertThat(metadataModel.getTemplateVersion()).isEqualTo("template version"); + } + + @Test + public void shouldReportAnErrorWhenMetadataParameterIsNotSupported(){ + // given + final Map metadata = givenPopulateMetadataData(); + metadata.put("NEW ENTRY DEFINITION TEMPLATE PARAMETER", "some value"); + final HashMap yaml = givenEmptyYamlData("tosca_simple_yaml_1_1"); + yaml.put("metadata", metadata); + + DefinitionMetadataParser definitionMetadataParser = new DefinitionMetadataParser( + yaml, DEFINITION_YAML, CSARArchive.Mode.WITHOUT_TOSCA_META_DIR); + + // when + final Pair> data = definitionMetadataParser.parse(); + + // then + final List errors = data.getRight(); + assertThat(convertToMessagesList(errors)).contains( + "Ignored. Entry [NEW ENTRY DEFINITION TEMPLATE PARAMETER]" + ); + } + + @Test + public void shouldReportErrorsWhenMetadataModelIsEmpty(){ + // given + final Map metadata = new HashMap<>(); + final HashMap yaml = givenEmptyYamlData("tosca_simple_yaml_1_1"); + yaml.put("metadata", metadata); + + DefinitionMetadataParser definitionMetadataParser = new DefinitionMetadataParser( + yaml, DEFINITION_YAML, CSARArchive.Mode.WITHOUT_TOSCA_META_DIR); + + // when + final Pair> data = definitionMetadataParser.parse(); + + // then + final List errors = data.getRight(); + assertThat(convertToMessagesList(errors)).contains( + "Missing. Entry [template_author]", + "Missing. Entry [template_name]", + "Missing. Entry [template_version]" + ); + } + + + private HashMap givenEmptyYamlData(String tosca_simple_yaml_0_0) { + final HashMap yaml = new HashMap<>(); + yaml.put(ENTRY_DEFINITION_TOSCA_DEFINITIONS_VERSION, tosca_simple_yaml_0_0); + return yaml; + } + + private Map givenPopulateMetadataData() { + final Map metadata = new HashMap<>(); + metadata.put(ENTRY_DEFINITION_TEMPLATE_AUTHOR, "author"); + metadata.put(ENTRY_DEFINITION_TEMPLATE_NAME, "template name"); + metadata.put(ENTRY_DEFINITION_TEMPLATE_VERSION, "template version"); + return metadata; + } +} diff --git a/csarvalidation/src/test/java/org/onap/cvc/csar/cc/sol004/IntegrationTestUtils.java b/csarvalidation/src/test/java/org/onap/cvc/csar/cc/sol004/IntegrationTestUtils.java index a0bb25d..6f183d7 100644 --- a/csarvalidation/src/test/java/org/onap/cvc/csar/cc/sol004/IntegrationTestUtils.java +++ b/csarvalidation/src/test/java/org/onap/cvc/csar/cc/sol004/IntegrationTestUtils.java @@ -84,7 +84,7 @@ public class IntegrationTestUtils { } } - static List convertToMessagesList(List errors) { + public static List convertToMessagesList(List errors) { return errors.stream().map(CSARArchive.CSARError::getMessage).collect(Collectors.toList()); } } -- cgit 1.2.3-korg