Add serialization tests for preference classes
This commit is contained in:
parent
580fd92c10
commit
da9978b365
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user