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 ++- 4 files changed, 164 insertions(+), 31 deletions(-) create mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaServiceTemplateFilterTest.java (limited to 'models-tosca/src/test/java/org/onap') 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 = -- cgit 1.2.3-korg