From 52229882d7ee3e934641de0bd2df74ed1268130e Mon Sep 17 00:00:00 2001 From: liamfallon Date: Fri, 5 Apr 2019 21:19:37 +0000 Subject: Add unit tests for models-base Brings coverage in models-base up over 99% Issue-ID: POLICY-1095 Change-Id: Ie1c9f3f132715275f44f349a415a8bb34a0115c9 Signed-off-by: liamfallon --- .../policy/models/base/PfConceptContainer.java | 3 +++ .../onap/policy/models/base/PfObjectFilter.java | 2 +- .../policy/models/base/PfConceptContainerTest.java | 16 +++++++++++++--- .../org/onap/policy/models/base/PfKeyTest.java | 8 ++++++++ .../org/onap/policy/models/base/PfKeyUseTest.java | 22 ++++++++++++++++++++-- 5 files changed, 45 insertions(+), 6 deletions(-) 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 e197e13a4..65a28cffe 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 @@ -170,6 +170,9 @@ public class PfConceptContainer ex if (incomingConceptEntry.getValue().getVersion() != null) { conceptKey.setVersion(incomingConceptEntry.getValue().getVersion()); } + else { + conceptKey.setVersion(PfKey.NULL_KEY_VERSION); + } // After all that, save the map entry conceptMap.put(conceptKey, jpaConcept); diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfObjectFilter.java b/models-base/src/main/java/org/onap/policy/models/base/PfObjectFilter.java index a377c24be..0fca00d39 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfObjectFilter.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfObjectFilter.java @@ -59,7 +59,7 @@ public interface PfObjectFilter> { * @return the filtered list */ public default List latestVersionFilter(final List originalList) { - List filteredList = new ArrayList<>(); + List filteredList = new ArrayList<>(originalList); Collections.sort(filteredList); List filteredOutList = new ArrayList<>(); 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 3ae7c4c9d..55f59f640 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 @@ -48,6 +48,7 @@ import org.onap.policy.models.base.testconcepts.DummyPfConceptSub; */ public class PfConceptContainerTest { + @SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testConceptContainer() { DummyPfConceptContainer container = new DummyPfConceptContainer(); @@ -62,6 +63,13 @@ public class PfConceptContainerTest { container = new DummyPfConceptContainer(new PfConceptKey(), new TreeMap()); assertNotNull(container); + try { + new PfConceptContainer((PfConceptKey) null, null); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("key is marked @NonNull but is null", exc.getMessage()); + } + try { container = new DummyPfConceptContainer((PfConceptKey) null, null); fail("test should throw an exception here"); @@ -190,7 +198,7 @@ public class PfConceptContainerTest { public void testAuthorative() { Map dacMap = new LinkedHashMap<>(); dacMap.put("name0", new DummyAuthorativeConcept("name0", "1.2.3", "Hello")); - dacMap.put("name1", new DummyAuthorativeConcept("name1", "1.2.3", "Hi")); + dacMap.put("name1", new DummyAuthorativeConcept(null, null, "Hi")); dacMap.put("name2", new DummyAuthorativeConcept("name2", "1.2.3", "Howdy")); List> authorativeList = new ArrayList<>(); @@ -200,12 +208,14 @@ public class PfConceptContainerTest { container.fromAuthorative(authorativeList); assertEquals("Hello", container.getConceptMap().get(new PfConceptKey("name0:1.2.3")).getDescription()); - assertEquals("Hi", container.getConceptMap().get(new PfConceptKey("name1:1.2.3")).getDescription()); + assertEquals("Hi", container.getConceptMap().get(new PfConceptKey("name1:0.0.0")).getDescription()); assertEquals("Howdy", container.getConceptMap().get(new PfConceptKey("name2:1.2.3")).getDescription()); List> outMapList = container.toAuthorative(); - assertEquals(dacMap, outMapList.get(0)); + assertEquals(dacMap.get("name0"), outMapList.get(0).get("name0")); + assertEquals(dacMap.get("name1").getDescription(), outMapList.get(0).get("name1").getDescription()); + assertEquals(dacMap.get("name2"), outMapList.get(0).get("name2")); DummyBadPfConceptContainer badContainer = new DummyBadPfConceptContainer(); assertThatThrownBy(() -> { diff --git a/models-base/src/test/java/org/onap/policy/models/base/PfKeyTest.java b/models-base/src/test/java/org/onap/policy/models/base/PfKeyTest.java index a4c504788..797dba0cf 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/PfKeyTest.java +++ b/models-base/src/test/java/org/onap/policy/models/base/PfKeyTest.java @@ -48,6 +48,13 @@ public class PfKeyTest { e.getMessage()); } + try { + new PfConceptKey((PfConceptKey) null); + fail("This test should throw an exception"); + } catch (Exception e) { + assertEquals("copyConcept is marked @NonNull but is null", e.getMessage()); + } + PfConceptKey someKey0 = new PfConceptKey(); assertEquals(PfConceptKey.getNullKey(), someKey0); @@ -56,6 +63,7 @@ public class PfKeyTest { PfConceptKey someKey3 = new PfConceptKey(someKey1.getId()); assertEquals(someKey1, someKey2); assertEquals(someKey1, someKey3); + assertFalse(someKey1.isNullVersion()); assertEquals(someKey2, someKey1.getKey()); assertEquals(1, someKey1.getKeys().size()); 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 72df28f04..68494309c 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 @@ -1,4 +1,4 @@ -/* +/*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. * ================================================================================ @@ -41,7 +41,7 @@ public class PfKeyUseTest { assertNotNull(new PfKeyUse(new PfReferenceKey())); try { - new PfKeyUse((PfKeyUse)null); + new PfKeyUse((PfKeyUse) null); fail("test should throw an exception"); } catch (Exception exc) { assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); @@ -56,6 +56,14 @@ public class PfKeyUseTest { assertFalse(keyUse.isNullKey()); assertEquals(Compatibility.IDENTICAL, keyUse.getCompatibility(key)); + + try { + key.getCompatibility(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("otherKey is marked @NonNull but is null", exc.getMessage()); + } + assertTrue(keyUse.isCompatible(key)); keyUse.clean(); @@ -132,6 +140,16 @@ public class PfKeyUseTest { assertEquals("error copying concept key: Some error message", exc.getMessage()); } + try { + keyUse.isNewerThan(null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("otherKey is marked @NonNull but is null", exc.getMessage()); + } + + assertEquals(false, testKeyUse.isNewerThan(keyUse)); + assertEquals(false, testKeyUse.isNewerThan(testKeyUse)); + assertEquals(0, testKeyUse.getMajorVersion()); assertEquals(0, testKeyUse.getMinorVersion()); assertEquals(0, testKeyUse.getPatchVersion()); -- cgit 1.2.3-korg