Search in sources :

Example 26 with LogEvent

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

the class EventFactoryTest method createConversionEventIgnoresInvalidAndAcceptsValidAttributes.

/**
 * Verify that passing through an list value attribute causes that attribute to be ignored, rather than
 * causing an exception to be thrown and passing only the valid attributes.
 */
@Test
public void createConversionEventIgnoresInvalidAndAcceptsValidAttributes() {
    assumeTrue(datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()));
    EventType eventType = validProjectConfig.getEventTypes().get(0);
    Attribute attribute1 = validProjectConfig.getAttributes().get(0);
    Attribute attribute2 = validProjectConfig.getAttributes().get(1);
    Attribute doubleAttribute = validProjectConfig.getAttributes().get(5);
    Attribute integerAttribute = validProjectConfig.getAttributes().get(4);
    Attribute boolAttribute = validProjectConfig.getAttributes().get(3);
    Attribute emptyAttribute = validProjectConfig.getAttributes().get(6);
    BigInteger bigInteger = new BigInteger("12323");
    BigDecimal bigDecimal = new BigDecimal("123");
    double validDoubleAttribute = 13.1;
    int validIntegerAttribute = 12;
    boolean validBoolAttribute = true;
    Map<String, Object> eventTagMap = new HashMap<>();
    eventTagMap.put("boolean_param", false);
    eventTagMap.put("string_param", "123");
    HashMap<String, Object> attributes = new HashMap<>();
    attributes.put(attribute1.getKey(), bigInteger);
    attributes.put(attribute2.getKey(), bigDecimal);
    attributes.put(doubleAttribute.getKey(), validDoubleAttribute);
    attributes.put(integerAttribute.getKey(), validIntegerAttribute);
    attributes.put(boolAttribute.getKey(), validBoolAttribute);
    attributes.put(emptyAttribute.getKey(), validBoolAttribute);
    LogEvent conversionEvent = createConversionEvent(validProjectConfig, userId, eventType.getId(), eventType.getKey(), attributes, eventTagMap);
    EventBatch conversion = gson.fromJson(conversionEvent.getBody(), EventBatch.class);
    // Check valid attributes are getting passed.
    assertEquals(conversion.getVisitors().get(0).getAttributes().get(0).getKey(), boolAttribute.getKey());
    assertEquals(conversion.getVisitors().get(0).getAttributes().get(0).getValue(), validBoolAttribute);
    assertEquals(conversion.getVisitors().get(0).getAttributes().get(1).getKey(), doubleAttribute.getKey());
    assertEquals(conversion.getVisitors().get(0).getAttributes().get(1).getValue(), validDoubleAttribute);
    assertEquals(conversion.getVisitors().get(0).getAttributes().get(2).getKey(), integerAttribute.getKey());
    assertEquals((int) ((double) conversion.getVisitors().get(0).getAttributes().get(2).getValue()), validIntegerAttribute);
    // verify that no Feature is created for attribute.getKey() -> invalidAttribute
    for (com.optimizely.ab.event.internal.payload.Attribute feature : conversion.getVisitors().get(0).getAttributes()) {
        assertNotSame(feature.getKey(), attribute1.getKey());
        assertNotSame(feature.getValue(), bigInteger);
        assertNotSame(feature.getKey(), attribute2.getKey());
        assertNotSame(feature.getValue(), bigDecimal);
        assertNotSame(feature.getKey(), emptyAttribute.getKey());
        assertNotSame(feature.getValue(), doubleAttribute);
    }
}
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 27 with LogEvent

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

the class EventFactoryTest method createImpressionEventIgnoresUnknownAttributes.

/**
 * Verify that passing through an unknown attribute causes that attribute to be ignored, rather than
 * causing an exception to be thrown.
 */
@Test
public void createImpressionEventIgnoresUnknownAttributes() 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);
    LogEvent impressionEvent = createImpressionEvent(projectConfig, activatedExperiment, bucketedVariation, "userId", Collections.singletonMap("unknownAttribute", "blahValue"));
    EventBatch impression = gson.fromJson(impressionEvent.getBody(), EventBatch.class);
    // verify that no Feature is created for "unknownAtrribute" -> "blahValue"
    for (com.optimizely.ab.event.internal.payload.Attribute feature : impression.getVisitors().get(0).getAttributes()) {
        assertFalse(feature.getKey() == "unknownAttribute");
        assertFalse(feature.getValue() == "blahValue");
    }
}
Also used : LogEvent(com.optimizely.ab.event.LogEvent) EventBatch(com.optimizely.ab.event.internal.payload.EventBatch) Test(org.junit.Test)

Example 28 with LogEvent

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

the class EventFactoryTest method createConversionEventIgnoresNegativeInvalidAndAcceptsValidValueOfValidTypeAttributes.

/**
 * Verify that passing through an list of -ve invalid attribute value causes that attribute to be ignored, rather than
 * causing an exception to be thrown and passing only the valid attributes.
 */
