Skip to content

Commit 73a938c

Browse files
committed
test
1 parent 0fb1a47 commit 73a938c

3 files changed

Lines changed: 115 additions & 2 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package app.component;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.uid2.shared.util.Mapper;
6+
import common.Const;
7+
import common.EnvUtil;
8+
import common.HttpClient;
9+
10+
/**
11+
* Component for interacting with the UID2 Optout service.
12+
*/
13+
public class Optout extends App {
14+
private static final ObjectMapper OBJECT_MAPPER = Mapper.getInstance();
15+
private static final String OPTOUT_INTERNAL_API_KEY = EnvUtil.getEnv(Const.Config.Core.OPTOUT_INTERNAL_API_KEY);
16+
public static final String OPTOUT_URL = EnvUtil.getEnv(Const.Config.Core.OPTOUT_URL);
17+
18+
// The SQS delta producer runs on port 8082 (8081 + 1)
19+
private static final int DELTA_PRODUCER_PORT_OFFSET = 1;
20+
21+
public Optout(String host, Integer port, String name) {
22+
super(host, port, name);
23+
}
24+
25+
public Optout(String host, String name) {
26+
super(host, null, name);
27+
}
28+
29+
/**
30+
* Triggers delta production on the optout service.
31+
* This reads from the SQS queue and produces delta files.
32+
* The endpoint is on port 8082 (optout port + 1).
33+
*/
34+
public JsonNode triggerDeltaProduce() throws Exception {
35+
String deltaProduceUrl = getDeltaProducerBaseUrl() + "/optout/deltaproduce";
36+
String response = HttpClient.post(deltaProduceUrl, "", OPTOUT_INTERNAL_API_KEY);
37+
return OBJECT_MAPPER.readTree(response);
38+
}
39+
40+
/**
41+
* Gets the status of the current delta production job.
42+
*/
43+
public JsonNode getDeltaProduceStatus() throws Exception {
44+
String statusUrl = getDeltaProducerBaseUrl() + "/optout/deltaproduce/status";
45+
String response = HttpClient.get(statusUrl, OPTOUT_INTERNAL_API_KEY);
46+
return OBJECT_MAPPER.readTree(response);
47+
}
48+
49+
/**
50+
* Triggers delta production and waits for it to complete.
51+
* @param maxWaitSeconds Maximum time to wait for completion
52+
* @return true if delta production completed successfully
53+
*/
54+
public boolean triggerDeltaProduceAndWait(int maxWaitSeconds) throws Exception {
55+
triggerDeltaProduce();
56+
57+
long startTime = System.currentTimeMillis();
58+
long maxWaitMs = maxWaitSeconds * 1000L;
59+
60+
while (System.currentTimeMillis() - startTime < maxWaitMs) {
61+
Thread.sleep(2000); // Poll every 2 seconds
62+
63+
JsonNode status = getDeltaProduceStatus();
64+
String state = status.path("state").asText();
65+
66+
if ("COMPLETED".equals(state) || "FAILED".equals(state)) {
67+
return "COMPLETED".equals(state);
68+
}
69+
}
70+
71+
return false; // Timed out
72+
}
73+
74+
private String getDeltaProducerBaseUrl() {
75+
// Delta producer runs on optout port + 1
76+
if (getPort() != null) {
77+
return "http://" + getHost() + ":" + (getPort() + DELTA_PRODUCER_PORT_OFFSET);
78+
}
79+
// If port not specified, assume default optout port (8081) + 1
80+
return "http://" + getHost() + ":8082";
81+
}
82+
}

