diff --git a/javatests/google/registry/rdap/AbstractJsonableObjectTest.java b/javatests/google/registry/rdap/AbstractJsonableObjectTest.java index f6271086f..bbd1a04bf 100644 --- a/javatests/google/registry/rdap/AbstractJsonableObjectTest.java +++ b/javatests/google/registry/rdap/AbstractJsonableObjectTest.java @@ -20,7 +20,6 @@ import static google.registry.testing.JUnitBackports.assertThrows; import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonPrimitive; import google.registry.rdap.AbstractJsonableObject.JsonableException; @@ -34,7 +33,7 @@ import org.junit.runners.JUnit4; @RunWith(JUnit4.class) public final class AbstractJsonableObjectTest { - private final Gson gson = new GsonBuilder().create(); + private final Gson gson = new Gson(); private JsonElement createJson(String... lines) { return gson.fromJson(Joiner.on("\n").join(lines), JsonElement.class); diff --git a/javatests/google/registry/rdap/RdapActionBaseTest.java b/javatests/google/registry/rdap/RdapActionBaseTest.java index 878124718..f42764f98 100644 --- a/javatests/google/registry/rdap/RdapActionBaseTest.java +++ b/javatests/google/registry/rdap/RdapActionBaseTest.java @@ -30,7 +30,6 @@ import google.registry.rdap.RdapSearchResults.IncompletenessWarningType; import google.registry.request.Action; import google.registry.request.auth.Auth; import java.util.Optional; -import org.json.simple.JSONValue; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -168,7 +167,7 @@ public class RdapActionBaseTest extends RdapActionBaseTestCase { protected final String actionPath; protected final Class rdapActionClass; + private static final Gson GSON = new Gson(); + protected RdapActionBaseTestCase(Class rdapActionClass) { this.rdapActionClass = rdapActionClass; this.actionPath = Actions.getPathForAction(rdapActionClass); @@ -117,11 +119,15 @@ public class RdapActionBaseTestCase { metricRole = ADMINISTRATOR; } - protected JSONObject generateActualJson(String domainName) { + protected static JsonObject parseJsonObject(String jsonString) { + return GSON.fromJson(jsonString, JsonObject.class); + } + + protected JsonObject generateActualJson(String domainName) { action.requestPath = actionPath + domainName; action.requestMethod = GET; action.run(); - return (JSONObject) JSONValue.parse(response.getPayload()); + return parseJsonObject(response.getPayload()); } protected String generateHeadPayload(String domainName) { @@ -141,7 +147,7 @@ public class RdapActionBaseTestCase { * @param keysAndValues alternating substitution key and value. The substitutions are applied to * the file before parsing it to JSON. */ - protected JSONObject loadJsonFile(String filename, String... keysAndValues) { + protected static JsonObject loadJsonFile(String filename, String... keysAndValues) { checkArgument(keysAndValues.length % 2 == 0); ImmutableMap.Builder builder = new ImmutableMap.Builder<>(); for (int i = 0; i < keysAndValues.length; i += 2) { @@ -159,11 +165,11 @@ public class RdapActionBaseTestCase { * @param substitutions map of substitutions to apply to the file. The substitutions are applied * to the file before parsing it to JSON. */ - protected JSONObject loadJsonFile(String filename, Map substitutions) { - return (JSONObject) JSONValue.parse(loadFile(this.getClass(), filename, substitutions)); + protected static JsonObject loadJsonFile(String filename, Map substitutions) { + return parseJsonObject(loadFile(RdapActionBaseTestCase.class, filename, substitutions)); } - protected JSONObject generateExpectedJsonError( + protected JsonObject generateExpectedJsonError( String description, int code) { String title; diff --git a/javatests/google/registry/rdap/RdapDomainActionTest.java b/javatests/google/registry/rdap/RdapDomainActionTest.java index d2e98d0d9..3fb688de9 100644 --- a/javatests/google/registry/rdap/RdapDomainActionTest.java +++ b/javatests/google/registry/rdap/RdapDomainActionTest.java @@ -28,7 +28,7 @@ import static org.mockito.Mockito.verify; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; +import com.google.gson.JsonObject; import google.registry.model.contact.ContactResource; import google.registry.model.domain.DomainBase; import google.registry.model.domain.Period; @@ -42,10 +42,8 @@ import google.registry.rdap.RdapMetrics.WildcardType; import google.registry.rdap.RdapSearchResults.IncompletenessWarningType; import google.registry.request.Action; import java.util.List; -import java.util.Map; import java.util.Optional; import javax.annotation.Nullable; -import org.json.simple.JSONObject; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; @@ -228,7 +226,7 @@ public class RdapDomainActionTest extends RdapActionBaseTestCase nameserverNames, @Nullable String registrarName, String expectedOutputFile) { - JSONObject obj = + JsonObject obj = generateExpectedJson( expectedOutputFile, name, @@ -311,59 +309,43 @@ public class RdapDomainActionTest extends RdapActionBaseTestCase map = (Map) obj; - ImmutableMap.Builder builder = - RdapTestHelper.getBuilderExcluding(map, ImmutableSet.of("notices")); - RdapTestHelper.addDomainBoilerplateNotices( - builder, RdapTestHelper.createNotices("https://example.tld/rdap/", map.get("notices"))); - obj = new JSONObject(builder.build()); + RdapTestHelper.addDomainBoilerplateNotices(obj, "https://example.tld/rdap/"); return obj; } - private void assertJsonEqual(Object json1, Object json2) { - if (json1 instanceof Map) { - @SuppressWarnings("unchecked") - Map map = (Map) json1; - assertThat(map).isEqualTo(json2); - } else { - assertThat(json1).isEqualTo(json2); - } - } - private void assertProperResponseForCatLol(String queryString, String expectedOutputFile) { - assertJsonEqual( - generateActualJson(queryString), - generateExpectedJsonWithTopLevelEntries( - "cat.lol", - null, - "C-LOL", - ImmutableList.of("4-ROID", "6-ROID", "2-ROID"), - ImmutableList.of("8-ROID", "A-ROID"), - "Yes Virginia