From c914f6ab9e608bd821bc0c571d2e0cd59d1abd64 Mon Sep 17 00:00:00 2001 From: Ben McIlwain Date: Wed, 18 Nov 2026 19:19:33 -0500 Subject: [PATCH] Add test that all @Action classes are routed Add RequestComponentTest.testAllProductionActionsAreRouted, which scans the classpath with Guava ClassPath for every concrete @Action class compiled from the same production code source as RequestComponent (excluding @Action classes defined in tests), and compares it against the @Action classes exposed by RequestComponent's zero-argument Runnable factory methods (the same methods Router turns into routes). Any unrouted production action must be listed in KNOWN_UNROUTED_ACTIONS. The test fails in both directions with actionable messages: a new @Action that is not registered in RequestComponent (and routing.txt), or a stale allowlist entry for an action that has since been wired up or deleted. Sanity checks ensure the scan finds known actions and that every routed action is a discovered production action. CheckBulkComplianceAction is currently the only unrouted production @Action; it is allowlisted with a TODO pointing at its tracking bug. BUG= http://b/587437992 --- .../registry/module/RequestComponentTest.java | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/core/src/test/java/google/registry/module/RequestComponentTest.java b/core/src/test/java/google/registry/module/RequestComponentTest.java index f0f9aaed026..3057273dbba 100644 --- a/core/src/test/java/google/registry/module/RequestComponentTest.java +++ b/core/src/test/java/google/registry/module/RequestComponentTest.java @@ -14,12 +14,26 @@ package google.registry.module; +import static com.google.common.collect.ImmutableSet.toImmutableSet; import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth.assertWithMessage; import com.google.common.base.Splitter; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Sets; +import com.google.common.reflect.ClassPath; +import com.google.common.reflect.ClassPath.ClassInfo; +import google.registry.batch.CheckBulkComplianceAction; +import google.registry.flows.EppTlsAction; +import google.registry.request.Action; import google.registry.testing.GoldenFileTestHelper; import google.registry.testing.TestDataHelper; +import java.io.IOException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.security.CodeSource; +import java.util.Arrays; import java.util.Set; import java.util.stream.Collectors; import org.junit.jupiter.api.Test; @@ -27,6 +41,18 @@ /** Unit tests for {@link RequestComponent}. */ public class RequestComponentTest { + /** + * Production {@link Action} classes that are intentionally not (yet) wired into {@link + * RequestComponent}, and are therefore unreachable at runtime. + * + *

Do not add new entries here without a tracking bug explaining why the action is unrouted. + */ + private static final ImmutableSet> KNOWN_UNROUTED_ACTIONS = + ImmutableSet.of( + // TODO(b/261763205): Wire this into RequestComponent (and schedule it) once bulk pricing + // packages launch, or delete it if bulk pricing is retired. + CheckBulkComplianceAction.class); + @Test void testRoutingMap() { GoldenFileTestHelper.assertThatRoutesFromComponent(RequestComponent.class) @@ -34,6 +60,60 @@ void testRoutingMap() { .isEqualToGolden(RequestComponentTest.class, "routing.txt"); } + @Test + void testAllProductionActionsAreRouted() throws IOException { + ImmutableSet> allProductionActions = getAllProductionActions(); + ImmutableSet> routedActions = getRoutedActions(); + + // Sanity checks guarding against the classpath scan silently finding nothing. + assertWithMessage("Production @Action classes discovered on the classpath") + .that(allProductionActions) + .containsAtLeast(CheckBulkComplianceAction.class, EppTlsAction.class); + assertWithMessage("@Action classes routed by RequestComponent") + .that(allProductionActions) + .containsAtLeastElementsIn(routedActions); + + Set> unroutedActions = Sets.difference(allProductionActions, routedActions); + assertWithMessage( + "These production @Action classes are not wired into RequestComponent, so they are" + + " unreachable. Register them in RequestComponent (and update routing.txt)") + .that(Sets.difference(unroutedActions, KNOWN_UNROUTED_ACTIONS)) + .isEmpty(); + assertWithMessage( + "These KNOWN_UNROUTED_ACTIONS entries are now routed or no longer exist. Remove them" + + " from KNOWN_UNROUTED_ACTIONS") + .that(Sets.difference(KNOWN_UNROUTED_ACTIONS, unroutedActions)) + .isEmpty(); + } + + /** + * Returns all concrete {@link Action} classes compiled from the same (production) source set as + * {@link RequestComponent}, which excludes {@link Action} classes defined in tests. + */ + private static ImmutableSet> getAllProductionActions() throws IOException { + CodeSource productionCodeSource = RequestComponent.class.getProtectionDomain().getCodeSource(); + return ClassPath.from(RequestComponent.class.getClassLoader()).getAllClasses().stream() + .filter(classInfo -> classInfo.getName().startsWith("google.registry.")) + .map(ClassInfo::load) + .filter(clazz -> clazz.isAnnotationPresent(Action.class)) + .filter(clazz -> !clazz.isInterface() && !Modifier.isAbstract(clazz.getModifiers())) + .filter(clazz -> productionCodeSource.equals(clazz.getProtectionDomain().getCodeSource())) + .collect(toImmutableSet()); + } + + /** + * Returns the {@link Action} classes provided by {@link RequestComponent}, mirroring how {@code + * Router} discovers routes from a component. + */ + private static ImmutableSet> getRoutedActions() { + return Arrays.stream(RequestComponent.class.getMethods()) + .filter(method -> method.getParameterCount() == 0) + .map(Method::getReturnType) + .filter(Runnable.class::isAssignableFrom) + .filter(returnType -> returnType.isAnnotationPresent(Action.class)) + .collect(toImmutableSet()); + } + private Set getRoutes(Class context, String filename) { return TestDataHelper.loadFile(context, filename) .trim()