Serialization tests for expression commands
This commit is contained in:
parent
65231e2140
commit
fd69e4b980
@ -47,7 +47,6 @@ import org.json.JSONWriter;
|
|||||||
|
|
||||||
import com.google.refine.ProjectManager;
|
import com.google.refine.ProjectManager;
|
||||||
import com.google.refine.commands.Command;
|
import com.google.refine.commands.Command;
|
||||||
import com.google.refine.model.Project;
|
|
||||||
import com.google.refine.preference.TopList;
|
import com.google.refine.preference.TopList;
|
||||||
|
|
||||||
public class GetExpressionHistoryCommand extends Command {
|
public class GetExpressionHistoryCommand extends Command {
|
||||||
@ -61,8 +60,6 @@ public class GetExpressionHistoryCommand extends Command {
|
|||||||
throws ServletException, IOException {
|
throws ServletException, IOException {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Project project = getProject(request);
|
|
||||||
|
|
||||||
List<String> localExpressions = toExpressionList(ProjectManager.singleton.getPreferenceStore().get("scripting.expressions"));
|
List<String> localExpressions = toExpressionList(ProjectManager.singleton.getPreferenceStore().get("scripting.expressions"));
|
||||||
localExpressions = localExpressions.size() > 20 ? localExpressions.subList(0, 20) : localExpressions;
|
localExpressions = localExpressions.size() > 20 ? localExpressions.subList(0, 20) : localExpressions;
|
||||||
|
|
||||||
|
@ -0,0 +1,70 @@
|
|||||||
|
package com.google.refine.tests.commands.expr;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.io.StringWriter;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
|
import org.testng.annotations.BeforeMethod;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonParseException;
|
||||||
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||||
|
|
||||||
|
import com.google.refine.commands.Command;
|
||||||
|
import com.google.refine.io.FileProjectManager;
|
||||||
|
import com.google.refine.tests.util.TestUtils;
|
||||||
|
|
||||||
|
public class ExpressionCommandTestBase {
|
||||||
|
protected HttpServletRequest request = null;
|
||||||
|
protected HttpServletResponse response = null;
|
||||||
|
protected Command command = null;
|
||||||
|
protected StringWriter writer = null;
|
||||||
|
|
||||||
|
@BeforeMethod
|
||||||
|
public void setUpRequestResponse() {
|
||||||
|
request = mock(HttpServletRequest.class);
|
||||||
|
response = mock(HttpServletResponse.class);
|
||||||
|
writer = new StringWriter();
|
||||||
|
try {
|
||||||
|
when(response.getWriter()).thenReturn(new PrintWriter(writer));
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void initWorkspace(String expressionsJson, String starredExpressionsJson) {
|
||||||
|
String starred = starredExpressionsJson == null ? "{\"class\":\"com.google.refine.preference.TopList\",\"top\":2147483647," +
|
||||||
|
"\"list\":[]}" : starredExpressionsJson;
|
||||||
|
String expressions = expressionsJson == null ? "{\"class\":\"com.google.refine.preference.TopList\",\"top\":100,\"list\":[]}" : expressionsJson;
|
||||||
|
try {
|
||||||
|
File workspaceDir = TestUtils.createTempDirectory("openrefine-test-workspace-dir");
|
||||||
|
File jsonPath = new File(workspaceDir, "workspace.json");
|
||||||
|
FileUtils.writeStringToFile(jsonPath, "{\"projectIDs\":[]\n" +
|
||||||
|
",\"preferences\":{\"entries\":{\"scripting.starred-expressions\":" + starred +
|
||||||
|
",\"scripting.expressions\":"+expressions+"}}}");
|
||||||
|
FileProjectManager.initialize(workspaceDir);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void assertResponseJsonIs(String expectedJson) {
|
||||||
|
String actualJson = writer.toString();
|
||||||
|
if(!TestUtils.equalAsJson(expectedJson, actualJson)) {
|
||||||
|
try {
|
||||||
|
TestUtils.jsonDiff(expectedJson, actualJson);
|
||||||
|
} catch (JsonParseException | JsonMappingException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TestUtils.assertEqualAsJson(expectedJson, actualJson);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
package com.google.refine.tests.commands.expr;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
|
||||||
|
import org.testng.annotations.BeforeMethod;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import com.google.refine.commands.expr.GetExpressionHistoryCommand;
|
||||||
|
|
||||||
|
public class GetExpressionHistoryCommandTests extends ExpressionCommandTestBase {
|
||||||
|
|
||||||
|
@BeforeMethod
|
||||||
|
public void setUp() {
|
||||||
|
command = new GetExpressionHistoryCommand();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testJsonResponse() throws ServletException, IOException {
|
||||||
|
|
||||||
|
initWorkspace("{\n" +
|
||||||
|
" \"class\": \"com.google.refine.preference.TopList\",\n" +
|
||||||
|
" \"top\": 100,\n" +
|
||||||
|
" \"list\": [\n" +
|
||||||
|
" \"grel:facetCount(value, 'value', 'Column 1')\",\n" +
|
||||||
|
" \"grel:facetCount(value, 'value', 'Column 3')\",\n" +
|
||||||
|
" \"grel:cell.recon.match.id\"" +
|
||||||
|
"]}", "{\n" +
|
||||||
|
" \"class\": \"com.google.refine.preference.TopList\",\n" +
|
||||||
|
" \"top\": 100,\n" +
|
||||||
|
" \"list\": [\n" +
|
||||||
|
" \"grel:cell.recon.match.id\"\n" +
|
||||||
|
"]}");
|
||||||
|
|
||||||
|
String json = "{\n" +
|
||||||
|
" \"expressions\" : [ {\n" +
|
||||||
|
" \"code\" : \"grel:facetCount(value, 'value', 'Column 1')\",\n" +
|
||||||
|
" \"global\" : false,\n" +
|
||||||
|
" \"starred\" : false\n" +
|
||||||
|
" }, {\n" +
|
||||||
|
" \"code\" : \"grel:facetCount(value, 'value', 'Column 3')\",\n" +
|
||||||
|
" \"global\" : false,\n" +
|
||||||
|
" \"starred\" : false\n" +
|
||||||
|
" }, {\n" +
|
||||||
|
" \"code\" : \"grel:cell.recon.match.id\",\n" +
|
||||||
|
" \"global\" : false,\n" +
|
||||||
|
" \"starred\" : true\n" +
|
||||||
|
" } ]\n" +
|
||||||
|
" }";
|
||||||
|
command.doGet(request, response);
|
||||||
|
assertResponseJsonIs(json);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package com.google.refine.tests.commands.expr;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
|
||||||
|
import org.testng.annotations.BeforeMethod;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
|
||||||
|
import com.google.refine.commands.expr.GetExpressionLanguageInfoCommand;
|
||||||
|
import com.google.refine.tests.util.TestUtils;
|
||||||
|
import com.google.refine.util.ParsingUtilities;
|
||||||
|
|
||||||
|
public class GetExpressionLanguageInfoCommandTests extends ExpressionCommandTestBase {
|
||||||
|
|
||||||
|
@BeforeMethod
|
||||||
|
public void setUp() {
|
||||||
|
command = new GetExpressionLanguageInfoCommand();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testJsonResponse() throws ServletException, IOException {
|
||||||
|
|
||||||
|
initWorkspace(null, null);
|
||||||
|
|
||||||
|
command.doGet(request, response);
|
||||||
|
String jsonResponse = writer.toString();
|
||||||
|
JsonNode result = ParsingUtilities.mapper.readValue(jsonResponse, JsonNode.class);
|
||||||
|
TestUtils.assertEqualAsJson("{\n" +
|
||||||
|
" \"description\" : \"Evaluates expression a to an array. Then for each array element, binds its value to variable name v, evaluates expression test which should return a boolean. If the boolean is true, pushes v onto the result array.\",\n" +
|
||||||
|
" \"params\" : \"expression a, variable v, expression test\",\n" +
|
||||||
|
" \"returns\" : \"array\"\n" +
|
||||||
|
" }", result.get("controls").get("filter").toString());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
package com.google.refine.tests.commands.expr;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
|
||||||
|
import org.testng.annotations.BeforeMethod;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import com.google.refine.commands.expr.GetStarredExpressionsCommand;
|
||||||
|
|
||||||
|
public class GetStarredExpressionsCommandTests extends ExpressionCommandTestBase {
|
||||||
|
|
||||||
|
@BeforeMethod
|
||||||
|
public void setUp() {
|
||||||
|
command = new GetStarredExpressionsCommand();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testJsonResponse() throws ServletException, IOException {
|
||||||
|
|
||||||
|
initWorkspace("{\n" +
|
||||||
|
" \"class\": \"com.google.refine.preference.TopList\",\n" +
|
||||||
|
" \"top\": 100,\n" +
|
||||||
|
" \"list\": [\n" +
|
||||||
|
" \"grel:facetCount(value, 'value', 'Column 1')\",\n" +
|
||||||
|
" \"grel:facetCount(value, 'value', 'Column 3')\",\n" +
|
||||||
|
" \"grel:cell.recon.match.id\"" +
|
||||||
|
"]}", "{\n" +
|
||||||
|
" \"class\": \"com.google.refine.preference.TopList\",\n" +
|
||||||
|
" \"top\": 100,\n" +
|
||||||
|
" \"list\": [\n" +
|
||||||
|
" \"grel:cell.recon.match.id\"\n" +
|
||||||
|
"]}");
|
||||||
|
|
||||||
|
String json = "{\n" +
|
||||||
|
" \"expressions\" : [ {\n" +
|
||||||
|
" \"code\" : \"grel:cell.recon.match.id\"\n" +
|
||||||
|
" } ]\n" +
|
||||||
|
" }";
|
||||||
|
command.doGet(request, response);
|
||||||
|
assertResponseJsonIs(json);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,76 @@
|
|||||||
|
package com.google.refine.tests.commands.expr;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.io.StringWriter;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import org.testng.annotations.BeforeMethod;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import com.google.refine.commands.Command;
|
||||||
|
import com.google.refine.commands.expr.PreviewExpressionCommand;
|
||||||
|
import com.google.refine.model.Project;
|
||||||
|
import com.google.refine.tests.RefineTest;
|
||||||
|
import com.google.refine.tests.util.TestUtils;
|
||||||
|
|
||||||
|
public class PreviewExpressionCommandTests extends RefineTest {
|
||||||
|
protected Project project = null;
|
||||||
|
protected HttpServletRequest request = null;
|
||||||
|
protected HttpServletResponse response = null;
|
||||||
|
protected Command command = null;
|
||||||
|
protected StringWriter writer = null;
|
||||||
|
|
||||||
|
@BeforeMethod
|
||||||
|
public void setUpRequestResponse() {
|
||||||
|
request = mock(HttpServletRequest.class);
|
||||||
|
response = mock(HttpServletResponse.class);
|
||||||
|
writer = new StringWriter();
|
||||||
|
try {
|
||||||
|
when(response.getWriter()).thenReturn(new PrintWriter(writer));
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
command = new PreviewExpressionCommand();
|
||||||
|
project = createCSVProject("a,b\nc,d\ne,f\ng,h");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testJsonResponse() throws ServletException, IOException {
|
||||||
|
|
||||||
|
when(request.getParameter("project")).thenReturn(Long.toString(project.id));
|
||||||
|
when(request.getParameter("cellIndex")).thenReturn("1");
|
||||||
|
when(request.getParameter("expression")).thenReturn("grel:value + \"_u\"");
|
||||||
|
when(request.getParameter("rowIndices")).thenReturn("[0,2]");
|
||||||
|
|
||||||
|
String json = "{\n" +
|
||||||
|
" \"code\" : \"ok\",\n" +
|
||||||
|
" \"results\" : [ \"d_u\", \"h_u\" ]\n" +
|
||||||
|
" }";
|
||||||
|
command.doPost(request, response);
|
||||||
|
TestUtils.assertEqualAsJson(json, writer.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParseError() throws ServletException, IOException {
|
||||||
|
|
||||||
|
when(request.getParameter("project")).thenReturn(Long.toString(project.id));
|
||||||
|
when(request.getParameter("cellIndex")).thenReturn("1");
|
||||||
|
when(request.getParameter("expression")).thenReturn("grel:value +");
|
||||||
|
when(request.getParameter("rowIndices")).thenReturn("[0,2]");
|
||||||
|
|
||||||
|
String json = "{\n" +
|
||||||
|
" \"code\" : \"error\",\n" +
|
||||||
|
" \"message\" : \"Parsing error at offset 7: Expecting something more at end of expression\",\n" +
|
||||||
|
" \"type\" : \"parser\"\n" +
|
||||||
|
" }";
|
||||||
|
command.doPost(request, response);
|
||||||
|
TestUtils.assertEqualAsJson(json, writer.toString());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
package com.google.refine.tests.commands.expr;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
|
||||||
|
import org.testng.annotations.BeforeMethod;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import com.google.refine.commands.expr.ToggleStarredExpressionCommand;
|
||||||
|
|
||||||
|
public class ToggleStarredExpressionCommandTests extends ExpressionCommandTestBase {
|
||||||
|
|
||||||
|
@BeforeMethod
|
||||||
|
public void setUp() {
|
||||||
|
command = new ToggleStarredExpressionCommand();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testJsonResponse() throws ServletException, IOException {
|
||||||
|
|
||||||
|
initWorkspace("{\n" +
|
||||||
|
" \"class\": \"com.google.refine.preference.TopList\",\n" +
|
||||||
|
" \"top\": 100,\n" +
|
||||||
|
" \"list\": [\n" +
|
||||||
|
" \"grel:facetCount(value, 'value', 'Column 1')\",\n" +
|
||||||
|
" \"grel:facetCount(value, 'value', 'Column 3')\",\n" +
|
||||||
|
" \"grel:cell.recon.match.id\"" +
|
||||||
|
"]}", "{\n" +
|
||||||
|
" \"class\": \"com.google.refine.preference.TopList\",\n" +
|
||||||
|
" \"top\": 100,\n" +
|
||||||
|
" \"list\": [\n" +
|
||||||
|
" \"grel:cell.recon.match.id\"\n" +
|
||||||
|
"]}");
|
||||||
|
|
||||||
|
String json = "{\n" +
|
||||||
|
" \"expressions\" : [ {\n" +
|
||||||
|
" \"code\" : \"grel:facetCount(value, 'value', 'Column 1')\"\n" +
|
||||||
|
" }, {\n" +
|
||||||
|
" \"code\" : \"grel:cell.recon.match.id\"\n" +
|
||||||
|
" } ]\n" +
|
||||||
|
" }";
|
||||||
|
when(request.getParameter("expression")).thenReturn("grel:facetCount(value, 'value', 'Column 1')");
|
||||||
|
when(request.getParameter("returnList")).thenReturn("yes");
|
||||||
|
command.doPost(request, response);
|
||||||
|
assertResponseJsonIs(json);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user