From b2c0528da052b5c73e3fc1f7f98d22578acd91f0 Mon Sep 17 00:00:00 2001 From: xuegao Date: Fri, 19 Mar 2021 14:04:17 +0100 Subject: Improve test coverage Add unit tests to improve test coverage. Issue-ID: SDC-3428 Change-Id: I29a8f1d49f1dfd6bca79f3d873d1e1ff7adc4fb2 Signed-off-by: xuegao --- .../openecomp/sdc/be/resources/data/TagData.java | 51 ++++---------------- .../openecomp/sdc/be/dao/utils/MapUtilTest.java | 56 +++++++++++++++++++--- .../sdc/be/resources/JsonParserUtilsTests.java | 17 +++++++ .../sdc/be/resources/data/TagDataTest.java | 9 ++-- .../data/auditing/AuditingGenericEventTest.java | 24 +++++++++- .../fixtures/ListCapabilityDataDefinition2.json | 17 +++++++ 6 files changed, 121 insertions(+), 53 deletions(-) create mode 100644 catalog-dao/src/test/resources/fixtures/ListCapabilityDataDefinition2.json (limited to 'catalog-dao/src') diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/resources/data/TagData.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/resources/data/TagData.java index 43b52e2a54..949aa83c14 100644 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/resources/data/TagData.java +++ b/catalog-dao/src/main/java/org/openecomp/sdc/be/resources/data/TagData.java @@ -20,6 +20,10 @@ package org.openecomp.sdc.be.resources.data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; import org.openecomp.sdc.be.dao.graph.datatype.GraphNode; import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; @@ -27,6 +31,10 @@ import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; import java.util.HashMap; import java.util.Map; +@Getter +@Setter +@EqualsAndHashCode +@ToString public class TagData extends GraphNode { private String name; @@ -56,49 +64,6 @@ public class TagData extends GraphNode { return map; } - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - @Override - public String toString() { - return "Tag [Name=" + name + "]"; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((name == null) ? 0 : name.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - TagData other = (TagData) obj; - if (name == null) { - if (other.getClass() != null) { - return false; - } - } else if (!name.equals(other.getName())) { - return false; - } - return true; - } - @Override public String getUniqueIdKey() { return GraphPropertiesDictionary.NAME.getProperty(); diff --git a/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/utils/MapUtilTest.java b/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/utils/MapUtilTest.java index 0cb045aaad..ea562b1f3e 100644 --- a/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/utils/MapUtilTest.java +++ b/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/utils/MapUtilTest.java @@ -21,13 +21,17 @@ package org.openecomp.sdc.be.dao.utils; import com.google.common.collect.ImmutableMap; -import org.junit.Test; +import org.junit.jupiter.api.Test; + import java.util.*; import java.util.function.Function; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.openecomp.sdc.be.dao.utils.MapUtil.mergeMaps; public class MapUtilTest { @@ -64,14 +68,54 @@ public class MapUtilTest { } @Test public void testGet() throws Exception { - Map map = null; + Map mapWildcard = null; String path = ""; Object result; - // default test - result = MapUtil.get(map, path); - path = "\\mock\\mock"; - result = MapUtil.get(map, path); + result = MapUtil.get(mapWildcard, path); + assertNull(result); + + path = "mock1.mock2"; + + Map map = new HashMap<>(); + map.put("mock1", "test"); + mapWildcard = map; + result = MapUtil.get(mapWildcard, path); + assertNull(result); + + Map subMap = new HashMap<>(); + subMap.put("mock2", 1); + Map subMapWildcard = subMap; + map.put("mock1", subMapWildcard); + mapWildcard = map; + result = MapUtil.get(mapWildcard, path); + assertEquals(1, result); + } + + @Test + public void testFlattenMapValues() throws Exception { + assertNotNull(MapUtil.flattenMapValues(null)); + + Map> map = new HashMap<>(); + List list1 = new LinkedList<>(); + list1.add("test1"); + List list2 = new LinkedList<>(); + list2.add("test2"); + map.put("key1", list1); + map.put("key2", list2); + List result = MapUtil.flattenMapValues(map); + assertEquals(2, result.size()); + assertEquals("test1", result.get(0)); + assertEquals("test2", result.get(1)); + } + + @Test + public void testStreamOfNullable() throws Exception { + assertEquals(0, MapUtil.streamOfNullable(null).count()); + + Collection collectionTest = new LinkedList(); + collectionTest.add("test"); + assertEquals(1, MapUtil.streamOfNullable(collectionTest).count()); } @Test diff --git a/catalog-dao/src/test/java/org/openecomp/sdc/be/resources/JsonParserUtilsTests.java b/catalog-dao/src/test/java/org/openecomp/sdc/be/resources/JsonParserUtilsTests.java index 21a728053c..09e751580f 100644 --- a/catalog-dao/src/test/java/org/openecomp/sdc/be/resources/JsonParserUtilsTests.java +++ b/catalog-dao/src/test/java/org/openecomp/sdc/be/resources/JsonParserUtilsTests.java @@ -37,12 +37,16 @@ import java.util.Map; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatCode; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.openecomp.sdc.be.utils.FixtureHelpers.fixture; import static org.openecomp.sdc.be.utils.JsonTester.testJsonMap; public class JsonParserUtilsTests { private static final String FIXTURE_PATH = "fixtures/ListCapabilityDataDefinition.json"; + private static final String FIXTURE_PATH2 = "fixtures/ListCapabilityDataDefinition2.json"; @Test public void testToMap() { @@ -53,6 +57,19 @@ public class JsonParserUtilsTests { assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } + @Test + public void testToList() { + assertNull(JsonParserUtils.toList(null, ListCapabilityDataDefinition.class)); + String json = fixture(FIXTURE_PATH2); + List actual = JsonParserUtils.toList(json, + ListCapabilityDataDefinition.class); + assertEquals(1, actual.size()); + CapabilityDataDefinition expectedDef = buildListCapabilityDataDefinition().getListToscaDataDefinition().get(0); + CapabilityDataDefinition actualDef = actual.get(0).getListToscaDataDefinition().get(0); + assertEquals(1, actual.get(0).getListToscaDataDefinition().size()); + assertEquals(expectedDef, actualDef); + } + @Test public void testJacksonFasterXml() { ObjectMapper mapper = new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) diff --git a/catalog-dao/src/test/java/org/openecomp/sdc/be/resources/data/TagDataTest.java b/catalog-dao/src/test/java/org/openecomp/sdc/be/resources/data/TagDataTest.java index c1729d0f3d..919b7c7663 100644 --- a/catalog-dao/src/test/java/org/openecomp/sdc/be/resources/data/TagDataTest.java +++ b/catalog-dao/src/test/java/org/openecomp/sdc/be/resources/data/TagDataTest.java @@ -20,13 +20,15 @@ package org.openecomp.sdc.be.resources.data; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; import java.util.HashMap; import java.util.Map; +import static org.junit.jupiter.api.Assertions.assertEquals; + + public class TagDataTest { private TagData createTestSubject() { @@ -90,6 +92,7 @@ public class TagDataTest { result = testSubject.hashCode(); } + @Test public void testEquals() throws Exception { TagData testSubject; @@ -100,7 +103,7 @@ public class TagDataTest { testSubject = createTestSubject(); obj = null; result = testSubject.equals(obj); - Assert.assertEquals(false, result); + assertEquals(false, result); } @Test diff --git a/catalog-dao/src/test/java/org/openecomp/sdc/be/resources/data/auditing/AuditingGenericEventTest.java b/catalog-dao/src/test/java/org/openecomp/sdc/be/resources/data/auditing/AuditingGenericEventTest.java index f0557aaee5..7a94b4da27 100644 --- a/catalog-dao/src/test/java/org/openecomp/sdc/be/resources/data/auditing/AuditingGenericEventTest.java +++ b/catalog-dao/src/test/java/org/openecomp/sdc/be/resources/data/auditing/AuditingGenericEventTest.java @@ -20,7 +20,14 @@ package org.openecomp.sdc.be.resources.data.auditing; -import org.junit.Test; + +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; public class AuditingGenericEventTest { @@ -28,6 +35,21 @@ public class AuditingGenericEventTest { return new AuditingGenericEvent(); } + @Test + public void testTimestamp() throws Exception { + AuditingGenericEvent testSubject = createTestSubject(); + testSubject.setTimestamp("timestamp"); + assertEquals("timestamp", testSubject.getTimestamp()); + } + + @Test + public void testFields() throws Exception { + AuditingGenericEvent testSubject = createTestSubject(); + Map map = new HashMap<>(); + testSubject.setFields(map); + assertNotNull(testSubject.getFields()); + } + @Test public void testGetRequestId() throws Exception { AuditingGenericEvent testSubject; diff --git a/catalog-dao/src/test/resources/fixtures/ListCapabilityDataDefinition2.json b/catalog-dao/src/test/resources/fixtures/ListCapabilityDataDefinition2.json new file mode 100644 index 0000000000..81485110d0 --- /dev/null +++ b/catalog-dao/src/test/resources/fixtures/ListCapabilityDataDefinition2.json @@ -0,0 +1,17 @@ +[[ + { + "name": "forwarder", + "type": "org.openecomp.capabilities.Forwarder", + "uniqueId": "capability.deb142fd-95eb-48f7-99ae-81ab09466b1e.forwarder", + "ownerId": "deb142fd-95eb-48f7-99ae-81ab09466b1e", + "capabilitySources": [ + "org.openecomp.resource.cp.nodes.network.Port", + "org.openecomp.resource.cp.v2.extCP", + "org.openecomp.resource.cp.v2.extContrailCP" + ], + "minOccurrences": "1", + "leftOccurrences": "UNBOUNDED", + "maxOccurrences": "UNBOUNDED", + "empty": false + } +]] -- cgit 1.2.3-korg