Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class HelloWorld {
DVCCloudClient dvcCloud;
DVCLocalClient dvcLocal;

public HelloWorld(@Qualifier("devcycleServerKey") String serverKey) throws Exception {
public HelloWorld(@Qualifier("devcycleServerKey") String serverKey) {
dvcCloud = new DVCCloudClient(serverKey);
dvcLocal = new DVCLocalClient(serverKey);
}
Expand All @@ -35,7 +35,7 @@ public String homePage(Model model) {

@GetMapping("/cloud/activateFlag")
public String homePageActivatedFlag(Model model) {
Variable<String> updateHomePage = dvcCloud.variable(getUser(), "string-var", "default string");
Variable<String> updateHomePage = dvcCloud.variable(getUser(false), "string-var", "default string");

String variationValue = updateHomePage.getValue();

Expand All @@ -46,8 +46,8 @@ public String homePageActivatedFlag(Model model) {
}

@GetMapping("/local/activateFlag")
public String homePageActivatedFlagLocal(Model model) throws Exception {
Variable<String> updateHomePage = dvcLocal.variable(getUser(), "string-var", "default string");
public String homePageActivatedFlagLocal(Model model) {
Variable<String> updateHomePage = dvcLocal.variable(getUser(true), "string-var", "default string");

String variationValue = updateHomePage.getValue();

Expand All @@ -57,9 +57,14 @@ public String homePageActivatedFlagLocal(Model model) throws Exception {
return "fragments/flagData :: value ";
}

private User getUser() {
return User.builder()
.userId("j_test")
.build();
private User getUser(boolean isLocal) {
return isLocal ?
User.builder()
.userId("j_test")
.platform("java-local")
.build() :
User.builder()
.userId("j_test")
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.devcycle.sdk.server.common.api.IDVCApi;
import com.devcycle.sdk.server.local.model.DVCLocalOptions;
import com.devcycle.sdk.server.local.model.FlushPayload;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.annotation.JsonInclude;

Expand Down
50 changes: 36 additions & 14 deletions src/main/java/com/devcycle/sdk/server/local/api/DVCLocalClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,40 @@ public final class DVCLocalClient {

private EventQueueManager eventQueueManager;

public DVCLocalClient(String serverKey) throws Exception {
public DVCLocalClient(String serverKey) {
this(serverKey, DVCLocalOptions.builder().build());
}

public DVCLocalClient(String serverKey, DVCLocalOptions dvcOptions) throws Exception {
public DVCLocalClient(String serverKey, DVCLocalOptions dvcOptions) {
configManager = new EnvironmentConfigManager(serverKey, localBucketing, dvcOptions);
this.serverKey = serverKey;
OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
eventQueueManager = new EventQueueManager(serverKey, localBucketing, dvcOptions);
try {
eventQueueManager = new EventQueueManager(serverKey, localBucketing, dvcOptions);
} catch (Exception e) {
System.out.printf("Error creating event queue due to error: %s%n", e.getMessage());
}
}

/**
* Get all features for user data
*
* @param user (required)
*/
public Map<String, Feature> allFeatures(User user) throws JsonProcessingException {
public Map<String, Feature> allFeatures(User user) {
validateUser(user);
localBucketing.setPlatformData(user.getPlatformData().toString());
String userString = OBJECT_MAPPER.writeValueAsString(user);
String userString = null;
BucketedUserConfig bucketedUserConfig = null;

try {
userString = OBJECT_MAPPER.writeValueAsString(user);

BucketedUserConfig bucketedUserConfig = localBucketing.generateBucketedConfig(serverKey, userString);
bucketedUserConfig = localBucketing.generateBucketedConfig(serverKey, userString);
} catch (JsonProcessingException e) {
System.out.printf("Unable to parse JSON for allFeatures due to error: %s%n", e.getMessage());
return Collections.emptyMap();
}
return bucketedUserConfig.features;
}

Expand All @@ -59,7 +71,7 @@ public Map<String, Feature> allFeatures(User user) throws JsonProcessingExceptio
* (required)
* @return Variable
*/
public <T> Variable<T> variable(User user, String key, T defaultValue) throws Exception {
public <T> Variable<T> variable(User user, String key, T defaultValue) {
validateUser(user);
localBucketing.setPlatformData(user.getPlatformData().toString());

Expand Down Expand Up @@ -95,13 +107,13 @@ public <T> Variable<T> variable(User user, String key, T defaultValue) throws Ex
eventQueueManager.queueAggregateEvent(Event.builder().type("aggVariableDefaulted").target(key).build(), bucketedUserConfig);
return defaultVariable;
}
} catch (JsonProcessingException e) {
} catch (Exception e) {
System.out.printf("Unable to parse JSON for Variable %s due to error: %s", key, e.toString());
}

try {
eventQueueManager.queueAggregateEvent(Event.builder().type("aggVariableDefaulted").target(key).build(), null);
} catch (JsonProcessingException e) {
} catch (Exception e) {
System.out.printf("Unable to parse aggVariableDefaulted event for Variable %s due to error: %s", key, e.toString());
}
return defaultVariable;
Expand All @@ -112,12 +124,18 @@ public <T> Variable<T> variable(User user, String key, T defaultValue) throws Ex
*
* @param user (required)
*/
public Map<String, Variable> allVariables(User user) throws JsonProcessingException {
public Map<String, Variable> allVariables(User user) {
validateUser(user);
localBucketing.setPlatformData(user.getPlatformData().toString());
String userString = OBJECT_MAPPER.writeValueAsString(user);
BucketedUserConfig bucketedUserConfig = null;
try {
String userString = OBJECT_MAPPER.writeValueAsString(user);

BucketedUserConfig bucketedUserConfig = localBucketing.generateBucketedConfig(serverKey, userString);
bucketedUserConfig = localBucketing.generateBucketedConfig(serverKey, userString);
} catch (JsonProcessingException e) {
System.out.printf("Unable to parse JSON for allVariables due to error: %s%n", e.getMessage());
return Collections.emptyMap();
}
return bucketedUserConfig.variables;
}

Expand All @@ -127,11 +145,15 @@ public Map<String, Variable> allVariables(User user) throws JsonProcessingExcept
* @param user (required)
* @param event (required)
*/
public void track(User user, Event event) throws Exception {
public void track(User user, Event event) {
validateUser(user);
localBucketing.setPlatformData(user.getPlatformData().toString());

eventQueueManager.queueEvent(user, event);
try {
eventQueueManager.queueEvent(user, event);
} catch (Exception e) {
System.out.printf("Failed to queue event due to error: %s%n", e.getMessage());
}
}

private void validateUser(User user) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.devcycle.sdk.server.local.bucketing.LocalBucketing;
import com.devcycle.sdk.server.local.model.*;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import retrofit2.Call;
import retrofit2.Response;
Expand Down Expand Up @@ -73,7 +72,7 @@ public void flushEvents() throws Exception {
if (isFlushingEvents) return;

if (serverKey == null || serverKey.equals("")) {
throw new Exception("DevCycle is not yet initialized to publish events."); // TODO: change to DVCException
throw new Exception("DevCycle is not yet initialized to publish events.");
}

FlushPayload[] flushPayloads = new FlushPayload[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.devcycle.sdk.server.common.model.Feature;
import com.devcycle.sdk.server.common.model.Variable;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.devcycle.sdk.server.helpers.WhiteBox;
import com.devcycle.sdk.server.local.api.DVCLocalClient;
import com.devcycle.sdk.server.local.bucketing.LocalBucketing;
import com.fasterxml.jackson.core.JsonProcessingException;

@RunWith(MockitoJUnitRunner.class)
public class DVCLocalClientTest {
Expand All @@ -38,7 +37,7 @@ public static void setup() throws Exception {
WhiteBox.setInternalState(client, "eventQueueManager", eventQueueManager);
}
@Test
public void variableTest() throws Exception {
public void variableTest() {
User user = getUser();
user.setEmail("giveMeVariationOff@email.com");
Variable<String> var = client.variable(user, "string-var", "default string");
Expand All @@ -50,15 +49,15 @@ public void variableTest() throws Exception {
}

@Test
public void allFeaturesTest() throws JsonProcessingException {
public void allFeaturesTest() {
User user = getUser();
Map<String, Feature> features = client.allFeatures(user);
Assert.assertEquals(features.get("a-cool-new-feature").getId(), "62fbf6566f1ba302829f9e32");
Assert.assertEquals(features.size(), 1);
}

@Test
public void allVariablesTest() throws JsonProcessingException {
public void allVariablesTest() {
User user = getUser();
Map<String, Variable> variables = client.allVariables(user);
Assert.assertEquals(variables.get("string-var").getId(), "63125320a4719939fd57cb2b");
Expand Down