From fd809717ca774dfabeddd3984fbbbbdfd029601e Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Mon, 26 Aug 2019 12:20:27 -0400 Subject: Replace copyTo methods with copy constructors Deleted the copyTo() method from PfConcepts and replaced uses with deep-copy constructors. Also added mapMap() and makeCopy() methods to PfUtils to facilitate. Change-Id: Id6391bb806ef0dfab6c1089278bf2b514c7e303e Issue-ID: POLICY-1600 Signed-off-by: Jim Hahn --- .../onap/policy/models/pdp/concepts/PdpGroup.java | 3 ++- .../onap/policy/models/pdp/concepts/PdpStatus.java | 6 +++-- .../policy/models/pdp/concepts/PdpSubGroup.java | 8 ++++--- .../models/pdp/persistence/concepts/JpaPdp.java | 21 ++++------------ .../pdp/persistence/concepts/JpaPdpGroup.java | 24 ++++--------------- .../pdp/persistence/concepts/JpaPdpSubGroup.java | 28 +++++++--------------- .../pdp/persistence/concepts/JpaPdpGroupTest.java | 6 ++--- .../persistence/concepts/JpaPdpSubGroupTest.java | 6 ++--- .../pdp/persistence/concepts/JpaPdpTest.java | 6 ++--- 9 files changed, 37 insertions(+), 71 deletions(-) (limited to 'models-pdp') diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroup.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroup.java index 4e7fc4117..6d5f804f8 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroup.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpGroup.java @@ -22,6 +22,7 @@ package org.onap.policy.models.pdp.concepts; import com.fasterxml.jackson.annotation.JsonIgnore; +import java.util.ArrayList; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; @@ -69,7 +70,7 @@ public class PdpGroup implements PfNameVersion, Comparable { this.description = source.description; this.pdpGroupState = source.pdpGroupState; this.properties = (source.properties == null ? null : new LinkedHashMap<>(source.properties)); - this.pdpSubgroups = PfUtils.mapList(source.pdpSubgroups, PdpSubGroup::new); + this.pdpSubgroups = PfUtils.mapList(source.pdpSubgroups, PdpSubGroup::new, new ArrayList<>(0)); } @Override diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpStatus.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpStatus.java index 3655aa796..e50694b93 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpStatus.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpStatus.java @@ -21,6 +21,7 @@ package org.onap.policy.models.pdp.concepts; +import java.util.ArrayList; import java.util.List; import lombok.Getter; import lombok.Setter; @@ -78,8 +79,9 @@ public class PdpStatus extends PdpMessage { this.state = source.state; this.healthy = source.healthy; this.description = source.description; - this.supportedPolicyTypes = PfUtils.mapList(source.supportedPolicyTypes, ToscaPolicyTypeIdentifier::new); - this.policies = PfUtils.mapList(source.policies, ToscaPolicyIdentifier::new); + this.supportedPolicyTypes = PfUtils.mapList(source.supportedPolicyTypes, ToscaPolicyTypeIdentifier::new, + new ArrayList<>(0)); + this.policies = PfUtils.mapList(source.policies, ToscaPolicyIdentifier::new, new ArrayList<>(0)); this.deploymentInstanceInfo = source.deploymentInstanceInfo; this.properties = source.properties; this.statistics = (source.statistics == null ? null : new PdpStatistics(source.statistics)); 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 2618a5017..236cc858a 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 @@ -21,6 +21,7 @@ package org.onap.policy.models.pdp.concepts; +import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -64,12 +65,13 @@ public class PdpSubGroup { */ public PdpSubGroup(@NonNull final PdpSubGroup source) { this.pdpType = source.pdpType; - this.supportedPolicyTypes = PfUtils.mapList(source.supportedPolicyTypes, ToscaPolicyTypeIdentifier::new); - this.policies = PfUtils.mapList(source.policies, ToscaPolicyIdentifier::new); + this.supportedPolicyTypes = PfUtils.mapList(source.supportedPolicyTypes, ToscaPolicyTypeIdentifier::new, + new ArrayList<>(0)); + this.policies = PfUtils.mapList(source.policies, ToscaPolicyIdentifier::new, new ArrayList<>(0)); this.currentInstanceCount = source.currentInstanceCount; this.desiredInstanceCount = source.desiredInstanceCount; this.properties = (source.properties == null ? null : new LinkedHashMap<>(source.properties)); - this.pdpInstances = PfUtils.mapList(source.pdpInstances, Pdp::new); + this.pdpInstances = PfUtils.mapList(source.pdpInstances, Pdp::new, new ArrayList<>(0)); } /** diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdp.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdp.java index 7d90c03b1..5820a355b 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdp.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdp.java @@ -25,21 +25,17 @@ package org.onap.policy.models.pdp.persistence.concepts; import java.io.Serializable; import java.util.List; - import javax.persistence.Column; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.Table; - import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NonNull; - import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; -import org.onap.policy.common.utils.validation.Assertions; import org.onap.policy.models.base.PfAuthorative; import org.onap.policy.models.base.PfConcept; import org.onap.policy.models.base.PfKey; @@ -113,6 +109,10 @@ public class JpaPdp extends PfConcept implements PfAuthorative, Serializabl */ public JpaPdp(@NonNull final JpaPdp copyConcept) { super(copyConcept); + this.key = new PfReferenceKey(copyConcept.key); + this.pdpState = copyConcept.pdpState; + this.healthy = copyConcept.healthy; + this.message = copyConcept.message; } /** @@ -230,17 +230,4 @@ public class JpaPdp extends PfConcept implements PfAuthorative, Serializabl return ObjectUtils.compare(message, other.message); } - - @Override - public PfConcept copyTo(@NonNull final PfConcept target) { - Assertions.instanceOf(target, JpaPdp.class); - - final JpaPdp copy = ((JpaPdp) target); - copy.setKey(new PfReferenceKey(key)); - copy.setPdpState(pdpState); - copy.setHealthy(healthy); - copy.setMessage(message); - - return copy; - } } 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 36d5cc18e..0df620bcf 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 @@ -28,7 +28,6 @@ import java.util.LinkedHashMap; 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; @@ -41,14 +40,11 @@ import javax.persistence.InheritanceType; import javax.persistence.JoinColumn; import javax.persistence.OneToMany; import javax.persistence.Table; - import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NonNull; - import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; -import org.onap.policy.common.utils.validation.Assertions; import org.onap.policy.common.utils.validation.ParameterValidationUtils; import org.onap.policy.models.base.PfAuthorative; import org.onap.policy.models.base.PfConcept; @@ -135,6 +131,11 @@ public class JpaPdpGroup extends PfConcept implements PfAuthorative { */ public JpaPdpGroup(@NonNull final JpaPdpGroup copyConcept) { super(copyConcept); + this.key = new PfConceptKey(copyConcept.key); + this.description = copyConcept.description; + this.pdpGroupState = copyConcept.pdpGroupState; + this.properties = (copyConcept.properties == null ? null : new LinkedHashMap<>(copyConcept.properties)); + this.pdpSubGroups = PfUtils.mapList(copyConcept.pdpSubGroups, JpaPdpSubGroup::new, new ArrayList<>(0)); } /** @@ -310,19 +311,4 @@ public class JpaPdpGroup extends PfConcept implements PfAuthorative { return PfUtils.compareObjects(pdpSubGroups, other.pdpSubGroups); } - - @Override - public PfConcept copyTo(@NonNull final PfConcept target) { - Assertions.instanceOf(target, JpaPdpGroup.class); - - final JpaPdpGroup copy = ((JpaPdpGroup) target); - copy.setKey(new PfConceptKey(key)); - - copy.setDescription(description); - copy.setPdpGroupState(pdpGroupState); - copy.setProperties(properties == null ? null : new LinkedHashMap<>(properties)); - copy.setPdpSubGroups(PfUtils.mapList(pdpSubGroups, JpaPdpSubGroup::new)); - - return copy; - } } 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 723d427de..3a81c0b30 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,7 +28,6 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; - import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.ElementCollection; @@ -44,8 +43,6 @@ 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; import org.onap.policy.models.base.PfConcept; @@ -145,6 +142,14 @@ public class JpaPdpSubGroup extends PfConcept implements PfAuthorative(0)); + this.policies = PfUtils.mapList(copyConcept.policies, PfConceptKey::new, new ArrayList<>(0)); + this.currentInstanceCount = copyConcept.currentInstanceCount; + this.desiredInstanceCount = copyConcept.desiredInstanceCount; + this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null); + this.pdpInstances = PfUtils.mapList(copyConcept.pdpInstances, JpaPdp::new, new ArrayList<>(0)); } /** @@ -393,21 +398,4 @@ public class JpaPdpSubGroup extends PfConcept implements PfAuthorative(properties)); - copy.setPdpInstances(PfUtils.mapList(pdpInstances, JpaPdp::new)); - - return copy; - } } diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpGroupTest.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpGroupTest.java index a2f502bcc..b40c91982 100644 --- a/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpGroupTest.java +++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpGroupTest.java @@ -119,9 +119,7 @@ public class JpaPdpGroupTest { testJpaPdpGroup.setKey(new PfConceptKey(PDP_GROUP0, VERSION)); testJpaPdpGroup.fromAuthorative(testPdpGroup); - assertThatThrownBy(() -> { - testJpaPdpGroup.copyTo(null); - }).hasMessage("target is marked @NonNull but is null"); + assertThatThrownBy(() -> new JpaPdpGroup((JpaPdpGroup) null)).isInstanceOf(NullPointerException.class); assertEquals(PDP_GROUP0, testJpaPdpGroup.getKey().getName()); assertEquals(PDP_GROUP0, new JpaPdpGroup(testPdpGroup).getKey().getName()); @@ -230,5 +228,7 @@ public class JpaPdpGroupTest { assertEquals(2, testJpaPdpGroup.getKeys().size()); testJpaPdpGroup.clean(); assertEquals(2, testJpaPdpGroup.getKeys().size()); + + assertEquals(testJpaPdpGroup, new JpaPdpGroup(testJpaPdpGroup)); } } diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpSubGroupTest.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpSubGroupTest.java index 981f40f06..ad0164ad4 100644 --- a/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpSubGroupTest.java +++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpSubGroupTest.java @@ -127,9 +127,7 @@ public class JpaPdpSubGroupTest { testJpaPdpSubGroup.fromAuthorative(null); }).hasMessage("pdpSubgroup is marked @NonNull but is null"); - assertThatThrownBy(() -> { - testJpaPdpSubGroup.copyTo(null); - }).hasMessage("target is marked @NonNull but is null"); + assertThatThrownBy(() -> new JpaPdpSubGroup((JpaPdpSubGroup) null)).isInstanceOf(NullPointerException.class); assertEquals(PDP_A, testJpaPdpSubGroup.getKey().getLocalName()); assertEquals(PDP_A, new JpaPdpSubGroup(testPdpSubgroup).getKey().getLocalName()); @@ -279,5 +277,7 @@ public class JpaPdpSubGroupTest { assertEquals("Prop Value", testJpaPdpSubGroup.getProperties().get("PropKey")); assertEquals(4, testJpaPdpSubGroup.getKeys().size()); + + assertEquals(testJpaPdpSubGroup, new JpaPdpSubGroup(testJpaPdpSubGroup)); } } diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpTest.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpTest.java index 5ffba10b9..b6d5161f8 100644 --- a/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpTest.java +++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpTest.java @@ -99,9 +99,7 @@ public class JpaPdpTest { testJpaPdp.fromAuthorative(null); }).hasMessage("pdp is marked @NonNull but is null"); - assertThatThrownBy(() -> { - testJpaPdp.copyTo(null); - }).hasMessage("target is marked @NonNull but is null"); + assertThatThrownBy(() -> new JpaPdp((JpaPdp) null)).isInstanceOf(NullPointerException.class); assertEquals(PDP1, testJpaPdp.getKey().getLocalName()); assertEquals(PDP1, new JpaPdp(testPdp).getKey().getLocalName()); @@ -184,5 +182,7 @@ public class JpaPdpTest { assertEquals(-13, testJpaPdp.compareTo(otherJpaPdp)); testJpaPdp.setMessage("Valid Message"); assertEquals(0, testJpaPdp.compareTo(otherJpaPdp)); + + assertEquals(testJpaPdp, new JpaPdp(testJpaPdp)); } } -- cgit 1.2.3-korg