@Test
public void createConversionEventIgnoresNegativeInvalidAndAcceptsValidValueOfValidTypeAttributes() {
    assumeTrue(datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString()));
    EventType eventType = validProjectConfig.getEventTypes().get(0);
    Attribute validFloatAttribute = validProjectConfig.getAttributes().get(0);
    Attribute invalidFloatAttribute = validProjectConfig.getAttributes().get(1);
    Attribute doubleAttribute = validProjectConfig.getAttributes().get(5);
    Attribute integerAttribute = validProjectConfig.getAttributes().get(4);
    Attribute emptyAttribute = validProjectConfig.getAttributes().get(6);
    float validFloatValue = -2.1f;
    float invalidFloatValue = -((float) (Math.pow(2, 53) + 2000000000));
    double invalidDoubleAttribute = -(Math.pow(2, 53) + 2);
    long validLongAttribute = -12;
    Map<String, Object> eventTagMap = new HashMap<>();
    eventTagMap.put("boolean_param", false);
    eventTagMap.put("string_param", "123");
    HashMap<String, Object> attributes = new HashMap<>();
    attributes.put(validFloatAttribute.getKey(), validFloatValue);
    attributes.put(invalidFloatAttribute.getKey(), invalidFloatValue);
    attributes.put(doubleAttribute.getKey(), invalidDoubleAttribute);
    attributes.put(integerAttribute.getKey(), validLongAttribute);
    LogEvent conversionEvent = createConversionEvent(validProjectConfig, userId, eventType.getId(), eventType.getKey(), attributes, eventTagMap);
    EventBatch conversion = gson.fromJson(conversionEvent.getBody(), EventBatch.class);
    // Check valid attributes are getting passed.
    assertEquals(conversion.getVisitors().get(0).getAttributes().get(0).getKey(), validFloatAttribute.getKey());
    // In below condition I am checking Value of float with double value because impression gets visitors from json so that converts it into double
    assertEquals(conversion.getVisitors().get(0).getAttributes().get(0).getValue(), -2.1);
    assertEquals(conversion.getVisitors().get(0).getAttributes().get(1).getKey(), integerAttribute.getKey());
    assertEquals((long) ((double) conversion.getVisitors().get(0).getAttributes().get(1).getValue()), validLongAttribute);
    // verify that no Feature is created for attribute.getKey() -> invalidAttribute
    for (com.optimizely.ab.event.internal.payload.Attribute feature : conversion.getVisitors().get(0).getAttributes()) {
        assertNotSame(feature.getKey(), invalidFloatAttribute.getKey());
        assertNotSame(feature.getValue(), invalidFloatValue);
        assertNotSame(feature.getKey(), doubleAttribute.getKey());
        assertNotSame(feature.getValue(), invalidDoubleAttribute);
        assertNotSame(feature.getKey(), emptyAttribute.getKey());
        assertNotSame(feature.getValue(), doubleAttribute);
    }
}
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 29 with LogEvent

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

the class EventFactoryTest method createImpressionEventIgnoresNullAttributes.

/**
 * Verify that passing through an null value attribute causes that attribute to be ignored, rather than
 * causing an exception to be thrown.
 */
@Test
public void createImpressionEventIgnoresNullAttributes() {
    // 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 = validProjectConfig.getAttributes().get(0);
    LogEvent impressionEvent = createImpressionEvent(projectConfig, activatedExperiment, bucketedVariation, "userId", Collections.singletonMap(attribute.getKey(), null));
    EventBatch impression = gson.fromJson(impressionEvent.getBody(), EventBatch.class);
    // verify that no Feature is created for attribute.getKey() -> null
    for (com.optimizely.ab.event.internal.payload.Attribute feature : impression.getVisitors().get(0).getAttributes()) {
        assertNotSame(feature.getKey(), attribute.getKey());
        assertNotSame(feature.getValue(), null);
    }
}
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 30 with LogEvent

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

the class Optimizely method sendImpression.

private void sendImpression(@Nonnull ProjectConfig projectConfig, @Nonnull Experiment experiment, @Nonnull String userId, @Nonnull Map<String, String> filteredAttributes, @Nonnull Variation variation) {
    if (experiment.isRunning()) {
        LogEvent impressionEvent = eventBuilder.createImpressionEvent(projectConfig, experiment, variation, userId, filteredAttributes);
        logger.info("Activating user \"{}\" in experiment \"{}\".", userId, experiment.getKey());
        logger.debug("Dispatching impression event to URL {} with params {} and payload \"{}\".", impressionEvent.getEndpointUrl(), impressionEvent.getRequestParams(), impressionEvent.getBody());
        try {
            eventHandler.dispatchEvent(impressionEvent);
        } catch (Exception e) {
            logger.error("Unexpected exception in event dispatcher", e);
        }
        notificationCenter.sendNotifications(NotificationCenter.NotificationType.Activate, experiment, userId, filteredAttributes, variation, impressionEvent);
    } else {
        logger.info("Experiment has \"Launched\" status so not dispatching event during activation.");
    }
}
Also used : LogEvent(com.optimizely.ab.event.LogEvent) ConfigParseException(com.optimizely.ab.config.parser.ConfigParseException)

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