22
22
import com .github .tomakehurst .wiremock .client .WireMock ;
23
23
import com .github .tomakehurst .wiremock .junit5 .WireMockExtension ;
24
24
import java .io .File ;
25
-
26
25
import java .lang .reflect .Field ;
27
26
import java .util .stream .Stream ;
28
-
29
27
import org .apache .commons .io .FileUtils ;
28
+ import org .junit .jupiter .api .AfterAll ;
30
29
import org .junit .jupiter .api .AfterEach ;
31
30
import org .junit .jupiter .api .BeforeAll ;
32
- import org .junit .jupiter .api .AfterAll ;
33
31
import org .junit .jupiter .api .Test ;
34
32
import org .junit .jupiter .api .extension .ExtendWith ;
35
33
import org .junit .jupiter .params .ParameterizedTest ;
@@ -45,7 +43,8 @@ public class EppoClientTest {
45
43
private static WireMockServer mockServer ;
46
44
47
45
private static final String DUMMY_FLAG_API_KEY = "dummy-flags-api-key" ; // Will load flags-v1
48
- private static final String DUMMY_BANDIT_API_KEY = "dummy-bandits-api-key" ; // Will load bandit-flags-v1
46
+ private static final String DUMMY_BANDIT_API_KEY =
47
+ "dummy-bandits-api-key" ; // Will load bandit-flags-v1
49
48
private AssignmentLogger mockAssignmentLogger ;
50
49
private BanditLogger mockBanditLogger ;
51
50
@@ -55,20 +54,21 @@ public static void initMockServer() {
55
54
mockServer .start ();
56
55
57
56
// If we get the dummy flag API key, return flags-v1.json
58
- String ufcFlagsResponseJson =
59
- readConfig (
60
- "src/test/resources/shared/ufc/flags-v1.json" );
57
+ String ufcFlagsResponseJson = readConfig ("src/test/resources/shared/ufc/flags-v1.json" );
61
58
mockServer .stubFor (
62
- WireMock .get (WireMock .urlMatching (".*flag-config/v1/config\\ ?.*apiKey=" +DUMMY_FLAG_API_KEY +".*" ))
59
+ WireMock .get (
60
+ WireMock .urlMatching (
61
+ ".*flag-config/v1/config\\ ?.*apiKey=" + DUMMY_FLAG_API_KEY + ".*" ))
63
62
.willReturn (WireMock .okJson (ufcFlagsResponseJson )));
64
63
65
64
// If we get the dummy bandit API key, return bandit-flags-v1.json
66
65
String banditFlagsResponseJson =
67
- readConfig (
68
- "src/test/resources/shared/ufc/bandit-flags-v1.json" );
66
+ readConfig ("src/test/resources/shared/ufc/bandit-flags-v1.json" );
69
67
mockServer .stubFor (
70
- WireMock .get (WireMock .urlMatching (".*flag-config/v1/config\\ ?.*apiKey=" +DUMMY_BANDIT_API_KEY +".*" ))
71
- .willReturn (WireMock .okJson (banditFlagsResponseJson )));
68
+ WireMock .get (
69
+ WireMock .urlMatching (
70
+ ".*flag-config/v1/config\\ ?.*apiKey=" + DUMMY_BANDIT_API_KEY + ".*" ))
71
+ .willReturn (WireMock .okJson (banditFlagsResponseJson )));
72
72
73
73
// Return bandit models (no need to switch on API key)
74
74
String banditModelsResponseJson =
@@ -154,7 +154,7 @@ public void testLoggers() {
154
154
actions .put ("reebok" , rebookAttributes );
155
155
156
156
BanditResult banditResult =
157
- eppoClient .getBanditAction (flagKey , subjectKey , subjectAttributes , actions , "control" );
157
+ eppoClient .getBanditAction (flagKey , subjectKey , subjectAttributes , actions , "control" );
158
158
159
159
// Verify assignment
160
160
assertEquals ("banner_bandit" , banditResult .getVariation ());
@@ -166,7 +166,7 @@ public void testLoggers() {
166
166
167
167
// Verify bandit logger called
168
168
ArgumentCaptor <BanditAssignment > banditLogCaptor =
169
- ArgumentCaptor .forClass (BanditAssignment .class );
169
+ ArgumentCaptor .forClass (BanditAssignment .class );
170
170
verify (mockBanditLogger , times (1 )).logBanditAssignment (banditLogCaptor .capture ());
171
171
}
172
172
@@ -180,33 +180,32 @@ public void getInstanceWhenUninitialized() {
180
180
public void testErrorGracefulModeOn () {
181
181
initBuggyClient ();
182
182
EppoClient .getInstance ().setIsGracefulFailureMode (true );
183
- assertEquals (1.234 , EppoClient .getInstance ().getDoubleAssignment ("numeric_flag" , "subject1" , 1.234 ));
183
+ assertEquals (
184
+ 1.234 , EppoClient .getInstance ().getDoubleAssignment ("numeric_flag" , "subject1" , 1.234 ));
184
185
}
185
186
186
187
@ Test
187
188
public void testErrorGracefulModeOff () {
188
189
initBuggyClient ();
189
190
EppoClient .getInstance ().setIsGracefulFailureMode (false );
190
- assertThrows (Exception .class , () -> EppoClient .getInstance ().getDoubleAssignment ("numeric_flag" , "subject1" , 1.234 ));
191
+ assertThrows (
192
+ Exception .class ,
193
+ () -> EppoClient .getInstance ().getDoubleAssignment ("numeric_flag" , "subject1" , 1.234 ));
191
194
}
192
195
193
196
@ Test
194
197
public void testReinitializeWithoutForcing () {
195
198
EppoClient firstInstance = initClient (DUMMY_FLAG_API_KEY );
196
- EppoClient secondInstance = new EppoClient .Builder ()
197
- .apiKey (DUMMY_FLAG_API_KEY )
198
- .buildAndInit ();
199
+ EppoClient secondInstance = new EppoClient .Builder ().apiKey (DUMMY_FLAG_API_KEY ).buildAndInit ();
199
200
200
201
assertSame (firstInstance , secondInstance );
201
202
}
202
203
203
204
@ Test
204
205
public void testReinitializeWitForcing () {
205
206
EppoClient firstInstance = initClient (DUMMY_FLAG_API_KEY );
206
- EppoClient secondInstance = new EppoClient .Builder ()
207
- .apiKey (DUMMY_FLAG_API_KEY )
208
- .forceReinitialize (true )
209
- .buildAndInit ();
207
+ EppoClient secondInstance =
208
+ new EppoClient .Builder ().apiKey (DUMMY_FLAG_API_KEY ).forceReinitialize (true ).buildAndInit ();
210
209
211
210
assertNotSame (firstInstance , secondInstance );
212
211
}
@@ -218,11 +217,12 @@ public void testPolling() {
218
217
TestUtils .setBaseClientHttpClientOverrideField (httpClientSpy );
219
218
220
219
new EppoClient .Builder ()
221
- .apiKey (DUMMY_FLAG_API_KEY )
222
- .pollingIntervalMs (20 )
223
- .forceReinitialize (true )
224
- .buildAndInit ();
220
+ .apiKey (DUMMY_FLAG_API_KEY )
221
+ .pollingIntervalMs (20 )
222
+ .forceReinitialize (true )
223
+ .buildAndInit ();
225
224
225
+ // Method will be called immediately on init
226
226
verify (httpClientSpy , times (1 )).get (anyString ());
227
227
228
228
// Sleep for 25 ms to allow another polling cycle to complete
@@ -252,13 +252,13 @@ private EppoClient initClient(String apiKey) {
252
252
mockBanditLogger = mock (BanditLogger .class );
253
253
254
254
return new EppoClient .Builder ()
255
- .apiKey (apiKey )
256
- .host (TEST_HOST )
257
- .assignmentLogger (mockAssignmentLogger )
258
- .banditLogger (mockBanditLogger )
259
- .isGracefulMode (false )
260
- .forceReinitialize (true ) // Useful for tests
261
- .buildAndInit ();
255
+ .apiKey (apiKey )
256
+ .host (TEST_HOST )
257
+ .assignmentLogger (mockAssignmentLogger )
258
+ .banditLogger (mockBanditLogger )
259
+ .isGracefulMode (false )
260
+ .forceReinitialize (true ) // Useful for tests
261
+ .buildAndInit ();
262
262
}
263
263
264
264
private void uninitClient () {
0 commit comments