From cba3b4f6a26aee3009a56b899a1cb9f093aba805 Mon Sep 17 00:00:00 2001 From: Serban Jora Date: Mon, 2 Oct 2017 13:20:50 -0400 Subject: Add unit testing JUnit based testing added to the checker lib and service, work in progress. Includes grammar correction. Change-Id: I8fa950f89faba24e7b45ef41cbc0402362b3f394 Issue-Id: MODELING-26 Signed-off-by: Serban Jora --- javatoscachecker/checker/pom.xml | 13 + .../main/java/org/onap/tosca/checker/Checker.java | 2 +- .../resources/tosca/tosca_simple_yaml_1_1.grammar | 7 +- .../org/onap/tosca/checker/test/Annotations.java | 49 + .../org/onap/tosca/checker/test/CheckerTest.java | 133 ++ .../org/onap/tosca/checker/test/ModelTest.java | 186 +++ .../java/org/onap/tosca/checker/test/Tools.java | 104 ++ .../checker/test/assertions/CheckerAssert.java | 102 ++ .../checker/test/assertions/CheckerAssertions.java | 30 + .../test/assertions/CheckerBuildAssert.java | 38 + .../src/test/resources/config/annotations.yaml | 11 + .../config/tosca_simple_yaml_1_1_extended.grammar | 1656 ++++++++++++++++++++ .../checker/src/test/resources/models/full.yaml | 107 ++ .../resources/spec/capability_types/basic.yaml | 11 + .../spec/data_types/user_defined_basic.yaml | 25 + .../user_defined_basic_with_defaults.yaml | 27 + ...efined_basic_with_invalid_default_bad_type.yaml | 27 + ...d_basic_with_invalid_default_unknown_value.yaml | 27 + ...r_defined_basic_with_invalid_property_type.yaml | 26 + ..._defined_basic_with_invalid_required_value.yaml | 27 + .../spec/data_types/user_defined_nested.yaml | 33 + .../spec/data_types/user_defined_redeclared.yaml | 18 + .../data_types/user_defined_with_collections.yaml | 30 + .../user_defined_with_common_supertype.yaml | 26 + .../data_types/user_defined_with_hierarchy.yaml | 29 + ...ed_with_hierarchy_and_invalid_redefinition.yaml | 33 + ...er_defined_with_hierarchy_and_redefinition.yaml | 33 + ...ser_defined_with_invalid_entry_schema_type.yaml | 30 + .../user_defined_with_unknown_supertype.yaml | 27 + .../spec/examples/spec-example-11.1.11.3.yaml | 49 + .../spec/examples/spec-example-11.1.12.3.yaml | 45 + .../spec/examples/spec-example-11.1.15.3.yaml | 114 ++ .../resources/spec/examples/spec-example-2.1.yaml | 25 + .../spec/examples/spec-example-5.3.2.2.yaml | 27 + .../spec/examples/spec-example-7.3.3.1.yaml | 39 + .../spec-example-7.3.5-invalid-version.yaml | 45 + .../spec/examples/spec-example-7.3.5.yaml | 42 + .../resources/spec/node_types/node_type_basic.yaml | 26 + .../spec/node_types/node_type_with_hierarchy.yaml | 44 + .../node_type_with_invalid_hierarchy.yaml | 10 + .../node_type_with_user_defined_properties.yaml | 29 + .../resources/spec/topology_template/basic.yaml | 24 + .../basic_invalid_property_assignment.yaml | 24 + javatoscachecker/service/pom.xml | 19 + .../checker/service/test/CheckerServiceTest.java | 151 ++ .../service/src/test/resources/standalone.yaml | 21 + .../src/test/resources/standalone_with_errors.yaml | 23 + .../service/src/test/resources/test_schema.yaml | 17 + .../service/src/test/resources/test_template.yaml | 11 + 49 files changed, 3647 insertions(+), 5 deletions(-) create mode 100644 javatoscachecker/checker/src/test/java/org/onap/tosca/checker/test/Annotations.java create mode 100644 javatoscachecker/checker/src/test/java/org/onap/tosca/checker/test/CheckerTest.java create mode 100644 javatoscachecker/checker/src/test/java/org/onap/tosca/checker/test/ModelTest.java create mode 100644 javatoscachecker/checker/src/test/java/org/onap/tosca/checker/test/Tools.java create mode 100644 javatoscachecker/checker/src/test/java/org/onap/tosca/checker/test/assertions/CheckerAssert.java create mode 100644 javatoscachecker/checker/src/test/java/org/onap/tosca/checker/test/assertions/CheckerAssertions.java create mode 100644 javatoscachecker/checker/src/test/java/org/onap/tosca/checker/test/assertions/CheckerBuildAssert.java create mode 100644 javatoscachecker/checker/src/test/resources/config/annotations.yaml create mode 100644 javatoscachecker/checker/src/test/resources/config/tosca_simple_yaml_1_1_extended.grammar create mode 100644 javatoscachecker/checker/src/test/resources/models/full.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/capability_types/basic.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/data_types/user_defined_basic.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/data_types/user_defined_basic_with_defaults.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/data_types/user_defined_basic_with_invalid_default_bad_type.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/data_types/user_defined_basic_with_invalid_default_unknown_value.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/data_types/user_defined_basic_with_invalid_property_type.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/data_types/user_defined_basic_with_invalid_required_value.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/data_types/user_defined_nested.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/data_types/user_defined_redeclared.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/data_types/user_defined_with_collections.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/data_types/user_defined_with_common_supertype.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/data_types/user_defined_with_hierarchy.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/data_types/user_defined_with_hierarchy_and_invalid_redefinition.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/data_types/user_defined_with_hierarchy_and_redefinition.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/data_types/user_defined_with_invalid_entry_schema_type.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/data_types/user_defined_with_unknown_supertype.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/examples/spec-example-11.1.11.3.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/examples/spec-example-11.1.12.3.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/examples/spec-example-11.1.15.3.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/examples/spec-example-2.1.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/examples/spec-example-5.3.2.2.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/examples/spec-example-7.3.3.1.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/examples/spec-example-7.3.5-invalid-version.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/examples/spec-example-7.3.5.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/node_types/node_type_basic.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/node_types/node_type_with_hierarchy.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/node_types/node_type_with_invalid_hierarchy.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/node_types/node_type_with_user_defined_properties.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/topology_template/basic.yaml create mode 100644 javatoscachecker/checker/src/test/resources/spec/topology_template/basic_invalid_property_assignment.yaml create mode 100644 javatoscachecker/service/src/test/java/org/onap/tosca/checker/service/test/CheckerServiceTest.java create mode 100644 javatoscachecker/service/src/test/resources/standalone.yaml create mode 100644 javatoscachecker/service/src/test/resources/standalone_with_errors.yaml create mode 100644 javatoscachecker/service/src/test/resources/test_schema.yaml create mode 100644 javatoscachecker/service/src/test/resources/test_template.yaml diff --git a/javatoscachecker/checker/pom.xml b/javatoscachecker/checker/pom.xml index f8bbbd3..3fb0d2f 100644 --- a/javatoscachecker/checker/pom.xml +++ b/javatoscachecker/checker/pom.xml @@ -157,6 +157,19 @@ + + junit + junit + 4.12 + test + + + org.assertj + assertj-core + 3.8.0 + test + + commons-io commons-io diff --git a/javatoscachecker/checker/src/main/java/org/onap/tosca/checker/Checker.java b/javatoscachecker/checker/src/main/java/org/onap/tosca/checker/Checker.java index 4218b85..358dc2b 100644 --- a/javatoscachecker/checker/src/main/java/org/onap/tosca/checker/Checker.java +++ b/javatoscachecker/checker/src/main/java/org/onap/tosca/checker/Checker.java @@ -3651,7 +3651,7 @@ System.out.println(" **** unassigned -> " + unassignedEntry.getKey() + " : " + u private String defaultImportsPath = null; private List grammars = new LinkedList(); - protected CheckerConfiguration() { + public CheckerConfiguration() { withGrammars(Checker.grammarFiles); } diff --git a/javatoscachecker/checker/src/main/resources/tosca/tosca_simple_yaml_1_1.grammar b/javatoscachecker/checker/src/main/resources/tosca/tosca_simple_yaml_1_1.grammar index 39b11d9..abd6c39 100644 --- a/javatoscachecker/checker/src/main/resources/tosca/tosca_simple_yaml_1_1.grammar +++ b/javatoscachecker/checker/src/main/resources/tosca/tosca_simple_yaml_1_1.grammar @@ -643,10 +643,9 @@ _workflow_step_definition: &workflow_step_definition type: str filter: desc: "Filter is a map of attribute name, list of constraint clause that allows to provide a filtering logic" - type: map - mapping: - =: - *constraints_sequence + type: seq + sequence: + - *constraints_sequence activities: desc: "The list of sequential activities to be performed in this step" type: seq diff --git a/javatoscachecker/checker/src/test/java/org/onap/tosca/checker/test/Annotations.java b/javatoscachecker/checker/src/test/java/org/onap/tosca/checker/test/Annotations.java new file mode 100644 index 0000000..807f432 --- /dev/null +++ b/javatoscachecker/checker/src/test/java/org/onap/tosca/checker/test/Annotations.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2017 . 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. + */ +package org.onap.tosca.checker.test; + +import java.util.List; +import java.util.Map; +import java.util.HashMap; +import java.util.Iterator; + +import org.onap.tosca.checker.Checker; +import org.onap.tosca.checker.annotations.Checks; + + +@Checks +public class Annotations { + + private Map annons = new HashMap(); + + @Checks(path="/topology_template/node_templates") + public void check_node_templates_annotations(Map theTemplates, Checker.CheckContext theContext) { + + for (Iterator> i = theTemplates.entrySet().iterator(); i.hasNext(); ) { + Map.Entry e = i.next(); + List nodeAnnons = (List)e.getValue().get("annotations"); + if (nodeAnnons != null) + this.annons.put(e.getKey(), nodeAnnons); + } + } + + public boolean hasAnnotation(String thePath, String theValue) { + + List nodeAnnotations = annons.get(thePath); + if (nodeAnnotations == null) + return false; + + return nodeAnnotations.contains(theValue); + } + +} diff --git a/javatoscachecker/checker/src/test/java/org/onap/tosca/checker/test/CheckerTest.java b/javatoscachecker/checker/src/test/java/org/onap/tosca/checker/test/CheckerTest.java new file mode 100644 index 0000000..6bde63c --- /dev/null +++ b/javatoscachecker/checker/src/test/java/org/onap/tosca/checker/test/CheckerTest.java @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2017 . 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. + */ +package org.onap.tosca.checker.test; + +import java.io.IOException; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.*; + +import static org.onap.tosca.checker.test.assertions.CheckerAssertions.*; + +import org.onap.tosca.checker.Checker; +import org.onap.tosca.checker.Target; + +/** + * Basic tests of Checker + */ +public class CheckerTest { + + + + @Test + public void testCreateChecker() { + assertThatCode(() -> { + new Checker(); + }).doesNotThrowAnyException(); + } + + + @Test + public void testCreateCheckerWithEmptyConfiguraton() { + assertThatCode(() -> { + new Checker(new Checker.CheckerConfiguration()); + }).doesNotThrowAnyException(); + } + + @Test + public void testCreateCheckerWithConfiguratonWithGrammar() { + assertThatCode(() -> { + new Checker(new Checker.CheckerConfiguration() + .withGrammars("config/tosca_simple_yaml_1_1_extended.grammar")); + }).doesNotThrowAnyException(); + } + + @Test + public void testCreateCheckerWithConfiguratonWithPaths() { + assertThatCode(() -> { + new Checker(new Checker.CheckerConfiguration() + .withDefaultImportsPath("config/")); + }).doesNotThrowAnyException(); + } + + /* + * this is the positive test + */ + @Test + public void testCheckGrammarExtension() { + + assertThat(new Checker.CheckerConfiguration() + .withGrammars("config/tosca_simple_yaml_1_1_extended.grammar")) + .builds() + .checks(Tools.getTarget("config/annotations.yaml")) + .withNoErrors(); + } + + /** test that provided extensions get called at every relevant processing stage */ + @Test + public void testCheckExtension() { + + assertThat(new Checker.CheckerConfiguration() + .withGrammars("config/tosca_simple_yaml_1_1_extended.grammar")) + .builds() + .checks(Tools.getTarget("config/annotations.yaml")) + .withNoErrors() + .withTruePredicate(checker -> checker.getHandler(Annotations.class).hasAnnotation("test", "this")); + } + + + @Test + public void testCheckDataTypes() { + testCheckInputFiles("spec/data_types"); + } + + @Test + public void testCheckCapabilityTypes() { + testCheckInputFiles("spec/capability_types"); + } + + @Test + public void testCheckNodeTypes() { + testCheckInputFiles("spec/node_types"); + } + + @Test + public void testCheckTopologyTemplate() { + testCheckInputFiles("spec/topology_template"); + } + + @Test + public void testCheckExamples() { + testCheckInputFiles("spec/examples"); + } + + private void testCheckInputFiles(final String theLocation) { + + //creates a checker for each test file, is that really necessary? + for (String filename: Tools.getResourceFiles(theLocation)) { + assertThat() + .builds() + .checks(Tools.getTarget(theLocation + "/" + filename)) + .withExpectedOutcome(); + } + } + +} + diff --git a/javatoscachecker/checker/src/test/java/org/onap/tosca/checker/test/ModelTest.java b/javatoscachecker/checker/src/test/java/org/onap/tosca/checker/test/ModelTest.java new file mode 100644 index 0000000..d6b3d8b --- /dev/null +++ b/javatoscachecker/checker/src/test/java/org/onap/tosca/checker/test/ModelTest.java @@ -0,0 +1,186 @@ +/* + * Copyright (c) 2017 . 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. + */ +package org.onap.tosca.checker.test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.FixMethodOrder; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runners.MethodSorters; + +import static org.assertj.core.api.Assertions.*; + +import java.util.Map; + +import org.onap.tosca.checker.Target; +import org.onap.tosca.checker.Checker; +import org.onap.tosca.checker.model.*; + +/* + * This is purely a navigational model, no processing, so we test that the data we expect can be found + */ + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class ModelTest { + + static Target target; + static ServiceTemplate template; + + @BeforeClass + public static void initialize() throws Exception { + + Checker checker = new Checker(); + checker.check(Tools.getTarget("models/full.yaml")); + + for (Target t: checker.catalog().targets()) { + if (!t.getReport().isEmpty()) + System.out.println("Model checker issues:\n" + t.getReport()); + } + + target = checker.catalog().targets().iterator().next(); + template = Models.service_template(target); + } + + @Test + public void testServiceTemplate() { + + assertTrue("full service template".equals(template.description())); + } + + @Test + public void testServiceTemplateMetadata() { + + assertTrue(template.metadata() == null); + } + + @Test + public void testServiceTemplateDataTypes() { + + assertTrue(template.data_types() != null); + + for (Map.Entry type: template.data_types().entrySet()) { + assertTrue(type.getKey().startsWith("data_type_")); + + Properties props = type.getValue().properties(); + assertTrue(props != null); + + for (Map.Entry prop: props.entrySet()) { + assertTrue(prop.getKey().startsWith("data_field_")); + } + } + } + + @Test + public void testServiceTemplateCapabilityTypes() { + + assertTrue(template.capability_types() != null); + for (Map.Entry type: template.capability_types().entrySet()) { + assertTrue(type.getKey().matches("capability_type_[0-9]")); + + Properties props = type.getValue().properties(); + assertTrue(props != null); + + for (Map.Entry prop: props.entrySet()) { + assertTrue(prop.getKey().matches("capability_type_[0-9]_property_[0-9]")); + } + } + } + + @Test + public void testServiceTemplateInterfaceTypes() { + + assertTrue(template.interface_types() != null); + for (Map.Entry type: template.interface_types().entrySet()) { + assertTrue(type.getKey().matches("interface_type_[0-9]")); + + Operations ops = type.getValue().operations(); + assertTrue(ops != null); + + for (Map.Entry op: ops.entrySet()) { + assertTrue(op.getKey().matches("interface_type_[0-9]_op_[0-9]")); + } + } + } + + @Test + public void testServiceTemplateRelationshipTypes() { + + assertTrue(template.relationship_types() != null); + for (Map.Entry type: template.relationship_types().entrySet()) { + assertTrue(type.getKey().matches("relationship_type_[0-9]")); + + TypeInterfaces infs = type.getValue().interfaces(); + assertTrue(infs != null); + + for (Map.Entry inf: infs.entrySet()) { + assertTrue(inf.getKey().matches("relationship_type_[0-9]_interface_[0-9]")); + } + } + } + + @Test + public void testServiceTemplateNodeTypes() { + + assertTrue(template.node_types() != null); + for (Map.Entry type: template.node_types().entrySet()) { + assertTrue(type.getKey().matches("node_type_[0-9]")); + + Capabilities caps = type.getValue().capabilities(); + assertTrue(caps != null); + + for (Map.Entry cap: caps.entrySet()) { + assertTrue(cap.getKey().matches("node_type_[0-9]_capability_[0-9]")); + } + } + } + + @Test + public void testServiceTemplateGroupTypes() { + + assertTrue(template.group_types() == null); + } + + @Test + public void testServiceTemplateTopologyTemplate() { + + assertTrue(template.topology_template() != null); + } + + @Test + public void testTopologyTemplateNodeTemplates() { + + assertTrue(template.topology_template().node_templates() != null); + + for (Map.Entry node: template.topology_template().node_templates().entrySet()) { + assertTrue(node.getKey().matches("node_[0-9]")); + + PropertiesAssignments props = node.getValue().properties(); + assertTrue(props != null); + + for (Map.Entry prop: props.entrySet()) { + assertTrue(prop.getKey().matches("node_type_[0-9]_property_[0-9]")); + } + } + } + + @Test + public void testTopologyTemplateRelationshipTemplates() { + + assertTrue(template.topology_template().relationship_templates() != null); + } + +} + diff --git a/javatoscachecker/checker/src/test/java/org/onap/tosca/checker/test/Tools.java b/javatoscachecker/checker/src/test/java/org/onap/tosca/checker/test/Tools.java new file mode 100644 index 0000000..20bc334 --- /dev/null +++ b/javatoscachecker/checker/src/test/java/org/onap/tosca/checker/test/Tools.java @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2017 . 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. + */ +package org.onap.tosca.checker.test; + +import java.util.List; +import java.util.ArrayList; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.UncheckedIOException; + +import java.net.URI; +import java.net.URISyntaxException; + +import org.onap.tosca.checker.Target; + + + +/** + * Credit for getResourceFiles goes to + * https://stackoverflow.com/questions/3923129/get-a-list-of-resources-from-classpath-directory + * + * The spring framework contains classpath URL handler but I wanted to avoid bring all that in here. + */ +public class Tools { + + public static List getResourceFiles(String thePath) { + List filenames = new ArrayList<>(); + + try { + InputStream in = getResourceAsStream(thePath); + BufferedReader br = new BufferedReader(new InputStreamReader(in)); + + try { + String resource; + + while( (resource = br.readLine()) != null ) { + filenames.add( resource ); + } + } + finally { + try { br.close(); } catch (IOException iox) {} + } + } + catch (IOException iox) { + throw new UncheckedIOException(iox); + } + + return filenames; + } + + private static InputStream getResourceAsStream(String theResource) { + final InputStream in + = getContextClassLoader().getResourceAsStream(theResource); + + return in == null ? in.getClass().getResourceAsStream(theResource) : in; + } + + private static ClassLoader getContextClassLoader() { + return Thread.currentThread().getContextClassLoader(); + } + + public static Target getTarget(String theClassPath) { + try { + return new ClasspathTarget(theClassPath); + } + catch(URISyntaxException urix) { + throw new RuntimeException(urix); + } + } + + /** + * Simpler than handling classpath URIs + */ + public static class ClasspathTarget extends Target { + + public ClasspathTarget(String theLocation) throws URISyntaxException { + super(theLocation, new URI("classpath:" + theLocation)); + } + + @Override + public Reader open() throws IOException { + + return new BufferedReader( + new InputStreamReader( + getResourceAsStream(getName()))); + } + } + + +} diff --git a/javatoscachecker/checker/src/test/java/org/onap/tosca/checker/test/assertions/CheckerAssert.java b/javatoscachecker/checker/src/test/java/org/onap/tosca/checker/test/assertions/CheckerAssert.java new file mode 100644 index 0000000..c973c08 --- /dev/null +++ b/javatoscachecker/checker/src/test/java/org/onap/tosca/checker/test/assertions/CheckerAssert.java @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2017 . 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. + */ +package org.onap.tosca.checker.test.assertions; + +import java.util.Map; + +import java.util.function.Predicate; + +import org.assertj.core.api.AbstractAssert; +import static org.assertj.core.api.Assertions.*; + +import org.onap.tosca.checker.Checker; +import org.onap.tosca.checker.Target; +import org.onap.tosca.checker.Report; +import org.onap.tosca.checker.Catalog; + +/** + */ +public class CheckerAssert extends AbstractAssert { + + protected CheckerAssert(Checker theActualChecker) { + super(theActualChecker, CheckerAssert.class); + } + + public CheckerAssert checks(String theTarget) { + assertThatCode(() -> { + this.actual.check(theTarget); + }).doesNotThrowAnyException(); + + return this; + } + + public CheckerAssert checks(Target theTarget) { + assertThatCode(() -> { + this.actual.check(theTarget); + }).doesNotThrowAnyException(); + + return this; + } + + public CheckerAssert withNoErrors() { + Catalog cat = this.actual.catalog(); + for (Target t: cat.targets()) { + assertThat(t.getReport().isEmpty()) + .as("No errors were expected for " + t + "\n" + t.getReport()) + .isTrue(); + } + + return this; + } + + public CheckerAssert withExpectedOutcome() { + Catalog cat = this.actual.catalog(); + for (Target t: cat.targets()) { + + //no content means that parsing might have failed .. + Map content = (Map)t.getTarget(); + assertThat(content != null) + .as("Parsing failed for " + t + "\n" + t.getReport()) + .isTrue(); + + String outcome = "success"; + Map metadata = (Map)content.get("metadata"); + //assertThat(metadata != null) + // .as("No metadata was defined in " + t) + // .isTrue(); + if (metadata != null) { + outcome = (String)metadata.get("outcome"); + } + //assertThat(outcome != null) + // .as("No expected outcome was defined in " + t) + // .isTrue(); + + if ("fail".equals(outcome)) + assertThat(t.getReport().isEmpty()) + .as("Checking was expected to fail but succeeded for " + t) + .isFalse(); //should go further and make sure teh expected error was generated + else if ("success".equals(outcome)) + assertThat(t.getReport().isEmpty()) + .as("Checking was expected to succeed but failed for " + t + "\n" + t.getReport()) + .isTrue(); //should go further and make sure teh expected error was generated + } + + return this; + } + + public CheckerAssert withTruePredicate(Predicate thePredicate) { + assertThat(thePredicate.test(this.actual)) + .isTrue(); + return this; + } +} diff --git a/javatoscachecker/checker/src/test/java/org/onap/tosca/checker/test/assertions/CheckerAssertions.java b/javatoscachecker/checker/src/test/java/org/onap/tosca/checker/test/assertions/CheckerAssertions.java new file mode 100644 index 0000000..1478512 --- /dev/null +++ b/javatoscachecker/checker/src/test/java/org/onap/tosca/checker/test/assertions/CheckerAssertions.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2017 . 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. + */ +package org.onap.tosca.checker.test.assertions; + +import org.onap.tosca.checker.Checker; + + +/** */ +public class CheckerAssertions { + + public static CheckerBuildAssert assertThat() { + return new CheckerBuildAssert(null); + } + + public static CheckerBuildAssert assertThat(Checker.CheckerConfiguration theConfig) { + return new CheckerBuildAssert(theConfig); + } + +} + diff --git a/javatoscachecker/checker/src/test/java/org/onap/tosca/checker/test/assertions/CheckerBuildAssert.java b/javatoscachecker/checker/src/test/java/org/onap/tosca/checker/test/assertions/CheckerBuildAssert.java new file mode 100644 index 0000000..483ba85 --- /dev/null +++ b/javatoscachecker/checker/src/test/java/org/onap/tosca/checker/test/assertions/CheckerBuildAssert.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2017 . 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. + */ +package org.onap.tosca.checker.test.assertions; + +import org.assertj.core.api.AbstractAssert; + +import org.onap.tosca.checker.Checker; +import org.onap.tosca.checker.CheckerException; + +/** + */ +public class CheckerBuildAssert extends AbstractAssert { + + protected CheckerBuildAssert(Checker.CheckerConfiguration theConfig) { + super(theConfig, CheckerBuildAssert.class); + } + + public CheckerAssert builds() { + try { + return new CheckerAssert(this.actual == null ? new Checker() : new Checker(this.actual)); + } + catch (CheckerException cx) { + failWithMessage("Failed to build checker with given configuration: %s", cx.getMessage()); + return null; //we should not make it here + } + } + +} diff --git a/javatoscachecker/checker/src/test/resources/config/annotations.yaml b/javatoscachecker/checker/src/test/resources/config/annotations.yaml new file mode 100644 index 0000000..2102a22 --- /dev/null +++ b/javatoscachecker/checker/src/test/resources/config/annotations.yaml @@ -0,0 +1,11 @@ +tosca_definitions_version: tosca_simple_yaml_1_1_x +description: test extension + +topology_template: + + node_templates: + + test: + type: tosca.nodes.Compute + annotations: ["this", "that"] + diff --git a/javatoscachecker/checker/src/test/resources/config/tosca_simple_yaml_1_1_extended.grammar b/javatoscachecker/checker/src/test/resources/config/tosca_simple_yaml_1_1_extended.grammar new file mode 100644 index 0000000..f204384 --- /dev/null +++ b/javatoscachecker/checker/src/test/resources/config/tosca_simple_yaml_1_1_extended.grammar @@ -0,0 +1,1656 @@ +# +# This is an example of an extension added to the standard 1.1 grammar +# We add the keyword annotations (as a list) to node templates (as an example). +# look in the node_template_definition rule +# + +_status_values: &status_values + enum: + - supported + - unsupported + - experimental + - deprecated + +#I do not know that the lists and maps qualify as 'primitive' .. +_primitive_types: &primitive_types + enum: [string,integer,float,boolean,timestamp,list,map,version,range,scalar-unit.size,scalar_unit.frequency,scalar_unit.time] + +#needs custom validation as we have to make sure there are 2 elements and allow for the +#UNBOUNDED keyword as second element +_range_definition: &range_definition + desc: "Section used to declare additional metadata information" + required: no + type: seq + size: 2 + name: range_definition + sequence: + - type: scalar + +_version_definition: &version_definition + desc: "An optional TOSCA version number" + required: no + type: str + name: version_definition +# pattern: .[.[.[- + TOSCA simple profile with 1 server bound to a new network + +metadata: + outcome: "success" + +imports: + - network: tosca/tosca-network-types.yaml + +topology_template: + + inputs: + network_name: + type: string + description: Network name + + node_templates: + my_server: + type: tosca.nodes.Compute + capabilities: + host: + properties: + disk_size: 10 GB + num_cpus: 1 + mem_size: 4096 MB + os: + properties: + architecture: x86_64 + type: Linux + distribution: CirrOS + version: 0.3.2 + + my_network: + type: tosca.nodes.network.Network + properties: + network_name: { get_input: network_name } + ip_version: 4 + cidr: '192.168.0.0/24' + start_ip: '192.168.0.50' + end_ip: '192.168.0.200' + gateway_ip: '192.168.0.1' + + my_port: + type: tosca.nodes.network.Port + requirements: + - binding: my_server + - link: my_network diff --git a/javatoscachecker/checker/src/test/resources/spec/examples/spec-example-11.1.12.3.yaml b/javatoscachecker/checker/src/test/resources/spec/examples/spec-example-11.1.12.3.yaml new file mode 100644 index 0000000..cbc2b24 --- /dev/null +++ b/javatoscachecker/checker/src/test/resources/spec/examples/spec-example-11.1.12.3.yaml @@ -0,0 +1,45 @@ +tosca_definitions_version: tosca_simple_yaml_1_0 + +description: > + TOSCA simple profile with 1 server bound to an existing network + +metadata: + outcome: "success" + +imports: + - network: tosca/tosca-network-types.yaml + +topology_template: + inputs: + network_name: + type: string + description: Network name + + node_templates: + my_server: + type: tosca.nodes.Compute + capabilities: + host: + properties: + disk_size: 10 GB + num_cpus: 1 + mem_size: 4096 MB + os: + properties: + architecture: x86_64 + type: Linux + distribution: CirrOS + version: 0.3.2 + + my_network: + type: tosca.nodes.network.Network + properties: + network_name: { get_input: network_name } + + my_port: + type: tosca.nodes.network.Port + requirements: + - binding: + node: my_server + - link: + node: my_network diff --git a/javatoscachecker/checker/src/test/resources/spec/examples/spec-example-11.1.15.3.yaml b/javatoscachecker/checker/src/test/resources/spec/examples/spec-example-11.1.15.3.yaml new file mode 100644 index 0000000..0efd978 --- /dev/null +++ b/javatoscachecker/checker/src/test/resources/spec/examples/spec-example-11.1.15.3.yaml @@ -0,0 +1,114 @@ +tosca_definitions_version: tosca_simple_yaml_1_0 + +description: > + TOSCA simple profile with WordPress, a web server, a MySQL DBMS hosting the application’s database content on the same server. Does not have input defaults or constraints. + +# well, whomever wrote this example .. there are references to non-existent template and interface inputs. +metadata: + outcome: "fail" + +imports: + - network: tosca/tosca-network-types.yaml + - examples: tosca/tosca-examples-types.yaml + +topology_template: + inputs: + cpus: + type: integer + description: Number of CPUs for the server. + db_name: + type: string + description: The name of the database. + db_user: + type: string + description: The username of the DB user. + db_pwd: + type: string + description: The WordPress database admin account password. + db_root_pwd: + type: string + description: Root password for MySQL. + db_port: + type: tosca.datatypes.network.PortDef + description: Port for the MySQL database + + node_templates: + wordpress: + type: tosca.nodes.WebApplication.WordPress + properties: + context_root: { get_input: context_root } + requirements: + - host: webserver + - database_endpoint: mysql_database + interfaces: + Standard: + create: wordpress_install.sh + configure: + implementation: wordpress_configure.sh + inputs: + wp_db_name: { get_property: [ mysql_database, name ] } + wp_db_user: { get_property: [ mysql_database, user ] } + wp_db_password: { get_property: [ mysql_database, password ] } + # In my own template, find requirement/capability, find port property + wp_db_port: { get_property: [ SELF, database_endpoint, port ] } + + mysql_database: + type: tosca.nodes.Database + properties: + name: { get_input: db_name } + user: { get_input: db_user } + password: { get_input: db_pwd } + port: { get_input: db_port } + capabilities: + database_endpoint: + properties: + port: { get_input: db_port } + requirements: + - host: mysql_dbms + interfaces: + Standard: + configure: mysql_database_configure.sh + + mysql_dbms: + type: tosca.nodes.DBMS + properties: + root_password: { get_input: db_root_pwd } + port: { get_input: db_port } + requirements: + - host: server + interfaces: + Standard: + inputs: + db_root_password: { get_property: [ mysql_dbms, root_password ] } + create: mysql_dbms_install.sh + start: mysql_dbms_start.sh + configure: mysql_dbms_configure.sh + + webserver: + type: tosca.nodes.WebServer + requirements: + - host: server + interfaces: + Standard: + create: webserver_install.sh + start: webserver_start.sh + + server: + type: tosca.nodes.Compute + capabilities: + host: + properties: + disk_size: 10 GB + num_cpus: { get_input: cpus } + mem_size: 4096 MB + os: + properties: + architecture: x86_64 + type: linux + distribution: fedora + version: 17.0 + + outputs: + website_url: + description: URL for Wordpress wiki. + value: { get_attribute: [server, public_address] } diff --git a/javatoscachecker/checker/src/test/resources/spec/examples/spec-example-2.1.yaml b/javatoscachecker/checker/src/test/resources/spec/examples/spec-example-2.1.yaml new file mode 100644 index 0000000..e8d5538 --- /dev/null +++ b/javatoscachecker/checker/src/test/resources/spec/examples/spec-example-2.1.yaml @@ -0,0 +1,25 @@ +tosca_definitions_version: tosca_simple_yaml_1_0 +description: Template for deploying a single server with predefined properties. + +metadata: + outcome: "success" + +topology_template: + node_templates: + my_server: + type: tosca.nodes.Compute + capabilities: + # Host container properties + host: + properties: + num_cpus: 1 + disk_size: 10 GB + mem_size: 4096 MB + # Guest Operating System properties + os: + properties: + # host Operating System image properties + architecture: x86_64 + type: linux + distribution: rhel + version: 6.5 diff --git a/javatoscachecker/checker/src/test/resources/spec/examples/spec-example-5.3.2.2.yaml b/javatoscachecker/checker/src/test/resources/spec/examples/spec-example-5.3.2.2.yaml new file mode 100644 index 0000000..65f0aea --- /dev/null +++ b/javatoscachecker/checker/src/test/resources/spec/examples/spec-example-5.3.2.2.yaml @@ -0,0 +1,27 @@ +tosca_definitions_version: tosca_simple_yaml_1_0 +description: expected to fail as this redeclares a common type (and the checker provides them by default) + +metadata: + outcome: "fail" + +data_types: + + tosca.datatypes.Credential: + derived_from: tosca.datatypes.Root + properties: + protocol: + type: string + required: false + token_type: + type: string + default: password + token: + type: string + keys: + type: map + required: false + entry_schema: + type: string + user: + type: string + required: false diff --git a/javatoscachecker/checker/src/test/resources/spec/examples/spec-example-7.3.3.1.yaml b/javatoscachecker/checker/src/test/resources/spec/examples/spec-example-7.3.3.1.yaml new file mode 100644 index 0000000..7e6e375 --- /dev/null +++ b/javatoscachecker/checker/src/test/resources/spec/examples/spec-example-7.3.3.1.yaml @@ -0,0 +1,39 @@ +tosca_definitions_version: tosca_simple_yaml_1_1 + +#it fails because DBMS node type has no Backup interface defined + +metadata: + outcome: "fail" + +imports: + - examples: tosca/tosca-examples-types.yaml + +topology_template: + node_templates: + my_server: + type: tosca.nodes.Compute + mysql: + type: tosca.nodes.DBMS.MySQL + requirements: + - host: my_server + interfaces: + tosca.interfaces.nodes.custom.Backup: + backup: backup.sh + workflows: + backup: + description: Performs a snapshot of the MySQL data. + preconditions: + - target: my_server + condition: + - assert: + - state: [{equal: available}] + - target: mysql + condition: + - assert: + - state: [{valid_values: [started, available]}] + - my_attribute: [{equal: ready }] + steps: + my_step: + target: mysql + activities: + - call_operation: tosca.interfaces.nodes.custom.Backup.backup diff --git a/javatoscachecker/checker/src/test/resources/spec/examples/spec-example-7.3.5-invalid-version.yaml b/javatoscachecker/checker/src/test/resources/spec/examples/spec-example-7.3.5-invalid-version.yaml new file mode 100644 index 0000000..d86177a --- /dev/null +++ b/javatoscachecker/checker/src/test/resources/spec/examples/spec-example-7.3.5-invalid-version.yaml @@ -0,0 +1,45 @@ +tosca_definitions_version: tosca_simple_yaml_1_0 + +metadata: + outcome: "fail" + +imports: + - examples: tosca/tosca-examples-types.yaml + +topology_template: + node_templates: + my_server: + type: tosca.nodes.Compute + db: + type: tosca.nodes.DBMS.MySQL + requirements: + - host: my_server + interfaces: + Standard: + stop: stop.sh + + workflows: + backup: + description: Performs a snapshot of the MySQL data. + preconditions: + - target: my_server + condition: + - assert: + - state: [{equal: available}] + - target: my_server + condition: + - assert: + - state: [{valid_values: [started, available]}] + - my_attribute: [{equal: ready }] + steps: + backup_step: + target: db + filter: # filter is a list of clauses. Matching between clauses is and. + - or: # only one of sub-clauses must be true. + - assert: + - foo: [{equals: true}] + - assert: + - bar: [{greater_than: 2}, {less_than: 20}] + activities: + - call_operation: tosca.interfaces.node.lifecycle.Standard.stop + diff --git a/javatoscachecker/checker/src/test/resources/spec/examples/spec-example-7.3.5.yaml b/javatoscachecker/checker/src/test/resources/spec/examples/spec-example-7.3.5.yaml new file mode 100644 index 0000000..b26f53a --- /dev/null +++ b/javatoscachecker/checker/src/test/resources/spec/examples/spec-example-7.3.5.yaml @@ -0,0 +1,42 @@ +tosca_definitions_version: tosca_simple_yaml_1_1 + +imports: + - examples: tosca/tosca-examples-types.yaml + +topology_template: + node_templates: + my_server: + type: tosca.nodes.Compute + db: + type: tosca.nodes.DBMS.MySQL + requirements: + - host: my_server + interfaces: + Standard: + stop: stop.sh + + workflows: + backup: + description: Performs a snapshot of the MySQL data. + preconditions: + - target: my_server + condition: + - assert: + - state: [{equal: available}] + - target: my_server + condition: + - assert: + - state: [{valid_values: [started, available]}] + - my_attribute: [{equal: ready }] + steps: + backup_step: + target: db + filter: # filter is a list of clauses. Matching between clauses is and. + - or: # only one of sub-clauses must be true. + - assert: + - foo: [{equals: true}] + - assert: + - bar: [{greater_than: 2}, {less_than: 20}] + activities: + - call_operation: Standard.stop + diff --git a/javatoscachecker/checker/src/test/resources/spec/node_types/node_type_basic.yaml b/javatoscachecker/checker/src/test/resources/spec/node_types/node_type_basic.yaml new file mode 100644 index 0000000..0600a12 --- /dev/null +++ b/javatoscachecker/checker/src/test/resources/spec/node_types/node_type_basic.yaml @@ -0,0 +1,26 @@ +tosca_definitions_version: tosca_simple_yaml_1_1 + +metadata: + outcome: "success" + +node_types: + org.onap.tosca.checker.Appliance: + properties: + brand: + type: string + required: true + color: + type: string + required: true + warrantyDuration: + type: string + required: true + price: + type: float + required: true + power_type: + type: string + default: "electric" + constraints: + - valid_values: ["electric", "gas"] + diff --git a/javatoscachecker/checker/src/test/resources/spec/node_types/node_type_with_hierarchy.yaml b/javatoscachecker/checker/src/test/resources/spec/node_types/node_type_with_hierarchy.yaml new file mode 100644 index 0000000..b095297 --- /dev/null +++ b/javatoscachecker/checker/src/test/resources/spec/node_types/node_type_with_hierarchy.yaml @@ -0,0 +1,44 @@ +tosca_definitions_version: tosca_simple_yaml_1_1 + +metadata: + outcome: "success" + +node_types: + org.onap.tosca.checker.Appliance: + properties: + brand: + type: string + required: true + color: + type: string + required: true + warrantyDuration: + type: string + required: true + price: + type: float + required: true + power_type: + type: string + default: "electric" + constraints: + - valid_values: ["electric", "gas"] + + org.onap.tosca.checker.Dishwasher: + derived_from: org.onap.tosca.checker.Appliance + properties: + waterFiltration: + type: boolean + required: false + default: false + numberOfCycles: + type: integer + required: false + rackMaterial: + type: string + required: true + energyStar: + type: boolean + required: true + + diff --git a/javatoscachecker/checker/src/test/resources/spec/node_types/node_type_with_invalid_hierarchy.yaml b/javatoscachecker/checker/src/test/resources/spec/node_types/node_type_with_invalid_hierarchy.yaml new file mode 100644 index 0000000..d71d466 --- /dev/null +++ b/javatoscachecker/checker/src/test/resources/spec/node_types/node_type_with_invalid_hierarchy.yaml @@ -0,0 +1,10 @@ +tosca_definitions_version: tosca_simple_yaml_1_1 +description: reference to non-existing super type + +metadata: + outcome: "fail" + +node_types: + org.onap.tosca.checker.Appliance: + derived_from: org.onap.tosca.checker.Object + diff --git a/javatoscachecker/checker/src/test/resources/spec/node_types/node_type_with_user_defined_properties.yaml b/javatoscachecker/checker/src/test/resources/spec/node_types/node_type_with_user_defined_properties.yaml new file mode 100644 index 0000000..752a973 --- /dev/null +++ b/javatoscachecker/checker/src/test/resources/spec/node_types/node_type_with_user_defined_properties.yaml @@ -0,0 +1,29 @@ +tosca_definitions_version: tosca_simple_yaml_1_1 +description: defines a property of a user defined data type + +metadata: + outcome: "success" + +imports: + - basic: "spec/node_types/node_type_basic.yaml" + +data_types: + org.onap.tosca.checker.SmartHouseSupport: + properties: + alexaSupport: + type: boolean + default: false + homekitSuppport: + type: boolean + default: false + googlehomeSupport: + type: boolean + default: false + +node_types: + org.onap.tosca.checker.SmartAppliance: + derived_from: org.onap.tosca.checker.Appliance + properties: + environmentSupport: + type: org.onap.tosca.checker.SmartHouseSupport + diff --git a/javatoscachecker/checker/src/test/resources/spec/topology_template/basic.yaml b/javatoscachecker/checker/src/test/resources/spec/topology_template/basic.yaml new file mode 100644 index 0000000..66ff83d --- /dev/null +++ b/javatoscachecker/checker/src/test/resources/spec/topology_template/basic.yaml @@ -0,0 +1,24 @@ +tosca_definitions_version: tosca_simple_yaml_1_1 +description: test basic property value assignment + +metadata: + outcome: "success" + +imports: + - appliances: spec/node_types/node_type_with_hierarchy.yaml + +topology_template: + + node_templates: + dishwasher: + type: org.onap.tosca.checker.Dishwasher + properties: + brand: Samsung + color: white + warrantyDuration: 1 year + price: 550 + numberOfCycles: 6 + rackMaterial: stainless + energyStar: false + + diff --git a/javatoscachecker/checker/src/test/resources/spec/topology_template/basic_invalid_property_assignment.yaml b/javatoscachecker/checker/src/test/resources/spec/topology_template/basic_invalid_property_assignment.yaml new file mode 100644 index 0000000..bb418a7 --- /dev/null +++ b/javatoscachecker/checker/src/test/resources/spec/topology_template/basic_invalid_property_assignment.yaml @@ -0,0 +1,24 @@ +tosca_definitions_version: tosca_simple_yaml_1_1 +description: test invalid property value assignment (numberOfCycles expects an integer) + +metadata: + outcome: "fail" + +imports: + - appliances: spec/node_types/node_type_with_hierarchy.yaml + +topology_template: + + node_templates: + dishwasher: + type: org.onap.tosca.checker.Dishwasher + properties: + brand: Samsung + color: white + warrantyDuration: 1 year + price: 550 + numberOfCycles: unknown + rackMaterial: stainless + energyStar: false + + diff --git a/javatoscachecker/service/pom.xml b/javatoscachecker/service/pom.xml index ea9258d..26597c4 100644 --- a/javatoscachecker/service/pom.xml +++ b/javatoscachecker/service/pom.xml @@ -120,6 +120,25 @@ + + junit + junit + 4.12 + test + + + org.assertj + assertj-core + 3.8.0 + test + + + org.springframework.boot + spring-boot-starter-test + 1.5.3.RELEASE + test + + com.fasterxml.jackson.core jackson-databind diff --git a/javatoscachecker/service/src/test/java/org/onap/tosca/checker/service/test/CheckerServiceTest.java b/javatoscachecker/service/src/test/java/org/onap/tosca/checker/service/test/CheckerServiceTest.java new file mode 100644 index 0000000..b6eb644 --- /dev/null +++ b/javatoscachecker/service/src/test/java/org/onap/tosca/checker/service/test/CheckerServiceTest.java @@ -0,0 +1,151 @@ +/* + * Copyright (c) 2017 . 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. + */ +package org.onap.tosca.checker.service.test; + +import java.util.Scanner; + +import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.FixMethodOrder; +import org.junit.runners.MethodSorters; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.boot.test.web.client.TestRestTemplate; + +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpEntity; +import org.springframework.http.ResponseEntity; + +import org.onap.tosca.checker.Report; + +/** + * The test order is relevant here .. + */ + +//@RunWith(SpringJUnit4ClassRunner.class) +@RunWith(SpringRunner.class) +@SpringBootTest(classes = org.onap.tosca.checker.service.CheckerEngine.class, + webEnvironment = WebEnvironment.RANDOM_PORT) +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class CheckerServiceTest { + + @Autowired + private TestRestTemplate restTemplate; + + @Test + public void testCatalogExists() { + assertTrue( + this.restTemplate.getForEntity("/check_template/nosuchcatalog", String.class) + .getStatusCodeValue() == 404); + } + + @Test + public void testStandaloneTemplate() { + + ResponseEntity response = + this.restTemplate.exchange("/check_template/", HttpMethod.POST, prepareRequest("standalone.yaml"), Report.class); + + assertTrue(response.getStatusCodeValue() == 200); + assertTrue(response.getBody().size() == 0); //no errors + } + + @Test + public void testStandaloneTemplateWithErrors() { + + ResponseEntity response = + this.restTemplate.exchange("/check_template/", HttpMethod.POST, prepareRequest("standalone_with_errors.yaml"), Report.class); + + assertTrue(response.getStatusCodeValue() == 200); + assertTrue(response.getBody().size() > 0); //some errors + } + + @Test + public void testCatalog1WithNamedTemplate() { + + ResponseEntity response = + this.restTemplate.exchange("/check_template/test/schema.yaml", HttpMethod.POST, prepareRequest("test_schema.yaml"), Report.class); + + assertTrue(response.getStatusCodeValue() == 200); + assertThat(response.getBody().size() == 0) + .as("Processing failed: " + response.getBody()) + .isTrue(); + } + + @Test + public void testCatalog2WithTemplate() { + + ResponseEntity response = + this.restTemplate.exchange("/check_template/test/", HttpMethod.POST, prepareRequest("test_template.yaml"), Report.class); + + assertTrue(response.getStatusCodeValue() == 200); + assertThat(response.getBody().size() == 0) + .as("Processing failed: " + response.getBody()) + .isTrue(); + } + + @Test + public void testCatalog3NamedTemplateExists() { + + ResponseEntity response = + this.restTemplate.exchange("/check_template/test/schema.yaml", HttpMethod.GET, null, String.class); + + assertTrue(response.getStatusCodeValue() == 200); + } + + @Test + public void testCatalog4NamedTemplateDoesNotExists() { + ResponseEntity response = + this.restTemplate.exchange("/check_template/test/test_schema.yaml", HttpMethod.GET, null, String.class); + + assertTrue(response.getStatusCodeValue() == 404); + } + + @Test + public void testCatalog5NamedTemplateDoesNotExists() { + ResponseEntity response = + this.restTemplate.exchange("/check_template/test/test_schema.yaml", HttpMethod.GET, null, String.class); + + assertThat(response.getStatusCodeValue() == 404) + .as("Existence check failed, got " + response.getStatusCodeValue()) + .isTrue(); + } + + @Test + public void testCatalog6Delete() { + ResponseEntity response = + this.restTemplate.exchange("/check_template/test/", HttpMethod.DELETE, null, String.class); + + assertThat(response.getStatusCodeValue() == 200) + .as("Existence check failed, got " + response.getStatusCodeValue()) + .isTrue(); + } + + private HttpEntity prepareRequest(String theResourceName) { + String content = new Scanner( + Thread.currentThread().getContextClassLoader().getResourceAsStream(theResourceName), "UTF-8") + .useDelimiter("\\Z").next(); + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + return new HttpEntity(content, headers); + } +} diff --git a/javatoscachecker/service/src/test/resources/standalone.yaml b/javatoscachecker/service/src/test/resources/standalone.yaml new file mode 100644 index 0000000..26797ab --- /dev/null +++ b/javatoscachecker/service/src/test/resources/standalone.yaml @@ -0,0 +1,21 @@ +tosca_definitions_version: tosca_simple_yaml_1_0 + +topology_template: + node_templates: + my_server: + type: tosca.nodes.Compute + capabilities: + # Host container properties + host: + properties: + num_cpus: 1 + disk_size: 10 GB + mem_size: 4096 MB + # Guest Operating System properties + os: + properties: + # host Operating System image properties + architecture: x86_64 + type: linux + distribution: rhel + version: 6.5 diff --git a/javatoscachecker/service/src/test/resources/standalone_with_errors.yaml b/javatoscachecker/service/src/test/resources/standalone_with_errors.yaml new file mode 100644 index 0000000..d5c1a15 --- /dev/null +++ b/javatoscachecker/service/src/test/resources/standalone_with_errors.yaml @@ -0,0 +1,23 @@ +tosca_definitions_version: tosca_simple_yaml_1_0 +description: uses unknown capability propertya to trigger an error + +topology_template: + node_templates: + my_server: + type: tosca.nodes.Compute + capabilities: + # Host container properties + host: + properties: + num_cpus: 1 + disk_size: 10 GB + mem_size: 4096 MB + virtualization_support: true + # Guest Operating System properties + os: + properties: + # host Operating System image properties + architecture: x86_64 + type: linux + distribution: rhel + version: 6.5 diff --git a/javatoscachecker/service/src/test/resources/test_schema.yaml b/javatoscachecker/service/src/test/resources/test_schema.yaml new file mode 100644 index 0000000..9575e27 --- /dev/null +++ b/javatoscachecker/service/src/test/resources/test_schema.yaml @@ -0,0 +1,17 @@ +tosca_definitions_version: tosca_simple_yaml_1_1 + +data_types: + org.onap.tosca.checker.service.Person: + properties: + firstName: + type: string + required: true + lastName: + type: string + required: true + +node_types: + org.onap.tosca.checker.service.Residence: + properties: + owner: + type: org.onap.tosca.checker.service.Person diff --git a/javatoscachecker/service/src/test/resources/test_template.yaml b/javatoscachecker/service/src/test/resources/test_template.yaml new file mode 100644 index 0000000..478aec0 --- /dev/null +++ b/javatoscachecker/service/src/test/resources/test_template.yaml @@ -0,0 +1,11 @@ +tosca_definitions_version: tosca_simple_yaml_1_0 + +imports: + - schema: schema.yaml + +topology_template: + node_templates: + my_house: + type: org.onap.tosca.checker.service.Residence + properties: + owner: {firstName: "Serban", lastName: "Jora"} -- cgit 1.2.3-korg