diff options
author | liamfallon <liam.fallon@est.tech> | 2019-04-08 13:58:53 +0000 |
---|---|---|
committer | liamfallon <liam.fallon@est.tech> | 2019-04-08 13:58:53 +0000 |
commit | 9ce39af891ccf063d46e18ecf5a2a47eb1408930 (patch) | |
tree | eb55cc63c83ea34f73aeaaae5039c96283b66ef8 /models-pdp/src/test/java | |
parent | eb7127ac85b3df30a09277721a5f9271033843e7 (diff) |
Add bug fixes and tests for filters
Fixed bugs on filtering where lack of null checks was blocking
all results.
Added unit test for PDP related JPA objects.
Fixed cascading and orphan control on JPA objects.
Added partial testing of PdpProvider.
Added partial testing of filters.
Changed tag for content of operational policies from "Content" to "content".
Issue-ID: POLICY-1095
Change-Id: Ieb22e06955a8434b490bae7d0f6b77d4479515e8
Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'models-pdp/src/test/java')
3 files changed, 218 insertions, 84 deletions
diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/PdpGroupFilterTest.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/PdpGroupFilterTest.java new file mode 100644 index 000000000..61974dc89 --- /dev/null +++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/PdpGroupFilterTest.java @@ -0,0 +1,108 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.pdp.concepts; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.List; + +import org.junit.Before; +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; + +/** + * Test of the {@link PdpGroupFilter} class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class PdpGroupFilterTest { + private List<PdpGroup> pdpGroupList; + + /** + * Set up a PDP group list for filtering. + * + * @throws CoderException on JSON decoding errors + */ + @Before + public void setupPdpGroupList() throws CoderException { + String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroupsForFiltering.json"); + PdpGroups pdpGroups = new StandardCoder().decode(originalJson, PdpGroups.class); + pdpGroupList = pdpGroups.getGroups(); + } + + @Test + public void testNullList() { + PdpGroupFilter filter = PdpGroupFilter.builder().build(); + + assertThatThrownBy(() -> { + filter.filter(null); + }).hasMessage("originalList is marked @NonNull but is null"); + } + + @Test + public void testFilterNothing() { + PdpGroupFilter filter = PdpGroupFilter.builder().build(); + + List<PdpGroup> filteredList = filter.filter(pdpGroupList); + assertTrue(filteredList.containsAll(pdpGroupList)); + } + + @Test + public void testFilterLatestVersion() { + PdpGroupFilter filter = PdpGroupFilter.builder().version(PdpGroupFilter.LATEST_VERSION).build(); + + List<PdpGroup> filteredList = filter.filter(pdpGroupList); + assertEquals(2, filteredList.size()); + assertEquals("1.2.4", filteredList.get(0).getVersion()); + assertEquals("1.2.3", filteredList.get(1).getVersion()); + } + + @Test + public void testFilterNameVersion() { + PdpGroupFilter filter = PdpGroupFilter.builder().name("PdpGroup0").build(); + List<PdpGroup> filteredList = filter.filter(pdpGroupList); + assertEquals(3, filteredList.size()); + + filter = PdpGroupFilter.builder().name("PdpGroup1").build(); + filteredList = filter.filter(pdpGroupList); + assertEquals(2, filteredList.size()); + + filter = PdpGroupFilter.builder().name("PdpGroup2").build(); + filteredList = filter.filter(pdpGroupList); + assertEquals(0, filteredList.size()); + + filter = PdpGroupFilter.builder().version("1.2.3").build(); + filteredList = filter.filter(pdpGroupList); + assertEquals(2, filteredList.size()); + + filter = PdpGroupFilter.builder().name("PdpGroup0").version("1.2.3").build(); + filteredList = filter.filter(pdpGroupList); + assertEquals(1, filteredList.size()); + + filter = PdpGroupFilter.builder().name("PdpGroup1").version("1.2.9").build(); + filteredList = filter.filter(pdpGroupList); + assertEquals(0, filteredList.size()); + } +} diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/TestPdpGroup.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/TestPdpGroup.java index 4de1f2ee3..4698ece7b 100644 --- a/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/TestPdpGroup.java +++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/TestPdpGroup.java @@ -25,7 +25,9 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import java.util.Map; import java.util.TreeMap; import org.junit.Test; @@ -83,4 +85,27 @@ public class TestPdpGroup { group.setDescription("B"); assertTrue(hash != group.hashCode()); } + + @Test + public void testCompareTo() { + PdpGroup pdpGroup0 = new PdpGroup(); + pdpGroup0.setName("Name0"); + pdpGroup0.setVersion("1.2.3"); + + PdpGroup pdpGroup1 = new PdpGroup(); + pdpGroup1.setName("Name0"); + pdpGroup1.setVersion("1.2.3"); + + assertEquals(0, pdpGroup0.compareTo(pdpGroup1)); + + PdpGroups pdpGroups = new PdpGroups(); + pdpGroups.setGroups(new ArrayList<>()); + pdpGroups.getGroups().add(pdpGroup0); + pdpGroups.getGroups().add(pdpGroup1); + + List<Map<String, PdpGroup>> mapList = pdpGroups.toMapList(); + + assertEquals(1, mapList.size()); + assertEquals(1, mapList.get(0).size()); + } } diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpProviderTest.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpProviderTest.java index 468f3d4f9..4012eaa1c 100644 --- a/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpProviderTest.java +++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpProviderTest.java @@ -24,10 +24,12 @@ package org.onap.policy.models.pdp.persistence.provider; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; import java.sql.Connection; import java.sql.DriverManager; import java.util.ArrayList; +import java.util.List; import org.junit.After; import org.junit.Before; @@ -38,8 +40,9 @@ 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.pdp.concepts.Pdp; +import org.onap.policy.models.pdp.concepts.PdpGroup; import org.onap.policy.models.pdp.concepts.PdpGroups; -import org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup; import org.onap.policy.models.pdp.persistence.provider.PdpProvider; import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider; @@ -91,7 +94,7 @@ public class PdpProviderTest { } @Test - public void testPoliciesGet() throws Exception { + public void testGroupsGet() throws Exception { assertThatThrownBy(() -> { new PdpProvider().getPdpGroups(null, null, null); }).hasMessage("dao is marked @NonNull but is null"); @@ -105,7 +108,6 @@ public class PdpProviderTest { }).hasMessage("dao is marked @NonNull but is null"); String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0.json"); - PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class); PdpGroups createdPdpGroups0 = new PdpGroups(); @@ -122,7 +124,7 @@ public class PdpProviderTest { } @Test - public void testPolicyCreate() throws Exception { + public void testGroupsCreate() throws Exception { assertThatThrownBy(() -> { new PdpProvider().createPdpGroups(null, null); }).hasMessage("dao is marked @NonNull but is null"); @@ -136,7 +138,6 @@ public class PdpProviderTest { }).hasMessage("pdpGroups is marked @NonNull but is null"); String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0.json"); - PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class); PdpGroups createdPdpGroups0 = new PdpGroups(); @@ -152,7 +153,7 @@ public class PdpProviderTest { } @Test - public void testPolicyCreateNoPdp() throws Exception { + public void testGroupsCreateNoPdp() throws Exception { String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroupsNoPDPs.json"); PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class); @@ -171,82 +172,82 @@ public class PdpProviderTest { String gotJson = standardCoder.encode(gotPdpGroups0); assertEquals(originalTweakedJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", "")); } - /* - * @Test public void testPolicyUpdate() throws Exception { try { new SimpleToscaProvider().updatePolicies(null, - * null); fail("test should throw an exception here"); } catch (Exception exc) { - * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); } - * - * try { new SimpleToscaProvider().updatePolicies(null, new JpaToscaServiceTemplate()); - * fail("test should throw an exception here"); } catch (Exception exc) { - * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); } - * - * try { new SimpleToscaProvider().updatePolicies(pfDao, null); fail("test should throw an exception here"); } catch - * (Exception exc) { assertEquals("serviceTemplate is marked @NonNull but is null", exc.getMessage()); } - * - * ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( - * ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), - * ToscaServiceTemplate.class); - * - * JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate(); - * originalServiceTemplate.fromAuthorative(toscaServiceTemplate); - * - * assertNotNull(originalServiceTemplate); JpaToscaServiceTemplate updatedServiceTemplate = new - * SimpleToscaProvider().updatePolicies(pfDao, originalServiceTemplate); - * - * assertEquals(originalServiceTemplate, updatedServiceTemplate); } - * - * @Test public void testPoliciesDelete() throws Exception { try { new SimpleToscaProvider().deletePolicy(null, - * null); fail("test should throw an exception here"); } catch (Exception exc) { - * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); } - * - * try { new SimpleToscaProvider().deletePolicy(null, new PfConceptKey()); - * fail("test should throw an exception here"); } catch (Exception exc) { - * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); } - * - * try { new SimpleToscaProvider().deletePolicy(pfDao, null); fail("test should throw an exception here"); } catch - * (Exception exc) { assertEquals("policyKey is marked @NonNull but is null", exc.getMessage()); } - * - * ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( - * ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), - * ToscaServiceTemplate.class); - * - * JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate(); - * originalServiceTemplate.fromAuthorative(toscaServiceTemplate); - * - * assertNotNull(originalServiceTemplate); JpaToscaServiceTemplate createdServiceTemplate = new - * SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate); - * - * assertEquals(originalServiceTemplate, createdServiceTemplate); - * - * PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); - * - * JpaToscaServiceTemplate deletedServiceTemplate = new SimpleToscaProvider().deletePolicy(pfDao, new - * PfConceptKey(policyKey)); - * - * assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey), - * deletedServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey)); - * - * try { new SimpleToscaProvider().getPolicies(pfDao, new PfConceptKey(policyKey)); - * fail("test should throw an exception here"); } catch (Exception exc) { - * assertEquals("policy not found: onap.restart.tca:1.0.0", exc.getMessage()); } } - * - * @Test public void testAssertPoliciesExist() throws PfModelException { JpaToscaServiceTemplate testServiceTemplate - * = new JpaToscaServiceTemplate(); - * - * try { new SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate); - * fail("test should throw an exception here"); } catch (Exception exc) { - * assertEquals("topology template not specified on service template", exc.getMessage()); } - * - * testServiceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); try { new - * SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate); fail("test should throw an exception here"); } - * catch (Exception exc) { assertEquals("no policies specified on topology template of service template", - * exc.getMessage()); } - * - * testServiceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies()); try { new - * SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate); fail("test should throw an exception here"); } - * catch (Exception exc) { - * assertEquals("list of policies specified on topology template of service template is empty", exc.getMessage()); } - * - * } - */ + + @Test + public void testGroupsUpdate() throws Exception { + assertThatThrownBy(() -> { + new PdpProvider().updatePdpGroups(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new PdpProvider().updatePdpGroups(null, new ArrayList<>()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new PdpProvider().updatePdpGroups(pfDao, null); + }).hasMessage("pdpGroups is marked @NonNull but is null"); + + String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0.json"); + PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class); + + PdpGroups createdPdpGroups0 = new PdpGroups(); + createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups())); + String createdJson = standardCoder.encode(createdPdpGroups0); + assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", "")); + + PdpGroups gotPdpGroups0 = new PdpGroups(); + gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, "PdpGroup0", "1.2.3")); + + String gotJson = standardCoder.encode(gotPdpGroups0); + assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", "")); + + String updateJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0Update.json"); + PdpGroups updatePdpGroups0 = standardCoder.decode(updateJson, PdpGroups.class); + + PdpGroups updatedPdpGroups0 = new PdpGroups(); + updatedPdpGroups0.setGroups(new PdpProvider().updatePdpGroups(pfDao, updatePdpGroups0.getGroups())); + + List<Pdp> beforePdpInstances = updatePdpGroups0.getGroups().get(0).getPdpSubgroups().get(0).getPdpInstances(); + List<Pdp> afterPdpInstances = updatedPdpGroups0.getGroups().get(0).getPdpSubgroups().get(0).getPdpInstances(); + assertTrue(beforePdpInstances.containsAll(afterPdpInstances)); + } + + @Test + public void testPoliciesDelete() throws Exception { + assertThatThrownBy(() -> { + new PdpProvider().deletePdpGroup(null, null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new PdpProvider().deletePdpGroup(null, null, "version"); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new PdpProvider().deletePdpGroup(null, "name", "version"); + }).hasMessage("dao is marked @NonNull but is null"); + + String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0.json"); + PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class); + + PdpGroups createdPdpGroups0 = new PdpGroups(); + createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups())); + String createdJson = standardCoder.encode(createdPdpGroups0); + assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", "")); + + PdpGroups gotPdpGroups0 = new PdpGroups(); + gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, "PdpGroup0", "1.2.3")); + + String gotJson = standardCoder.encode(gotPdpGroups0); + assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", "")); + + PdpGroup deletedPdpGroup = new PdpProvider().deletePdpGroup(pfDao, "PdpGroup0", "1.2.3"); + + assertEquals(createdPdpGroups0.getGroups().get(0), deletedPdpGroup); + + assertEquals(0, new PdpProvider().getPdpGroups(pfDao, "PdpGroup0", "1.2.3").size()); + + assertThatThrownBy(() -> { + new PdpProvider().deletePdpGroup(pfDao, "PdpGroup0", "1.2.3"); + }).hasMessage("delete of PDP group \"PdpGroup0:1.2.3\" failed, PDP group does not exist"); + } } |