aboutsummaryrefslogtreecommitdiffstats
path: root/models-base/src
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2019-08-26 12:20:27 -0400
committerJim Hahn <jrh3@att.com>2019-08-27 11:49:30 -0400
commitfd809717ca774dfabeddd3984fbbbbdfd029601e (patch)
treea4070c2529b0be1899ece44e3687b72ed32505a4 /models-base/src
parenta930b0105c2e45a657427cfcb41fc0330d0c2e99 (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')
-rw-r--r--models-base/src/main/java/org/onap/policy/models/base/PfConcept.java12
-rw-r--r--models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java31
-rw-r--r--models-base/src/main/java/org/onap/policy/models/base/PfConceptKey.java18
-rw-r--r--models-base/src/main/java/org/onap/policy/models/base/PfKeyUse.java23
-rw-r--r--models-base/src/main/java/org/onap/policy/models/base/PfModel.java12
-rw-r--r--models-base/src/main/java/org/onap/policy/models/base/PfReferenceKey.java17
-rw-r--r--models-base/src/main/java/org/onap/policy/models/base/PfUtils.java85
-rw-r--r--models-base/src/test/java/org/onap/policy/models/base/PfConceptContainerTest.java4
-rw-r--r--models-base/src/test/java/org/onap/policy/models/base/PfKeyUseTest.java9
-rw-r--r--models-base/src/test/java/org/onap/policy/models/base/PfModelTest.java2
-rw-r--r--models-base/src/test/java/org/onap/policy/models/base/PfReferenceKeyTest.java9
-rw-r--r--models-base/src/test/java/org/onap/policy/models/base/PfUtilsTest.java65
-rw-r--r--models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConcept.java18
-rw-r--r--models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfConceptKeySub.java10
-rw-r--r--models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfKey.java11
-rw-r--r--models-base/src/test/java/org/onap/policy/models/base/testconcepts/DummyPfModel.java38
16 files changed, 189 insertions, 175 deletions
diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfConcept.java b/models-base/src/main/java/org/onap/policy/models/base/PfConcept.java
index 9a376feff..a1cfac4ae 100644
--- a/models-base/src/main/java/org/onap/policy/models/base/PfConcept.java
+++ b/models-base/src/main/java/org/onap/policy/models/base/PfConcept.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.
@@ -44,7 +45,7 @@ public abstract class PfConcept implements Serializable, Comparable<PfConcept> {
* @param copyConcept the concept to copy from
*/
public PfConcept(@NonNull final PfConcept copyConcept) {
- copyConcept.copyTo(this);
+ // nothing else to do (other than @NonNull check)
}
/**
@@ -87,15 +88,6 @@ public abstract class PfConcept implements Serializable, Comparable<PfConcept> {
public abstract int hashCode();
/**
- * Copy this concept to another object. The target object must have the same class as the source
- * object.
- *
- * @param target the target object to which this object is copied
- * @return the copied object
- */
- public abstract PfConcept copyTo(@NonNull PfConcept target);
-
- /**
* Gets the ID string of this concept.
*
* @return the ID string of this concept
diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java b/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java
index 63ab14ee5..1b60932e9 100644
--- a/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java
+++ b/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.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.
@@ -29,7 +30,6 @@ import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.Set;
import java.util.TreeMap;
-
import javax.persistence.CascadeType;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
@@ -37,12 +37,9 @@ import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.ws.rs.core.Response;
-
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
-
-import org.onap.policy.common.utils.validation.Assertions;
import org.onap.policy.models.base.PfValidationResult.ValidationResult;
// @formatter:off
@@ -111,6 +108,14 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
*/
public PfConceptContainer(@NonNull final PfConceptContainer<C, A> copyConcept) {
super(copyConcept);
+ this.key = new PfConceptKey(copyConcept.key);
+
+ this.conceptMap = new TreeMap<>();
+ for (final Entry<PfConceptKey, C> conceptMapEntry : copyConcept.conceptMap.entrySet()) {
+ PfConceptKey newK = new PfConceptKey(conceptMapEntry.getKey());
+ C newC = PfUtils.makeCopy(conceptMapEntry.getValue());
+ this.conceptMap.put(newK, newC);
+ }
}
@Override
@@ -279,24 +284,6 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex
}
@Override
- public PfConcept copyTo(@NonNull final PfConcept target) {
- Assertions.instanceOf(target, PfConceptContainer.class);
-
- @SuppressWarnings("unchecked")
- final PfConceptContainer<C, A> copy = (PfConceptContainer<C, A>) target;
- copy.setKey(new PfConceptKey(key));
- final Map<PfConceptKey, C> newConceptMap = new TreeMap<>();
- for (final Entry<PfConceptKey, C> conceptMapEntry : conceptMap.entrySet()) {
- C newC = getConceptNewInstance();
- conceptMapEntry.getValue().copyTo(newC);
- newConceptMap.put(new PfConceptKey(conceptMapEntry.getKey()), newC);
- }
- copy.setConceptMap(newConceptMap);
-
- return copy;
- }
-
- @Override
public C get(final PfConceptKey conceptKey) {
return new PfConceptGetterImpl<>((NavigableMap<PfConceptKey, C>) conceptMap).get(conceptKey);
}
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 17fda7cfe..f5baae7fe 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
@@ -1,7 +1,7 @@
/*
* ============LICENSE_START=======================================================
* Copyright (C) 2019 Nordix Foundation.
- * ModificationsCopyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * 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.
@@ -73,6 +73,8 @@ public class PfConceptKey extends PfKey {
*/
public PfConceptKey(@NonNull final PfConceptKey copyConcept) {
super(copyConcept);
+ this.name = copyConcept.name;
+ this.version = copyConcept.version;
}
/**
@@ -289,20 +291,6 @@ public class PfConceptKey extends PfKey {
}
@Override
- public PfConcept copyTo(final PfConcept target) {
- Assertions.argumentNotNull(target, "target may not be null");
-
- final PfConcept copyObject = target;
- Assertions.instanceOf(copyObject, PfConceptKey.class);
-
- final PfConceptKey copy = ((PfConceptKey) copyObject);
- copy.setName(name);
- copy.setVersion(version);
-
- return copyObject;
- }
-
- @Override
public int compareTo(@NonNull final PfConcept otherObj) {
Assertions.argumentNotNull(otherObj, "comparison object may not be null");
diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfKeyUse.java b/models-base/src/main/java/org/onap/policy/models/base/PfKeyUse.java
index d56b0dbab..03873236c 100644
--- a/models-base/src/main/java/org/onap/policy/models/base/PfKeyUse.java
+++ b/models-base/src/main/java/org/onap/policy/models/base/PfKeyUse.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.
@@ -21,13 +22,9 @@
package org.onap.policy.models.base;
import java.util.List;
-
-import javax.ws.rs.core.Response;
-
import lombok.EqualsAndHashCode;
import lombok.NonNull;
import lombok.ToString;
-
import org.onap.policy.common.utils.validation.Assertions;
import org.onap.policy.models.base.PfValidationResult.ValidationResult;
@@ -69,6 +66,7 @@ public class PfKeyUse extends PfKey {
*/
public PfKeyUse(@NonNull final PfKeyUse copyConcept) {
super(copyConcept);
+ this.usedKey = PfUtils.makeCopy(copyConcept.usedKey);
}
@Override
@@ -159,21 +157,4 @@ public class PfKeyUse extends PfKey {
return usedKey.compareTo(other.usedKey);
}
-
- @Override
- public PfConcept copyTo(@NonNull final PfConcept target) {
- final Object copyObject = target;
- Assertions.instanceOf(copyObject, PfKeyUse.class);
-
- final PfKeyUse copy = ((PfKeyUse) copyObject);
- try {
- copy.usedKey = usedKey.getClass().newInstance();
- } catch (final Exception e) {
- throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
- "error copying concept key: " + e.getMessage(), e);
- }
- usedKey.copyTo(copy.usedKey);
-
- return copy;
- }
}
diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfModel.java b/models-base/src/main/java/org/onap/policy/models/base/PfModel.java
index 07ec3af04..83ae71cca 100644
--- a/models-base/src/main/java/org/onap/policy/models/base/PfModel.java
+++ b/models-base/src/main/java/org/onap/policy/models/base/PfModel.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.
@@ -91,6 +92,7 @@ public abstract class PfModel extends PfConcept {
*/
public PfModel(@NonNull final PfModel copyConcept) {
super(copyConcept);
+ this.key = new PfConceptKey(copyConcept.key);
}
/**
@@ -281,14 +283,4 @@ public abstract class PfModel extends PfConcept {
return key.compareTo(other.key);
}
-
- @Override
- public PfConcept copyTo(@NonNull final PfConcept target) {
- Assertions.instanceOf(target, PfModel.class);
-
- final PfModel copy = ((PfModel) target);
- copy.setKey(new PfConceptKey(key));
-
- return copy;
- }
}
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 bdf16dcdf..a5ae6c51e 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
@@ -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.
@@ -382,22 +383,6 @@ public class PfReferenceKey extends PfKey {
}
@Override
- public PfConcept copyTo(final PfConcept target) {
- Assertions.argumentNotNull(target, "target may not be null");
-
- final Object copyObject = target;
- Assertions.instanceOf(copyObject, PfReferenceKey.class);
-
- final PfReferenceKey copy = ((PfReferenceKey) copyObject);
- copy.setParentKeyName(parentKeyName);
- copy.setParentKeyVersion(parentKeyVersion);
- copy.setLocalName(localName);
- copy.setParentLocalName(parentLocalName);
-
- return copy;
- }
-
- @Override
public int compareTo(final PfConcept otherObj) {
Assertions.argumentNotNull(otherObj, "comparison object may not be null");
diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java b/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java
index c33271d78..8f1040bfe 100644
--- a/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java
+++ b/models-base/src/main/java/org/onap/policy/models/base/PfUtils.java
@@ -21,10 +21,14 @@
package org.onap.policy.models.base;
-import java.util.ArrayList;
+import java.lang.reflect.InvocationTargetException;
+import java.util.LinkedHashMap;
import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
import java.util.function.Function;
import java.util.stream.Collectors;
+import javax.ws.rs.core.Response;
/**
* Utility class for Policy Framework concept utilities.
@@ -69,13 +73,88 @@ public final class PfUtils {
*
* @param source list whose elements are to be mapped, or {@code null}
* @param mapFunc mapping function
+ * @param defaultValue value to be returned if source is {@code null}
* @return a new list, containing mappings of all of the items in the original list
*/
- public static <T> List<T> mapList(List<T> source, Function<T, T> mapFunc) {
+ public static <T> List<T> mapList(List<T> source, Function<T, T> mapFunc, List<T> defaultValue) {
if (source == null) {
- return new ArrayList<>(0);
+ return defaultValue;
}
return source.stream().map(mapFunc).collect(Collectors.toList());
}
+
+ /**
+ * Convenience method to apply a mapping function to all of the elements of a list,
+ * generating a new list.
+ *
+ * @param source list whose elements are to be mapped, or {@code null}
+ * @param mapFunc mapping function
+ * @return a new list, containing mappings of all of the items in the original list,
+ * or {@code null} if the source is {@code null}
+ */
+ public static <T> List<T> mapList(List<T> source, Function<T, T> mapFunc) {
+ return mapList(source, mapFunc, null);
+ }
+
+ /**
+ * Convenience method to apply a mapping function to all of the values of a map,
+ * generating a new map.
+ *
+ * @param source map whose values are to be mapped, or {@code null}
+ * @param mapFunc mapping function
+ * @param defaultValue value to be returned if source is {@code null}
+ * @return a new map, containing mappings of all of the items in the original map
+ */
+ public static <T> Map<String, T> mapMap(Map<String, T> source, Function<T, T> mapFunc,
+ Map<String, T> defaultValue) {
+ if (source == null) {
+ return defaultValue;
+ }
+
+ Map<String, T> map = new LinkedHashMap<>();
+ for (Entry<String, T> ent : source.entrySet()) {
+ map.put(ent.getKey(), mapFunc.apply(ent.getValue()));
+ }
+
+ return map;
+ }
+
+ /**
+ * Convenience method to apply a mapping function to all of the values of a map,
+ * generating a new map.
+ *
+ * @param source map whose values are to be mapped, or {@code null}
+ * @param mapFunc mapping function
+ * @return a new map, containing mappings of all of the items in the original map,
+ * or {@code null} if the source is {@code null}
+ */
+ public static <T> Map<String, T> mapMap(Map<String, T> source, Function<T, T> mapFunc) {
+ return mapMap(source, mapFunc, null);
+ }
+
+ /**
+ * Makes a copy of an object using the copy constructor from the object's class.
+ *
+ * @param source object to be copied
+ * @return a copy of the source, or {@code null} if the source is {@code null}
+ * @throws PfModelRuntimeException if the object cannot be copied
+ */
+ public static <T> T makeCopy(T source) {
+ if (source == null) {
+ return null;
+ }
+
+ try {
+ @SuppressWarnings("unchecked")
+ Class<? extends T> clazz = (Class<? extends T>) source.getClass();
+
+ return clazz.getConstructor(clazz).newInstance(source);
+
+ } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException
+ | RuntimeException e) {
+ throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
+ "error copying concept key class: " + source.getClass().getName(), e);
+ }
+ }
}
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;
- }
}