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 --- .../policy/models/base/PfConceptContainerTest.java | 4 +- .../org/onap/policy/models/base/PfKeyUseTest.java | 9 +-- .../org/onap/policy/models/base/PfModelTest.java | 2 +- .../policy/models/base/PfReferenceKeyTest.java | 9 +-- .../org/onap/policy/models/base/PfUtilsTest.java | 65 +++++++++++++++++++--- .../models/base/testconcepts/DummyPfConcept.java | 18 +----- .../base/testconcepts/DummyPfConceptKeySub.java | 10 ++++ .../models/base/testconcepts/DummyPfKey.java | 11 ++-- .../models/base/testconcepts/DummyPfModel.java | 38 ++----------- 9 files changed, 88 insertions(+), 78 deletions(-) (limited to 'models-base/src/test/java') diff --git a/models-base/src/test/java/org/onap/policy/models/base/PfConceptContainerTest.java b/models-base/src/test/java/org/onap/policy/models/base/PfConceptContainerTest.java index 984d2b9d3..5e84dff9a 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/PfConceptContainerTest.java +++ b/models-base/src/test/java/org/onap/policy/models/base/PfConceptContainerTest.java @@ -113,8 +113,8 @@ public class PfConceptContainerTest { assertEquals(0, container.compareTo(clonedContainer)); - final DummyPfConceptContainer container2 = container; - assertThatThrownBy(() -> container2.copyTo(null)).hasMessage("target is marked @NonNull but is null"); + assertThatThrownBy(() -> new DummyPfConceptContainer((DummyPfConceptContainer) null)) + .isInstanceOf(NullPointerException.class); assertFalse(container.compareTo(null) == 0); assertEquals(0, container.compareTo(container)); diff --git a/models-base/src/test/java/org/onap/policy/models/base/PfKeyUseTest.java b/models-base/src/test/java/org/onap/policy/models/base/PfKeyUseTest.java index f7d1d9a5f..3e5d738bd 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/PfKeyUseTest.java +++ b/models-base/src/test/java/org/onap/policy/models/base/PfKeyUseTest.java @@ -96,14 +96,9 @@ public class PfKeyUseTest { assertThatThrownBy(() -> keyUse.validate(null)).hasMessage("result is marked @NonNull but is null"); PfKeyUse testKeyUse = new PfKeyUse(new DummyPfConceptKeySub(new PfConceptKey())); - PfKeyUse targetKeyUse = new PfKeyUse(key); + assertEquals(testKeyUse, new PfKeyUse(testKeyUse)); - assertThatThrownBy(() -> keyUse.copyTo(null)).hasMessage("target is marked @NonNull but is null"); - - assertThatThrownBy(() -> { - testKeyUse.copyTo(targetKeyUse); - keyUse.isCompatible(null); - }).hasMessage("error copying concept key: Some error message"); + assertThatThrownBy(() -> new PfKeyUse((PfKeyUse) null)).isInstanceOf(NullPointerException.class); assertThatThrownBy(() -> keyUse.isNewerThan(null)).hasMessage(OTHER_KEY_IS_NULL); diff --git a/models-base/src/test/java/org/onap/policy/models/base/PfModelTest.java b/models-base/src/test/java/org/onap/policy/models/base/PfModelTest.java index 2f4a1beb9..9b1a778ac 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/PfModelTest.java +++ b/models-base/src/test/java/org/onap/policy/models/base/PfModelTest.java @@ -60,7 +60,7 @@ public class PfModelTest { dpmClone.clean(); assertEquals(dpm, dpmClone); - assertThatThrownBy(() -> dpm.copyTo(null)).hasMessage("target is marked @NonNull but is null"); + assertThatThrownBy(() -> new DummyPfModel((DummyPfModel) null)).isInstanceOf(NullPointerException.class); assertEquals(0, dpm.compareTo(dpmClone)); assertEquals(-1, dpm.compareTo(null)); diff --git a/models-base/src/test/java/org/onap/policy/models/base/PfReferenceKeyTest.java b/models-base/src/test/java/org/onap/policy/models/base/PfReferenceKeyTest.java index 21b82fcf1..b12ce4de0 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/PfReferenceKeyTest.java +++ b/models-base/src/test/java/org/onap/policy/models/base/PfReferenceKeyTest.java @@ -125,14 +125,9 @@ public class PfReferenceKeyTest { assertFalse(testReferenceKey.equals(null)); - assertThatThrownBy(() -> testReferenceKey.copyTo(null)).hasMessage("target may not be null"); + assertThatThrownBy(() -> new PfReferenceKey((PfReferenceKey) null)).isInstanceOf(NullPointerException.class); - assertThatThrownBy(() -> testReferenceKey.copyTo(new PfConceptKey("Key", VERSION001))) - .hasMessage("org.onap.policy.models.base.PfConceptKey" - + " is not an instance of org.onap.policy.models.base.PfReferenceKey"); - - PfReferenceKey targetRefKey = new PfReferenceKey(); - assertEquals(testReferenceKey, testReferenceKey.copyTo(targetRefKey)); + assertEquals(testReferenceKey, new PfReferenceKey(testReferenceKey)); } @Test diff --git a/models-base/src/test/java/org/onap/policy/models/base/PfUtilsTest.java b/models-base/src/test/java/org/onap/policy/models/base/PfUtilsTest.java index bd55dcd9a..a66aba2c6 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/PfUtilsTest.java +++ b/models-base/src/test/java/org/onap/policy/models/base/PfUtilsTest.java @@ -21,12 +21,17 @@ package org.onap.policy.models.base; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertNull; import java.util.Arrays; import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import lombok.Getter; +import lombok.ToString; import org.junit.Test; /** @@ -52,13 +57,7 @@ public class PfUtilsTest { List resultList = PfUtils.mapList(null, item -> { throw new RuntimeException("should not be invoked"); }); - assertTrue(resultList.isEmpty()); - - // verify that we can modify the empty list without throwing an exception - resultList.add("xyz"); - resultList.add("pdq"); - resultList.remove("xyz"); - + assertNull(resultList); List origList = Arrays.asList("abc", "def"); List newList = PfUtils.mapList(origList, text -> text + "X"); @@ -69,4 +68,54 @@ public class PfUtilsTest { newList.remove("abcX"); newList.add("something else"); } + + @Test + public void testMapMap() { + Map resultMap = PfUtils.mapMap(null, item -> { + throw new RuntimeException("should not be invoked"); + }); + assertNull(resultMap); + + Map origMap = new TreeMap<>(); + origMap.put("key2A", "xyz2"); + origMap.put("key2B", "pdq2"); + Map newMap = PfUtils.mapMap(origMap, text -> text + "X"); + + assertEquals("{key2A=xyz2X, key2B=pdq2X}", newMap.toString()); + + // verify that we can modify the map without throwing an exception + newMap.remove("abcX"); + newMap.put("something", "else"); + } + + @Test + public void testMakeCopy() { + assertNull(PfUtils.makeCopy((MyObject) null)); + + MyObject origObject = new MyObject(); + origObject.name = HELLO; + assertEquals(origObject.toString(), PfUtils.makeCopy(origObject).toString()); + + assertThatThrownBy(() -> PfUtils.makeCopy(new NoCopyConstructor())).isInstanceOf(PfModelRuntimeException.class); + } + + @Getter + @ToString + private static class MyObject { + private String name; + + public MyObject() { + // do nothing + } + + @SuppressWarnings("unused") + public MyObject(MyObject source) { + this.name = source.name; + } + } + + @Getter + private static class NoCopyConstructor { + private String name; + } } diff --git a/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConcept.java b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConcept.java index 6cb44e6b5..be398b95f 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConcept.java +++ b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConcept.java @@ -22,15 +22,11 @@ package org.onap.policy.models.base.testconcepts; import java.util.List; - import javax.persistence.EmbeddedId; - import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NonNull; - import org.apache.commons.lang3.ObjectUtils; -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.PfConceptKey; @@ -72,6 +68,8 @@ public class DummyPfConcept extends PfConcept implements PfAuthorative(); + for (final PfKey pfKey : copyConcept.keyList) { + keyList.add(PfUtils.makeCopy(pfKey)); + } } @Override @@ -147,30 +147,4 @@ public class DummyPfModel extends PfModel { return 0; } - - @Override - public PfConcept copyTo(final PfConcept targetObject) { - super.copyTo(targetObject); - - Assertions.instanceOf(targetObject, DummyPfModel.class); - - final DummyPfModel copy = ((DummyPfModel) targetObject); - - final List newKeyList = new ArrayList<>(); - for (final PfKey pfKey : keyList) { - PfKey newPfKey; - try { - newPfKey = pfKey.getClass().newInstance(); - } catch (final Exception e) { - throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, - "error copying concept key: " + e.getMessage(), e); - } - newPfKey.copyTo(pfKey); - newKeyList.add(newPfKey); - } - copy.setKeyList(newKeyList); - - - return copy; - } } -- cgit 1.2.3-korg