Search in sources :

Example 56 with LogEvent

use of com.optimizely.ab.event.LogEvent in project java-sdk by optimizely.

the class EventFactoryTest method createImpressionEventWithIntegerDecimalBoolAndStringAttributes.

/**
 * Verify that Integer, Decimal, Bool and String variables are allowed to pass.
 */
@Test
public void createImpressionEventWithIntegerDecimalBoolAndStringAttributes() {
    assumeTrue(datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()));
    // use the "valid" project config and its associated experiment, variation, and attributes
    ProjectConfig projectConfig = validProjectConfig;
    Experiment activatedExperiment = projectConfig.getExperiments().get(0);
    Variation bucketedVariation = activatedExperiment.getVariations().get(0);
    Attribute doubleAttribute = validProjectConfig.getAttributes().get(5);
    Attribute integerAttribute = validProjectConfig.getAttributes().get(4);
    Attribute boolAttribute = validProjectConfig.getAttributes().get(3);
    Attribute stringAttribute = validProjectConfig.getAttributes().get(0);
    double validDoubleAttribute = 13.1;
    int validIntegerAttribute = 12;
    boolean validBoolAttribute = true;
    String validStringAttribute = "grayfindor";
    HashMap<String, Object> attributes = new HashMap<>();
    attributes.put(doubleAttribute.getKey(), validDoubleAttribute);
    attributes.put(integerAttribute.getKey(), validIntegerAttribute);
    attributes.put(boolAttribute.getKey(), validBoolAttribute);
    attributes.put(stringAttribute.getKey(), validStringAttribute);
    LogEvent impressionEvent = createImpressionEvent(projectConfig, activatedExperiment, bucketedVariation, "userId", attributes);
    EventBatch impression = gson.fromJson(impressionEvent.getBody(), EventBatch.class);
    assertEquals(impression.getVisitors().get(0).getAttributes().get(0).getKey(), boolAttribute.getKey());
    assertEquals(impression.getVisitors().get(0).getAttributes().get(0).getValue(), validBoolAttribute);
    assertEquals(impression.getVisitors().get(0).getAttributes().get(1).getKey(), doubleAttribute.getKey());
    assertEquals(impression.getVisitors().get(0).getAttributes().get(1).getValue(), validDoubleAttribute);
    assertEquals(impression.getVisitors().get(0).getAttributes().get(2).getKey(), integerAttribute.getKey());
    assertEquals((int) ((double) impression.getVisitors().get(0).getAttributes().get(2).getValue()), validIntegerAttribute);
    assertEquals(impression.getVisitors().get(0).getAttributes().get(3).getKey(), stringAttribute.getKey());
    assertEquals(impression.getVisitors().get(0).getAttributes().get(3).getValue(), validStringAttribute);
}
Also used : ControlAttribute(com.optimizely.ab.internal.ControlAttribute) LogEvent(com.optimizely.ab.event.LogEvent) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) Test(org.junit.Test)

Example 57 with LogEvent

use of com.optimizely.ab.event.LogEvent in project java-sdk by optimizely.

the class EventFactoryTest method createImpressionEventPassingUserAgentAttribute.

/**
 * Verify {@link com.optimizely.ab.event.internal.payload.EventBatch} event creation
 */
