aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java
diff options
context:
space:
mode:
authorOfir Sonsino <os0695@att.com>2018-01-31 17:19:00 +0200
committerOfir Sonsino <os0695@att.com>2018-01-31 17:19:00 +0200
commit1cfb08779ea0e00be69e072a940b3063e049fe6b (patch)
tree6602a900387c8393ed0dcd81c0539381632903c6 /vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java
parent2f20b001b9243e0f8b44aecc768ec265fd538732 (diff)
org.onap migration
Change-Id: I52f0b2851f2c765752b6d21f49b32136d7d72a3d Issue-ID: VID-86 Signed-off-by: Ofir Sonsino <os0695@att.com>
Diffstat (limited to 'vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java')
-rw-r--r--vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java68
1 files changed, 68 insertions, 0 deletions
diff --git a/vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java b/vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java
new file mode 100644
index 000000000..73a79cd21
--- /dev/null
+++ b/vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java
@@ -0,0 +1,68 @@
+package org.onap.vid.testUtils;
+
+import org.json.JSONArray;
+import org.json.JSONObject;
+import org.junit.Assert;
+
+import java.util.Iterator;
+
+import static fj.parser.Parser.fail;
+
+/**
+ * Created by Oren on 6/7/17.
+ */
+public class TestUtils {
+
+ /**
+ * The method compares between two jsons. the function assert that the actual object does not reduce or change the functionallity/parsing of the expected json.
+ * This means that if the expected JSON has a key which is null or the JSON doesn't have a key which contained in the expected JSON the assert will succeed and the will pass.
+ * For example : For JSON expected = {a:null} and actual {a:3} the test will pass
+ * Other example : For JSON expected = {a:3} and actual {a:null} the test will fail
+ *
+ * @param expected JSON
+ * @param actual JSON
+ */
+ public static void assertJsonStringEqualsIgnoreNulls(String expected, String actual) {
+ if (expected == null || expected == JSONObject.NULL) {return;}
+
+ JSONObject expectedJSON = new JSONObject(expected);
+ JSONObject actualJSON = new JSONObject(actual);
+ Iterator<?> keys = expectedJSON.keys();
+
+ while( keys.hasNext() ) {
+ String key = (String)keys.next();
+ Object expectedValue = expectedJSON.get(key);
+ if (expectedValue == JSONObject.NULL){
+ continue;
+ }
+
+ Object actualValue = actualJSON.get(key);
+
+ if (expectedValue instanceof JSONObject) {
+ String expectedVal = expectedValue.toString();
+ String actualVal = actualValue.toString();
+ assertJsonStringEqualsIgnoreNulls(expectedVal, actualVal);
+ }
+ else if (expectedValue instanceof JSONArray) {
+ if (actualValue instanceof JSONArray) {
+ JSONArray expectedJSONArray = (JSONArray)expectedValue;
+ JSONArray actualJSONArray = (JSONArray)actualValue;
+ for (int i = 0; i < expectedJSONArray.length(); i++) {
+ String expectedItem = expectedJSONArray.get(i).toString();
+ String actualItem = actualJSONArray.get(i).toString();
+ if (expectedValue instanceof JSONObject)
+ assertJsonStringEqualsIgnoreNulls(expectedItem, actualItem);
+ }
+ }
+ else {
+ fail("expected: " + expectedValue + " got:" + actualValue);
+ }
+ }
+ else {
+ Assert.assertEquals(expectedValue, actualValue);
+ }
+ }
+ }
+
+
+}