From b87e0242ce1a957740ee988bec3b82e3628adbed Mon Sep 17 00:00:00 2001 From: liamfallon Date: Thu, 17 Dec 2020 12:10:21 +0000 Subject: Add Service Template TOSCA handling Today we can only handle a single service template in the database. We should be able to handle multiple service templates and assign arbitrary policy types and policies to maned and versioned service templates. This review brings in the Java API in models provider for handling service templates in this way and uses a simplistic single-teplate implementation in models-tosca, that will work but still only handles a single service template under the hood. Issue-ID: POLICY-2900 Change-Id: Ia02dea8abe44b7f407e685090a4b8e0360889653 Signed-off-by: liamfallon --- .../concepts/ToscaPolicyTypeFilterTest.java | 11 +- .../concepts/ToscaServiceTemplateFilterTest.java | 125 ++++++++++++++ .../AuthorativeToscaProviderGenericTest.java | 42 ++--- .../AuthorativeToscaProviderPolicyTest.java | 17 +- .../servicetemplates/TestServiceTemplates.yaml | 185 +++++++++++++++++++++ 5 files changed, 349 insertions(+), 31 deletions(-) create mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaServiceTemplateFilterTest.java create mode 100644 models-tosca/src/test/resources/servicetemplates/TestServiceTemplates.yaml (limited to 'models-tosca/src/test') diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilterTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilterTest.java index 439ec6282..031fceb9a 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilterTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilterTest.java @@ -47,13 +47,12 @@ import org.yaml.snakeyaml.Yaml; * @author Liam Fallon (liam.fallon@est.tech) */ public class ToscaPolicyTypeFilterTest { - private static final String VERSION_100 = "1.0.0"; - - private static final String VERSION_000 = "0.0.0"; - // Logger for this class private static final Logger LOGGER = LoggerFactory.getLogger(ToscaPolicyTypeFilterTest.class); + private static final String VERSION_100 = "1.0.0"; + private static final String VERSION_000 = "0.0.0"; + // @formatter:off private static final String[] policyTypeResourceNames = { "policytypes/onap.policies.optimization.resource.DistancePolicy.yaml", @@ -151,11 +150,11 @@ public class ToscaPolicyTypeFilterTest { // got changed - perhaps we change this test to find a specific name // to test for vs an index which never remains consistent? // - //assertEquals("2.0.0", filteredList.get(18).getVersion()); + // assertEquals("2.0.0", filteredList.get(18).getVersion()); // // And now this index changes again?? // - //assertEquals(VERSION_100, filteredList.get(17).getVersion()); + // assertEquals(VERSION_100, filteredList.get(17).getVersion()); typeList.get(12).setVersion(VERSION_100); filteredList = filter.filter(typeList); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaServiceTemplateFilterTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaServiceTemplateFilterTest.java new file mode 100644 index 000000000..ba72e621b --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaServiceTemplateFilterTest.java @@ -0,0 +1,125 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019-2020 Nordix Foundation. + * Modifications Copyright (C) 2019 AT&T Intellectual Property. 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.authorative.concepts; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.util.List; +import org.junit.Test; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardYamlCoder; +import org.onap.policy.models.base.PfKey; + +/** + * Test of the {@link ToscaServiceTemplateFilter} class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaServiceTemplateFilterTest { + @Test + public void testNullList() { + ToscaServiceTemplateFilter filter = ToscaServiceTemplateFilter.builder().build(); + + assertThatThrownBy(() -> { + filter.filter(null); + }).hasMessageMatching("originalList is marked .*on.*ull but is null"); + } + + @Test + public void testFilterNothing() throws CoderException { + ToscaServiceTemplates serviceTemplates = new StandardYamlCoder().decode( + new File("src/test/resources/servicetemplates/TestServiceTemplates.yaml"), ToscaServiceTemplates.class); + + ToscaServiceTemplateFilter filter = ToscaServiceTemplateFilter.builder().build(); + + List filteredList = filter.filter(serviceTemplates.getServiceTemplates()); + assertTrue(filteredList.containsAll(serviceTemplates.getServiceTemplates())); + } + + @Test + public void testFilterLatestVersion() throws CoderException { + ToscaServiceTemplates serviceTemplates = new StandardYamlCoder().decode( + new File("src/test/resources/servicetemplates/TestServiceTemplates.yaml"), ToscaServiceTemplates.class); + + ToscaServiceTemplateFilter filter = + ToscaServiceTemplateFilter.builder().version(ToscaServiceTemplateFilter.LATEST_VERSION).build(); + + List filteredList = filter.filter(serviceTemplates.getServiceTemplates()); + assertEquals(4, filteredList.size()); + assertEquals("0.0.0", filteredList.get(0).getVersion()); + assertEquals("1.2.8", filteredList.get(1).getVersion()); + assertEquals("1.2.3", filteredList.get(2).getVersion()); + assertEquals("1.8.3", filteredList.get(3).getVersion()); + + filter = ToscaServiceTemplateFilter.builder().version("1.2.3").build(); + filteredList = filter.filter(serviceTemplates.getServiceTemplates()); + assertEquals(3, filteredList.size()); + filter = ToscaServiceTemplateFilter.builder().version(PfKey.NULL_KEY_VERSION).build(); + filteredList = filter.filter(serviceTemplates.getServiceTemplates()); + assertEquals(6, filteredList.size()); + + serviceTemplates.getServiceTemplates().get(12).setVersion("0.0.0"); + filteredList = filter.filter(serviceTemplates.getServiceTemplates()); + assertEquals(7, filteredList.size()); + assertEquals(PfKey.NULL_KEY_VERSION, filteredList.get(0).getVersion()); + assertEquals(PfKey.NULL_KEY_VERSION, filteredList.get(6).getVersion()); + } + + @Test + public void testFilterNameVersion() throws CoderException { + ToscaServiceTemplates serviceTemplates = new StandardYamlCoder().decode( + new File("src/test/resources/servicetemplates/TestServiceTemplates.yaml"), ToscaServiceTemplates.class); + + ToscaServiceTemplateFilter filter = ToscaServiceTemplateFilter.builder().name("name0").build(); + List filteredList = filter.filter(serviceTemplates.getServiceTemplates()); + assertEquals(13, filteredList.size()); + + filter = ToscaServiceTemplateFilter.builder().name("not.found").build(); + filteredList = filter.filter(serviceTemplates.getServiceTemplates()); + assertEquals(0, filteredList.size()); + + filter = ToscaServiceTemplateFilter.builder().name(PfKey.NULL_KEY_NAME).build(); + filteredList = filter.filter(serviceTemplates.getServiceTemplates()); + assertEquals(2, filteredList.size()); + + filter = ToscaServiceTemplateFilter.builder().version(PfKey.NULL_KEY_VERSION).build(); + filteredList = filter.filter(serviceTemplates.getServiceTemplates()); + assertEquals(6, filteredList.size()); + + filter = ToscaServiceTemplateFilter.builder().name(PfKey.NULL_KEY_NAME).version(PfKey.NULL_KEY_VERSION).build(); + filteredList = filter.filter(serviceTemplates.getServiceTemplates()); + assertEquals(2, filteredList.size()); + + filter = ToscaServiceTemplateFilter.builder().name("name2").build(); + filteredList = filter.filter(serviceTemplates.getServiceTemplates()); + assertEquals(1, filteredList.size()); + + filter = ToscaServiceTemplateFilter.builder().name("name2").version("1.8.3").build(); + filteredList = filter.filter(serviceTemplates.getServiceTemplates()); + assertEquals(1, filteredList.size()); + assertEquals("name2", filteredList.get(0).getName()); + assertEquals("1.8.3", filteredList.get(0).getVersion()); + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderGenericTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderGenericTest.java index 53e8311ee..dc0115486 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderGenericTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderGenericTest.java @@ -114,14 +114,14 @@ public class AuthorativeToscaProviderGenericTest { @Test public void testCreateGetDelete() throws Exception { assertThatThrownBy(() -> { - new AuthorativeToscaProvider().getServiceTemplate(null, null, null); + new AuthorativeToscaProvider().getServiceTemplateList(null, null, null); }).hasMessageMatching(DAO_IS_NULL); ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); assertNotNull(toscaServiceTemplate); ToscaServiceTemplate createdServiceTemplate = - new AuthorativeToscaProvider().createServiceTemplate(pfDao, toscaServiceTemplate); + new AuthorativeToscaProvider().createServiceTemplate(pfDao, toscaServiceTemplate); PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1); @@ -130,14 +130,15 @@ public class AuthorativeToscaProviderGenericTest { assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName())); assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); - ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getServiceTemplate(pfDao, null, null); + List gotServiceTemplateList = + new AuthorativeToscaProvider().getServiceTemplateList(pfDao, null, null); - ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName()); + ToscaPolicyType gotPolicyType = gotServiceTemplateList.get(0).getPolicyTypes().get(policyTypeKey.getName()); assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); List gotPolicyTypeList = - new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001); + new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001); assertEquals(2, gotPolicyTypeList.size()); assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); @@ -153,50 +154,49 @@ public class AuthorativeToscaProviderGenericTest { assertEquals(2, gotPolicyTypeList.size()); assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); - assertThatThrownBy( - () -> new AuthorativeToscaProvider().getPolicyTypeList(new DefaultPfDao(), POLICY_NO_VERSION, VERSION_001)) - .hasMessageContaining("Policy Framework DAO has not been initialized"); + assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypeList(new DefaultPfDao(), POLICY_NO_VERSION, + VERSION_001)).hasMessageContaining("Policy Framework DAO has not been initialized"); assertTrue(new AuthorativeToscaProvider().getPolicyTypeList(pfDao, "i.dont.Exist", VERSION_001).isEmpty()); ToscaServiceTemplate deletedServiceTemplate = - new AuthorativeToscaProvider().deleteServiceTemplate(pfDao, "Dummy", "0.0.1"); + new AuthorativeToscaProvider().deleteServiceTemplate(pfDao, "Dummy", "0.0.1"); assertEquals(2, deletedServiceTemplate.getPolicyTypes().size()); } @Test public void testNullParameters() throws Exception { - assertThatThrownBy(() -> new AuthorativeToscaProvider().getServiceTemplate(null, null, null)) - .hasMessageMatching("^dao is marked .*on.*ull but is null$"); + assertThatThrownBy(() -> new AuthorativeToscaProvider().getServiceTemplateList(null, null, null)) + .hasMessageMatching("^dao is marked .*on.*ull but is null$"); assertThatThrownBy(() -> new AuthorativeToscaProvider().createServiceTemplate(null, null)) - .hasMessageMatching("^dao is marked .*on.*ull but is null$"); + .hasMessageMatching("^dao is marked .*on.*ull but is null$"); assertThatThrownBy(() -> new AuthorativeToscaProvider().createServiceTemplate(pfDao, null)) - .hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$"); + .hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$"); assertThatThrownBy(() -> new AuthorativeToscaProvider().createServiceTemplate(null, new ToscaServiceTemplate())) - .hasMessageMatching("^dao is marked .*on.*ull but is null$"); + .hasMessageMatching("^dao is marked .*on.*ull but is null$"); assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(null, null, null)) - .hasMessageMatching("^dao is marked .*on.*ull but is null$"); + .hasMessageMatching("^dao is marked .*on.*ull but is null$"); assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(null, null, "0.0.1")) - .hasMessageMatching("^dao is marked .*on.*ull but is null$"); + .hasMessageMatching("^dao is marked .*on.*ull but is null$"); assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(null, "Dummy", null)) - .hasMessageMatching("^dao is marked .*on.*ull but is null$"); + .hasMessageMatching("^dao is marked .*on.*ull but is null$"); assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(null, "Dummy", "0.0.1")) - .hasMessageMatching("^dao is marked .*on.*ull but is null$"); + .hasMessageMatching("^dao is marked .*on.*ull but is null$"); assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(pfDao, null, null)) - .hasMessageMatching("^name is marked .*on.*ull but is null$"); + .hasMessageMatching("^name is marked .*on.*ull but is null$"); assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(pfDao, null, "0.0.1")) - .hasMessageMatching("^name is marked .*on.*ull but is null$"); + .hasMessageMatching("^name is marked .*on.*ull but is null$"); assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(pfDao, "Dummy", null)) - .hasMessageMatching("^version is marked .*on.*ull but is null$"); + .hasMessageMatching("^version is marked .*on.*ull but is null$"); } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java index 7d900ab3a..f66a8e8a5 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java @@ -43,6 +43,7 @@ import org.onap.policy.models.dao.DaoParameters; import org.onap.policy.models.dao.PfDao; import org.onap.policy.models.dao.PfDaoFactory; import org.onap.policy.models.dao.impl.DefaultPfDao; +import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; @@ -410,8 +411,8 @@ public class AuthorativeToscaProviderPolicyTest { @Test public void testEntityMaps() throws CoderException, PfModelException { - Object yamlObject = new Yaml().load( - ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.tcagen2.yaml")); + Object yamlObject = + new Yaml().load(ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.tcagen2.yaml")); String yamlAsJsonString = new StandardCoder().encode(yamlObject); ToscaServiceTemplate toscaServiceTemplatePolicyType = @@ -449,11 +450,19 @@ public class AuthorativeToscaProviderPolicyTest { assertThatThrownBy(() -> { createdServiceTemplate.getToscaTopologyTemplate().getPoliciesAsMap(); }).hasMessageContaining("list of map of entities contains more than one entity with key"); + + + ToscaDataType duplDataType = toscaServiceTemplatePolicyType.getDataTypes().values().iterator().next(); + toscaServiceTemplatePolicyType.getDataTypes().put("DuplicateDataType", duplDataType); + + assertThatThrownBy(() -> { + toscaServiceTemplatePolicyType.getDataTypesAsMap(); + }).hasMessageContaining("list of map of entities contains more than one entity with key"); } private void createPolicyTypes() throws CoderException, PfModelException { - Object yamlObject = new Yaml().load( - ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.tcagen2.yaml")); + Object yamlObject = + new Yaml().load(ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.tcagen2.yaml")); String yamlAsJsonString = new StandardCoder().encode(yamlObject); ToscaServiceTemplate toscaServiceTemplatePolicyType = diff --git a/models-tosca/src/test/resources/servicetemplates/TestServiceTemplates.yaml b/models-tosca/src/test/resources/servicetemplates/TestServiceTemplates.yaml new file mode 100644 index 000000000..d2aea5819 --- /dev/null +++ b/models-tosca/src/test/resources/servicetemplates/TestServiceTemplates.yaml @@ -0,0 +1,185 @@ +service_templates: +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + policy_types: + onap.policies.Policytype0: + derived_from: tosca.policies.Root + version: 1.0.0 + name: onap.policies.Policytype0 + description: a policy type + properties: + policytype0.property0: + type: onap.datatypes.datatype0 + description: a property + required: true + data_types: + onap.datatypes.datatype0: + derived_from: tosca.datatypes.Root + properties: + domain0: + type: string + required: true + description: Domain name + default: domainDefault +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + policy_types: + onap.policies.Policytype1: + derived_from: tosca.policies.Root + version: 1.0.0 + name: onap.policies.Policytype1 + description: a policy type + properties: + policytype1.property1: + type: onap.datatypes.datatype1 + description: a property + required: true + data_types: + onap.datatypes.datatype1: + derived_from: tosca.datatypes.Root + properties: + domain1: + type: string + required: true + description: Domain name + default: domainDefault +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name0 + policy_types: + onap.policies.Policytype2: + derived_from: tosca.policies.Root + version: 1.0.0 + name: onap.policies.Policytype2 + description: a policy type + properties: + policytype2.property2: + type: onap.datatypes.datatype2 + description: a property + required: true + data_types: + onap.datatypes.datatype2: + derived_from: tosca.datatypes.Root + properties: + domain2: + type: string + required: true + description: Domain name + default: domainDefault +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name0 + policy_types: + onap.policies.Policytype3: + derived_from: tosca.policies.Root + version: 1.0.0 + name: onap.policies.Policytype3 + description: a policy type + properties: + policytype3.property3: + type: onap.datatypes.datatype3 + description: a property + required: true + data_types: + onap.datatypes.datatype3: + derived_from: tosca.datatypes.Root + properties: + domain3: + type: string + required: true + description: Domain name + default: domainDefault +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name0 + policy_types: + onap.policies.Policytype4: + derived_from: tosca.policies.Root + version: 1.0.0 + name: onap.policies.Policytype4 + description: a policy type + properties: + policytype4.property4: + type: onap.datatypes.datatype4 + description: a property + required: true + data_types: + onap.datatypes.datatype4: + derived_from: tosca.datatypes.Root + properties: + domain4: + type: string + required: true + description: Domain name + default: domainDefault +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name0 + vesion: 1.2.3 + policy_types: + onap.policies.Policytype5: + derived_from: tosca.policies.Root + version: 1.0.0 + name: onap.policies.Policytype5 + description: a policy type + properties: + policytype5.property5: + type: onap.datatypes.datatype5 + description: a property + required: true + data_types: + onap.datatypes.datatype5: + derived_from: tosca.datatypes.Root + properties: + domain5: + type: string + required: true + description: Domain name + default: domainDefault +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name0 + version: 1.2.3 + policy_types: + onap.policies.Policytype6: + derived_from: tosca.policies.Root + version: 1.0.0 + name: onap.policies.Policytype6 + description: a policy type + properties: + policytype6.property6: + type: onap.datatypes.datatype6 + description: a property + required: true + data_types: + onap.datatypes.datatype6: + derived_from: tosca.datatypes.Root + properties: + domain6: + type: string + required: true + description: Domain name + default: domainDefault +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name0 + version: 1.2.3 +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name0 + version: 1.2.4 +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name0 + version: 1.2.5 +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name0 + version: 1.2.6 +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name0 + version: 1.2.7 +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name0 + version: 1.2.8 +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name0 + version: 1.2.8 +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name0 + version: 1.2.8 +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name1 + version: 1.2.3 +- tosca_definitions_version: tosca_simple_yaml_1_1_0 + name: name2 + version: 1.8.3 -- cgit 1.2.3-korg