-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add support for getParsedJSONAssignment method returning JsonNode (FF-896) #26
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
import com.eppo.sdk.helpers.InputValidator; | ||
import com.eppo.sdk.helpers.RuleValidator; | ||
import com.eppo.sdk.helpers.Shard; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
|
@@ -60,8 +61,7 @@ private EppoClient(ConfigurationStore configurationStore, Timer poller, EppoClie | |
private Optional<EppoValue> getAssignmentValue( | ||
String subjectKey, | ||
String flagKey, | ||
SubjectAttributes subjectAttributes | ||
) { | ||
SubjectAttributes subjectAttributes) { | ||
// Validate Input Values | ||
InputValidator.validateNotBlank(subjectKey, "Invalid argument: subjectKey cannot be blank"); | ||
InputValidator.validateNotBlank(flagKey, "Invalid argument: flagKey cannot be blank"); | ||
|
@@ -142,6 +142,8 @@ private Optional<?> getTypedAssignment(String subjectKey, String experimentKey, | |
return Optional.of(value.get().boolValue()); | ||
case NUMBER: | ||
return Optional.of(value.get().doubleValue()); | ||
case JSON_NODE: | ||
return Optional.of(value.get().jsonNodeValue()); | ||
default: | ||
return Optional.of(value.get().stringValue()); | ||
} | ||
|
@@ -258,7 +260,7 @@ public Optional<Double> getDoubleAssignment(String subjectKey, String experiment | |
* @param subjectAttributes | ||
* @return | ||
*/ | ||
public Optional<String> getJSONAssignment(String subjectKey, String experimentKey, | ||
public Optional<String> getJSONStringAssignment(String subjectKey, String experimentKey, | ||
SubjectAttributes subjectAttributes) { | ||
return this.getStringAssignment(subjectKey, experimentKey, subjectAttributes); | ||
} | ||
|
@@ -271,8 +273,34 @@ public Optional<String> getJSONAssignment(String subjectKey, String experimentKe | |
* @param experimentKey | ||
* @return | ||
*/ | ||
public Optional<String> getJSONAssignment(String subjectKey, String experimentKey) { | ||
return this.getJSONAssignment(subjectKey, experimentKey, new SubjectAttributes()); | ||
public Optional<String> getJSONStringAssignment(String subjectKey, String experimentKey) { | ||
return this.getJSONStringAssignment(subjectKey, experimentKey, new SubjectAttributes()); | ||
} | ||
|
||
/** | ||
* This function will return JSON assignment value | ||
* | ||
* @param subjectKey | ||
* @param experimentKey | ||
* @param subjectAttributes | ||
* @return | ||
*/ | ||
public Optional<JsonNode> getParsedJSONAssignment(String subjectKey, String experimentKey, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔥 |
||
SubjectAttributes subjectAttributes) { | ||
return (Optional<JsonNode>) this.getTypedAssignment(subjectKey, experimentKey, EppoValueType.JSON_NODE, | ||
subjectAttributes); | ||
} | ||
|
||
/** | ||
* This function will return JSON assignment value without passing | ||
* subjectAttributes | ||
* | ||
* @param subjectKey | ||
* @param experimentKey | ||
* @return | ||
*/ | ||
public Optional<JsonNode> getParsedJSONAssignment(String subjectKey, String experimentKey) { | ||
return this.getParsedJSONAssignment(subjectKey, experimentKey, new SubjectAttributes()); | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,6 @@ | |
|
||
import com.eppo.sdk.dto.*; | ||
import com.eppo.sdk.helpers.Converter; | ||
import com.fasterxml.jackson.core.JacksonException; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.*; | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
|
@@ -31,7 +30,6 @@ | |
|
||
import lombok.Data; | ||
|
||
|
||
@ExtendWith(WireMockExtension.class) | ||
public class EppoClientTest { | ||
|
||
|
@@ -120,23 +118,24 @@ public AssignmentValueType deserialize(JsonParser jsonParser, DeserializationCon | |
void init() { | ||
setupMockRacServer(); | ||
EppoClientConfig config = EppoClientConfig.builder() | ||
.apiKey("mock-api-key") | ||
.baseURL("http://localhost:4001") | ||
.assignmentLogger(new IAssignmentLogger() { | ||
@Override | ||
public void logAssignment(AssignmentLogData logData) { | ||
// Auto-generated method stub | ||
} | ||
}) | ||
.build(); | ||
.apiKey("mock-api-key") | ||
.baseURL("http://localhost:4001") | ||
.assignmentLogger(new IAssignmentLogger() { | ||
@Override | ||
public void logAssignment(AssignmentLogData logData) { | ||
// Auto-generated method stub | ||
} | ||
}) | ||
.build(); | ||
EppoClient.init(config); | ||
} | ||
|
||
private void setupMockRacServer() { | ||
this.mockServer = new WireMockServer(TEST_PORT); | ||
this.mockServer.start(); | ||
String racResponseJson = getMockRandomizedAssignmentResponse(); | ||
this.mockServer.stubFor(WireMock.get(WireMock.urlMatching(".*randomized_assignment.*")).willReturn(WireMock.okJson(racResponseJson))); | ||
this.mockServer.stubFor( | ||
WireMock.get(WireMock.urlMatching(".*randomized_assignment.*")).willReturn(WireMock.okJson(racResponseJson))); | ||
} | ||
|
||
@AfterEach | ||
|
@@ -159,8 +158,14 @@ void testAssignments(AssignmentTestCase testCase) throws IOException { | |
assertEquals(expectedBooleanAssignments, actualBooleanAssignments); | ||
break; | ||
case JSON: | ||
List<String> actualJSONAssignments = this.getJSONAssignments(testCase); | ||
assertEquals(testCase.expectedAssignments, actualJSONAssignments); | ||
List<JsonNode> actualJSONAssignments = this.getJSONAssignments(testCase); | ||
for (JsonNode node : actualJSONAssignments) { | ||
assertEquals(JsonNodeType.OBJECT, node.getNodeType()); | ||
} | ||
|
||
assertEquals(testCase.expectedAssignments, actualJSONAssignments.stream() | ||
.map(node -> node.toString()) | ||
.collect(Collectors.toList())); | ||
Comment on lines
+166
to
+168
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
break; | ||
default: | ||
List<String> actualStringAssignments = this.getStringAssignments(testCase); | ||
|
@@ -173,48 +178,49 @@ private List<?> getAssignments(AssignmentTestCase testCase, AssignmentValueType | |
EppoClient client = EppoClient.getInstance(); | ||
if (testCase.subjectsWithAttributes != null) { | ||
return testCase.subjectsWithAttributes.stream() | ||
.map(subject -> { | ||
try { | ||
switch (valueType) { | ||
case NUMERIC: | ||
return client.getDoubleAssignment(subject.subjectKey, testCase.experiment, subject.subjectAttributes) | ||
.orElse(null); | ||
case BOOLEAN: | ||
return client.getBooleanAssignment(subject.subjectKey, testCase.experiment, subject.subjectAttributes) | ||
.orElse(null); | ||
case JSON: | ||
return client | ||
.getParsedJSONAssignment(subject.subjectKey, testCase.experiment, subject.subjectAttributes) | ||
.orElse(null); | ||
default: | ||
return client.getStringAssignment(subject.subjectKey, testCase.experiment, subject.subjectAttributes) | ||
.orElse(null); | ||
} | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
}).collect(Collectors.toList()); | ||
} | ||
return testCase.subjects.stream() | ||
.map(subject -> { | ||
try { | ||
switch (valueType) { | ||
case NUMERIC: | ||
return client.getDoubleAssignment(subject.subjectKey, testCase.experiment, subject.subjectAttributes) | ||
.orElse(null); | ||
return client.getDoubleAssignment(subject, testCase.experiment) | ||
.orElse(null); | ||
case BOOLEAN: | ||
return client.getBooleanAssignment(subject.subjectKey, testCase.experiment, subject.subjectAttributes) | ||
.orElse(null); | ||
return client.getBooleanAssignment(subject, testCase.experiment) | ||
.orElse(null); | ||
case JSON: | ||
return client.getJSONAssignment(subject.subjectKey, testCase.experiment, subject.subjectAttributes) | ||
.orElse(null); | ||
return client.getParsedJSONAssignment(subject, testCase.experiment) | ||
.orElse(null); | ||
default: | ||
return client.getStringAssignment(subject.subjectKey, testCase.experiment, subject.subjectAttributes) | ||
.orElse(null); | ||
return client.getStringAssignment(subject, testCase.experiment) | ||
.orElse(null); | ||
} | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
}).collect(Collectors.toList()); | ||
} | ||
return testCase.subjects.stream() | ||
.map(subject -> { | ||
try { | ||
switch (valueType) { | ||
case NUMERIC: | ||
return client.getDoubleAssignment(subject, testCase.experiment) | ||
.orElse(null); | ||
case BOOLEAN: | ||
return client.getBooleanAssignment(subject, testCase.experiment) | ||
.orElse(null); | ||
case JSON: | ||
return client.getJSONAssignment(subject, testCase.experiment) | ||
.orElse(null); | ||
default: | ||
return client.getStringAssignment(subject, testCase.experiment) | ||
.orElse(null); | ||
} | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
}).collect(Collectors.toList()); | ||
} | ||
|
||
private List<String> getStringAssignments(AssignmentTestCase testCase) { | ||
|
@@ -229,8 +235,8 @@ private List<Boolean> getBooleanAssignments(AssignmentTestCase testCase) { | |
return (List<Boolean>) this.getAssignments(testCase, AssignmentValueType.BOOLEAN); | ||
} | ||
|
||
private List<String> getJSONAssignments(AssignmentTestCase testCase) { | ||
return (List<String>) this.getAssignments(testCase, AssignmentValueType.JSON); | ||
private List<JsonNode> getJSONAssignments(AssignmentTestCase testCase) { | ||
return (List<JsonNode>) this.getAssignments(testCase, AssignmentValueType.JSON); | ||
} | ||
|
||
private static Stream<Arguments> getAssignmentTestData() throws IOException { | ||
|
@@ -248,7 +254,7 @@ private static Stream<Arguments> getAssignmentTestData() throws IOException { | |
private static String getMockRandomizedAssignmentResponse() { | ||
File mockRacResponse = new File("src/test/resources/rac-experiments-v3.json"); | ||
try { | ||
return FileUtils.readFileToString(mockRacResponse, "UTF8"); | ||
return FileUtils.readFileToString(mockRacResponse, "UTF8"); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Error reading mock RAC data", e); | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