X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=models-base%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fpolicy%2Fmodels%2Fbase%2FPfUtilsTest.java;h=a66aba2c643c4b7116b7cce9d1f14f95c11fd2c3;hb=fd809717ca774dfabeddd3984fbbbbdfd029601e;hp=bd55dcd9a138701825ca0490fa393032c35069af;hpb=a930b0105c2e45a657427cfcb41fc0330d0c2e99;p=policy%2Fmodels.git 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; + } }