Skip to content

Commit

Permalink
update with default handler constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkxuyang committed Nov 26, 2024
1 parent 63ab0a8 commit 7537bb5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@ public class RequestCapturingHandler implements Handler<RoutingContext> {
private Queue<String> _capturedRequests = null;
private final Map<String, Counter> _apiMetricCounters = new HashMap<>();
private final Map<String, Counter> _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;
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,35 +16,50 @@
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;
import org.junit.jupiter.api.extension.ExtendWith;
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;
private static final Handler<RoutingContext> dummyResponseHandler = routingContext -> {
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 -> {
Expand Down Expand Up @@ -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 -> {
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -160,11 +179,14 @@ public void getSiteIdFromRoutingContextData(String key, Object value, String sit
private static Stream<Arguments> 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")
);
}
}

0 comments on commit 7537bb5

Please sign in to comment.