Jackson deserialization for ReconJudgeSimilarCellsOperation

This commit is contained in:
Antonin Delpeuch 2018-10-22 10:27:39 +01:00
parent 408703f13c
commit 3271e53ecf
2 changed files with 34 additions and 44 deletions

View File

@ -33,14 +33,14 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package com.google.refine.operations.recon; package com.google.refine.operations.recon;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
@ -61,6 +61,7 @@ import com.google.refine.model.changes.CellChange;
import com.google.refine.model.changes.ReconChange; import com.google.refine.model.changes.ReconChange;
import com.google.refine.model.recon.ReconConfig; import com.google.refine.model.recon.ReconConfig;
import com.google.refine.operations.EngineDependentMassCellOperation; import com.google.refine.operations.EngineDependentMassCellOperation;
import com.google.refine.util.ParsingUtilities;
public class ReconJudgeSimilarCellsOperation extends EngineDependentMassCellOperation { public class ReconJudgeSimilarCellsOperation extends EngineDependentMassCellOperation {
final protected String _similarValue; final protected String _similarValue;
@ -68,55 +69,30 @@ public class ReconJudgeSimilarCellsOperation extends EngineDependentMassCellOper
final protected ReconCandidate _match; final protected ReconCandidate _match;
final protected boolean _shareNewTopics; final protected boolean _shareNewTopics;
static public AbstractOperation reconstruct(Project project, JSONObject obj) throws JSONException { static public AbstractOperation reconstruct(Project project, JSONObject obj) throws IOException {
JSONObject engineConfig = obj.getJSONObject("engineConfig"); return ParsingUtilities.mapper.readValue(obj.toString(), ReconJudgeSimilarCellsOperation.class);
ReconCandidate match = null;
if (obj.has("match")) {
JSONObject matchObj = obj.getJSONObject("match");
JSONArray types = matchObj.getJSONArray("types");
String[] typeIDs = new String[types.length()];
for (int i = 0; i < typeIDs.length; i++) {
typeIDs[i] = types.getString(i);
}
match = new ReconCandidate(
matchObj.getString("id"),
matchObj.getString("name"),
typeIDs,
matchObj.getDouble("score")
);
}
Judgment judgment = Judgment.None;
if (obj.has("judgment")) {
judgment = Recon.stringToJudgment(obj.getString("judgment"));
}
return new ReconJudgeSimilarCellsOperation(
EngineConfig.reconstruct(engineConfig),
obj.getString("columnName"),
obj.getString("similarValue"),
judgment,
match,
obj.has("shareNewTopics") ? obj.getBoolean("shareNewTopics") : false
);
} }
@JsonCreator
public ReconJudgeSimilarCellsOperation( public ReconJudgeSimilarCellsOperation(
@JsonProperty("engineConfig")
EngineConfig engineConfig, EngineConfig engineConfig,
@JsonProperty("columnName")
String columnName, String columnName,
@JsonProperty("similarValue")
String similarValue, String similarValue,
@JsonProperty("judgment")
Judgment judgment, Judgment judgment,
@JsonProperty("match")
ReconCandidate match, ReconCandidate match,
boolean shareNewTopics @JsonProperty("shareNewTopics")
Boolean shareNewTopics
) { ) {
super(engineConfig, columnName, false); super(engineConfig, columnName, false);
this._similarValue = similarValue; this._similarValue = similarValue;
this._judgment = judgment; this._judgment = judgment;
this._match = match; this._match = match;
this._shareNewTopics = shareNewTopics; this._shareNewTopics = shareNewTopics == null ? false : shareNewTopics;
} }
@JsonProperty("columnName") @JsonProperty("columnName")

View File

@ -4,10 +4,10 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import java.util.Properties; import java.util.Properties;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeTest;
@ -39,7 +39,7 @@ public class ReconJudgeSimilarCellsTests extends RefineTest {
} }
@Test @Test
public void serializeReconJudgeSimilarCellsOperation() throws JSONException { public void serializeReconJudgeSimilarCellsOperation() throws IOException {
String json = "{\"op\":\"core/recon-judge-similar-cells\"," String json = "{\"op\":\"core/recon-judge-similar-cells\","
+ "\"description\":\"Mark to create one single new item for all cells containing \\\"foo\\\" in column A\"," + "\"description\":\"Mark to create one single new item for all cells containing \\\"foo\\\" in column A\","
+ "\"engineConfig\":{\"mode\":\"row-based\",\"facets\":[]}," + "\"engineConfig\":{\"mode\":\"row-based\",\"facets\":[]},"
@ -50,6 +50,20 @@ public class ReconJudgeSimilarCellsTests extends RefineTest {
TestUtils.isSerializedTo(ReconJudgeSimilarCellsOperation.reconstruct(mock(Project.class), new JSONObject(json)), json); TestUtils.isSerializedTo(ReconJudgeSimilarCellsOperation.reconstruct(mock(Project.class), new JSONObject(json)), json);
} }
@Test
public void serializeReconJudgeSimilarCellsOperationMatch() throws IOException {
String json = "{\"op\":\"core/recon-judge-similar-cells\","
+ "\"description\":\"Match item Douglas Adams (Q42) for cells containing \\\"foo\\\" in column A\","
+ "\"engineConfig\":{\"mode\":\"row-based\",\"facets\":[]},"
+ "\"columnName\":\"A\","
+ "\"similarValue\":\"foo\","
+ "\"judgment\":\"matched\","
+ "\"match\":{\"id\":\"Q42\",\"name\":\"Douglas Adams\",\"types\":[\"Q5\"],\"score\":85},"
+ "\"shareNewTopics\":false"
+ "}";
TestUtils.isSerializedTo(ReconJudgeSimilarCellsOperation.reconstruct(mock(Project.class), new JSONObject(json)), json);
}
@Test @Test
public void testMarkNewTopics() throws Exception { public void testMarkNewTopics() throws Exception {
Project project = createCSVProject( Project project = createCSVProject(