Search in sources :

Example 31 with ProjectConfig

use of com.optimizely.ab.config.ProjectConfig in project java-sdk by optimizely.

the class EventBuilderTest method createImpressionEvent.

/**
 * Verify {@link com.optimizely.ab.event.internal.payload.EventBatch} event creation
 */
@Test
public void createImpressionEvent() 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 = Collections.singletonMap(attribute.getKey(), "value");
    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");
    List<com.optimizely.ab.event.internal.payload.Attribute> expectedUserFeatures = Collections.singletonList(feature);
    LogEvent impressionEvent = builder.createImpressionEvent(projectConfig, activatedExperiment, bucketedVariation, userId, attributeMap);
    // verify that request endpoint is correct
    assertThat(impressionEvent.getEndpointUrl(), is(EventBuilder.EVENT_ENDPOINT));
    EventBatch eventBatch = gson.fromJson(impressionEvent.getBody(), EventBatch.class);
    // verify payload information
    assertThat(eventBatch.getVisitors().get(0).getVisitorId(), is(userId));
    assertThat((double) eventBatch.getVisitors().get(0).getSnapshots().get(0).getEvents().get(0).getTimestamp(), closeTo((double) System.currentTimeMillis(), 1000.0));
    assertFalse(eventBatch.getVisitors().get(0).getSnapshots().get(0).getDecisions().get(0).getIsCampaignHoldback());
    assertThat(eventBatch.getAnonymizeIp(), is(projectConfig.getAnonymizeIP()));
    assertThat(eventBatch.getProjectId(), is(projectConfig.getProjectId()));
    assertThat(eventBatch.getVisitors().get(0).getSnapshots().get(0).getDecisions().get(0), is(expectedDecision));
    assertThat(eventBatch.getVisitors().get(0).getSnapshots().get(0).getDecisions().get(0).getCampaignId(), is(activatedExperiment.getLayerId()));
    assertThat(eventBatch.getAccountId(), is(projectConfig.getAccountId()));
    assertThat(eventBatch.getVisitors().get(0).getAttributes(), is(expectedUserFeatures));
    assertThat(eventBatch.getClientName(), is(EventBatch.ClientEngine.JAVA_SDK.getClientEngineValue()));
    assertThat(eventBatch.getClientVersion(), is(BuildVersionInfo.VERSION));
    assertNull(eventBatch.getVisitors().get(0).getSessionId());
}
Also used : Attribute(com.optimizely.ab.config.Attribute) LogEvent(com.optimizely.ab.event.LogEvent) Experiment(com.optimizely.ab.config.Experiment) Decision(com.optimizely.ab.event.internal.payload.Decision) ProjectConfig(com.optimizely.ab.config.ProjectConfig) Variation(com.optimizely.ab.config.Variation) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) Test(org.junit.Test)

Example 32 with ProjectConfig

use of com.optimizely.ab.config.ProjectConfig in project java-sdk by optimizely.

the class ProjectConfigGsonDeserializer method deserialize.

@Override
public ProjectConfig deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    String accountId = jsonObject.get("accountId").getAsString();
    String projectId = jsonObject.get("projectId").getAsString();
    String revision = jsonObject.get("revision").getAsString();
    String version = jsonObject.get("version").getAsString();
    int datafileVersion = Integer.parseInt(version);
    // generic list type tokens
    Type groupsType = new TypeToken<List<Group>>() {
    }.getType();
    Type experimentsType = new TypeToken<List<Experiment>>() {
    }.getType();
    Type attributesType = new TypeToken<List<Attribute>>() {
    }.getType();
    Type eventsType = new TypeToken<List<EventType>>() {
    }.getType();
    Type audienceType = new TypeToken<List<Audience>>() {
    }.getType();
    List<Group> groups = context.deserialize(jsonObject.get("groups").getAsJsonArray(), groupsType);
    List<Experiment> experiments = context.deserialize(jsonObject.get("experiments").getAsJsonArray(), experimentsType);
    List<Attribute> attributes;
    attributes = context.deserialize(jsonObject.get("attributes"), attributesType);
    List<EventType> events = context.deserialize(jsonObject.get("events").getAsJsonArray(), eventsType);
    List<Audience> audiences = context.deserialize(jsonObject.get("audiences").getAsJsonArray(), audienceType);
    boolean anonymizeIP = false;
    // live variables should be null if using V2
    List<LiveVariable> liveVariables = null;
    if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V3.toString())) {
        Type liveVariablesType = new TypeToken<List<LiveVariable>>() {
        }.getType();
        liveVariables = context.deserialize(jsonObject.getAsJsonArray("variables"), liveVariablesType);
        anonymizeIP = jsonObject.get("anonymizeIP").getAsBoolean();
    }
    List<FeatureFlag> featureFlags = null;
    List<Rollout> rollouts = null;
    if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString())) {
        Type featureFlagsType = new TypeToken<List<FeatureFlag>>() {
        }.getType();
        featureFlags = context.deserialize(jsonObject.getAsJsonArray("featureFlags"), featureFlagsType);
        Type rolloutsType = new TypeToken<List<Rollout>>() {
        }.getType();
        rollouts = context.deserialize(jsonObject.get("rollouts").getAsJsonArray(), rolloutsType);
    }
    return new ProjectConfig(accountId, anonymizeIP, projectId, revision, version, attributes, audiences, events, experiments, featureFlags, groups, liveVariables, rollouts);
}
Also used : Group(com.optimizely.ab.config.Group) Attribute(com.optimizely.ab.config.Attribute) EventType(com.optimizely.ab.config.EventType) JsonObject(com.google.gson.JsonObject) FeatureFlag(com.optimizely.ab.config.FeatureFlag) List(java.util.List) Rollout(com.optimizely.ab.config.Rollout) Audience(com.optimizely.ab.config.audience.Audience) Experiment(com.optimizely.ab.config.Experiment) LiveVariable(com.optimizely.ab.config.LiveVariable) ProjectConfig(com.optimizely.ab.config.ProjectConfig) EventType(com.optimizely.ab.config.EventType) Type(java.lang.reflect.Type)

