From 5b9219fbdde8a630d8f3f03549101443b182f4af Mon Sep 17 00:00:00 2001 From: cgoldfeder Date: Tue, 13 Dec 2016 14:22:48 -0800 Subject: [PATCH] Add filter support to the test server The lack of ObjectifyFilter means that in any tests using RegistryTestServer the Objectify session cache persists between "requests" in the same test method. This is wrong but had not caused any failures because we didn't assert anything that mattered. However, a CL I'm working on asserts that there is a creationTime on a created resource, which is set automatically on Datastore save, and therefore is still null in the session cache's version of the resource if you don't clear it before the next command. Fixing it here separately from that CL. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=141939330 --- java/google/registry/util/NetworkUtils.java | 6 +++--- javatests/google/registry/server/BUILD | 3 ++- .../registry/server/RegistryTestServer.java | 11 +++++++++-- .../google/registry/server/TestServer.java | 18 +++++++++++++++--- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/java/google/registry/util/NetworkUtils.java b/java/google/registry/util/NetworkUtils.java index d62c2aa6f..3048b7227 100644 --- a/java/google/registry/util/NetworkUtils.java +++ b/java/google/registry/util/NetworkUtils.java @@ -46,9 +46,9 @@ public final class NetworkUtils { /** * Returns random unused local port that can be used for TCP listening server. * - * @throws IOException if failed to find free port after {@value #PICK_ATTEMPTS} attempts + * @throws RuntimeException if failed to find free port after {@value #PICK_ATTEMPTS} attempts */ - public static int pickUnusedPort() throws IOException { + public static int pickUnusedPort() { // In an ideal world, we would just listen on port 0 and use whatever random port the kernel // assigns us. But our CI testing system reports there are rare circumstances in which this // doesn't work. @@ -58,7 +58,7 @@ public final class NetworkUtils { return serverSocket.getLocalPort(); } catch (IOException e) { if (!ports.hasNext()) { - throw new IOException("Failed to acquire random port", e); + throw new RuntimeException("Failed to acquire random port", e); } } } diff --git a/javatests/google/registry/server/BUILD b/javatests/google/registry/server/BUILD index 1e4844281..de36c9b80 100644 --- a/javatests/google/registry/server/BUILD +++ b/javatests/google/registry/server/BUILD @@ -54,10 +54,11 @@ java_library( ":TestServer", "//java/com/google/common/collect", "//java/com/google/common/net", + "//java/google/registry/model", "//java/google/registry/module/backend", "//java/google/registry/module/frontend", - "//java/google/registry/ui/server/registrar", "//third_party/java/jsr305_annotations", + "//third_party/java/objectify/v4_1:v4_1_3", "//third_party/java/servlet/servlet_api", ], ) diff --git a/javatests/google/registry/server/RegistryTestServer.java b/javatests/google/registry/server/RegistryTestServer.java index e487db87b..ccdf44da7 100644 --- a/javatests/google/registry/server/RegistryTestServer.java +++ b/javatests/google/registry/server/RegistryTestServer.java @@ -19,9 +19,12 @@ import static google.registry.server.Route.route; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.net.HostAndPort; +import com.googlecode.objectify.ObjectifyFilter; +import google.registry.model.ofy.OfyFilter; import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; +import javax.servlet.Filter; /** Lightweight HTTP server for testing the Nomulus Admin and Registrar consoles. */ public final class RegistryTestServer { @@ -90,11 +93,15 @@ public final class RegistryTestServer { route("/registrar-payment-setup", google.registry.module.frontend.FrontendServlet.class)); + private static final ImmutableList> FILTERS = ImmutableList.of( + ObjectifyFilter.class, + OfyFilter.class); + private final TestServer server; - /** @see TestServer#TestServer(HostAndPort, java.util.Map, Iterable) */ + /** @see TestServer#TestServer(HostAndPort, java.util.Map, Iterable, Iterable) */ public RegistryTestServer(HostAndPort address) { - server = new TestServer(address, RUNFILES, ROUTES); + server = new TestServer(address, RUNFILES, ROUTES, FILTERS); } /** @see TestServer#start() */ diff --git a/javatests/google/registry/server/TestServer.java b/javatests/google/registry/server/TestServer.java index a5e64ea51..f5f8c6784 100644 --- a/javatests/google/registry/server/TestServer.java +++ b/javatests/google/registry/server/TestServer.java @@ -31,8 +31,10 @@ import java.util.concurrent.FutureTask; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.TimeUnit; import javax.annotation.Nullable; +import javax.servlet.Filter; import javax.servlet.http.HttpServlet; import org.mortbay.jetty.Connector; +import org.mortbay.jetty.Handler; import org.mortbay.jetty.Server; import org.mortbay.jetty.bio.SocketConnector; import org.mortbay.jetty.servlet.Context; @@ -80,10 +82,14 @@ public final class TestServer { * @param runfiles map of server paths to local directories or files, to be served statically * @param routes list of servlet endpoints */ - public TestServer(HostAndPort address, Map runfiles, Iterable routes) { + public TestServer( + HostAndPort address, + Map runfiles, + Iterable routes, + Iterable> filters) { urlAddress = createUrlAddress(address); server.addConnector(createConnector(address)); - server.addHandler(createHandler(runfiles, routes)); + server.addHandler(createHandler(runfiles, routes, filters)); } /** Starts the HTTP server in a new thread and returns once it's online. */ @@ -143,7 +149,10 @@ public final class TestServer { } } - private Context createHandler(Map runfiles, Iterable routes) { + private Context createHandler( + Map runfiles, + Iterable routes, + Iterable> filters) { Context context = new Context(server, CONTEXT_PATH, Context.SESSIONS); context.addServlet(new ServletHolder(HealthzServlet.class), "/healthz"); for (Map.Entry runfile : runfiles.entrySet()) { @@ -154,6 +163,9 @@ public final class TestServer { for (Route route : routes) { context.addServlet(new ServletHolder(wrapServlet(route.servletClass())), route.path()); } + for (Class filter : filters) { + context.addFilter(filter, "/*", Handler.REQUEST); + } ServletHolder holder = new ServletHolder(DefaultServlet.class); holder.setInitParameter("aliases", "1"); context.addServlet(holder, "/*");