From ed54f1f94a43e9fc5e353eb2c7111dcc552be18e Mon Sep 17 00:00:00 2001 From: liamfallon Date: Sun, 7 Feb 2021 14:51:45 +0000 Subject: Make filters on TOSCA entities generic The classes PolicyTypeFilter and PolicyFilter only work on Policy types and policies. However they just filter on name/version/type/typeversion so there is no reason that the filters could not be applied to other entities. This commit adapts the PolicyTypeFilter to EntityFilter and PolicyFilter to TypedEntityFilter, so the filters can be applied to collections of any ToscaEntity class. Issue-ID: POLICY-2995 Change-Id: I8fcf76ee8c8497836b3286b35f928b0c341dac34 Signed-off-by: liamfallon --- .../tosca/authorative/concepts/PojosTest.java | 5 +- .../concepts/ToscaEntityFilterTest.java | 195 ++++++++++++++ .../concepts/ToscaPolicyFilterTest.java | 279 -------------------- .../authorative/concepts/ToscaPolicyTest.java | 9 +- .../concepts/ToscaPolicyTypeFilterTest.java | 193 -------------- .../authorative/concepts/ToscaPolicyTypeTest.java | 6 +- .../concepts/ToscaServiceTemplateFilterTest.java | 125 --------- .../concepts/ToscaTypedEntityFilterTest.java | 287 +++++++++++++++++++++ .../AuthorativeToscaProviderPolicyTest.java | 26 +- .../AuthorativeToscaProviderPolicyTypeTest.java | 73 +++--- 10 files changed, 545 insertions(+), 653 deletions(-) create mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntityFilterTest.java delete mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilterTest.java delete mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilterTest.java delete mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaServiceTemplateFilterTest.java create mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaTypedEntityFilterTest.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/PojosTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/PojosTest.java index c9f162a3a..fe60a52c9 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/PojosTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/PojosTest.java @@ -3,6 +3,7 @@ * ONAP Policy Model * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -57,9 +58,9 @@ public class PojosTest { validator.validate(POJO_PACKAGE, new FilterPackageInfo(), new FilterClassName( - org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter.class.getName()), + org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter.class.getName()), new FilterClassName( - org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter.class.getName()), + org.onap.policy.models.tosca.authorative.concepts.ToscaEntityFilter.class.getName()), new FilterClassName( ToscaIdentifierTestBase.class.getName()) ); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntityFilterTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntityFilterTest.java new file mode 100644 index 000000000..ec2a91733 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntityFilterTest.java @@ -0,0 +1,195 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019-2021 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.assertNotNull; +import static org.junit.Assert.assertTrue; + +import com.google.gson.GsonBuilder; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.models.base.PfKey; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.yaml.snakeyaml.Yaml; + +/** + * Test of the {@link ToscaEntityFilter} class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaEntityFilterTest { + // Logger for this class + private static final Logger LOGGER = LoggerFactory.getLogger(ToscaEntityFilterTest.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", + "policytypes/onap.policies.optimization.resource.VnfPolicy.yaml", + "policytypes/onap.policies.optimization.resource.PciPolicy.yaml", + "policytypes/onap.policies.optimization.resource.OptimizationPolicy.yaml", + "policytypes/onap.policies.controlloop.guard.common.Blacklist.yaml", + "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server.yaml", + "policytypes/onap.policies.optimization.resource.HpaPolicy.yaml", + "policytypes/onap.policies.optimization.resource.Vim_fit.yaml", + "policytypes/onap.policies.optimization.service.SubscriberPolicy.yaml", + "policytypes/onap.policies.optimization.resource.AffinityPolicy.yaml", + "policytypes/onap.policies.optimization.service.QueryPolicy.yaml", + "policytypes/onap.policies.controlloop.guard.common.MinMax.yaml", + "policytypes/onap.policies.controlloop.guard.common.FrequencyLimiter.yaml", + "policytypes/onap.policies.controlloop.guard.coordination.FirstBlocksSecond.yaml", + "policytypes/onap.policies.Optimization.yaml", + "policytypes/onap.policies.monitoring.tcagen2.yaml" + }; + // @formatter:on + + private static List typeList = new ArrayList<>(); + + /** + * Set up a Tosca Policy type list for filtering. + * + * @throws CoderException on JSON decoding errors + */ + @BeforeClass + public static void setupTypeList() throws CoderException { + for (String policyTypeResourceName : policyTypeResourceNames) { + String policyTypeString = ResourceUtils.getResourceAsString(policyTypeResourceName); + Object yamlObject = new Yaml().load(policyTypeString); + String yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject); + + ToscaServiceTemplate serviceTemplate = + new StandardCoder().decode(yamlAsJsonString, ToscaServiceTemplate.class); + assertNotNull(serviceTemplate); + + addPolicyTypes(serviceTemplate.getPolicyTypes()); + } + + for (ToscaPolicyType type : typeList) { + LOGGER.info("using policy type-" + type.getName() + ":" + type.getVersion()); + } + } + + private static void addPolicyTypes(Map foundPolicyTypeMap) { + for (Entry policyTypeEntry : foundPolicyTypeMap.entrySet()) { + ToscaPolicyType policyType = policyTypeEntry.getValue(); + if (policyType.getName() == null) { + policyType.setName(policyTypeEntry.getKey()); + } + if (policyType.getVersion() == null) { + policyType.setVersion(PfKey.NULL_KEY_VERSION); + } + if (!typeList.contains(policyType)) { + typeList.add(policyType); + } + } + } + + @Test + public void testNullList() { + ToscaEntityFilter filter = ToscaEntityFilter.builder().build(); + + assertThatThrownBy(() -> { + filter.filter(null); + }).hasMessageMatching("originalList is marked .*on.*ull but is null"); + } + + @Test + public void testFilterNothing() { + ToscaEntityFilter filter = ToscaEntityFilter.builder().build(); + + List filteredList = filter.filter(typeList); + assertTrue(filteredList.containsAll(typeList)); + } + + @Test + public void testFilterLatestVersion() { + ToscaEntityFilter filter = + ToscaEntityFilter.builder().version(ToscaEntityFilter.LATEST_VERSION).build(); + + List filteredList = filter.filter(typeList); + assertEquals(19, filteredList.size()); + assertEquals(VERSION_100, filteredList.get(0).getVersion()); + assertEquals(VERSION_100, filteredList.get(11).getVersion()); + + typeList.get(12).setVersion("2.0.0"); + filteredList = filter.filter(typeList); + assertEquals(19, filteredList.size()); + // + // This seems to change around as to where this policy type + // 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()); + // + // And now this index changes again?? + // + // assertEquals(VERSION_100, filteredList.get(17).getVersion()); + + typeList.get(12).setVersion(VERSION_100); + filteredList = filter.filter(typeList); + assertEquals(19, filteredList.size()); + assertEquals(VERSION_100, filteredList.get(0).getVersion()); + assertEquals(VERSION_100, filteredList.get(18).getVersion()); + } + + @Test + public void testFilterNameVersion() { + ToscaEntityFilter filter = + ToscaEntityFilter.builder().name("onap.policies.Monitoring").build(); + List filteredList = filter.filter(typeList); + assertEquals(1, filteredList.size()); + + filter = ToscaEntityFilter.builder().name("onap.policies.monitoring.tcagen2").build(); + filteredList = filter.filter(typeList); + assertEquals(1, filteredList.size()); + + filter = ToscaEntityFilter.builder().name("onap.policies.optimization.LpaPolicy").build(); + filteredList = filter.filter(typeList); + assertEquals(0, filteredList.size()); + + filter = ToscaEntityFilter.builder().version(VERSION_100).build(); + filteredList = filter.filter(typeList); + assertEquals(19, filteredList.size()); + + filter = ToscaEntityFilter.builder().name("onap.policies.optimization.Vim_fit") + .version(VERSION_000).build(); + filteredList = filter.filter(typeList); + assertEquals(0, filteredList.size()); + + filter = ToscaEntityFilter.builder().name("onap.policies.optimization.Vim_fit") + .version("0.0.1").build(); + filteredList = filter.filter(typeList); + assertEquals(0, filteredList.size()); + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilterTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilterTest.java deleted file mode 100644 index 01c5ff813..000000000 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilterTest.java +++ /dev/null @@ -1,279 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019-2020 Nordix Foundation. - * Modifications Copyright (C) 2019-2020 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.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import com.google.gson.GsonBuilder; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import org.junit.BeforeClass; -import org.junit.Test; -import org.onap.policy.common.utils.coder.CoderException; -import org.onap.policy.common.utils.coder.StandardCoder; -import org.onap.policy.common.utils.resources.ResourceUtils; -import org.onap.policy.models.base.PfKey; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.yaml.snakeyaml.Yaml; - -/** - * Test of the {@link ToscaPolicyFilter} class. - * - * @author Liam Fallon (liam.fallon@est.tech) - */ -public class ToscaPolicyFilterTest { - 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(ToscaPolicyFilterTest.class); - - // @formatter:off - private static final String[] policyResourceNames = { - "policies/vCPE.policies.optimization.input.tosca.yaml", - "policies/vCPE.policy.monitoring.input.tosca.yaml", - "policies/vCPE.policy.operational.input.tosca.yaml", - "policies/vDNS.policy.monitoring.input.tosca.yaml", - "policies/vDNS.policy.operational.input.tosca.yaml", - "policies/vDNS.policy.guard.frequencylimiter.input.tosca.yaml", - "policies/vDNS.policy.guard.minmaxvnfs.input.tosca.yaml", - "policies/vFirewall.policy.monitoring.input.tosca.yaml", - "policies/vFirewall.policy.operational.input.tosca.yaml" - }; - // @formatter:on - - private static List policyList = new ArrayList<>(); - - /** - * Set up a Tosca Policy type list for filtering. - * - * @throws CoderException on JSON decoding errors - */ - @BeforeClass - public static void setupTypeList() throws CoderException { - for (String policyResourceName : policyResourceNames) { - String policyString = ResourceUtils.getResourceAsString(policyResourceName); - if (policyResourceName.endsWith("yaml")) { - LOGGER.info("loading {}", policyResourceName); - Object yamlObject = new Yaml().load(policyString); - policyString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject); - } - - ToscaServiceTemplate serviceTemplate = new StandardCoder().decode(policyString, ToscaServiceTemplate.class); - assertNotNull(serviceTemplate); - - for (Map foundPolicyMap : serviceTemplate.getToscaTopologyTemplate().getPolicies()) { - addPolicies(foundPolicyMap); - } - } - - for (ToscaPolicy policy : policyList) { - LOGGER.info("using policy-" + policy.getName() + ":" + policy.getVersion() + ", type-" + policy.getType() - + ":" + policy.getTypeVersion()); - } - } - - private static void addPolicies(Map foundPolicyMap) { - for (Entry policyEntry : foundPolicyMap.entrySet()) { - ToscaPolicy policy = policyEntry.getValue(); - if (policy.getName() == null) { - policy.setName(policyEntry.getKey()); - } - - if (policy.getVersion() == null) { - policy.setVersion(PfKey.NULL_KEY_VERSION); - } - if (policy.getTypeVersion() == null) { - policy.setTypeVersion(PfKey.NULL_KEY_VERSION); - } - if (!policyList.contains(policy)) { - policyList.add(policy); - } - } - } - - @Test - public void testNullList() { - ToscaPolicyFilter filter = ToscaPolicyFilter.builder().build(); - - assertThatThrownBy(() -> { - filter.filter(null); - }).hasMessageMatching("originalList is marked .*on.*ull but is null"); - } - - @Test - public void testFilterNothing() { - ToscaPolicyFilter filter = ToscaPolicyFilter.builder().build(); - - List filteredList = filter.filter(policyList); - assertTrue(filteredList.containsAll(policyList)); - } - - @Test - public void testFilterLatestVersion() { - ToscaPolicyFilter filter = ToscaPolicyFilter.builder().version(ToscaPolicyFilter.LATEST_VERSION).build(); - - List filteredList = filter.filter(policyList); - assertEquals(22, filteredList.size()); - assertEquals(VERSION_100, filteredList.get(7).getVersion()); - assertEquals(VERSION_100, filteredList.get(12).getVersion()); - - assertEquals(22, policyList.size()); - assertEquals(22, filteredList.size()); - - // - // Change versions to a couple of policies - // - policyList.forEach(policy -> { - if ("onap.vfirewall.tca".equals(policy.getName())) { - policy.setVersion("2.0.0"); - } else if ("operational.modifyconfig".equals(policy.getName())) { - policy.setVersion("3.4.5"); - } - }); - // - // We'll still get back the same number of policies - // - filteredList = filter.filter(policyList); - assertEquals(22, filteredList.size()); - // - // Assert that the correct versions are returned - // - policyList.forEach(policy -> { - if ("onap.vfirewall.tca".equals(policy.getName())) { - assertThat(policy.getVersion()).isEqualTo("2.0.0"); - } else if ("operational.modifyconfig".equals(policy.getName())) { - assertThat(policy.getVersion()).isEqualTo("3.4.5"); - } else { - assertThat(policy.getVersion()).isEqualTo(VERSION_100); - } - }); - - // - // Change versions back - // - policyList.forEach(policy -> { - if ("onap.vfirewall.tca".equals(policy.getName())) { - policy.setVersion(VERSION_100); - } else if ("operational.modifyconfig".equals(policy.getName())) { - policy.setVersion(VERSION_100); - } - }); - // - // We'll still get back the same number of policies - // - filteredList = filter.filter(policyList); - assertEquals(22, filteredList.size()); - // - // Assert that the correct versions are returned - // - policyList.forEach(policy -> assertThat(policy.getVersion()).isEqualTo(VERSION_100)); - } - - @Test - public void testFilterNameVersion() { - ToscaPolicyFilter filter = ToscaPolicyFilter.builder().name("operational.modifyconfig").build(); - List filteredList = filter.filter(policyList); - assertEquals(1, filteredList.size()); - - filter = ToscaPolicyFilter.builder().name("guard.frequency.scaleout").build(); - filteredList = filter.filter(policyList); - assertEquals(1, filteredList.size()); - - filter = ToscaPolicyFilter.builder().name("guard.frequency.scalein").build(); - filteredList = filter.filter(policyList); - assertEquals(0, filteredList.size()); - - filter = ToscaPolicyFilter.builder().version(VERSION_100).build(); - filteredList = filter.filter(policyList); - assertEquals(22, filteredList.size()); - - filter = ToscaPolicyFilter.builder().name("OSDF_CASABLANCA.SubscriberPolicy_v1").version(VERSION_100).build(); - filteredList = filter.filter(policyList); - assertEquals(1, filteredList.size()); - - filter = ToscaPolicyFilter.builder().name("operational.modifyconfig").version(VERSION_100).build(); - filteredList = filter.filter(policyList); - assertEquals(1, filteredList.size()); - } - - @Test - public void testFilterVersionPrefix() { - // null pattern - ToscaPolicyFilter filter = ToscaPolicyFilter.builder().versionPrefix(null).build(); - List filteredList = filter.filter(policyList); - assertEquals(22, filteredList.size()); - - filter = ToscaPolicyFilter.builder().versionPrefix("1.").build(); - filteredList = filter.filter(policyList); - assertEquals(22, filteredList.size()); - - filter = ToscaPolicyFilter.builder().versionPrefix("100.").build(); - filteredList = filter.filter(policyList); - assertEquals(0, filteredList.size()); - } - - @Test - public void testFilterTypeVersion() { - ToscaPolicyFilter filter = ToscaPolicyFilter.builder().type("onap.policies.controlloop.Operational").build(); - List filteredList = filter.filter(policyList); - assertEquals(0, filteredList.size()); - - filter = ToscaPolicyFilter.builder().type("onap.policies.controlloop.operational.common.Apex").build(); - filteredList = filter.filter(policyList); - assertEquals(0, filteredList.size()); - - filter = ToscaPolicyFilter.builder().type("onap.policies.controlloop.operational.common.Drools").build(); - filteredList = filter.filter(policyList); - assertEquals(3, filteredList.size()); - - filter = ToscaPolicyFilter.builder().type("onap.policies.monitoring.tcagen2").build(); - filteredList = filter.filter(policyList); - assertEquals(3, filteredList.size()); - - filter = ToscaPolicyFilter.builder().type("onap.policies.controlloop.NonOperational").build(); - filteredList = filter.filter(policyList); - assertEquals(0, filteredList.size()); - - filter = ToscaPolicyFilter.builder().typeVersion(VERSION_000).build(); - filteredList = filter.filter(policyList); - assertEquals(0, filteredList.size()); - - filter = ToscaPolicyFilter.builder().type("onap.policies.optimization.resource.HpaPolicy") - .typeVersion(VERSION_100).build(); - filteredList = filter.filter(policyList); - assertEquals(2, filteredList.size()); - - filter = ToscaPolicyFilter.builder().type("onap.policies.controlloop.Operational").typeVersion(VERSION_000) - .build(); - filteredList = filter.filter(policyList); - assertEquals(0, filteredList.size()); - } -} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTest.java index ba39621c6..1dc9e535e 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTest.java @@ -23,6 +23,7 @@ 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.assertNull; import java.util.LinkedHashMap; import org.junit.Test; @@ -45,6 +46,10 @@ public class ToscaPolicyTest { policy.setType("my_type"); policy.setTypeVersion("3.2.1"); + ToscaEntity te = new ToscaEntity(); + assertNull(te.getType()); + assertNull(te.getTypeVersion()); + assertEquals("ToscaEntityKey(name=my_name, version=1.2.3)", policy.getKey().toString()); assertEquals(new ToscaConceptIdentifier("my_name", "1.2.3"), policy.getKey().asIdentifier()); @@ -57,11 +62,11 @@ public class ToscaPolicyTest { assertEquals("3.2.1", type.getVersion()); ToscaPolicy clonedPolicy0 = new ToscaPolicy(policy); - assertEquals(0, new ToscaPolicyComparator().compare(policy, clonedPolicy0)); + assertEquals(0, new ToscaEntityComparator().compare(policy, clonedPolicy0)); policy.setProperties(new LinkedHashMap()); policy.getProperties().put("PropertyKey", "PropertyValue"); ToscaPolicy clonedPolicy1 = new ToscaPolicy(policy); - assertEquals(0, new ToscaPolicyComparator().compare(policy, clonedPolicy1)); + assertEquals(0, new ToscaEntityComparator().compare(policy, clonedPolicy1)); } } 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 deleted file mode 100644 index 031fceb9a..000000000 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilterTest.java +++ /dev/null @@ -1,193 +0,0 @@ -/*- - * ============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.assertNotNull; -import static org.junit.Assert.assertTrue; - -import com.google.gson.GsonBuilder; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import org.junit.BeforeClass; -import org.junit.Test; -import org.onap.policy.common.utils.coder.CoderException; -import org.onap.policy.common.utils.coder.StandardCoder; -import org.onap.policy.common.utils.resources.ResourceUtils; -import org.onap.policy.models.base.PfKey; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.yaml.snakeyaml.Yaml; - -/** - * Test of the {@link ToscaPolicyTypeFilter} class. - * - * @author Liam Fallon (liam.fallon@est.tech) - */ -public class ToscaPolicyTypeFilterTest { - // 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", - "policytypes/onap.policies.optimization.resource.VnfPolicy.yaml", - "policytypes/onap.policies.optimization.resource.PciPolicy.yaml", - "policytypes/onap.policies.optimization.resource.OptimizationPolicy.yaml", - "policytypes/onap.policies.controlloop.guard.common.Blacklist.yaml", - "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server.yaml", - "policytypes/onap.policies.optimization.resource.HpaPolicy.yaml", - "policytypes/onap.policies.optimization.resource.Vim_fit.yaml", - "policytypes/onap.policies.optimization.service.SubscriberPolicy.yaml", - "policytypes/onap.policies.optimization.resource.AffinityPolicy.yaml", - "policytypes/onap.policies.optimization.service.QueryPolicy.yaml", - "policytypes/onap.policies.controlloop.guard.common.MinMax.yaml", - "policytypes/onap.policies.controlloop.guard.common.FrequencyLimiter.yaml", - "policytypes/onap.policies.controlloop.guard.coordination.FirstBlocksSecond.yaml", - "policytypes/onap.policies.Optimization.yaml", - "policytypes/onap.policies.monitoring.tcagen2.yaml" - }; - // @formatter:on - - private static List typeList = new ArrayList<>(); - - /** - * Set up a Tosca Policy type list for filtering. - * - * @throws CoderException on JSON decoding errors - */ - @BeforeClass - public static void setupTypeList() throws CoderException { - for (String policyTypeResourceName : policyTypeResourceNames) { - String policyTypeString = ResourceUtils.getResourceAsString(policyTypeResourceName); - Object yamlObject = new Yaml().load(policyTypeString); - String yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject); - - ToscaServiceTemplate serviceTemplate = - new StandardCoder().decode(yamlAsJsonString, ToscaServiceTemplate.class); - assertNotNull(serviceTemplate); - - addPolicyTypes(serviceTemplate.getPolicyTypes()); - } - - for (ToscaPolicyType type : typeList) { - LOGGER.info("using policy type-" + type.getName() + ":" + type.getVersion()); - } - } - - private static void addPolicyTypes(Map foundPolicyTypeMap) { - for (Entry policyTypeEntry : foundPolicyTypeMap.entrySet()) { - ToscaPolicyType policyType = policyTypeEntry.getValue(); - if (policyType.getName() == null) { - policyType.setName(policyTypeEntry.getKey()); - } - if (policyType.getVersion() == null) { - policyType.setVersion(PfKey.NULL_KEY_VERSION); - } - if (!typeList.contains(policyType)) { - typeList.add(policyType); - } - } - } - - @Test - public void testNullList() { - ToscaPolicyTypeFilter filter = ToscaPolicyTypeFilter.builder().build(); - - assertThatThrownBy(() -> { - filter.filter(null); - }).hasMessageMatching("originalList is marked .*on.*ull but is null"); - } - - @Test - public void testFilterNothing() { - ToscaPolicyTypeFilter filter = ToscaPolicyTypeFilter.builder().build(); - - List filteredList = filter.filter(typeList); - assertTrue(filteredList.containsAll(typeList)); - } - - @Test - public void testFilterLatestVersion() { - ToscaPolicyTypeFilter filter = - ToscaPolicyTypeFilter.builder().version(ToscaPolicyTypeFilter.LATEST_VERSION).build(); - - List filteredList = filter.filter(typeList); - assertEquals(19, filteredList.size()); - assertEquals(VERSION_100, filteredList.get(0).getVersion()); - assertEquals(VERSION_100, filteredList.get(11).getVersion()); - - typeList.get(12).setVersion("2.0.0"); - filteredList = filter.filter(typeList); - assertEquals(19, filteredList.size()); - // - // This seems to change around as to where this policy type - // 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()); - // - // And now this index changes again?? - // - // assertEquals(VERSION_100, filteredList.get(17).getVersion()); - - typeList.get(12).setVersion(VERSION_100); - filteredList = filter.filter(typeList); - assertEquals(19, filteredList.size()); - assertEquals(VERSION_100, filteredList.get(0).getVersion()); - assertEquals(VERSION_100, filteredList.get(18).getVersion()); - } - - @Test - public void testFilterNameVersion() { - ToscaPolicyTypeFilter filter = ToscaPolicyTypeFilter.builder().name("onap.policies.Monitoring").build(); - List filteredList = filter.filter(typeList); - assertEquals(1, filteredList.size()); - - filter = ToscaPolicyTypeFilter.builder().name("onap.policies.monitoring.tcagen2").build(); - filteredList = filter.filter(typeList); - assertEquals(1, filteredList.size()); - - filter = ToscaPolicyTypeFilter.builder().name("onap.policies.optimization.LpaPolicy").build(); - filteredList = filter.filter(typeList); - assertEquals(0, filteredList.size()); - - filter = ToscaPolicyTypeFilter.builder().version(VERSION_100).build(); - filteredList = filter.filter(typeList); - assertEquals(19, filteredList.size()); - - filter = ToscaPolicyTypeFilter.builder().name("onap.policies.optimization.Vim_fit").version(VERSION_000) - .build(); - filteredList = filter.filter(typeList); - assertEquals(0, filteredList.size()); - - filter = ToscaPolicyTypeFilter.builder().name("onap.policies.optimization.Vim_fit").version("0.0.1").build(); - filteredList = filter.filter(typeList); - assertEquals(0, filteredList.size()); - } -} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeTest.java index 75c6b51e3..81e9c4f63 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019-2020 Nordix Foundation. + * Copyright (C) 2019-2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,7 +46,7 @@ public class ToscaPolicyTypeTest { tpt.setDescription("Desc"); ToscaPolicyType clonedTpt0 = new ToscaPolicyType(tpt); - assertEquals(0, new ToscaPolicyTypeComparator().compare(tpt, clonedTpt0)); + assertEquals(0, new ToscaEntityComparator().compare(tpt, clonedTpt0)); tpt.setMetadata(new LinkedHashMap<>()); tpt.setProperties(new LinkedHashMap<>()); @@ -57,6 +57,6 @@ public class ToscaPolicyTypeTest { tpt.getProperties().put("Property0", tp); ToscaPolicyType clonedTpt1 = new ToscaPolicyType(tpt); - assertEquals(0, new ToscaPolicyTypeComparator().compare(tpt, clonedTpt1)); + assertEquals(0, new ToscaEntityComparator().compare(tpt, clonedTpt1)); } } 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 deleted file mode 100644 index ba72e621b..000000000 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaServiceTemplateFilterTest.java +++ /dev/null @@ -1,125 +0,0 @@ -/*- - * ============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/concepts/ToscaTypedEntityFilterTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaTypedEntityFilterTest.java new file mode 100644 index 000000000..b83245d1f --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaTypedEntityFilterTest.java @@ -0,0 +1,287 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019-2021 Nordix Foundation. + * Modifications Copyright (C) 2019-2020 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.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import com.google.gson.GsonBuilder; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.models.base.PfKey; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.yaml.snakeyaml.Yaml; + +/** + * Test of the {@link ToscaTypedEntityFilter} class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaTypedEntityFilterTest { + 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(ToscaTypedEntityFilterTest.class); + + // @formatter:off + private static final String[] policyResourceNames = { + "policies/vCPE.policies.optimization.input.tosca.yaml", + "policies/vCPE.policy.monitoring.input.tosca.yaml", + "policies/vCPE.policy.operational.input.tosca.yaml", + "policies/vDNS.policy.monitoring.input.tosca.yaml", + "policies/vDNS.policy.operational.input.tosca.yaml", + "policies/vDNS.policy.guard.frequencylimiter.input.tosca.yaml", + "policies/vDNS.policy.guard.minmaxvnfs.input.tosca.yaml", + "policies/vFirewall.policy.monitoring.input.tosca.yaml", + "policies/vFirewall.policy.operational.input.tosca.yaml" + }; + // @formatter:on + + private static List policyList = new ArrayList<>(); + + /** + * Set up a Tosca Policy type list for filtering. + * + * @throws CoderException on JSON decoding errors + */ + @BeforeClass + public static void setupTypeList() throws CoderException { + for (String policyResourceName : policyResourceNames) { + String policyString = ResourceUtils.getResourceAsString(policyResourceName); + if (policyResourceName.endsWith("yaml")) { + LOGGER.info("loading {}", policyResourceName); + Object yamlObject = new Yaml().load(policyString); + policyString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject); + } + + ToscaServiceTemplate serviceTemplate = new StandardCoder().decode(policyString, ToscaServiceTemplate.class); + assertNotNull(serviceTemplate); + + for (Map foundPolicyMap : serviceTemplate.getToscaTopologyTemplate().getPolicies()) { + addPolicies(foundPolicyMap); + } + } + + for (ToscaPolicy policy : policyList) { + LOGGER.info("using policy-" + policy.getName() + ":" + policy.getVersion() + ", type-" + policy.getType() + + ":" + policy.getTypeVersion()); + } + } + + private static void addPolicies(Map foundPolicyMap) { + for (Entry policyEntry : foundPolicyMap.entrySet()) { + ToscaPolicy policy = policyEntry.getValue(); + if (policy.getName() == null) { + policy.setName(policyEntry.getKey()); + } + + if (policy.getVersion() == null) { + policy.setVersion(PfKey.NULL_KEY_VERSION); + } + if (policy.getTypeVersion() == null) { + policy.setTypeVersion(PfKey.NULL_KEY_VERSION); + } + if (!policyList.contains(policy)) { + policyList.add(policy); + } + } + } + + @Test + public void testNullList() { + ToscaTypedEntityFilter filter = ToscaTypedEntityFilter.builder().build(); + + assertThatThrownBy(() -> { + filter.filter(null); + }).hasMessageMatching("originalList is marked .*on.*ull but is null"); + } + + @Test + public void testFilterNothing() { + ToscaTypedEntityFilter filter = ToscaTypedEntityFilter.builder().build(); + + List filteredList = filter.filter(policyList); + assertTrue(filteredList.containsAll(policyList)); + } + + @Test + public void testFilterLatestVersion() { + ToscaTypedEntityFilter filter = + ToscaTypedEntityFilter.builder().version(ToscaTypedEntityFilter.LATEST_VERSION).build(); + + List filteredList = filter.filter(policyList); + assertEquals(22, filteredList.size()); + assertEquals(VERSION_100, filteredList.get(7).getVersion()); + assertEquals(VERSION_100, filteredList.get(12).getVersion()); + + assertEquals(22, policyList.size()); + assertEquals(22, filteredList.size()); + + // + // Change versions to a couple of policies + // + policyList.forEach(policy -> { + if ("onap.vfirewall.tca".equals(policy.getName())) { + policy.setVersion("2.0.0"); + } else if ("operational.modifyconfig".equals(policy.getName())) { + policy.setVersion("3.4.5"); + } + }); + // + // We'll still get back the same number of policies + // + filteredList = filter.filter(policyList); + assertEquals(22, filteredList.size()); + // + // Assert that the correct versions are returned + // + policyList.forEach(policy -> { + if ("onap.vfirewall.tca".equals(policy.getName())) { + assertThat(policy.getVersion()).isEqualTo("2.0.0"); + } else if ("operational.modifyconfig".equals(policy.getName())) { + assertThat(policy.getVersion()).isEqualTo("3.4.5"); + } else { + assertThat(policy.getVersion()).isEqualTo(VERSION_100); + } + }); + + // + // Change versions back + // + policyList.forEach(policy -> { + if ("onap.vfirewall.tca".equals(policy.getName())) { + policy.setVersion(VERSION_100); + } else if ("operational.modifyconfig".equals(policy.getName())) { + policy.setVersion(VERSION_100); + } + }); + // + // We'll still get back the same number of policies + // + filteredList = filter.filter(policyList); + assertEquals(22, filteredList.size()); + // + // Assert that the correct versions are returned + // + policyList.forEach(policy -> assertThat(policy.getVersion()).isEqualTo(VERSION_100)); + } + + @Test + public void testFilterNameVersion() { + ToscaTypedEntityFilter filter = + ToscaTypedEntityFilter.builder().name("operational.modifyconfig").build(); + List filteredList = filter.filter(policyList); + assertEquals(1, filteredList.size()); + + filter = ToscaTypedEntityFilter.builder().name("guard.frequency.scaleout").build(); + filteredList = filter.filter(policyList); + assertEquals(1, filteredList.size()); + + filter = ToscaTypedEntityFilter.builder().name("guard.frequency.scalein").build(); + filteredList = filter.filter(policyList); + assertEquals(0, filteredList.size()); + + filter = ToscaTypedEntityFilter.builder().version(VERSION_100).build(); + filteredList = filter.filter(policyList); + assertEquals(22, filteredList.size()); + + filter = ToscaTypedEntityFilter.builder().name("OSDF_CASABLANCA.SubscriberPolicy_v1") + .version(VERSION_100).build(); + filteredList = filter.filter(policyList); + assertEquals(1, filteredList.size()); + + filter = ToscaTypedEntityFilter.builder().name("operational.modifyconfig").version(VERSION_100) + .build(); + filteredList = filter.filter(policyList); + assertEquals(1, filteredList.size()); + } + + @Test + public void testFilterVersionPrefix() { + // null pattern + ToscaTypedEntityFilter filter = + ToscaTypedEntityFilter.builder().versionPrefix(null).build(); + List filteredList = filter.filter(policyList); + assertEquals(22, filteredList.size()); + + filter = ToscaTypedEntityFilter.builder().versionPrefix("1.").build(); + filteredList = filter.filter(policyList); + assertEquals(22, filteredList.size()); + + filter = ToscaTypedEntityFilter.builder().versionPrefix("100.").build(); + filteredList = filter.filter(policyList); + assertEquals(0, filteredList.size()); + } + + @Test + public void testFilterTypeVersion() { + ToscaTypedEntityFilter filter = + ToscaTypedEntityFilter.builder().type("onap.policies.controlloop.Operational").build(); + List filteredList = filter.filter(policyList); + assertEquals(0, filteredList.size()); + + filter = ToscaTypedEntityFilter.builder().type("onap.policies.controlloop.operational.common.Apex") + .build(); + filteredList = filter.filter(policyList); + assertEquals(0, filteredList.size()); + + filter = ToscaTypedEntityFilter.builder() + .type("onap.policies.controlloop.operational.common.Drools").build(); + filteredList = filter.filter(policyList); + assertEquals(3, filteredList.size()); + + filter = ToscaTypedEntityFilter.builder().type("onap.policies.monitoring.tcagen2").build(); + filteredList = filter.filter(policyList); + assertEquals(3, filteredList.size()); + + filter = ToscaTypedEntityFilter.builder().type("onap.policies.controlloop.NonOperational").build(); + filteredList = filter.filter(policyList); + assertEquals(0, filteredList.size()); + + filter = ToscaTypedEntityFilter.builder().typeVersion(VERSION_000).build(); + filteredList = filter.filter(policyList); + assertEquals(0, filteredList.size()); + + filter = ToscaTypedEntityFilter.builder().type("onap.policies.optimization.resource.HpaPolicy") + .typeVersion(VERSION_100).build(); + filteredList = filter.filter(policyList); + assertEquals(2, filteredList.size()); + + filter = ToscaTypedEntityFilter.builder().type("onap.policies.controlloop.Operational") + .typeVersion(VERSION_000).build(); + filteredList = filter.filter(policyList); + assertEquals(0, filteredList.size()); + } +} 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 f66a8e8a5..0f9adf89a 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 @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019-2020 Nordix Foundation. + * Copyright (C) 2019-2021 Nordix Foundation. * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -45,9 +45,9 @@ 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; import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; +import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter; import org.yaml.snakeyaml.Yaml; /** @@ -166,7 +166,8 @@ public class AuthorativeToscaProviderPolicyTest { }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { - new AuthorativeToscaProvider().getFilteredPolicies(null, ToscaPolicyFilter.builder().build()); + new AuthorativeToscaProvider().getFilteredPolicies(null, + ToscaTypedEntityFilter.builder().build()); }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { @@ -178,7 +179,8 @@ public class AuthorativeToscaProviderPolicyTest { }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { - new AuthorativeToscaProvider().getFilteredPolicyList(null, ToscaPolicyFilter.builder().build()); + new AuthorativeToscaProvider().getFilteredPolicyList(null, + ToscaTypedEntityFilter.builder().build()); }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { @@ -203,8 +205,8 @@ public class AuthorativeToscaProviderPolicyTest { assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy)); assertEquals(beforePolicy.getType(), createdPolicy.getType()); - ToscaServiceTemplate gotServiceTemplate = - new AuthorativeToscaProvider().getFilteredPolicies(pfDao, ToscaPolicyFilter.builder().build()); + ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicies(pfDao, + ToscaTypedEntityFilter.builder().build()); ToscaPolicy gotPolicy = gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); @@ -212,14 +214,14 @@ public class AuthorativeToscaProviderPolicyTest { assertEquals(beforePolicy.getType(), gotPolicy.getType()); gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicies(pfDao, - ToscaPolicyFilter.builder().name(policyKey.getName()).build()); + ToscaTypedEntityFilter.builder().name(policyKey.getName()).build()); gotPolicy = gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy)); assertEquals(beforePolicy.getType(), gotPolicy.getType()); gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicies(pfDao, - ToscaPolicyFilter.builder().name(policyKey.getName()).version(VERSION_100).build()); + ToscaTypedEntityFilter.builder().name(policyKey.getName()).version(VERSION_100).build()); gotPolicy = gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy)); @@ -229,18 +231,18 @@ public class AuthorativeToscaProviderPolicyTest { assertEquals(1, gotPolicyList.size()); assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); - gotPolicyList = - new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, ToscaPolicyFilter.builder().build()); + gotPolicyList = new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, + ToscaTypedEntityFilter.builder().build()); assertEquals(1, gotPolicyList.size()); assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); gotPolicyList = new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, - ToscaPolicyFilter.builder().name(policyKey.getName()).build()); + ToscaTypedEntityFilter.builder().name(policyKey.getName()).build()); assertEquals(1, gotPolicyList.size()); assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); gotPolicyList = new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, - ToscaPolicyFilter.builder().name(policyKey.getName()).version(VERSION_100).build()); + ToscaTypedEntityFilter.builder().name(policyKey.getName()).version(VERSION_100).build()); assertEquals(1, gotPolicyList.size()); assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTypeTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTypeTest.java index 0e1164598..088e8e847 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTypeTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTypeTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019-2020 Nordix Foundation. + * Copyright (C) 2019-2021 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -43,16 +43,14 @@ 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.ToscaEntityFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; import org.yaml.snakeyaml.Yaml; /** * Test of the {@link AuthorativeToscaProvider} class. - * - * @author Liam Fallon (liam.fallon@est.tech) */ public class AuthorativeToscaProviderPolicyTypeTest { private static final String VERSION = "version"; @@ -131,7 +129,7 @@ public class AuthorativeToscaProviderPolicyTypeTest { assertNotNull(toscaServiceTemplate); ToscaServiceTemplate createdServiceTemplate = - new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate); + new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate); PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1); @@ -140,15 +138,15 @@ public class AuthorativeToscaProviderPolicyTypeTest { assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName())); assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); - ToscaServiceTemplate gotServiceTemplate = - new AuthorativeToscaProvider().getPolicyTypes(pfDao, policyTypeKey.getName(), policyTypeKey.getVersion()); + ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao, + policyTypeKey.getName(), policyTypeKey.getVersion()); ToscaPolicyType gotPolicyType = gotServiceTemplate.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())); @@ -164,9 +162,8 @@ public class AuthorativeToscaProviderPolicyTypeTest { 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()); } @@ -178,7 +175,8 @@ public class AuthorativeToscaProviderPolicyTypeTest { }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { - new AuthorativeToscaProvider().getFilteredPolicyTypes(null, ToscaPolicyTypeFilter.builder().build()); + new AuthorativeToscaProvider().getFilteredPolicyTypes(null, + ToscaEntityFilter.builder().build()); }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { @@ -190,7 +188,8 @@ public class AuthorativeToscaProviderPolicyTypeTest { }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { - new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, ToscaPolicyTypeFilter.builder().build()); + new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, + ToscaEntityFilter.builder().build()); }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { @@ -198,17 +197,17 @@ public class AuthorativeToscaProviderPolicyTypeTest { }).hasMessageMatching("^filter is marked .*on.*ull but is null$"); assertThatThrownBy(() -> new AuthorativeToscaProvider().getFilteredPolicyTypeList(new DefaultPfDao(), - ToscaPolicyTypeFilter.builder().name("i.dont.Exist").build())) - .hasMessageContaining("Policy Framework DAO has not been initialized"); + ToscaEntityFilter.builder().name("i.dont.Exist").build())) + .hasMessageContaining("Policy Framework DAO has not been initialized"); - assertTrue(new AuthorativeToscaProvider() - .getFilteredPolicyTypeList(pfDao, ToscaPolicyTypeFilter.builder().name("i.dont.Exist").build()).isEmpty()); + assertTrue(new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, + ToscaEntityFilter.builder().name("i.dont.Exist").build()).isEmpty()); ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); assertNotNull(toscaServiceTemplate); ToscaServiceTemplate createdServiceTemplate = - new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate); + new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate); PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1); @@ -217,49 +216,49 @@ public class AuthorativeToscaProviderPolicyTypeTest { assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName())); assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); - ToscaServiceTemplate gotServiceTemplate = - new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaPolicyTypeFilter.builder().build()); + ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, + ToscaEntityFilter.builder().build()); ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName()); assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription())); gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, - ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build()); + ToscaEntityFilter.builder().name(policyTypeKey.getName()).build()); gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName()); assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription())); - gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, - ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version(VERSION_001).build()); + gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaEntityFilter + .builder().name(policyTypeKey.getName()).version(VERSION_001).build()); gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName()); assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.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())); - gotPolicyTypeList = - new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, ToscaPolicyTypeFilter.builder().build()); + gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, + ToscaEntityFilter.builder().build()); assertEquals(2, gotPolicyTypeList.size()); assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, - ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build()); + ToscaEntityFilter.builder().name(policyTypeKey.getName()).build()); assertEquals(1, gotPolicyTypeList.size()); assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); - gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, - ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version(VERSION_001).build()); + gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, ToscaEntityFilter + .builder().name(policyTypeKey.getName()).version(VERSION_001).build()); assertEquals(1, gotPolicyTypeList.size()); assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, - ToscaPolicyTypeFilter.builder().version("1.0.0").build()); + ToscaEntityFilter.builder().version("1.0.0").build()); assertEquals(1, gotPolicyTypeList.size()); assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); } @@ -292,7 +291,7 @@ public class AuthorativeToscaProviderPolicyTypeTest { assertNotNull(toscaServiceTemplate); ToscaServiceTemplate createdServiceTemplate = - new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate); + new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate); PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1); @@ -324,7 +323,7 @@ public class AuthorativeToscaProviderPolicyTypeTest { assertNotNull(toscaServiceTemplate); ToscaServiceTemplate createdServiceTemplate = - new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate); + new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate); PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1); @@ -334,7 +333,7 @@ public class AuthorativeToscaProviderPolicyTypeTest { assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); ToscaServiceTemplate updatedServiceTemplate = - new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate); + new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate); ToscaPolicyType updatedPolicy = updatedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName()); assertEquals(true, beforePolicyType.getName().equals(updatedPolicy.getName())); @@ -375,7 +374,7 @@ public class AuthorativeToscaProviderPolicyTypeTest { assertNotNull(toscaServiceTemplate); ToscaServiceTemplate createdServiceTemplate = - new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate); + new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate); PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1); @@ -384,8 +383,8 @@ public class AuthorativeToscaProviderPolicyTypeTest { assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName())); assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); - ToscaServiceTemplate deletedServiceTemplate = - new AuthorativeToscaProvider().deletePolicyType(pfDao, policyTypeKey.getName(), policyTypeKey.getVersion()); + ToscaServiceTemplate deletedServiceTemplate = new AuthorativeToscaProvider().deletePolicyType(pfDao, + policyTypeKey.getName(), policyTypeKey.getVersion()); ToscaPolicyType deletedPolicy = deletedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName()); assertEquals(true, beforePolicyType.getName().equals(deletedPolicy.getName())); @@ -422,6 +421,6 @@ public class AuthorativeToscaProviderPolicyTypeTest { @Test public void testNullParameters() throws Exception { assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypeList(null, null, null)) - .hasMessageMatching("^dao is marked .*on.*ull but is null$"); + .hasMessageMatching("^dao is marked .*on.*ull but is null$"); } } -- cgit 1.2.3-korg