11
11
12
12
import com .eppo .sdk .dto .*;
13
13
import com .eppo .sdk .helpers .Converter ;
14
- import com .fasterxml .jackson .core .JacksonException ;
15
14
import com .fasterxml .jackson .core .JsonParser ;
16
15
import com .fasterxml .jackson .databind .*;
17
16
import com .fasterxml .jackson .databind .annotation .JsonDeserialize ;
31
30
32
31
import lombok .Data ;
33
32
34
-
35
33
@ ExtendWith (WireMockExtension .class )
36
34
public class EppoClientTest {
37
35
@@ -120,23 +118,24 @@ public AssignmentValueType deserialize(JsonParser jsonParser, DeserializationCon
120
118
void init () {
121
119
setupMockRacServer ();
122
120
EppoClientConfig config = EppoClientConfig .builder ()
123
- .apiKey ("mock-api-key" )
124
- .baseURL ("http://localhost:4001" )
125
- .assignmentLogger (new IAssignmentLogger () {
126
- @ Override
127
- public void logAssignment (AssignmentLogData logData ) {
128
- // Auto-generated method stub
129
- }
130
- })
131
- .build ();
121
+ .apiKey ("mock-api-key" )
122
+ .baseURL ("http://localhost:4001" )
123
+ .assignmentLogger (new IAssignmentLogger () {
124
+ @ Override
125
+ public void logAssignment (AssignmentLogData logData ) {
126
+ // Auto-generated method stub
127
+ }
128
+ })
129
+ .build ();
132
130
EppoClient .init (config );
133
131
}
134
132
135
133
private void setupMockRacServer () {
136
134
this .mockServer = new WireMockServer (TEST_PORT );
137
135
this .mockServer .start ();
138
136
String racResponseJson = getMockRandomizedAssignmentResponse ();
139
- this .mockServer .stubFor (WireMock .get (WireMock .urlMatching (".*randomized_assignment.*" )).willReturn (WireMock .okJson (racResponseJson )));
137
+ this .mockServer .stubFor (
138
+ WireMock .get (WireMock .urlMatching (".*randomized_assignment.*" )).willReturn (WireMock .okJson (racResponseJson )));
140
139
}
141
140
142
141
@ AfterEach
@@ -159,8 +158,14 @@ void testAssignments(AssignmentTestCase testCase) throws IOException {
159
158
assertEquals (expectedBooleanAssignments , actualBooleanAssignments );
160
159
break ;
161
160
case JSON :
162
- List <String > actualJSONAssignments = this .getJSONAssignments (testCase );
163
- assertEquals (testCase .expectedAssignments , actualJSONAssignments );
161
+ List <JsonNode > actualJSONAssignments = this .getJSONAssignments (testCase );
162
+ for (JsonNode node : actualJSONAssignments ) {
163
+ assertEquals (JsonNodeType .OBJECT , node .getNodeType ());
164
+ }
165
+
166
+ assertEquals (testCase .expectedAssignments , actualJSONAssignments .stream ()
167
+ .map (node -> node .toString ())
168
+ .collect (Collectors .toList ()));
164
169
break ;
165
170
default :
166
171
List <String > actualStringAssignments = this .getStringAssignments (testCase );
@@ -173,48 +178,49 @@ private List<?> getAssignments(AssignmentTestCase testCase, AssignmentValueType
173
178
EppoClient client = EppoClient .getInstance ();
174
179
if (testCase .subjectsWithAttributes != null ) {
175
180
return testCase .subjectsWithAttributes .stream ()
181
+ .map (subject -> {
182
+ try {
183
+ switch (valueType ) {
184
+ case NUMERIC :
185
+ return client .getDoubleAssignment (subject .subjectKey , testCase .experiment , subject .subjectAttributes )
186
+ .orElse (null );
187
+ case BOOLEAN :
188
+ return client .getBooleanAssignment (subject .subjectKey , testCase .experiment , subject .subjectAttributes )
189
+ .orElse (null );
190
+ case JSON :
191
+ return client
192
+ .getParsedJSONAssignment (subject .subjectKey , testCase .experiment , subject .subjectAttributes )
193
+ .orElse (null );
194
+ default :
195
+ return client .getStringAssignment (subject .subjectKey , testCase .experiment , subject .subjectAttributes )
196
+ .orElse (null );
197
+ }
198
+ } catch (Exception e ) {
199
+ throw new RuntimeException (e );
200
+ }
201
+ }).collect (Collectors .toList ());
202
+ }
203
+ return testCase .subjects .stream ()
176
204
.map (subject -> {
177
205
try {
178
206
switch (valueType ) {
179
207
case NUMERIC :
180
- return client .getDoubleAssignment (subject . subjectKey , testCase .experiment , subject . subjectAttributes )
181
- .orElse (null );
208
+ return client .getDoubleAssignment (subject , testCase .experiment )
209
+ .orElse (null );
182
210
case BOOLEAN :
183
- return client .getBooleanAssignment (subject . subjectKey , testCase .experiment , subject . subjectAttributes )
184
- .orElse (null );
211
+ return client .getBooleanAssignment (subject , testCase .experiment )
212
+ .orElse (null );
185
213
case JSON :
186
- return client .getJSONAssignment (subject . subjectKey , testCase .experiment , subject . subjectAttributes )
187
- .orElse (null );
214
+ return client .getParsedJSONAssignment (subject , testCase .experiment )
215
+ .orElse (null );
188
216
default :
189
- return client .getStringAssignment (subject . subjectKey , testCase .experiment , subject . subjectAttributes )
190
- .orElse (null );
217
+ return client .getStringAssignment (subject , testCase .experiment )
218
+ .orElse (null );
191
219
}
192
220
} catch (Exception e ) {
193
221
throw new RuntimeException (e );
194
222
}
195
223
}).collect (Collectors .toList ());
196
- }
197
- return testCase .subjects .stream ()
198
- .map (subject -> {
199
- try {
200
- switch (valueType ) {
201
- case NUMERIC :
202
- return client .getDoubleAssignment (subject , testCase .experiment )
203
- .orElse (null );
204
- case BOOLEAN :
205
- return client .getBooleanAssignment (subject , testCase .experiment )
206
- .orElse (null );
207
- case JSON :
208
- return client .getJSONAssignment (subject , testCase .experiment )
209
- .orElse (null );
210
- default :
211
- return client .getStringAssignment (subject , testCase .experiment )
212
- .orElse (null );
213
- }
214
- } catch (Exception e ) {
215
- throw new RuntimeException (e );
216
- }
217
- }).collect (Collectors .toList ());
218
224
}
219
225
220
226
private List <String > getStringAssignments (AssignmentTestCase testCase ) {
@@ -229,8 +235,8 @@ private List<Boolean> getBooleanAssignments(AssignmentTestCase testCase) {
229
235
return (List <Boolean >) this .getAssignments (testCase , AssignmentValueType .BOOLEAN );
230
236
}
231
237
232
- private List <String > getJSONAssignments (AssignmentTestCase testCase ) {
233
- return (List <String >) this .getAssignments (testCase , AssignmentValueType .JSON );
238
+ private List <JsonNode > getJSONAssignments (AssignmentTestCase testCase ) {
239
+ return (List <JsonNode >) this .getAssignments (testCase , AssignmentValueType .JSON );
234
240
}
235
241
236
242
private static Stream <Arguments > getAssignmentTestData () throws IOException {
@@ -248,7 +254,7 @@ private static Stream<Arguments> getAssignmentTestData() throws IOException {
248
254
private static String getMockRandomizedAssignmentResponse () {
249
255
File mockRacResponse = new File ("src/test/resources/rac-experiments-v3.json" );
250
256
try {
251
- return FileUtils .readFileToString (mockRacResponse , "UTF8" );
257
+ return FileUtils .readFileToString (mockRacResponse , "UTF8" );
252
258
} catch (Exception e ) {
253
259
throw new RuntimeException ("Error reading mock RAC data" , e );
254
260
}
0 commit comments