src/test/java/common/Const.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public static final class Config {
1313
public static final class Core {
1414
public static final String OPERATOR_API_KEY = "UID2_CORE_E2E_OPERATOR_API_KEY";
1515
public static final String OPTOUT_API_KEY = "UID2_CORE_E2E_OPTOUT_API_KEY";
16+
public static final String OPTOUT_INTERNAL_API_KEY = "UID2_CORE_E2E_OPTOUT_INTERNAL_API_KEY";
1617
public static final String CORE_URL = "UID2_CORE_E2E_CORE_URL";
1718
public static final String OPTOUT_URL = "UID2_CORE_E2E_OPTOUT_URL";
1819
}

src/test/java/suite/optout/OptoutTest.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package suite.optout;
22

33
import app.component.Operator;
4+
import app.component.Optout;
45
import com.fasterxml.jackson.databind.JsonNode;
56
import com.fasterxml.jackson.databind.ObjectMapper;
67
import com.uid2.client.IdentityTokens;
@@ -9,6 +10,8 @@
910
import org.junit.jupiter.params.ParameterizedTest;
1011
import org.junit.jupiter.params.provider.Arguments;
1112
import org.junit.jupiter.params.provider.MethodSource;
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
1215

1316
import java.time.Instant;
1417
import java.util.HashSet;
@@ -23,19 +26,23 @@
2326
@SuppressWarnings("unused")
2427
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
2528
public class OptoutTest {
26-
// TODO: Test failure case
29+
private static final Logger LOGGER = LoggerFactory.getLogger(OptoutTest.class);
2730

2831
private static final ObjectMapper OBJECT_MAPPER = Mapper.getInstance();
2932
private static final int OPTOUT_DELAY_MS = 1000;
3033
private static final int OPTOUT_WAIT_SECONDS = 300;
34+
private static final int DELTA_PRODUCE_WAIT_SECONDS = 120;
3135

3236
private static Set<Arguments> outputArgs;
3337
private static Set<Arguments> outputAdvertisingIdArgs;
38+
private static Optout optoutService;
3439

3540
@BeforeAll
3641
public static void setupAll() {
3742
outputArgs = new HashSet<>();
3843
outputAdvertisingIdArgs = new HashSet<>();
44+
// Initialize optout service component for delta production
45+
optoutService = new Optout("optout", 8081, "Optout Service");
3946
}
4047

4148
@ParameterizedTest(name = "/v2/token/logout with /v2/token/generate - {0} - {2}")
@@ -78,7 +85,30 @@ public void testV2LogoutWithV2IdentityMap(String label, Operator operator, Strin
7885
outputAdvertisingIdArgs.add(Arguments.of(label, operator, operatorName, rawUID, toOptOut, beforeOptOutTimestamp));
7986
}
8087

88+
/**
89+
* Triggers delta production on the optout service after all logout requests.
90+
* This reads the opt-out requests from SQS and produces delta files that
91+
* the operator will sync to reflect the opt-outs.
92+
*/
93+
@Test
8194
@Order(4)
95+
public void triggerDeltaProduction() throws Exception {
96+
LOGGER.info("Triggering delta production on optout service");
97+
98+
// Trigger delta production
99+
JsonNode response = optoutService.triggerDeltaProduce();
100+
LOGGER.info("Delta production triggered: {}", response);
101+
102+
// Wait for completion
103+
boolean success = optoutService.triggerDeltaProduceAndWait(DELTA_PRODUCE_WAIT_SECONDS);
104+
assertThat(success).as("Delta production should complete successfully").isTrue();
105+
106+
// Get final status
107+
JsonNode status = optoutService.getDeltaProduceStatus();
108+
LOGGER.info("Delta production completed: {}", status);
109+
}
110+
111+
@Order(5)
82112
@ParameterizedTest(name = "/v2/token/refresh after {2} generate and {3} logout - {0} - {1}")
83113
@MethodSource({
84114
"afterOptoutTokenArgs"
@@ -89,7 +119,7 @@ public void testV2TokenRefreshAfterOptOut(String label, Operator operator, Strin
89119
with().pollInterval(5, TimeUnit.SECONDS).await("Get V2 Token Response").atMost(OPTOUT_WAIT_SECONDS, TimeUnit.SECONDS).until(() -> operator.v2TokenRefresh(refreshToken, refreshResponseKey).equals(OBJECT_MAPPER.readTree("{\"status\":\"optout\"}")));
90120
}
91121

92-
@Order(5)
122+
@Order(6)
93123
@ParameterizedTest(name = "/v2/optout/status after v2/identity/map and v2/token/logout - DII {0} - expecting {4} - {2}")
94124
@MethodSource({"afterOptoutAdvertisingIdArgs"})
95125
public void testV2OptOutStatus(String label, Operator operator, String operatorName, String rawUID,

0 commit comments

Comments
 (0)