From 9ce39af891ccf063d46e18ecf5a2a47eb1408930 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Mon, 8 Apr 2019 13:58:53 +0000 Subject: 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 --- .../org/onap/policy/models/base/PfConceptKey.java | 4 +- .../onap/policy/models/base/PfObjectFilter.java | 12 +- .../onap/policy/models/base/PfReferenceKey.java | 8 +- .../policy/models/base/PfObjectFilterTest.java | 21 ++- models-pdp/pom.xml | 9 +- .../org/onap/policy/models/pdp/concepts/Pdp.java | 6 +- .../policy/models/pdp/concepts/PdpGroupFilter.java | 9 +- .../policy/models/pdp/concepts/PdpSubGroup.java | 8 +- .../pdp/persistence/concepts/JpaPdpGroup.java | 4 +- .../pdp/persistence/concepts/JpaPdpSubGroup.java | 23 ++- .../models/pdp/concepts/PdpGroupFilterTest.java | 108 +++++++++++ .../policy/models/pdp/concepts/TestPdpGroup.java | 25 +++ .../pdp/persistence/provider/PdpProviderTest.java | 169 ++++++++--------- .../test/resources/testdata/PdpGroups0Update.json | 83 +++++++++ .../resources/testdata/PdpGroupsForFiltering.json | 199 +++++++++++++++++++++ .../authorative/concepts/ToscaPolicyFilter.java | 9 +- .../concepts/ToscaPolicyTypeFilter.java | 5 +- .../mapping/LegacyOperationalPolicyMapper.java | 8 +- 18 files changed, 570 insertions(+), 140 deletions(-) create mode 100644 models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/PdpGroupFilterTest.java create mode 100644 models-pdp/src/test/resources/testdata/PdpGroups0Update.json create mode 100644 models-pdp/src/test/resources/testdata/PdpGroupsForFiltering.json diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfConceptKey.java b/models-base/src/main/java/org/onap/policy/models/base/PfConceptKey.java index ee476e3b2..5d6ac13c1 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfConceptKey.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfConceptKey.java @@ -51,10 +51,10 @@ public class PfConceptKey extends PfKey { private static final String NAME_TOKEN = "name"; private static final String VERSION_TOKEN = "version"; - @Column(name = NAME_TOKEN, length = 128) + @Column(name = NAME_TOKEN, length = 120) private String name; - @Column(name = VERSION_TOKEN, length = 128) + @Column(name = VERSION_TOKEN, length = 20) private String version; /** diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfObjectFilter.java b/models-base/src/main/java/org/onap/policy/models/base/PfObjectFilter.java index 6ede4d9a3..501d9c353 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfObjectFilter.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfObjectFilter.java @@ -46,11 +46,11 @@ public interface PfObjectFilter> { * Check if a value matches a regular expression. * * @param value the incoming value to check - * @param regexp the regular expression to check against + * @param pattern the pattern to check against * @return match or not */ - public default boolean filterOnRegexp(@NonNull final String value, @NonNull final String regexp) { - return value.matches(regexp); + public default boolean filterString(@NonNull final String value, final String pattern) { + return pattern == null || value.equals(pattern); } /** @@ -74,10 +74,10 @@ public interface PfObjectFilter> { T lastElement = filteredList.get(j); /* - * The list is sorted so if the last element name is the same as the current - * element name, the current element should be removed. + * The list is sorted so if the last element name is the same as the current element name, the current + * element should be removed. */ - if (!((PfNameVersion)curElement).getName().equals(((PfNameVersion)lastElement).getName())) { + if (!((PfNameVersion) curElement).getName().equals(((PfNameVersion) lastElement).getName())) { // have a new name - done comparing with the old "current" ++icur; } diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfReferenceKey.java b/models-base/src/main/java/org/onap/policy/models/base/PfReferenceKey.java index 185ccfa69..bda5aed12 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfReferenceKey.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfReferenceKey.java @@ -73,16 +73,16 @@ public class PfReferenceKey extends PfKey { private static final int PARENT_LOCAL_NAME_FIELD = 2; private static final int LOCAL_NAME_FIELD = 3; - @Column(name = PARENT_KEY_NAME, length = 128) + @Column(name = PARENT_KEY_NAME, length = 120) private String parentKeyName; - @Column(name = PARENT_KEY_VERSION, length = 128) + @Column(name = PARENT_KEY_VERSION, length = 15) private String parentKeyVersion; - @Column(name = PARENT_LOCAL_NAME, length = 128) + @Column(name = PARENT_LOCAL_NAME, length = 120) private String parentLocalName; - @Column(name = LOCAL_NAME, length = 128) + @Column(name = LOCAL_NAME, length = 120) private String localName; /** diff --git a/models-base/src/test/java/org/onap/policy/models/base/PfObjectFilterTest.java b/models-base/src/test/java/org/onap/policy/models/base/PfObjectFilterTest.java index c3ccb4aa5..3d16f8e3f 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/PfObjectFilterTest.java +++ b/models-base/src/test/java/org/onap/policy/models/base/PfObjectFilterTest.java @@ -81,21 +81,21 @@ public class PfObjectFilterTest { doList.add(do5); DummyPfObjectFilter dof = new DummyPfObjectFilter(); - assertFalse(dof.filterOnRegexp("Hello", "Goodbye")); - assertTrue(dof.filterOnRegexp("Hello", "Hello")); + assertFalse(dof.filterString("Hello", "Goodbye")); + assertTrue(dof.filterString("Hello", "Hello")); assertThatThrownBy(() -> { - dof.filterOnRegexp(null, null); + dof.filterString(null, null); }).hasMessage("value is marked @NonNull but is null"); assertThatThrownBy(() -> { - dof.filterOnRegexp("hello", null); - }).hasMessage("regexp is marked @NonNull but is null"); - - assertThatThrownBy(() -> { - dof.filterOnRegexp(null, "hello"); + dof.filterString(null, "hello"); }).hasMessage("value is marked @NonNull but is null"); + assertEquals(false, dof.filterString("Hello", "Goodbye")); + assertEquals(true, dof.filterString("Hello", "Hello")); + assertEquals(true, dof.filterString("Hello", null)); + List latestVersionList = dof.latestVersionFilter(doList); assertEquals(3, latestVersionList.size()); assertEquals("aaaaa", latestVersionList.get(0).getName()); @@ -104,5 +104,10 @@ public class PfObjectFilterTest { assertEquals("1.0.0", latestVersionList.get(1).getVersion()); assertEquals("name1", latestVersionList.get(2).getName()); assertEquals("0.1.2", latestVersionList.get(2).getVersion()); + + latestVersionList.remove(2); + latestVersionList.remove(1); + List newestVersionList = dof.latestVersionFilter(latestVersionList); + assertEquals(latestVersionList, newestVersionList); } } diff --git a/models-pdp/pom.xml b/models-pdp/pom.xml index aaf9ee48f..029c76b8f 100644 --- a/models-pdp/pom.xml +++ b/models-pdp/pom.xml @@ -44,14 +44,13 @@ policy-models-tosca ${project.version} - - com.h2database - h2 - test - org.mariadb.jdbc mariadb-java-client + + + com.h2database + h2 test diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/Pdp.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/Pdp.java index 1eaa41547..034374c29 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/Pdp.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/Pdp.java @@ -21,10 +21,9 @@ package org.onap.policy.models.pdp.concepts; -import lombok.Getter; +import lombok.Data; import lombok.NoArgsConstructor; import lombok.NonNull; -import lombok.Setter; import lombok.ToString; import org.onap.policy.models.pdp.enums.PdpHealthStatus; import org.onap.policy.models.pdp.enums.PdpState; @@ -34,10 +33,9 @@ import org.onap.policy.models.pdp.enums.PdpState; * * @author Ram Krishna Verma (ram.krishna.verma@est.tech) */ -@Getter -@Setter @ToString @NoArgsConstructor +@Data public class Pdp { @NonNull diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroupFilter.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroupFilter.java index 5965b728d..3c07a9d0f 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroupFilter.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroupFilter.java @@ -46,7 +46,7 @@ public class PdpGroupFilter implements PfObjectFilter { // Regular expression private String name; - // Regular Expression, set to LATEST_VERRSION to get the latest version + // Regular Expression, set to to get the latest version private String version; private PdpState groupState; @@ -67,9 +67,10 @@ public class PdpGroupFilter implements PfObjectFilter { // @formatter:off List returnList = originalList.stream() - .filter(p -> filterOnRegexp(p.getName(), name)) - .filter(p -> version.equals(LATEST_VERSION) || filterOnRegexp(p.getVersion(), version)) - .filter(p -> ObjectUtils.compare(p.getPdpGroupState(), groupState) == 0) + .filter(p -> filterString(p.getName(), name)) + .filter(p -> (version != null && LATEST_VERSION.equals(version)) + || filterString(p.getVersion(), version)) + .filter(p -> groupState == null || ObjectUtils.compare(p.getPdpGroupState(), groupState) == 0) .filter(p -> filterOnPdpType(p, pdpType)) .filter(p -> filterOnPolicyType(p, policyType)) .filter(p -> filterOnPolicy(p, policy)) diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpSubGroup.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpSubGroup.java index 4e5843678..405408946 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpSubGroup.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpSubGroup.java @@ -25,10 +25,8 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import lombok.Getter; +import lombok.Data; import lombok.NonNull; -import lombok.Setter; -import lombok.ToString; import org.onap.policy.models.base.PfUtils; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; @@ -39,9 +37,7 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifi * * @author Ram Krishna Verma (ram.krishna.verma@est.tech) */ -@Getter -@Setter -@ToString +@Data public class PdpSubGroup { private String pdpType; private List supportedPolicyTypes; diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpGroup.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpGroup.java index 71f27c48b..d0fc216c2 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpGroup.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpGroup.java @@ -29,11 +29,13 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import javax.persistence.CascadeType; import javax.persistence.CollectionTable; import javax.persistence.Column; import javax.persistence.ElementCollection; import javax.persistence.EmbeddedId; import javax.persistence.Entity; +import javax.persistence.FetchType; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.JoinColumn; @@ -87,7 +89,7 @@ public class JpaPdpGroup extends PfConcept implements PfAuthorative { private Map properties; // @formatter:off - @OneToMany + @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) @CollectionTable(joinColumns = { @JoinColumn(name = "pdpGroupParentKeyName", referencedColumnName = "parentKeyName"), @JoinColumn(name = "pdpGroupParentKeyVersion", referencedColumnName = "parentKeyVersion"), diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpSubGroup.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpSubGroup.java index d51cfc67e..20e43f0b5 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpSubGroup.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpSubGroup.java @@ -28,19 +28,23 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import javax.persistence.CollectionTable; + +import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.ElementCollection; import javax.persistence.EmbeddedId; import javax.persistence.Entity; +import javax.persistence.FetchType; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; import javax.persistence.OneToMany; import javax.persistence.Table; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NonNull; + import org.onap.policy.common.utils.validation.Assertions; import org.onap.policy.common.utils.validation.ParameterValidationUtils; import org.onap.policy.models.base.PfAuthorative; @@ -89,13 +93,16 @@ public class JpaPdpSubGroup extends PfConcept implements PfAuthorative properties; - // @formatter:ofF - @OneToMany - @CollectionTable( - joinColumns = { @JoinColumn(name = "pdpSubGroupParentKeyName", referencedColumnName = "parentKeyName"), - @JoinColumn(name = "pdpSubGroupParentKeyVersion", referencedColumnName = "parentKeyVersion"), - @JoinColumn(name = "pdpSubGroupParentLocalName", referencedColumnName = "parentLocalName"), - @JoinColumn(name = "pdpSubGroupLocalName", referencedColumnName = "localName") }) + // @formatter:off + @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) + @JoinTable ( + joinColumns = { + @JoinColumn(name = "pdpParentKeyName", referencedColumnName = "parentKeyName"), + @JoinColumn(name = "pdpParentKeyVersion", referencedColumnName = "parentKeyVersion"), + @JoinColumn(name = "pdpParentLocalName", referencedColumnName = "parentLocalName"), + @JoinColumn(name = "pdpLocalName", referencedColumnName = "localName") + } + ) // formatter:on private List pdpInstances; 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 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 filteredList = filter.filter(pdpGroupList); + assertTrue(filteredList.containsAll(pdpGroupList)); + } + + @Test + public void testFilterLatestVersion() { + PdpGroupFilter filter = PdpGroupFilter.builder().version(PdpGroupFilter.LATEST_VERSION).build(); + + List 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 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> 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 beforePdpInstances = updatePdpGroups0.getGroups().get(0).getPdpSubgroups().get(0).getPdpInstances(); + List 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"); + } } diff --git a/models-pdp/src/test/resources/testdata/PdpGroups0Update.json b/models-pdp/src/test/resources/testdata/PdpGroups0Update.json new file mode 100644 index 000000000..a54ec53ea --- /dev/null +++ b/models-pdp/src/test/resources/testdata/PdpGroups0Update.json @@ -0,0 +1,83 @@ +{ + "groups": [ + { + "name": "PdpGroup0", + "version": "1.2.3", + "description": "group description", + "pdpGroupState": "PASSIVE", + "properties": { + "groupProperty0": "Value of Group Property 0" + }, + "pdpSubgroups": [ + { + "pdpType": "APEX", + "supportedPolicyTypes": [ + { + "name": "policy.type.0", + "version": "1.2.3" + } + ], + "policies": [ + { + "name": "Policy0", + "version": "4.5.6" + } + ], + "currentInstanceCount": 123, + "desiredInstanceCount": 456, + "properties": { + "subgroupProperty0": "Value of sub Group Property 0" + }, + "pdpInstances": [ + { + "instanceId": "apex-0", + "pdpState": "ACTIVE", + "healthy": "NOT_HEALTHY", + "message": "message from PDP" + }, + { + "instanceId": "apex-1", + "pdpState": "ACTIVE", + "healthy": "NOT_HEALTHY", + "message": "message from PDP" + }, + { + "instanceId": "apex-2", + "pdpState": "ACTIVE", + "healthy": "NOT_HEALTHY", + "message": "message from PDP" + } + ] + }, + { + "pdpType": "Drools", + "supportedPolicyTypes": [ + { + "name": "policy.type.1", + "version": "4.5.6" + } + ], + "policies": [ + { + "name": "Policy0", + "version": "4.5.6" + } + ], + "currentInstanceCount": 123, + "desiredInstanceCount": 456, + "properties": { + "subgroupProperty0": "Value of sub Group Property 0" + }, + "pdpInstances": [ + { + "instanceId": "drools-0", + "pdpState": "ACTIVE", + "healthy": "NOT_HEALTHY", + "message": "message from PDP" + } + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/models-pdp/src/test/resources/testdata/PdpGroupsForFiltering.json b/models-pdp/src/test/resources/testdata/PdpGroupsForFiltering.json new file mode 100644 index 000000000..c62e1ea58 --- /dev/null +++ b/models-pdp/src/test/resources/testdata/PdpGroupsForFiltering.json @@ -0,0 +1,199 @@ +{ + "groups": [ + { + "name": "PdpGroup0", + "version": "1.2.3", + "description": "group description", + "pdpGroupState": "PASSIVE", + "properties": { + "groupProperty0": "Value of Group Property 0" + }, + "pdpSubgroups": [ + { + "pdpType": "APEX", + "supportedPolicyTypes": [ + { + "name": "policy.type.0", + "version": "1.2.3" + } + ], + "policies": [ + { + "name": "Policy0", + "version": "4.5.6" + } + ], + "currentInstanceCount": 123, + "desiredInstanceCount": 456, + "properties": { + "subgroupProperty0": "Value of sub Group Property 0" + }, + "pdpInstances": [ + { + "instanceId": "apex-0", + "pdpState": "ACTIVE", + "healthy": "NOT_HEALTHY", + "message": "message from PDP" + } + ] + } + ] + }, + { + "name": "PdpGroup0", + "version": "1.2.4", + "description": "group description", + "pdpGroupState": "PASSIVE", + "properties": { + "groupProperty0": "Value of Group Property 0" + }, + "pdpSubgroups": [ + { + "pdpType": "APEX", + "supportedPolicyTypes": [ + { + "name": "policy.type.0", + "version": "1.2.3" + } + ], + "policies": [ + { + "name": "Policy0", + "version": "4.5.6" + } + ], + "currentInstanceCount": 123, + "desiredInstanceCount": 456, + "properties": { + "subgroupProperty0": "Value of sub Group Property 0" + }, + "pdpInstances": [ + { + "instanceId": "apex-0", + "pdpState": "ACTIVE", + "healthy": "NOT_HEALTHY", + "message": "message from PDP" + } + ] + } + ] + }, + { + "name": "PdpGroup0", + "version": "1.2.1", + "description": "group description", + "pdpGroupState": "PASSIVE", + "properties": { + "groupProperty0": "Value of Group Property 0" + }, + "pdpSubgroups": [ + { + "pdpType": "APEX", + "supportedPolicyTypes": [ + { + "name": "policy.type.0", + "version": "1.2.3" + } + ], + "policies": [ + { + "name": "Policy0", + "version": "4.5.6" + } + ], + "currentInstanceCount": 123, + "desiredInstanceCount": 456, + "properties": { + "subgroupProperty0": "Value of sub Group Property 0" + }, + "pdpInstances": [ + { + "instanceId": "apex-0", + "pdpState": "ACTIVE", + "healthy": "NOT_HEALTHY", + "message": "message from PDP" + } + ] + } + ] + }, + { + "name": "PdpGroup1", + "version": "1.2.1", + "description": "group description", + "pdpGroupState": "PASSIVE", + "properties": { + "groupProperty0": "Value of Group Property 0" + }, + "pdpSubgroups": [ + { + "pdpType": "APEX", + "supportedPolicyTypes": [ + { + "name": "policy.type.0", + "version": "1.2.3" + } + ], + "policies": [ + { + "name": "Policy0", + "version": "4.5.6" + } + ], + "currentInstanceCount": 123, + "desiredInstanceCount": 456, + "properties": { + "subgroupProperty0": "Value of sub Group Property 0" + }, + "pdpInstances": [ + { + "instanceId": "apex-0", + "pdpState": "ACTIVE", + "healthy": "NOT_HEALTHY", + "message": "message from PDP" + } + ] + } + ] + }, + { + "name": "PdpGroup1", + "version": "1.2.3", + "description": "group description", + "pdpGroupState": "PASSIVE", + "properties": { + "groupProperty0": "Value of Group Property 0" + }, + "pdpSubgroups": [ + { + "pdpType": "APEX", + "supportedPolicyTypes": [ + { + "name": "policy.type.0", + "version": "1.2.3" + } + ], + "policies": [ + { + "name": "Policy0", + "version": "4.5.6" + } + ], + "currentInstanceCount": 123, + "desiredInstanceCount": 456, + "properties": { + "subgroupProperty0": "Value of sub Group Property 0" + }, + "pdpInstances": [ + { + "instanceId": "apex-0", + "pdpState": "ACTIVE", + "healthy": "NOT_HEALTHY", + "message": "message from PDP" + } + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilter.java index b4d1b3ee3..102b1fe2e 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilter.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilter.java @@ -57,10 +57,11 @@ public class ToscaPolicyFilter implements PfObjectFilter { // @formatter:off List returnList = originalList.stream() - .filter(p -> filterOnRegexp(p.getName(), name)) - .filter(p -> version.equals(LATEST_VERSION) || filterOnRegexp(p.getVersion(), version)) - .filter(p -> filterOnRegexp(p.getType(), type)) - .filter(p -> filterOnRegexp(p.getTypeVersion(), typeVersion)) + .filter(p -> filterString(p.getName(), name)) + .filter(p -> (version != null && LATEST_VERSION.equals(version)) + || filterString(p.getVersion(), version)) + .filter(p -> filterString(p.getType(), type)) + .filter(p -> filterString(p.getTypeVersion(), typeVersion)) .collect(Collectors.toList()); // @formatter:off diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilter.java index 041179513..7d6fbacee 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilter.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilter.java @@ -51,8 +51,9 @@ public class ToscaPolicyTypeFilter implements PfObjectFilter { // @formatter:off List returnList = originalList.stream() - .filter(p -> filterOnRegexp(p.getName(), name)) - .filter(p -> version.equals(LATEST_VERSION) || filterOnRegexp(p.getVersion(), version)) + .filter(p -> filterString(p.getName(), name)) + .filter(p -> (version != null && LATEST_VERSION.equals(version)) + || filterString(p.getVersion(), version)) .collect(Collectors.toList()); // @formatter:off diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java index 4ddcd60cc..b6c5d3bba 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java @@ -44,6 +44,10 @@ import org.slf4j.LoggerFactory; */ public class LegacyOperationalPolicyMapper implements JpaToscaServiceTemplateMapper { + + // Property name for the operational policy content + private static final String CONTENT_PROPERTY = "content"; + private static final Logger LOGGER = LoggerFactory.getLogger(LegacyOperationalPolicyMapper.class); private static final PfConceptKey LEGACY_OPERATIONAL_TYPE = @@ -64,7 +68,7 @@ public class LegacyOperationalPolicyMapper final Map propertyMap = new HashMap<>(); toscaPolicy.setProperties(propertyMap); - toscaPolicy.getProperties().put("Content", legacyOperationalPolicy.getContent()); + toscaPolicy.getProperties().put(CONTENT_PROPERTY, legacyOperationalPolicy.getContent()); final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate(); serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0"); @@ -101,7 +105,7 @@ public class LegacyOperationalPolicyMapper throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); } - final String content = toscaPolicy.getProperties().get("Content"); + final String content = toscaPolicy.getProperties().get(CONTENT_PROPERTY); if (toscaPolicy.getProperties() == null) { String errorMessage = "property \"Content\" not defined on TOSCA policy"; LOGGER.warn(errorMessage); -- cgit 1.2.3-korg