diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/WikibaseSchema.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/WikibaseSchema.java index efd5c8125..f2c7e88de 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/schema/WikibaseSchema.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/WikibaseSchema.java @@ -190,4 +190,13 @@ public class WikibaseSchema implements OverlayModel { public void dispose(Project project) { } + + @Override + public boolean equals(Object other) { + if(other == null || !WikibaseSchema.class.isInstance(other)) { + return false; + } + WikibaseSchema otherSchema = (WikibaseSchema)other; + return itemDocumentExprs.equals(otherSchema.getItemDocumentExpressions()); + } } \ No newline at end of file diff --git a/extensions/wikidata/tests/data/operations/perform-edits.json b/extensions/wikidata/tests/data/operations/perform-edits.json new file mode 100644 index 000000000..403c136cf --- /dev/null +++ b/extensions/wikidata/tests/data/operations/perform-edits.json @@ -0,0 +1,9 @@ +{ + "op": "wikidata/perform-wikibase-edits", + "description": "Perform Wikibase edits", + "summary": "test null edit", + "engineConfig": { + "mode": "row-based", + "facets": [] + } +} diff --git a/extensions/wikidata/tests/data/operations/save-schema.json b/extensions/wikidata/tests/data/operations/save-schema.json new file mode 100644 index 000000000..2ca55e310 --- /dev/null +++ b/extensions/wikidata/tests/data/operations/save-schema.json @@ -0,0 +1,33 @@ +{ + "op": "wikidata/save-wikibase-schema", + "description": "Save Wikibase schema skeleton", + "schema": { + "itemDocuments": [ + { + "subject": { + "label": "University of Oxford", + "type": "wbitemconstant", + "qid": "Q34433" + }, + "statementGroups": [], + "nameDescs": [ + { + "name_type": "LABEL", + "value": { + "language": { + "id": "en", + "label": "en", + "type": "wblanguageconstant" + }, + "type": "wbmonolingualexpr", + "value": { + "type": "wbstringconstant", + "value": "University of Oxford" + } + } + } + ] + } + ] + } +} diff --git a/extensions/wikidata/tests/src/org/openrefine/wikidata/operations/OperationTest.java b/extensions/wikidata/tests/src/org/openrefine/wikidata/operations/OperationTest.java new file mode 100644 index 000000000..05f0faab2 --- /dev/null +++ b/extensions/wikidata/tests/src/org/openrefine/wikidata/operations/OperationTest.java @@ -0,0 +1,70 @@ +package org.openrefine.wikidata.operations; + +import java.io.IOException; +import java.io.LineNumberReader; +import java.io.StringReader; +import java.io.StringWriter; +import java.util.Properties; + +import org.json.JSONObject; +import org.json.JSONWriter; +import org.openrefine.wikidata.testing.JacksonSerializationTest; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.google.refine.history.Change; +import com.google.refine.model.AbstractOperation; +import com.google.refine.model.Project; +import com.google.refine.operations.OperationRegistry; +import com.google.refine.tests.RefineTest; +import com.google.refine.util.Pool; + +import edu.mit.simile.butterfly.ButterflyModule; + +public abstract class OperationTest extends RefineTest { + + protected Project project = null; + protected ButterflyModule module = null; + protected Pool pool = null; + + @BeforeMethod + public void setUp() { + project = createCSVProject("a,b\nc,d"); + module = mock(ButterflyModule.class); + when(module.getName()).thenReturn("wikidata"); + pool = new Pool(); + } + + protected void registerOperation(String name, Class klass) { + OperationRegistry.registerOperation(module, name, klass); + } + + public abstract AbstractOperation reconstruct() throws Exception; + + public abstract JSONObject getJson() throws Exception; + + @Test + public void testReconstruct() throws Exception { + JSONObject json = getJson(); + AbstractOperation op = reconstruct(); + StringWriter writer = new StringWriter(); + JSONWriter jsonWriter = new JSONWriter(writer); + op.write(jsonWriter, new Properties()); + JacksonSerializationTest.assertJsonEquals(json.toString(), writer.toString()); + } + + protected LineNumberReader makeReader(String input) { + StringReader reader = new StringReader(input); + return new LineNumberReader(reader); + } + + protected String saveChange(Change change) throws IOException { + StringWriter writer = new StringWriter(); + change.save(writer, new Properties()); + return writer.toString(); + } + +} diff --git a/extensions/wikidata/tests/src/org/openrefine/wikidata/operations/PerformWikibaseEditsOperationTest.java b/extensions/wikidata/tests/src/org/openrefine/wikidata/operations/PerformWikibaseEditsOperationTest.java new file mode 100644 index 000000000..f53e6d5bd --- /dev/null +++ b/extensions/wikidata/tests/src/org/openrefine/wikidata/operations/PerformWikibaseEditsOperationTest.java @@ -0,0 +1,55 @@ +package org.openrefine.wikidata.operations; + +import static org.junit.Assert.assertEquals; + +import java.io.LineNumberReader; + +import org.json.JSONObject; +import org.openrefine.wikidata.testing.TestingData; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import com.google.refine.history.Change; +import com.google.refine.model.AbstractOperation; +import com.google.refine.model.Recon; + +public class PerformWikibaseEditsOperationTest extends OperationTest { + + @BeforeMethod + public void registerOperation() { + registerOperation("perform-wikibase-edits", PerformWikibaseEditsOperation.class); + } + + @Override + public AbstractOperation reconstruct() throws Exception { + JSONObject json = getJson(); + return PerformWikibaseEditsOperation.reconstruct(project, json); + } + + @Override + public JSONObject getJson() throws Exception { + return TestingData.jsonFromFile("data/operations/perform-edits.json"); + } + + @Test + public void testLoadChange() throws Exception { + String changeString = "newItems={\"qidMap\":{\"1234\":\"Q789\"}}\n" + + "/ec/\n"; + LineNumberReader reader = makeReader(changeString); + Change change = PerformWikibaseEditsOperation.PerformWikibaseEditsChange.load(reader, pool); + + project.rows.get(0).cells.set(0, TestingData.makeNewItemCell(1234L, "my new item")); + + change.apply(project); + + assertEquals(Recon.Judgment.Matched, project.rows.get(0).cells.get(0).recon.judgment); + assertEquals("Q789", project.rows.get(0).cells.get(0).recon.match.id); + + change.revert(project); + + assertEquals(Recon.Judgment.New, project.rows.get(0).cells.get(0).recon.judgment); + + assertEquals(changeString, saveChange(change)); + } + +} diff --git a/extensions/wikidata/tests/src/org/openrefine/wikidata/operations/SaveWikibaseSchemaOperationTest.java b/extensions/wikidata/tests/src/org/openrefine/wikidata/operations/SaveWikibaseSchemaOperationTest.java new file mode 100644 index 000000000..3a69cb8e6 --- /dev/null +++ b/extensions/wikidata/tests/src/org/openrefine/wikidata/operations/SaveWikibaseSchemaOperationTest.java @@ -0,0 +1,57 @@ +package org.openrefine.wikidata.operations; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import java.io.LineNumberReader; + +import org.json.JSONObject; +import org.openrefine.wikidata.schema.WikibaseSchema; +import org.openrefine.wikidata.testing.TestingData; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import com.google.refine.history.Change; +import com.google.refine.model.AbstractOperation; + + +public class SaveWikibaseSchemaOperationTest extends OperationTest { + + @BeforeMethod + public void registerOperation() { + registerOperation("save-wikibase-schema", SaveWikibaseSchemaOperation.class); + } + + @Override + public AbstractOperation reconstruct() throws Exception { + return SaveWikibaseSchemaOperation.reconstruct(project, getJson()); + } + + @Override + public JSONObject getJson() throws Exception { + return TestingData.jsonFromFile("data/operations/save-schema.json"); + } + + @Test + public void testLoadChange() throws Exception { + JSONObject schemaJson = TestingData.jsonFromFile("data/schema/inception.json"); + String changeString = + "newSchema="+schemaJson.toString()+"\n" + + "oldSchema=\n" + + "/ec/"; + WikibaseSchema schema = WikibaseSchema.reconstruct(schemaJson); + + LineNumberReader reader = makeReader(changeString); + Change change = SaveWikibaseSchemaOperation.WikibaseSchemaChange.load(reader, pool); + + change.apply(project); + + assertEquals(schema, project.overlayModels.get(SaveWikibaseSchemaOperation.WikibaseSchemaChange.overlayModelKey)); + + change.revert(project); + + assertNull(project.overlayModels.get(SaveWikibaseSchemaOperation.WikibaseSchemaChange.overlayModelKey)); + + saveChange(change); // not checking for equality because JSON serialization varies + } +} diff --git a/extensions/wikidata/tests/src/org/openrefine/wikidata/testing/JacksonSerializationTest.java b/extensions/wikidata/tests/src/org/openrefine/wikidata/testing/JacksonSerializationTest.java index 8c2dc6dc7..634a1a076 100644 --- a/extensions/wikidata/tests/src/org/openrefine/wikidata/testing/JacksonSerializationTest.java +++ b/extensions/wikidata/tests/src/org/openrefine/wikidata/testing/JacksonSerializationTest.java @@ -6,7 +6,9 @@ import java.io.IOException; import org.testng.Assert; +import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; @@ -16,16 +18,12 @@ public class JacksonSerializationTest { public static void testSerialize(Object pojo, String expectedJson) { // Test that the pojo is correctly serialized try { - JsonNode parsedExpectedJson = mapper.readValue(expectedJson, JsonNode.class); - String actualJson = mapper.writeValueAsString(pojo); - JsonNode parsedActualJson = mapper.readValue(actualJson, JsonNode.class); - assertEquals(parsedExpectedJson, parsedActualJson); + + String actualJson = mapper.writeValueAsString(pojo); + assertJsonEquals(expectedJson, actualJson); } catch (JsonProcessingException e) { e.printStackTrace(); Assert.fail("Failed to serialize object"); - } catch (IOException e) { - e.printStackTrace(); - Assert.fail("Invalid test JSON provided"); } } @@ -46,4 +44,15 @@ public class JacksonSerializationTest { testSerialize(pojo, json); testDeserialize(targetClass, pojo, json); } + + public static void assertJsonEquals(String expectedJson, String actualJson) { + JsonNode parsedExpectedJson; + try { + parsedExpectedJson = mapper.readValue(expectedJson, JsonNode.class); + JsonNode parsedActualJson = mapper.readValue(actualJson, JsonNode.class); + assertEquals(parsedExpectedJson, parsedActualJson); + } catch (IOException e) { + Assert.fail("Invalid JSON"); + } + } }