Skip to content
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 2 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>cloud.eppo</groupId>
<artifactId>eppo-server-sdk</artifactId>
<version>2.0.2</version>
<version>2.1.0</version>

<name>${project.groupId}:${project.artifactId}</name>
<description>Eppo Server-Side SDK for Java</description>
Expand Down
38 changes: 33 additions & 5 deletions src/main/java/com/eppo/sdk/EppoClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

SubjectAttributes subjectAttributes) {
return this.getStringAssignment(subjectKey, experimentKey, subjectAttributes);
}
Expand All @@ -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,
Copy link
Collaborator

Choose a reason for hiding this comment

The 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());
}

/**
Expand Down
100 changes: 53 additions & 47 deletions src/test/java/com/eppo/sdk/EppoClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,7 +30,6 @@

import lombok.Data;


@ExtendWith(WireMockExtension.class)
public class EppoClientTest {

Expand Down Expand Up @@ -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
Expand All @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

break;
default:
List<String> actualStringAssignments = this.getStringAssignments(testCase);
Expand All @@ -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) {
Expand All @@ -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 {
Expand All @@ -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);
}
Expand Down