-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEppoClientTest.java
More file actions
360 lines (306 loc) · 12.7 KB
/
EppoClientTest.java
File metadata and controls
360 lines (306 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package cloud.eppo;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;
import cloud.eppo.api.Attributes;
import cloud.eppo.api.BanditActions;
import cloud.eppo.api.BanditResult;
import cloud.eppo.api.Configuration;
import cloud.eppo.api.dto.VariationType;
import cloud.eppo.logging.Assignment;
import cloud.eppo.logging.AssignmentLogger;
import cloud.eppo.logging.BanditAssignment;
import cloud.eppo.logging.BanditLogger;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
@ExtendWith(WireMockExtension.class)
public class EppoClientTest {
private static final int TEST_PORT = 4001;
private static final String TEST_HOST = "http://localhost:" + TEST_PORT;
private static WireMockServer mockServer;
private static final String DUMMY_FLAG_API_KEY = "dummy-flags-api-key"; // Will load flags-v1
private static final String DUMMY_BANDIT_API_KEY =
"dummy-bandits-api-key"; // Will load bandit-flags-v1
private AssignmentLogger mockAssignmentLogger;
private BanditLogger mockBanditLogger;
private static final byte[] EMPTY_CONFIG = "{\"flags\":{}}".getBytes();
@BeforeAll
public static void initMockServer() {
mockServer = new WireMockServer(TEST_PORT);
mockServer.start();
// If we get the dummy flag API key, return flags-v1.json
String ufcFlagsResponseJson = readConfig("src/test/resources/shared/ufc/flags-v1.json");
mockServer.stubFor(
WireMock.get(
WireMock.urlMatching(
".*flag-config/v1/config\\?.*apiKey=" + DUMMY_FLAG_API_KEY + ".*"))
.willReturn(WireMock.okJson(ufcFlagsResponseJson)));
// If we get the dummy bandit API key, return bandit-flags-v1.json
String banditFlagsResponseJson =
readConfig("src/test/resources/shared/ufc/bandit-flags-v1.json");
mockServer.stubFor(
WireMock.get(
WireMock.urlMatching(
".*flag-config/v1/config\\?.*apiKey=" + DUMMY_BANDIT_API_KEY + ".*"))
.willReturn(WireMock.okJson(banditFlagsResponseJson)));
// Return bandit models (no need to switch on API key)
String banditModelsResponseJson =
readConfig("src/test/resources/shared/ufc/bandit-models-v1.json");
mockServer.stubFor(
WireMock.get(WireMock.urlMatching(".*flag-config/v1/bandits\\?.*"))
.willReturn(WireMock.okJson(banditModelsResponseJson)));
}
private static String readConfig(String jsonToReturnFilePath) {
File mockResponseFile = new File(jsonToReturnFilePath);
try {
return FileUtils.readFileToString(mockResponseFile, "UTF8");
} catch (Exception e) {
throw new RuntimeException("Error reading mock data: " + e.getMessage(), e);
}
}
@AfterEach
public void cleanUp() {
try {
EppoClient.getInstance().stopPolling();
} catch (IllegalStateException ex) {
// pass: Indicates that the singleton Eppo Client has not yet been initialized.
}
}
@AfterAll
public static void tearDown() {
if (mockServer != null) {
mockServer.stop();
}
}
// TODO: Re-enable once sdk-common-jvm:tests artifact is updated with package relocations
// The test helpers reference cloud.eppo.ufc.dto.VariationType which has moved to
// cloud.eppo.api.dto
// @ParameterizedTest
// @MethodSource("getAssignmentTestData")
// public void testUnobfuscatedAssignments(File testFile) {
// AssignmentTestCase testCase = parseTestCaseFile(testFile);
// EppoClient eppoClient = initClient(DUMMY_FLAG_API_KEY);
// runTestCase(testCase, eppoClient);
// }
// private static Stream<Arguments> getAssignmentTestData() {
// return AssignmentTestCase.getAssignmentTestData();
// }
// @ParameterizedTest
// @MethodSource("getBanditTestData")
// public void testUnobfuscatedBanditAssignments(File testFile) {
// BanditTestCase testCase = parseBanditTestCaseFile(testFile);
// EppoClient eppoClient = initClient(DUMMY_BANDIT_API_KEY);
// runBanditTestCase(testCase, eppoClient);
// }
// private static Stream<Arguments> getBanditTestData() {
// return BanditTestCase.getBanditTestData();
// }
@SuppressWarnings("ExtractMethodRecommender")
@Test
public void testLoggers() {
EppoClient eppoClient = initClient(DUMMY_BANDIT_API_KEY);
String flagKey = "banner_bandit_flag";
String subjectKey = "bob";
Attributes subjectAttributes = new Attributes();
subjectAttributes.put("age", 25);
subjectAttributes.put("country", "USA");
subjectAttributes.put("gender_identity", "female");
BanditActions actions = new BanditActions();
Attributes nikeAttributes = new Attributes();
nikeAttributes.put("brand_affinity", 1.5);
nikeAttributes.put("loyalty_tier", "silver");
actions.put("nike", nikeAttributes);
Attributes adidasAttributes = new Attributes();
adidasAttributes.put("brand_affinity", -1.0);
adidasAttributes.put("loyalty_tier", "bronze");
actions.put("adidas", adidasAttributes);
Attributes rebookAttributes = new Attributes();
rebookAttributes.put("brand_affinity", 0.5);
rebookAttributes.put("loyalty_tier", "gold");
actions.put("reebok", rebookAttributes);
BanditResult banditResult =
eppoClient.getBanditAction(flagKey, subjectKey, subjectAttributes, actions, "control");
// Verify assignment
assertEquals("banner_bandit", banditResult.getVariation());
assertEquals("adidas", banditResult.getAction());
// Verify experiment assignment logger called
ArgumentCaptor<Assignment> assignmentLogCaptor = ArgumentCaptor.forClass(Assignment.class);
verify(mockAssignmentLogger, times(1)).logAssignment(assignmentLogCaptor.capture());
// Verify bandit logger called
ArgumentCaptor<BanditAssignment> banditLogCaptor =
ArgumentCaptor.forClass(BanditAssignment.class);
verify(mockBanditLogger, times(1)).logBanditAssignment(banditLogCaptor.capture());
}
@Test
public void getInstanceWhenUninitialized() {
uninitClient();
assertThrows(RuntimeException.class, EppoClient::getInstance);
}
@Test
public void testReinitializeWithoutForcing() {
EppoClient firstInstance = initClient(DUMMY_FLAG_API_KEY);
EppoClient secondInstance = EppoClient.builder(DUMMY_FLAG_API_KEY).buildAndInit();
assertSame(firstInstance, secondInstance);
}
@Test
public void testReinitializeWitForcing() {
EppoClient firstInstance = initClient(DUMMY_FLAG_API_KEY);
EppoClient secondInstance =
EppoClient.builder(DUMMY_FLAG_API_KEY).forceReinitialize(true).buildAndInit();
assertNotSame(firstInstance, secondInstance);
}
@Test
public void testPolling() {
// Initialize with polling enabled
EppoClient.builder(DUMMY_FLAG_API_KEY)
.apiBaseUrl(TEST_HOST)
.pollingIntervalMs(100)
.forceReinitialize(true)
.buildAndInit();
// Verify polling can be stopped without errors
EppoClient.getInstance().stopPolling();
}
// NOTE: Graceful mode during init is intrinsically true since the call is non-blocking and
// exceptions are caught without rethrowing in `FetchConfigurationsTask`
@Test
public void testClientMakesDefaultAssignmentsAfterFailingToInitialize() {
// Set up bad HTTP response via WireMock
mockHttpError();
// Initialize with a bad URL that will fail to fetch config
// No exception should be thrown in graceful mode
try {
mockAssignmentLogger = mock(AssignmentLogger.class);
mockBanditLogger = mock(BanditLogger.class);
EppoClient eppoClient =
EppoClient.builder("error-api-key")
.apiBaseUrl(TEST_HOST)
.assignmentLogger(mockAssignmentLogger)
.banditLogger(mockBanditLogger)
.isGracefulMode(true)
.forceReinitialize(true)
.buildAndInit();
Thread.sleep(25); // Sleep to allow the async config fetch call to happen (and fail)
assertEquals("default", eppoClient.getStringAssignment("experiment1", "subject1", "default"));
} catch (Exception e) {
fail("Unexpected exception: " + e);
}
}
@Test
public void testGetConfiguration() {
EppoClient eppoClient = initClient(DUMMY_FLAG_API_KEY);
Configuration configuration = eppoClient.getConfiguration();
assertNotNull(configuration);
assertNotNull(configuration.getFlag("numeric_flag"));
assertEquals(VariationType.NUMERIC, configuration.getFlagType("numeric_flag"));
}
@Test
public void testConfigurationChangeListener() {
List<Configuration> received = new ArrayList<>();
EppoClient.Builder clientBuilder =
EppoClient.builder(DUMMY_FLAG_API_KEY)
.apiBaseUrl(TEST_HOST)
.forceReinitialize(true)
.onConfigurationChange(received::add)
.isGracefulMode(false);
// Initialize and the callback should be triggered
EppoClient eppoClient = clientBuilder.buildAndInit();
// Configuration change callback should have been called at least once
assertTrue(received.size() >= 1);
// Trigger a reload of the client
eppoClient.loadConfiguration();
// Should have received another configuration
assertTrue(received.size() >= 2);
}
public static void mockHttpError() {
// Configure WireMock to return an error for the error API key
mockServer.stubFor(
WireMock.get(WireMock.urlMatching(".*flag-config/v1/config\\?.*apiKey=error-api-key.*"))
.willReturn(WireMock.serverError().withBody("Intentional Error")));
}
@SuppressWarnings("SameParameterValue")
private void sleepUninterruptedly(long sleepMs) {
try {
Thread.sleep(sleepMs);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
private EppoClient initClient(String apiKey) {
mockAssignmentLogger = mock(AssignmentLogger.class);
mockBanditLogger = mock(BanditLogger.class);
return EppoClient.builder(apiKey)
.apiBaseUrl(TEST_HOST)
.assignmentLogger(mockAssignmentLogger)
.banditLogger(mockBanditLogger)
.isGracefulMode(false)
.forceReinitialize(true) // Useful for tests
.buildAndInit();
}
private EppoClient initFailingGracefulClient(boolean isGracefulMode) {
mockAssignmentLogger = mock(AssignmentLogger.class);
mockBanditLogger = mock(BanditLogger.class);
return EppoClient.builder(DUMMY_FLAG_API_KEY)
.apiBaseUrl("blag")
.assignmentLogger(mockAssignmentLogger)
.banditLogger(mockBanditLogger)
.isGracefulMode(isGracefulMode)
.forceReinitialize(true) // Useful for tests
.buildAndInit();
}
private void uninitClient() {
try {
Field httpClientOverrideField = EppoClient.class.getDeclaredField("instance");
httpClientOverrideField.setAccessible(true);
httpClientOverrideField.set(null, null);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
private static final byte[] BOOL_FLAG_CONFIG =
("{\n"
+ " \"createdAt\": \"2024-04-17T19:40:53.716Z\",\n"
+ " \"format\": \"SERVER\",\n"
+ " \"environment\": {\n"
+ " \"name\": \"Test\"\n"
+ " },\n"
+ " \"flags\": {\n"
+ " \"9a2025738dde19ff44cd30b9d2967000\": {\n"
+ " \"key\": \"9a2025738dde19ff44cd30b9d2967000\",\n"
+ " \"enabled\": true,\n"
+ " \"variationType\": \"BOOLEAN\",\n"
+ " \"variations\": {\n"
+ " \"b24=\": {\n"
+ " \"key\": \"b24=\",\n"
+ " \"value\": \"dHJ1ZQ==\"\n"
+ " }\n"
+ " },\n"
+ " \"allocations\": [\n"
+ " {\n"
+ " \"key\": \"b24=\",\n"
+ " \"doLog\": true,\n"
+ " \"splits\": [\n"
+ " {\n"
+ " \"variationKey\": \"b24=\",\n"
+ " \"shards\": []\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ " ],\n"
+ " \"totalShards\": 10000\n"
+ " }\n"
+ " }\n"
+ "}")
.getBytes();
}