From 7537bb5f9e31f867daf41dfd412f25d401dae9e6 Mon Sep 17 00:00:00 2001 From: Xu Yang Date: Tue, 26 Nov 2024 15:30:22 -0800 Subject: [PATCH] update with default handler constructor --- .../shared/vertx/RequestCapturingHandler.java | 8 +++- .../vertx/RequestCapturingHandlerTest.java | 42 ++++++++++++++----- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/uid2/shared/vertx/RequestCapturingHandler.java b/src/main/java/com/uid2/shared/vertx/RequestCapturingHandler.java index 94e946bb..43422d06 100644 --- a/src/main/java/com/uid2/shared/vertx/RequestCapturingHandler.java +++ b/src/main/java/com/uid2/shared/vertx/RequestCapturingHandler.java @@ -34,12 +34,16 @@ public class RequestCapturingHandler implements Handler { private Queue _capturedRequests = null; private final Map _apiMetricCounters = new HashMap<>(); private final Map _clientAppVersionCounters = new HashMap<>(); - private final ISiteStore siteStore; + private ISiteStore siteStore; private static String formatRFC1123DateTime(long time) { return DateTimeFormatter.RFC_1123_DATE_TIME.format(Instant.ofEpochMilli(time).atZone(ZONE_GMT)); } + public RequestCapturingHandler() + { + } + public RequestCapturingHandler(ISiteStore siteStore) { this.siteStore = siteStore; @@ -115,7 +119,7 @@ private void capture(RoutingContext context, long timestamp, String remoteClient final Integer siteId = getSiteId(context); String siteName = "unknown"; - if (siteId != null) { + if (siteId != null && siteStore != null) { Site site = siteStore.getSite(siteId); if (site != null) { diff --git a/src/test/java/com/uid2/shared/vertx/RequestCapturingHandlerTest.java b/src/test/java/com/uid2/shared/vertx/RequestCapturingHandlerTest.java index e274a319..1691b992 100644 --- a/src/test/java/com/uid2/shared/vertx/RequestCapturingHandlerTest.java +++ b/src/test/java/com/uid2/shared/vertx/RequestCapturingHandlerTest.java @@ -4,6 +4,8 @@ import com.uid2.shared.auth.ClientKey; import com.uid2.shared.auth.OperatorKey; import com.uid2.shared.middleware.AuthMiddleware; +import com.uid2.shared.model.Site; +import com.uid2.shared.store.ISiteStore; import io.micrometer.core.instrument.Metrics; import io.micrometer.core.instrument.simple.SimpleMeterRegistry; import io.vertx.core.Handler; @@ -14,6 +16,7 @@ import io.vertx.ext.web.client.WebClient; import io.vertx.junit5.VertxExtension; import io.vertx.junit5.VertxTestContext; +import org.assertj.core.condition.AnyOf; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -21,11 +24,17 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import org.mockito.Mock; +import org.mockito.Mockito; import java.time.Instant; +import java.util.HashSet; import java.util.Set; import java.util.stream.Stream; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.Mockito.when; + @ExtendWith(VertxExtension.class) public class RequestCapturingHandlerTest { private static final int Port = 8080; @@ -33,16 +42,24 @@ public class RequestCapturingHandlerTest { routingContext.response().setStatusCode(200).end(); }; + private ISiteStore siteStore; + @BeforeEach public void before() { Metrics.globalRegistry.forEachMeter(Metrics.globalRegistry::remove); Metrics.globalRegistry.add(new SimpleMeterRegistry()); + + Site site100 = new Site(100, "test-100", true); + Site site200 = new Site(200, "test-200", true); + siteStore = Mockito.mock(ISiteStore.class); + when(siteStore.getSite(100)).thenReturn(site100); + when(siteStore.getSite(200)).thenReturn(site200); } @Test public void captureSimplePath(Vertx vertx, VertxTestContext testContext) { Router router = Router.router(vertx); - router.route().handler(new RequestCapturingHandler()); + router.route().handler(new RequestCapturingHandler(siteStore)); router.get("/v1/token/generate").handler(dummyResponseHandler); vertx.createHttpServer().requestHandler(router).listen(Port, testContext.succeeding(id -> { @@ -90,7 +107,7 @@ public void captureSubRouterPath(Vertx vertx, VertxTestContext testContext) { @Test public void captureStaticPath(Vertx vertx, VertxTestContext testContext) { Router router = Router.router(vertx); - router.route().handler(new RequestCapturingHandler()); + router.route().handler(new RequestCapturingHandler(siteStore)); router.get("/static/*").handler(dummyResponseHandler); vertx.createHttpServer().requestHandler(router).listen(Port, testContext.succeeding(id -> { @@ -112,7 +129,7 @@ public void captureStaticPath(Vertx vertx, VertxTestContext testContext) { @Test public void captureUnknownPath(Vertx vertx, VertxTestContext testContext) { Router router = Router.router(vertx); - router.route().handler(new RequestCapturingHandler()); + router.route().handler(new RequestCapturingHandler(siteStore)); vertx.createHttpServer().requestHandler(router).listen(Port, testContext.succeeding(id -> { WebClient client = WebClient.create(vertx); @@ -132,9 +149,10 @@ public void captureUnknownPath(Vertx vertx, VertxTestContext testContext) { @ParameterizedTest @MethodSource("siteIdRoutingContextData") - public void getSiteIdFromRoutingContextData(String key, Object value, String siteId, Vertx vertx, VertxTestContext testContext) { + public void getSiteIdFromRoutingContextData(String key, Object value, String siteId, String siteName, + Vertx vertx, VertxTestContext testContext) { Router router = Router.router(vertx); - router.route().handler(new RequestCapturingHandler()); + router.route().handler(new RequestCapturingHandler(siteStore)); router.get("/test").handler(ctx -> { if (key != null) { ctx.put(key, value); @@ -149,6 +167,7 @@ public void getSiteIdFromRoutingContextData(String key, Object value, String sit double actual = Metrics.globalRegistry .get("uid2.http_requests") .tag("site_id", siteId) + .tag("site_name", siteName) .counter() .count(); Assertions.assertEquals(1, actual); @@ -160,11 +179,14 @@ public void getSiteIdFromRoutingContextData(String key, Object value, String sit private static Stream siteIdRoutingContextData() { // Arguments are: routing context data key, routing context data value, site ID tag. return Stream.of( - Arguments.of(Const.RoutingContextData.SiteId, 100, "100"), - Arguments.of(AuthMiddleware.API_CLIENT_PROP, new ClientKey("keyHash", "keySalt", "secret", "", Instant.MIN, Set.of(), 200, "test-key-id-1"), "200"), - Arguments.of(AuthMiddleware.API_CLIENT_PROP, new OperatorKey("test-keyHash", "test-keySalt", "name", "contact", "protocol", 0, false, "test-key-id-2"), "null"), - Arguments.of(AuthMiddleware.API_CLIENT_PROP, new OperatorKey("test-keyHash", "test-keySalt", "name", "contact", "protocol", 0, false, 300, "test-key-id-3"), "300"), - Arguments.of(null, null, "null") + Arguments.of(Const.RoutingContextData.SiteId, 100, "100", "test-100"), + Arguments.of(AuthMiddleware.API_CLIENT_PROP, new ClientKey("keyHash", "keySalt", + "secret", "", Instant.MIN, Set.of(), 200, "test-key-id-1"), "200", "test-200"), + Arguments.of(AuthMiddleware.API_CLIENT_PROP, new OperatorKey("test-keyHash", "test-keySalt", + "name", "contact", "protocol", 0, false, "test-key-id-2"), "null", "unknown"), + Arguments.of(AuthMiddleware.API_CLIENT_PROP, new OperatorKey("test-keyHash", "test-keySalt", + "name", "contact", "protocol", 0, false, 300, "test-key-id-3"), "300", "unknown"), + Arguments.of(null, null, "null", "unknown") ); } }