@Test
public void createImpressionEventPassingUserAgentAttribute() throws Exception {
    // use the "valid" project config and its associated experiment, variation, and attributes
    Experiment activatedExperiment = validProjectConfig.getExperiments().get(0);
    Variation bucketedVariation = activatedExperiment.getVariations().get(0);
    Attribute attribute = validProjectConfig.getAttributes().get(0);
    String userId = "userId";
    String ruleType = "experiment";
    Map<String, String> attributeMap = new HashMap<String, String>();
    attributeMap.put(attribute.getKey(), "value");
    attributeMap.put(ControlAttribute.USER_AGENT_ATTRIBUTE.toString(), "Chrome");
    DecisionMetadata metadata = new DecisionMetadata(activatedExperiment.getKey(), activatedExperiment.getKey(), ruleType, "variationKey", true);
    Decision expectedDecision = new Decision.Builder().setCampaignId(activatedExperiment.getLayerId()).setExperimentId(activatedExperiment.getId()).setVariationId(bucketedVariation.getId()).setMetadata(metadata).setIsCampaignHoldback(false).build();
    com.optimizely.ab.event.internal.payload.Attribute feature = new com.optimizely.ab.event.internal.payload.Attribute.Builder().setEntityId(attribute.getId()).setKey(attribute.getKey()).setType(com.optimizely.ab.event.internal.payload.Attribute.CUSTOM_ATTRIBUTE_TYPE).setValue("value").build();
    com.optimizely.ab.event.internal.payload.Attribute userAgentFeature = new com.optimizely.ab.event.internal.payload.Attribute.Builder().setEntityId(ControlAttribute.USER_AGENT_ATTRIBUTE.toString()).setKey(ControlAttribute.USER_AGENT_ATTRIBUTE.toString()).setType(com.optimizely.ab.event.internal.payload.Attribute.CUSTOM_ATTRIBUTE_TYPE).setValue("Chrome").build();
    com.optimizely.ab.event.internal.payload.Attribute botFilteringFeature = getBotFilteringAttribute();
    List<com.optimizely.ab.event.internal.payload.Attribute> expectedUserFeatures;
    if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()))
        expectedUserFeatures = Arrays.asList(userAgentFeature, feature, botFilteringFeature);
    else
        expectedUserFeatures = Arrays.asList(userAgentFeature, feature);
    LogEvent impressionEvent = createImpressionEvent(validProjectConfig, activatedExperiment, bucketedVariation, userId, attributeMap);
    // verify that request endpoint is correct
    assertThat(impressionEvent.getEndpointUrl(), is(EventFactory.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(validProjectConfig.getAnonymizeIP()));
    assertTrue(eventBatch.getEnrichDecisions());
    assertThat(eventBatch.getProjectId(), is(validProjectConfig.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(validProjectConfig.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 : ControlAttribute(com.optimizely.ab.internal.ControlAttribute) LogEvent(com.optimizely.ab.event.LogEvent) GsonBuilder(com.google.gson.GsonBuilder) Decision(com.optimizely.ab.event.internal.payload.Decision) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) DecisionMetadata(com.optimizely.ab.event.internal.payload.DecisionMetadata) Test(org.junit.Test)

Example 58 with LogEvent

use of com.optimizely.ab.event.LogEvent in project java-sdk by optimizely.

the class EventFactoryTest method createImpressionEventAndroidClientEngineClientVersion.

/**
 * Verify that supplying {@link ClientEngineInfo} with a custom client engine and client version results in impression
 * events being sent with the overriden values.
 */
@Test
public void createImpressionEventAndroidClientEngineClientVersion() throws Exception {
    ClientEngineInfo.setClientEngine(EventBatch.ClientEngine.ANDROID_SDK);
    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 = createImpressionEvent(projectConfig, activatedExperiment, bucketedVariation, userId, attributeMap);
    EventBatch impression = gson.fromJson(impressionEvent.getBody(), EventBatch.class);
    assertThat(impression.getClientName(), is(EventBatch.ClientEngine.ANDROID_SDK.getClientEngineValue()));
// assertThat(impression.getClientVersion(), is("0.0.0"));
}
Also used : ControlAttribute(com.optimizely.ab.internal.ControlAttribute) LogEvent(com.optimizely.ab.event.LogEvent) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) Test(org.junit.Test)

Example 59 with LogEvent

use of com.optimizely.ab.event.LogEvent in project java-sdk by optimizely.

the class EventFactoryTest method createImpressionEventIgnoresInvalidAttributes.

/**
 * Verify that passing through an list value attribute causes that attribute to be ignored, rather than
 * causing an exception to be thrown.
 */
@Test
public void createImpressionEventIgnoresInvalidAttributes() {
    assumeTrue(datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()));
    // use the "valid" project config and its associated experiment, variation, and attributes
    ProjectConfig projectConfig = validProjectConfig;
    Experiment activatedExperiment = projectConfig.getExperiments().get(0);
    Variation bucketedVariation = activatedExperiment.getVariations().get(0);
    Attribute attribute1 = validProjectConfig.getAttributes().get(0);
    Attribute attribute2 = validProjectConfig.getAttributes().get(1);
    BigInteger bigInteger = new BigInteger("12323");
    BigDecimal bigDecimal = new BigDecimal("123");
    HashMap<String, Object> attributes = new HashMap<>();
    attributes.put(attribute1.getKey(), bigInteger);
    attributes.put(attribute2.getKey(), bigDecimal);
    LogEvent impressionEvent = createImpressionEvent(projectConfig, activatedExperiment, bucketedVariation, "userId", attributes);
    EventBatch impression = gson.fromJson(impressionEvent.getBody(), EventBatch.class);
    // verify that no Feature is created for attribute.getKey() -> invalidAttribute
    for (com.optimizely.ab.event.internal.payload.Attribute feature : impression.getVisitors().get(0).getAttributes()) {
        assertNotSame(feature.getKey(), attribute1.getKey());
        assertNotSame(feature.getValue(), bigInteger);
        assertNotSame(feature.getKey(), attribute2.getKey());
        assertNotSame(feature.getValue(), bigDecimal);
    }
}
Also used : ControlAttribute(com.optimizely.ab.internal.ControlAttribute) LogEvent(com.optimizely.ab.event.LogEvent) BigDecimal(java.math.BigDecimal) BigInteger(java.math.BigInteger) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) Test(org.junit.Test)