Example 33 with ProjectConfig

use of com.optimizely.ab.config.ProjectConfig in project java-sdk by optimizely.

the class GsonConfigParserTest method parseFeatureVariablesWithJsonPatched.

@Test
public void parseFeatureVariablesWithJsonPatched() throws Exception {
    JsonSimpleConfigParser parser = new JsonSimpleConfigParser();
    ProjectConfig actual = parser.parseProjectConfig(validConfigJsonV4());
    // "string" type + "json" subType
    FeatureFlag featureFlag = actual.getFeatureKeyMapping().get("multi_variate_feature");
    FeatureVariable variable = featureFlag.getVariableKeyToFeatureVariableMap().get("json_patched");
    assertEquals(variable.getType(), "json");
}
Also used : ProjectConfig(com.optimizely.ab.config.ProjectConfig) DatafileProjectConfigTestUtils.verifyProjectConfig(com.optimizely.ab.config.DatafileProjectConfigTestUtils.verifyProjectConfig) FeatureFlag(com.optimizely.ab.config.FeatureFlag) FeatureVariable(com.optimizely.ab.config.FeatureVariable) Test(org.junit.Test)

Example 34 with ProjectConfig

use of com.optimizely.ab.config.ProjectConfig in project java-sdk by optimizely.

the class JsonConfigParserTest method parseNullFeatureEnabledProjectConfigV4.

@Test
public void parseNullFeatureEnabledProjectConfigV4() throws Exception {
    JsonConfigParser parser = new JsonConfigParser();
    ProjectConfig actual = parser.parseProjectConfig(nullFeatureEnabledConfigJsonV4());
    assertNotNull(actual);
    assertNotNull(actual.getExperiments());
    assertNotNull(actual.getFeatureFlags());
}
Also used : ProjectConfig(com.optimizely.ab.config.ProjectConfig) DatafileProjectConfigTestUtils.verifyProjectConfig(com.optimizely.ab.config.DatafileProjectConfigTestUtils.verifyProjectConfig) Test(org.junit.Test)

Example 35 with ProjectConfig

use of com.optimizely.ab.config.ProjectConfig in project java-sdk by optimizely.

the class JsonConfigParserTest 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");
}
Also used : ProjectConfig(com.optimizely.ab.config.ProjectConfig) DatafileProjectConfigTestUtils.verifyProjectConfig(com.optimizely.ab.config.DatafileProjectConfigTestUtils.verifyProjectConfig) FeatureFlag(com.optimizely.ab.config.FeatureFlag) FeatureVariable(com.optimizely.ab.config.FeatureVariable) Test(org.junit.Test)

Aggregations

ProjectConfig (com.optimizely.ab.config.ProjectConfig)51 Test (org.junit.Test)45 DatafileProjectConfigTestUtils.verifyProjectConfig (com.optimizely.ab.config.DatafileProjectConfigTestUtils.verifyProjectConfig)28 Experiment (com.optimizely.ab.config.Experiment)19 FeatureFlag (com.optimizely.ab.config.FeatureFlag)14 FeatureVariable (com.optimizely.ab.config.FeatureVariable)12 LogEvent (com.optimizely.ab.event.LogEvent)10 Variation (com.optimizely.ab.config.Variation)9 Attribute (com.optimizely.ab.config.Attribute)7 EventBatch (com.optimizely.ab.event.internal.payload.EventBatch)7 ExhaustiveTest (com.optimizely.ab.categories.ExhaustiveTest)6 EventType (com.optimizely.ab.config.EventType)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 HashMap (java.util.HashMap)3 DecisionService (com.optimizely.ab.bucketing.DecisionService)2 Group (com.optimizely.ab.config.Group)2 LiveVariable (com.optimizely.ab.config.LiveVariable)2 Rollout (com.optimizely.ab.config.Rollout)2 Audience (com.optimizely.ab.config.audience.Audience)2 EventBuilder (com.optimizely.ab.event.internal.EventBuilder)2