diff options
author | Jim Hahn <jrh3@att.com> | 2019-08-26 12:20:27 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2019-08-27 11:49:30 -0400 |
commit | fd809717ca774dfabeddd3984fbbbbdfd029601e (patch) | |
tree | a4070c2529b0be1899ece44e3687b72ed32505a4 /models-base/src/test | |
parent | a930b0105c2e45a657427cfcb41fc0330d0c2e99 (diff) |
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 <jrh3@att.com>
Diffstat (limited to 'models-base/src/test')
9 files changed, 88 insertions, 78 deletions
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<Object> 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<String> origList = Arrays.asList("abc", "def"); List<String> 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<String,String> resultMap = PfUtils.mapMap(null, item -> { + throw new RuntimeException("should not be invoked"); + }); + assertNull(resultMap); + + Map<String,String> origMap = new TreeMap<>(); + origMap.put("key2A", "xyz2"); + origMap.put("key2B", "pdq2"); + Map<String,String> 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<DummyAuth */ public DummyPfConcept(final DummyPfConcept copyConcept) { super(copyConcept); + this.key = new PfConceptKey(copyConcept.key); + this.description = copyConcept.description; } @Override @@ -141,16 +139,4 @@ public class DummyPfConcept extends PfConcept implements PfAuthorative<DummyAuth return ObjectUtils.compare(description, other.description); } - - @Override - public PfConcept copyTo(@NonNull PfConcept target) { - final Object copyObject = target; - Assertions.instanceOf(copyObject, PfConcept.class); - - final DummyPfConcept copy = ((DummyPfConcept) copyObject); - copy.setKey(new PfConceptKey(key)); - copy.setDescription(description); - - return copy; - } } diff --git a/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConceptKeySub.java b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConceptKeySub.java index da18cba20..893dc5321 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConceptKeySub.java +++ b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConceptKeySub.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 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. @@ -50,4 +51,13 @@ public class DummyPfConceptKeySub extends PfConceptKey { public DummyPfConceptKeySub(@NonNull final PfConceptKey usedKey) { super(usedKey); } + + /** + * Copy constructor. + * + * @param source object to be copied + */ + public DummyPfConceptKeySub(@NonNull final DummyPfConceptKeySub source) { + super(source); + } } diff --git a/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfKey.java b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfKey.java index 944c1e602..058ef37c3 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfKey.java +++ b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfKey.java @@ -33,6 +33,12 @@ import org.onap.policy.models.base.PfValidationResult; public class DummyPfKey extends PfKey { private static final long serialVersionUID = 1L; + public DummyPfKey() { } + + public DummyPfKey(DummyPfKey source) { + super(source); + } + @Override public int compareTo(PfConcept arg0) { return 0; @@ -94,11 +100,6 @@ public class DummyPfKey extends PfKey { } @Override - public PfConcept copyTo(PfConcept target) { - return null; - } - - @Override public boolean isNewerThan(@NonNull PfKey otherKey) { // TODO Auto-generated method stub return false; diff --git a/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfModel.java b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfModel.java index 4a30f593f..764b91b24 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfModel.java +++ b/models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfModel.java @@ -23,18 +23,13 @@ package org.onap.policy.models.base.testconcepts; import java.util.ArrayList; import java.util.List; - -import javax.ws.rs.core.Response; - import lombok.Data; import lombok.EqualsAndHashCode; - -import org.onap.policy.common.utils.validation.Assertions; import org.onap.policy.models.base.PfConcept; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfKey; import org.onap.policy.models.base.PfModel; -import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.base.PfUtils; import org.onap.policy.models.base.PfValidationResult; @Data @@ -83,6 +78,11 @@ public class DummyPfModel extends PfModel { */ public DummyPfModel(final DummyPfModel copyConcept) { super(copyConcept); + + this.keyList = new ArrayList<>(); + 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<PfKey> 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; - } } |