assertEqualAsJson test helper refactor (#3113)
* Refactor test helper Create a version of the assert that uses the standard parameter order and deprecate the version that uses inverted order. * Use consistent Assert class and parameter ordering
This commit is contained in:
parent
42e840dec6
commit
259705ad5f
@ -36,6 +36,6 @@ public class CommandTestBase {
|
||||
* Convenience method to check that CSRF protection was triggered
|
||||
*/
|
||||
protected void assertCSRFCheckFailed() {
|
||||
TestUtils.assertEqualAsJson("{\"code\":\"error\",\"message\":\"Missing or invalid csrf_token parameter\"}", writer.toString());
|
||||
TestUtils.assertEqualsAsJson(writer.toString(), "{\"code\":\"error\",\"message\":\"Missing or invalid csrf_token parameter\"}");
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ package com.google.refine.commands.cell;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.testng.Assert.assertEquals;
|
||||
import static org.testng.AssertJUnit.assertTrue;
|
||||
import static org.testng.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
@ -59,10 +59,10 @@ public class EditOneCellCommandTests extends RefineTest {
|
||||
|
||||
command.doPost(request, response);
|
||||
|
||||
assertEquals("a", project.rows.get(0).cells.get(0).value);
|
||||
assertEquals("b", project.rows.get(0).cells.get(1).value);
|
||||
assertEquals("e", project.rows.get(1).cells.get(0).value);
|
||||
assertEquals("d", project.rows.get(1).cells.get(1).value);
|
||||
assertEquals(project.rows.get(0).cells.get(0).value, "a");
|
||||
assertEquals(project.rows.get(0).cells.get(1).value, "b");
|
||||
assertEquals(project.rows.get(1).cells.get(0).value, "e");
|
||||
assertEquals(project.rows.get(1).cells.get(1).value, "d");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -76,11 +76,11 @@ public class EditOneCellCommandTests extends RefineTest {
|
||||
|
||||
command.doPost(request, response);
|
||||
|
||||
assertEquals("a", project.rows.get(0).cells.get(0).value);
|
||||
assertEquals("b", project.rows.get(0).cells.get(1).value);
|
||||
assertEquals(project.rows.get(0).cells.get(0).value, "a");
|
||||
assertEquals(project.rows.get(0).cells.get(1).value, "b");
|
||||
assertTrue(project.rows.get(1).cells.get(0).value instanceof Long);
|
||||
assertEquals(new Long(12345), project.rows.get(1).cells.get(0).value);
|
||||
assertEquals("d", project.rows.get(1).cells.get(1).value);
|
||||
assertEquals(project.rows.get(1).cells.get(1).value, "d");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -94,11 +94,11 @@ public class EditOneCellCommandTests extends RefineTest {
|
||||
|
||||
command.doPost(request, response);
|
||||
|
||||
assertEquals("a", project.rows.get(0).cells.get(0).value);
|
||||
assertEquals("b", project.rows.get(0).cells.get(1).value);
|
||||
assertEquals(project.rows.get(0).cells.get(0).value, "a");
|
||||
assertEquals(project.rows.get(0).cells.get(1).value, "b");
|
||||
assertTrue(project.rows.get(1).cells.get(0).value instanceof Double);
|
||||
assertEquals(12345.123, project.rows.get(1).cells.get(0).value);
|
||||
assertEquals("d", project.rows.get(1).cells.get(1).value);
|
||||
assertEquals(project.rows.get(1).cells.get(0).value, 12345.123);
|
||||
assertEquals(project.rows.get(1).cells.get(1).value, "d");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -111,7 +111,7 @@ public class EditOneCellCommandTests extends RefineTest {
|
||||
|
||||
command.doPost(request, response);
|
||||
|
||||
assertEquals("c", project.rows.get(1).cells.get(0).value);
|
||||
assertEquals(project.rows.get(1).cells.get(0).value, "c");
|
||||
TestUtils.assertEqualAsJson("{\"code\":\"error\",\"message\":\"Missing or invalid csrf_token parameter\"}", writer.toString());
|
||||
}
|
||||
}
|
||||
|
@ -94,7 +94,7 @@ public class ExpressionCommandTestBase {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
TestUtils.assertEqualAsJson(expectedJson, actualJson);
|
||||
TestUtils.assertEqualsAsJson(actualJson, expectedJson);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -34,7 +34,6 @@ 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.util.ParsingUtilities;
|
||||
import com.google.refine.util.TestUtils;
|
||||
|
||||
@ -53,10 +52,11 @@ public class GetExpressionLanguageInfoCommandTests extends ExpressionCommandTest
|
||||
command.doGet(request, response);
|
||||
String jsonResponse = writer.toString();
|
||||
JsonNode result = ParsingUtilities.mapper.readValue(jsonResponse, JsonNode.class);
|
||||
TestUtils.assertEqualAsJson("{\n" +
|
||||
TestUtils.assertEqualsAsJson(result.get("controls").get("filter").toString(),
|
||||
"{\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());
|
||||
" }");
|
||||
}
|
||||
}
|
||||
|
@ -42,7 +42,6 @@ import org.testng.annotations.Test;
|
||||
|
||||
import com.google.refine.RefineTest;
|
||||
import com.google.refine.commands.Command;
|
||||
import com.google.refine.commands.expr.PreviewExpressionCommand;
|
||||
import com.google.refine.model.Project;
|
||||
import com.google.refine.util.TestUtils;
|
||||
|
||||
@ -80,7 +79,7 @@ public class PreviewExpressionCommandTests extends RefineTest {
|
||||
" \"results\" : [ \"d_u\", \"h_u\" ]\n" +
|
||||
" }";
|
||||
command.doPost(request, response);
|
||||
TestUtils.assertEqualAsJson(json, writer.toString());
|
||||
TestUtils.assertEqualsAsJson(writer.toString(), json);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -97,6 +96,6 @@ public class PreviewExpressionCommandTests extends RefineTest {
|
||||
" \"type\" : \"parser\"\n" +
|
||||
" }";
|
||||
command.doPost(request, response);
|
||||
TestUtils.assertEqualAsJson(json, writer.toString());
|
||||
TestUtils.assertEqualsAsJson(writer.toString(), json);
|
||||
}
|
||||
}
|
||||
|
@ -36,7 +36,6 @@ import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.refine.commands.Command;
|
||||
import com.google.refine.commands.expr.ToggleStarredExpressionCommand;
|
||||
import com.google.refine.util.TestUtils;
|
||||
|
||||
public class ToggleStarredExpressionCommandTests extends ExpressionCommandTestBase {
|
||||
@ -77,9 +76,9 @@ public class ToggleStarredExpressionCommandTests extends ExpressionCommandTestBa
|
||||
assertResponseJsonIs(json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCSRFProtection() throws ServletException, IOException {
|
||||
command.doPost(request, response);
|
||||
TestUtils.assertEqualAsJson("{\"code\":\"error\",\"message\":\"Missing or invalid csrf_token parameter\"}", writer.toString());
|
||||
}
|
||||
@Test
|
||||
public void testCSRFProtection() throws ServletException, IOException {
|
||||
command.doPost(request, response);
|
||||
TestUtils.assertEqualsAsJson(writer.toString(), "{\"code\":\"error\",\"message\":\"Missing or invalid csrf_token parameter\"}");
|
||||
}
|
||||
}
|
||||
|
@ -42,7 +42,6 @@ import org.testng.annotations.Test;
|
||||
|
||||
import com.google.refine.RefineTest;
|
||||
import com.google.refine.commands.Command;
|
||||
import com.google.refine.commands.row.GetRowsCommand;
|
||||
import com.google.refine.model.Project;
|
||||
import com.google.refine.util.TestUtils;
|
||||
|
||||
@ -101,7 +100,7 @@ public class GetRowsCommandTest extends RefineTest {
|
||||
|
||||
when(request.getParameter("engine")).thenReturn("{\"mode\":\"row-based\",\"facets\":[]}");
|
||||
command.doPost(request, response);
|
||||
TestUtils.assertEqualAsJson(rowJson, writer.toString());
|
||||
TestUtils.assertEqualsAsJson(writer.toString(), rowJson);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -137,6 +136,6 @@ public class GetRowsCommandTest extends RefineTest {
|
||||
|
||||
when(request.getParameter("engine")).thenReturn("{\"mode\":\"record-based\",\"facets\":[]}");
|
||||
command.doPost(request, response);
|
||||
TestUtils.assertEqualAsJson(recordJson, writer.toString());
|
||||
TestUtils.assertEqualsAsJson(writer.toString(), recordJson);
|
||||
}
|
||||
}
|
||||
|
@ -192,7 +192,7 @@ public class CancelProcessesCommandTests extends RefineTest {
|
||||
} catch (IOException e) {
|
||||
Assert.fail();
|
||||
}
|
||||
TestUtils.assertEqualAsJson("{ \"code\" : \"ok\" }", sw.toString());
|
||||
TestUtils.assertEqualsAsJson(sw.toString(), "{ \"code\" : \"ok\" }");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -1,7 +1,7 @@
|
||||
package com.google.refine.importing;
|
||||
|
||||
import static org.testng.AssertJUnit.assertEquals;
|
||||
import static org.testng.AssertJUnit.assertNotNull;
|
||||
import static org.testng.Assert.assertEquals;
|
||||
import static org.testng.Assert.assertNotNull;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
|
@ -47,10 +47,7 @@ import org.testng.annotations.BeforeTest;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.refine.RefineTest;
|
||||
import com.google.refine.model.Cell;
|
||||
import com.google.refine.model.Project;
|
||||
import com.google.refine.model.Recon;
|
||||
import com.google.refine.model.Row;
|
||||
|
||||
import com.google.refine.util.Pool;
|
||||
import com.google.refine.util.TestUtils;
|
||||
|
||||
@ -107,8 +104,8 @@ public class RowTests extends RefineTest {
|
||||
Row row = new Row(5);
|
||||
row.setCell(0, new Cell("I'm not empty", null));
|
||||
row.save(writer, options);
|
||||
TestUtils.assertEqualAsJson(writer.getBuffer().toString(),
|
||||
"{\"flagged\":false,\"starred\":false,\"cells\":[{\"v\":\"I'm not empty\"}]}");
|
||||
TestUtils.assertEqualsAsJson("{\"flagged\":false,\"starred\":false,\"cells\":[{\"v\":\"I'm not empty\"}]}",
|
||||
writer.getBuffer().toString());
|
||||
}
|
||||
|
||||
// This way of serializing a row with indices is now deprecated, see GetRowsCommand.
|
||||
|
@ -26,7 +26,7 @@
|
||||
******************************************************************************/
|
||||
package com.google.refine.model.changes;
|
||||
|
||||
import static org.testng.AssertJUnit.assertTrue;
|
||||
import static org.testng.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
@ -53,9 +53,6 @@ import com.google.refine.model.Project;
|
||||
import com.google.refine.model.Recon;
|
||||
import com.google.refine.model.ReconCandidate;
|
||||
import com.google.refine.model.Row;
|
||||
import com.google.refine.model.recon.ReconConfig;
|
||||
import com.google.refine.model.recon.ReconJob;
|
||||
import com.google.refine.model.recon.StandardReconConfig;
|
||||
import com.google.refine.model.recon.StandardReconConfig.ColumnDetail;
|
||||
import com.google.refine.model.recon.StandardReconConfig.ReconResult;
|
||||
import com.google.refine.operations.OperationRegistry;
|
||||
@ -206,13 +203,13 @@ public class StandardReconConfigTests extends RefineTest {
|
||||
StandardReconConfig r = StandardReconConfig.reconstruct(config);
|
||||
Row row = project.rows.get(0);
|
||||
ReconJob job = r.createJob(project, 0, row, "title", row.getCell(0));
|
||||
TestUtils.assertEqualAsJson("{"
|
||||
TestUtils.assertEqualsAsJson( job.toString(), "{"
|
||||
+ "\"query\":\"mulholland drive\","
|
||||
+ "\"type\":\"Q1234\","
|
||||
+ "\"properties\":["
|
||||
+ " {\"pid\":\"P123\",\"v\":\"david lynch\"}"
|
||||
+ "],"
|
||||
+ "\"type_strict\":\"should\"}", job.toString());
|
||||
+ "\"type_strict\":\"should\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -40,6 +40,7 @@ import static org.powermock.api.mockito.PowerMockito.mockStatic;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
@ -198,7 +199,7 @@ public class ExtendDataOperationTests extends RefineTest {
|
||||
Set<String> ids = Collections.singleton("Q2");
|
||||
String json = "{\"ids\":[\"Q2\"],\"properties\":[{\"id\":\"P571\"},{\"id\":\"P159\"},{\"id\":\"P625\"}]}";
|
||||
ReconciledDataExtensionJobStub stub = new ReconciledDataExtensionJobStub(config, "http://endpoint");
|
||||
TestUtils.assertEqualAsJson(json, stub.formulateQueryStub(ids, config));
|
||||
TestUtils.assertEqualsAsJson(stub.formulateQueryStub(ids, config), json);
|
||||
}
|
||||
|
||||
|
||||
@ -404,17 +405,17 @@ public class ExtendDataOperationTests extends RefineTest {
|
||||
// Make sure all the values are reconciled
|
||||
Assert.assertTrue(project.columnModel.getColumnByName("currency").getReconStats().matchedTopics == 5);
|
||||
}
|
||||
|
||||
|
||||
private void mockHttpCall(String query, String response) throws IOException {
|
||||
mockedResponses.put(ParsingUtilities.mapper.readTree(query), response);
|
||||
mockedResponses.put(ParsingUtilities.mapper.readTree(query), response);
|
||||
}
|
||||
|
||||
|
||||
InputStream fakeHttpCall(String endpoint, String query) throws IOException {
|
||||
JsonNode parsedQuery = ParsingUtilities.mapper.readTree(query);
|
||||
if (mockedResponses.containsKey(parsedQuery)) {
|
||||
return IOUtils.toInputStream(mockedResponses.get(parsedQuery));
|
||||
} else {
|
||||
throw new IllegalArgumentException("HTTP call not mocked for query: "+query);
|
||||
}
|
||||
JsonNode parsedQuery = ParsingUtilities.mapper.readTree(query);
|
||||
if (mockedResponses.containsKey(parsedQuery)) {
|
||||
return IOUtils.toInputStream(mockedResponses.get(parsedQuery), StandardCharsets.UTF_8);
|
||||
} else {
|
||||
throw new IllegalArgumentException("HTTP call not mocked for query: "+query);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -72,11 +72,22 @@ public class TestUtils {
|
||||
dir.mkdir();
|
||||
return dir;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Compare two JSON strings for equality.
|
||||
* Assert that two JSON strings are equal as JSON objects.
|
||||
*
|
||||
* @deprecated for 3.5 by Tom Morris Use the method with the same parameter
|
||||
* order as the rest of the assert
|
||||
* methods{@link #assertEqualsAsJson(String, String)}
|
||||
*/
|
||||
public static void assertEqualAsJson(String expected, String actual) {
|
||||
assertEqualsAsJson(actual, expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that two JSON strings are equal as JSON objects.
|
||||
*/
|
||||
public static void assertEqualsAsJson(String actual, String expected) {
|
||||
try {
|
||||
JsonNode jsonA = mapper.readValue(expected, JsonNode.class);
|
||||
JsonNode jsonB = mapper.readValue(actual, JsonNode.class);
|
||||
@ -85,20 +96,20 @@ public class TestUtils {
|
||||
fail("Objects above are not equal as JSON strings.");
|
||||
}
|
||||
} catch(Exception e) {
|
||||
fail("\""+expected+"\" and \""+actual+"\" are not equal as JSON strings.");
|
||||
fail("\""+actual+"\" and \""+expected+"\" are not equal as JSON strings.");
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean equalAsJson(String expected, String actual) {
|
||||
public static boolean equalAsJson(String a, String b) {
|
||||
try {
|
||||
JsonNode jsonA = mapper.readValue(expected, JsonNode.class);
|
||||
JsonNode jsonB = mapper.readValue(actual, JsonNode.class);
|
||||
JsonNode jsonA = mapper.readValue(a, JsonNode.class);
|
||||
JsonNode jsonB = mapper.readValue(b, JsonNode.class);
|
||||
return (jsonA == null && jsonB == null) || jsonA.equals(jsonB);
|
||||
} catch(Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks that a serializable object is serialized to the target JSON string.
|
||||
* @throws IOException
|
||||
@ -114,7 +125,7 @@ public class TestUtils {
|
||||
writer = ParsingUtilities.defaultWriter;
|
||||
}
|
||||
String jacksonJson = writer.writeValueAsString(o);
|
||||
assertEqualAsJson(targetJson, jacksonJson);
|
||||
assertEqualsAsJson(jacksonJson, targetJson);
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
fail("jackson serialization failed");
|
||||
|
Loading…
Reference in New Issue
Block a user