use of com.optimizely.ab.config.ProjectConfig in project java-sdk by optimizely.
the class EventBuilderTest method createImpressionEventWithBucketingId.
/**
* Verify {@link com.optimizely.ab.event.internal.payload.EventBatch} event creation
*/
@Test
public void createImpressionEventWithBucketingId() throws Exception {
// use the "valid" project config and its associated experiment, variation, and attributes
ProjectConfig projectConfig = validProjectConfigV2();
Experiment activatedExperiment = projectConfig.getExperiments().get(0);
Variation bucketedVariation = activatedExperiment.getVariations().get(0);
Attribute attribute = projectConfig.getAttributes().get(0);
String userId = "userId";
Map<String, String> attributeMap = new HashMap<String, String>();
attributeMap.put(attribute.getKey(), "value");
attributeMap.put(com.optimizely.ab.bucketing.DecisionService.BUCKETING_ATTRIBUTE, "variation");
Decision expectedDecision = new Decision(activatedExperiment.getLayerId(), activatedExperiment.getId(), bucketedVariation.getId(), false);
com.optimizely.ab.event.internal.payload.Attribute feature = new com.optimizely.ab.event.internal.payload.Attribute(attribute.getId(), attribute.getKey(), com.optimizely.ab.event.internal.payload.Attribute.CUSTOM_ATTRIBUTE_TYPE, "value");
com.optimizely.ab.event.internal.payload.Attribute feature1 = new com.optimizely.ab.event.internal.payload.Attribute(com.optimizely.ab.bucketing.DecisionService.BUCKETING_ATTRIBUTE, EventBuilder.ATTRIBUTE_KEY_FOR_BUCKETING_ATTRIBUTE, com.optimizely.ab.event.internal.payload.Attribute.CUSTOM_ATTRIBUTE_TYPE, "variation");
List<com.optimizely.ab.event.internal.payload.Attribute> expectedUserFeatures = Arrays.asList(feature, feature1);
LogEvent impressionEvent = builder.createImpressionEvent(projectConfig, activatedExperiment, bucketedVariation, userId, attributeMap);
// verify that request endpoint is correct
assertThat(impressionEvent.getEndpointUrl(), is(EventBuilder.EVENT_ENDPOINT));
EventBatch impression = gson.fromJson(impressionEvent.getBody(), EventBatch.class);
// verify payload information
assertThat(impression.getVisitors().get(0).getVisitorId(), is(userId));
assertThat((double) impression.getVisitors().get(0).getSnapshots().get(0).getEvents().get(0).getTimestamp(), closeTo((double) System.currentTimeMillis(), 1000.0));
assertFalse(impression.getVisitors().get(0).getSnapshots().get(0).getDecisions().get(0).getIsCampaignHoldback());
assertThat(impression.getAnonymizeIp(), is(projectConfig.getAnonymizeIP()));
assertThat(impression.getProjectId(), is(projectConfig.getProjectId()));
assertThat(impression.getVisitors().get(0).getSnapshots().get(0).getDecisions().get(0), is(expectedDecision));
assertThat(impression.getVisitors().get(0).getSnapshots().get(0).getDecisions().get(0).getCampaignId(), is(activatedExperiment.getLayerId()));
assertThat(impression.getAccountId(), is(projectConfig.getAccountId()));
assertThat(impression.getVisitors().get(0).getAttributes(), is(expectedUserFeatures));
assertThat(impression.getClientName(), is(EventBatch.ClientEngine.JAVA_SDK.getClientEngineValue()));
assertThat(impression.getClientVersion(), is(BuildVersionInfo.VERSION));
assertNull(impression.getVisitors().get(0).getSessionId());
}
use of com.optimizely.ab.config.ProjectConfig in project java-sdk by optimizely.
the class EventBuilderTest method createImpressionEventAndroidTVClientEngineClientVersion.
/**
* Verify that supplying {@link EventBuilder} with a custom Android TV client engine and client version
* results in impression events being sent with the overriden values.
*/
@Test
public void createImpressionEventAndroidTVClientEngineClientVersion() throws Exception {
String clientVersion = "0.0.0";
EventBuilder builder = new EventBuilder(EventBatch.ClientEngine.ANDROID_TV_SDK, clientVersion);
ProjectConfig projectConfig = validProjectConfigV2();
Experiment activatedExperiment = projectConfig.getExperiments().get(0);
Variation bucketedVariation = activatedExperiment.getVariations().get(0);
Attribute attribute = projectConfig.getAttributes().get(0);
String userId = "userId";
Map<String, String> attributeMap = Collections.singletonMap(attribute.getKey(), "value");
LogEvent impressionEvent = builder.createImpressionEvent(projectConfig, activatedExperiment, bucketedVariation, userId, attributeMap);
EventBatch impression = gson.fromJson(impressionEvent.getBody(), EventBatch.class);
assertThat(impression.getClientName(), is(EventBatch.ClientEngine.ANDROID_TV_SDK.getClientEngineValue()));
assertThat(impression.getClientVersion(), is(clientVersion));
}
use of com.optimizely.ab.config.ProjectConfig in project java-sdk by optimizely.
the class Optimizely method getVariation.
@Nullable
public Variation getVariation(@Nonnull String experimentKey, @Nonnull String userId, @Nonnull Map<String, String> attributes) {
if (!validateUserId(userId)) {
return null;
}
ProjectConfig currentConfig = getProjectConfig();
Experiment experiment = getExperimentOrThrow(currentConfig, experimentKey);
if (experiment == null) {
// if we're unable to retrieve the associated experiment, return null
return null;
}
Map<String, String> filteredAttributes = filterAttributes(projectConfig, attributes);
return decisionService.getVariation(experiment, userId, filteredAttributes);
}
use of com.optimizely.ab.config.ProjectConfig in project java-sdk by optimizely.
the class EventFactory method createLogEvent.
public static LogEvent createLogEvent(List<UserEvent> userEvents) {
EventBatch.Builder builder = new EventBatch.Builder();
List<Visitor> visitors = new ArrayList<>(userEvents.size());
for (UserEvent userEvent : userEvents) {
if (userEvent == null) {
continue;
}
if (userEvent instanceof ImpressionEvent) {
visitors.add(createVisitor((ImpressionEvent) userEvent));
}
if (userEvent instanceof ConversionEvent) {
visitors.add(createVisitor((ConversionEvent) userEvent));
}
// This needs an interface.
UserContext userContext = userEvent.getUserContext();
ProjectConfig projectConfig = userContext.getProjectConfig();
builder.setClientName(ClientEngineInfo.getClientEngine().getClientEngineValue()).setClientVersion(BuildVersionInfo.VERSION).setAccountId(projectConfig.getAccountId()).setAnonymizeIp(projectConfig.getAnonymizeIP()).setProjectId(projectConfig.getProjectId()).setRevision(projectConfig.getRevision());
}
if (visitors.isEmpty()) {
return null;
}
builder.setVisitors(visitors);
return new LogEvent(LogEvent.RequestMethod.POST, EVENT_ENDPOINT, Collections.emptyMap(), builder.build());
}
use of com.optimizely.ab.config.ProjectConfig in project java-sdk by optimizely.
the class GsonConfigParserTest method parseFeatureVariablesWithFutureType.
@Test
public void parseFeatureVariablesWithFutureType() throws Exception {
JsonSimpleConfigParser parser = new JsonSimpleConfigParser();
ProjectConfig actual = parser.parseProjectConfig(validConfigJsonV4());
// unknown type
FeatureFlag featureFlag = actual.getFeatureKeyMapping().get("multi_variate_future_feature");
FeatureVariable variable = featureFlag.getVariableKeyToFeatureVariableMap().get("future_variable");
assertEquals(variable.getType(), "future_type");
}
Aggregations