|
| 1 | +package com.uid2.operator.store; |
| 2 | + |
| 3 | +import com.uid2.operator.Const; |
| 4 | +import com.uid2.operator.model.IdentityScope; |
| 5 | +import com.uid2.operator.model.IdentityType; |
| 6 | +import com.uid2.operator.model.UserIdentity; |
| 7 | +import com.uid2.operator.service.EncodingUtils; |
| 8 | +import com.uid2.shared.cloud.MemCachedStorage; |
| 9 | +import com.uid2.shared.audit.Audit; |
| 10 | +import io.vertx.core.Vertx; |
| 11 | +import io.vertx.core.http.HttpMethod; |
| 12 | +import io.vertx.core.http.HttpServer; |
| 13 | +import io.vertx.core.json.JsonObject; |
| 14 | +import io.vertx.ext.web.Router; |
| 15 | +import io.vertx.ext.web.handler.BodyHandler; |
| 16 | +import org.junit.jupiter.api.AfterEach; |
| 17 | +import org.junit.jupiter.api.BeforeEach; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import java.time.Clock; |
| 21 | +import java.time.Instant; |
| 22 | +import java.util.concurrent.CountDownLatch; |
| 23 | +import java.util.concurrent.TimeUnit; |
| 24 | + |
| 25 | +import static org.junit.jupiter.api.Assertions.*; |
| 26 | + |
| 27 | +public class CloudSyncOptOutStoreTest { |
| 28 | + private Vertx vertx; |
| 29 | + private HttpServer server; |
| 30 | + private int port; |
| 31 | + private Router router; |
| 32 | + |
| 33 | + @BeforeEach |
| 34 | + void setUp() throws InterruptedException { |
| 35 | + vertx = Vertx.vertx(); |
| 36 | + CountDownLatch latch = new CountDownLatch(1); |
| 37 | + server = vertx.createHttpServer(); |
| 38 | + router = Router.router(vertx); |
| 39 | + router.route().handler(BodyHandler.create()); |
| 40 | + server.requestHandler(router); |
| 41 | + server.listen(0, ar -> { |
| 42 | + if (ar.succeeded()) { |
| 43 | + port = ar.result().actualPort(); |
| 44 | + latch.countDown(); |
| 45 | + } |
| 46 | + }); |
| 47 | + assertTrue(latch.await(5, TimeUnit.SECONDS)); |
| 48 | + } |
| 49 | + |
| 50 | + @AfterEach |
| 51 | + void tearDown() throws InterruptedException { |
| 52 | + CountDownLatch latch = new CountDownLatch(1); |
| 53 | + server.close(ar -> latch.countDown()); |
| 54 | + assertTrue(latch.await(5, TimeUnit.SECONDS)); |
| 55 | + CountDownLatch latch2 = new CountDownLatch(1); |
| 56 | + vertx.close(ar -> latch2.countDown()); |
| 57 | + assertTrue(latch2.await(5, TimeUnit.SECONDS)); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void addEntry_sendsPostWithQueryAndJsonBody() throws Exception { |
| 62 | + final String path = "/optout/replicate"; |
| 63 | + final String operatorKey = "test-operator-key"; |
| 64 | + final String uidTraceId = "trace-123"; |
| 65 | + final String email = "post@test.com"; |
| 66 | + final String phone = null; |
| 67 | + final String clientIp = "203.0.113.5"; |
| 68 | + |
| 69 | + final byte[] userIdBytes = new byte[] {10, 20, 30}; |
| 70 | + final byte[] advIdBytes = new byte[] {1, 2, 3}; |
| 71 | + final String expectedIdentityHash = EncodingUtils.toBase64String(userIdBytes); |
| 72 | + final String expectedAdvertisingId = EncodingUtils.toBase64String(advIdBytes); |
| 73 | + |
| 74 | + CountDownLatch received = new CountDownLatch(1); |
| 75 | + |
| 76 | + router.post(path).handler(ctx -> { |
| 77 | + try { |
| 78 | + assertEquals(HttpMethod.POST, ctx.request().method()); |
| 79 | + assertEquals(path, ctx.normalisedPath()); |
| 80 | + assertEquals(expectedIdentityHash, ctx.request().getParam("identity_hash")); |
| 81 | + assertEquals(expectedAdvertisingId, ctx.request().getParam("advertising_id")); |
| 82 | + assertEquals("Bearer " + operatorKey, ctx.request().getHeader("Authorization")); |
| 83 | + assertEquals(uidTraceId, ctx.request().getHeader(Audit.UID_TRACE_ID_HEADER)); |
| 84 | + |
| 85 | + JsonObject body = ctx.body().asJsonObject(); |
| 86 | + assertNotNull(body); |
| 87 | + assertEquals(email, body.getString("email")); |
| 88 | + assertEquals(clientIp, body.getString("client_ip")); |
| 89 | + assertFalse(body.containsKey("unexpected")); |
| 90 | + |
| 91 | + ctx.response().setStatusCode(200).end(); |
| 92 | + } finally { |
| 93 | + received.countDown(); |
| 94 | + } |
| 95 | + }); |
| 96 | + JsonObject config = new JsonObject() |
| 97 | + .put(Const.Config.OptOutApiUriProp, "http://localhost:" + port + path) |
| 98 | + .put(Const.Config.OptOutBloomFilterSizeProp, 8192) |
| 99 | + .put(Const.Config.OptOutHeapDefaultCapacityProp, 8192) |
| 100 | + .put(Const.Config.OptOutStatusApiEnabled, true) |
| 101 | + .put(Const.Config.OptOutDataDirProp, "/tmp/uid2-operator-test") |
| 102 | + // Additional required config for FileUtils/optout snapshot |
| 103 | + .put("optout_delta_rotate_interval", 300) |
| 104 | + .put("optout_delta_backtrack_in_days", 1) |
| 105 | + .put("optout_partition_interval", 86400) |
| 106 | + .put("optout_max_partitions", 30) |
| 107 | + .put("optout_s3_folder", "optout/") |
| 108 | + .put("optout_s3_path_compat", false); |
| 109 | + |
| 110 | + CloudSyncOptOutStore store = new CloudSyncOptOutStore(vertx, new MemCachedStorage(), config, operatorKey, Clock.systemUTC()); |
| 111 | + |
| 112 | + UserIdentity uid = new UserIdentity(IdentityScope.UID2, IdentityType.Email, userIdBytes, 0, Instant.now(), Instant.now()); |
| 113 | + |
| 114 | + CountDownLatch done = new CountDownLatch(1); |
| 115 | + store.addEntry(uid, advIdBytes, uidTraceId, "local-instance", email, phone, clientIp, ar -> done.countDown()); |
| 116 | + |
| 117 | + assertTrue(received.await(5, TimeUnit.SECONDS)); |
| 118 | + assertTrue(done.await(5, TimeUnit.SECONDS)); |
| 119 | + } |
| 120 | +} |
0 commit comments