Example 60 with LogEvent

use of com.optimizely.ab.event.LogEvent in project java-sdk by optimizely.

the class EventFactoryTest 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 = validProjectConfig;
    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(ControlAttribute.BUCKETING_ATTRIBUTE.toString(), "variation");
    Decision expectedDecision = new Decision.Builder().setCampaignId(activatedExperiment.getLayerId()).setExperimentId(activatedExperiment.getId()).setVariationId(bucketedVariation.getId()).setIsCampaignHoldback(false).build();
    com.optimizely.ab.event.internal.payload.Attribute feature = new com.optimizely.ab.event.internal.payload.Attribute.Builder().setEntityId(attribute.getId()).setKey(attribute.getKey()).setType(com.optimizely.ab.event.internal.payload.Attribute.CUSTOM_ATTRIBUTE_TYPE).setValue("value").build();
    com.optimizely.ab.event.internal.payload.Attribute feature1 = new com.optimizely.ab.event.internal.payload.Attribute.Builder().setEntityId(ControlAttribute.BUCKETING_ATTRIBUTE.toString()).setKey(ControlAttribute.BUCKETING_ATTRIBUTE.toString()).setType(com.optimizely.ab.event.internal.payload.Attribute.CUSTOM_ATTRIBUTE_TYPE).setValue("variation").build();
    List<com.optimizely.ab.event.internal.payload.Attribute> expectedUserFeatures = new ArrayList<com.optimizely.ab.event.internal.payload.Attribute>();
    expectedUserFeatures.add(feature);
    expectedUserFeatures.add(feature1);
    if (datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString())) {
        expectedUserFeatures.add(getBotFilteringAttribute());
    }
    LogEvent impressionEvent = createImpressionEvent(projectConfig, activatedExperiment, bucketedVariation, userId, attributeMap);
    // verify that request endpoint is correct
    assertThat(impressionEvent.getEndpointUrl(), is(EventFactory.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()));
    assertTrue(impression.getEnrichDecisions());
    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());
}
Also used : ControlAttribute(com.optimizely.ab.internal.ControlAttribute) LogEvent(com.optimizely.ab.event.LogEvent) Decision(com.optimizely.ab.event.internal.payload.Decision) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) Test(org.junit.Test)

Aggregations

LogEvent (com.optimizely.ab.event.LogEvent)66 Test (org.junit.Test)58 Experiment (com.optimizely.ab.config.Experiment)37 Variation (com.optimizely.ab.config.Variation)34 EventBatch (com.optimizely.ab.event.internal.payload.EventBatch)32 HashMap (java.util.HashMap)25 EventType (com.optimizely.ab.config.EventType)23 EventBuilder (com.optimizely.ab.event.internal.EventBuilder)21 Matchers.anyString (org.mockito.Matchers.anyString)21 ControlAttribute (com.optimizely.ab.internal.ControlAttribute)17 Map (java.util.Map)14 ImmutableMap (com.google.common.collect.ImmutableMap)11 Bucketer (com.optimizely.ab.bucketing.Bucketer)11 EventBuilderTest.createExperimentVariationMap (com.optimizely.ab.event.internal.EventBuilderTest.createExperimentVariationMap)11 ProjectConfig (com.optimizely.ab.config.ProjectConfig)10 Attribute (com.optimizely.ab.config.Attribute)9 Decision (com.optimizely.ab.event.internal.payload.Decision)9 DecisionService (com.optimizely.ab.bucketing.DecisionService)8 UserProfileService (com.optimizely.ab.bucketing.UserProfileService)6 NoOpErrorHandler (com.optimizely.ab.error.NoOpErrorHandler)6