From da9978b365ef7f379eb2a7cb411fd322042e8d80 Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Tue, 21 Aug 2018 17:26:14 +0200 Subject: [PATCH] Add serialization tests for preference classes --- .../com/google/refine/util/JSONUtilities.java | 17 ++++++++++++ .../preference/PreferenceStoreTests.java | 24 +++++++++++++++++ .../refine/tests/preference/TopListTests.java | 27 +++++++++++++++++++ .../google/refine/tests/util/TestUtils.java | 6 ++--- 4 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 main/tests/server/src/com/google/refine/tests/preference/PreferenceStoreTests.java create mode 100644 main/tests/server/src/com/google/refine/tests/preference/TopListTests.java diff --git a/main/src/com/google/refine/util/JSONUtilities.java b/main/src/com/google/refine/util/JSONUtilities.java index 967633cdb..db88eeb79 100644 --- a/main/src/com/google/refine/util/JSONUtilities.java +++ b/main/src/com/google/refine/util/JSONUtilities.java @@ -33,6 +33,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. package com.google.refine.util; +import java.io.StringWriter; +import java.io.Writer; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.OffsetDateTime; @@ -42,12 +44,15 @@ import java.util.Calendar; import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.Properties; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.json.JSONWriter; +import com.google.refine.Jsonizable; + public class JSONUtilities { static public JSONObject getObject(JSONObject obj, String key) { try { @@ -371,4 +376,16 @@ public class JSONUtilities { destArray.put(srcArray.get(i)); } } + + static public String serialize(Jsonizable obj, Properties options) { + Writer w = new StringWriter(); + JSONWriter jsonWriter = new JSONWriter(w); + obj.write(jsonWriter, options); + return w.toString(); + } + + static public String serialize(Jsonizable obj) { + Properties options = new Properties(); + return serialize(obj, options); + } } diff --git a/main/tests/server/src/com/google/refine/tests/preference/PreferenceStoreTests.java b/main/tests/server/src/com/google/refine/tests/preference/PreferenceStoreTests.java new file mode 100644 index 000000000..f71a42016 --- /dev/null +++ b/main/tests/server/src/com/google/refine/tests/preference/PreferenceStoreTests.java @@ -0,0 +1,24 @@ +package com.google.refine.tests.preference; + +import org.json.JSONObject; +import org.testng.annotations.Test; + +import com.google.refine.preference.PreferenceStore; +import com.google.refine.tests.util.TestUtils; + +public class PreferenceStoreTests { + @Test + public void serializePreferenceStore() { + String json = "{" + + "\"entries\":{" + + " \"reconciliation.standardServices\":[" + + " {\"propose_properties\":{\"service_url\":\"https://tools.wmflabs.org/openrefine-wikidata\",\"service_path\":\"/en/propose_properties\"}},\"preview\":{\"width\":320,\"url\":\"https://tools.wmflabs.org/openrefine-wikidata/en/preview?id={{id}}\",\"height\":90},\"view\":{\"url\":\"https://www.wikidata.org/wiki/{{id}}\"},\"ui\":{\"handler\":\"ReconStandardServicePanel\"},\"identifierSpace\":\"http://www.wikidata.org/entity/\",\"name\":\"Wikidata Reconciliation for OpenRefine (en)\",\"suggest\":{\"property\":{\"flyout_service_path\":\"/en/flyout/property?id=${id}\",\"service_url\":\"https://tools.wmflabs.org/openrefine-wikidata\",\"service_path\":\"/en/suggest/property\"},\"type\":{\"flyout_service_path\":\"/en/flyout/type?id=${id}\",\"service_url\":\"https://tools.wmflabs.org/openrefine-wikidata\",\"service_path\":\"/en/suggest/type\"},\"entity\":{\"flyout_service_path\":\"/en/flyout/entity?id=${id}\",\"service_url\":\"https://tools.wmflabs.org/openrefine-wikidata\",\"service_path\":\"/en/suggest/entity\"}},\"defaultTypes\":[{\"name\":\"entity\",\"id\":\"Q35120\"}],\"url\":\"https://tools.wmflabs.org/openrefine-wikidata/en/api\",\"schemaSpace\":\"http://www.wikidata.org/prop/direct/\"}" + + " ]," + + " \"scripting.starred-expressions\":{\"class\":\"com.google.refine.preference.TopList\",\"top\":2147483647,\"list\":[]}," + + " \"scripting.expressions\":{\"class\":\"com.google.refine.preference.TopList\",\"top\":100,\"list\":[]}" + + "}}"; + PreferenceStore prefStore = new PreferenceStore(); + prefStore.load(new JSONObject(json)); + TestUtils.isSerializedTo(prefStore, json); + } +} diff --git a/main/tests/server/src/com/google/refine/tests/preference/TopListTests.java b/main/tests/server/src/com/google/refine/tests/preference/TopListTests.java new file mode 100644 index 000000000..e9ed000d5 --- /dev/null +++ b/main/tests/server/src/com/google/refine/tests/preference/TopListTests.java @@ -0,0 +1,27 @@ +package com.google.refine.tests.preference; + +import org.json.JSONObject; +import org.testng.annotations.Test; + +import com.google.refine.preference.TopList; +import com.google.refine.tests.util.TestUtils; + +public class TopListTests { + @Test + public void serializeTopList() { + String json = "{" + + "\"class\":\"com.google.refine.preference.TopList\"," + + "\"top\":100," + + "\"list\":[" + + " \"grel:value.parseJson()[\\\"end-date\\\"][\\\"year\\\"][\\\"value\\\"]\"," + + " \"grel:value.parseJson()[\\\"start-date\\\"][\\\"year\\\"][\\\"value\\\"]\"," + + " \"grel:value.parseJson()[\\\"organization\\\"][\\\"disambiguated-organization\\\"][\\\"disambiguated-organization-identifier\\\"]\"," + + " \"grel:value.parseJson()[\\\"organization\\\"][\\\"address\\\"][\\\"country\\\"]\",\"grel:value.parseJson()[\\\"organization\\\"][\\\"name\\\"]\"," + + " \"grel:value.parseJson()[\\\"employment-summary\\\"].join('###')\"," + + " \"grel:\\\"https://pub.orcid.org/\\\"+value+\\\"/employments\\\"\"" + + "]}"; + TestUtils.isSerializedTo( + TopList.load(new JSONObject(json)), + json); + } +} diff --git a/main/tests/server/src/com/google/refine/tests/util/TestUtils.java b/main/tests/server/src/com/google/refine/tests/util/TestUtils.java index 54d772ffd..e148f8252 100644 --- a/main/tests/server/src/com/google/refine/tests/util/TestUtils.java +++ b/main/tests/server/src/com/google/refine/tests/util/TestUtils.java @@ -19,6 +19,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.refine.Jsonizable; +import com.google.refine.util.JSONUtilities; import com.google.refine.util.ParsingUtilities; @@ -59,10 +60,7 @@ public class TestUtils { * Checks that a serializable object is serialized to the target JSON string. */ public static void isSerializedTo(Jsonizable o, String targetJson, Properties options) { - Writer w = new StringWriter(); - JSONWriter jsonWriter = new JSONWriter(w); - o.write(jsonWriter, options); - equalAsJson(targetJson, w.toString()); + equalAsJson(targetJson, JSONUtilities.serialize(o, options)); } /**