Search in sources :

Example 6 with TypedProperty

use of com.microsoft.appcenter.ingestion.models.properties.TypedProperty in project mobile-center-sdk-android by Microsoft.

the class PropertyConfiguratorTest method eventPropertiesCascading.

@Test
public void eventPropertiesCascading() {
    /* Create transmission target hierarchy. */
    AnalyticsTransmissionTarget grandParent = Analytics.getTransmissionTarget("grandParent");
    AnalyticsTransmissionTarget parent = grandParent.getTransmissionTarget("parent");
    AnalyticsTransmissionTarget child = parent.getTransmissionTarget("child");
    /* Set common properties across hierarchy with some overrides. */
    grandParent.getPropertyConfigurator().setEventProperty("a", "1");
    grandParent.getPropertyConfigurator().setEventProperty("b", "2");
    grandParent.getPropertyConfigurator().setEventProperty("c", "3");
    /* Override some. */
    parent.getPropertyConfigurator().setEventProperty("a", "11");
    parent.getPropertyConfigurator().setEventProperty("b", "22");
    /* And a new one. */
    parent.getPropertyConfigurator().setEventProperty("d", "44");
    /* Just to show we still get value from grandParent if we remove an override. */
    parent.getPropertyConfigurator().setEventProperty("c", "33");
    parent.getPropertyConfigurator().removeEventProperty("c");
    /* Overrides in child. */
    child.getPropertyConfigurator().setEventProperty("d", "444");
    /* New in child. */
    child.getPropertyConfigurator().setEventProperty("e", "555");
    child.getPropertyConfigurator().setEventProperty("f", "666");
    /* Track event in child. Override properties in trackEvent. */
    Map<String, String> properties = new LinkedHashMap<>();
    properties.put("f", "6666");
    properties.put("g", "7777");
    child.trackEvent("eventName", properties);
    /* Verify log that was sent. */
    ArgumentCaptor<EventLog> logArgumentCaptor = ArgumentCaptor.forClass(EventLog.class);
    verify(mChannel).enqueue(logArgumentCaptor.capture(), anyString(), eq(DEFAULTS));
    EventLog log = logArgumentCaptor.getValue();
    assertNotNull(log);
    assertEquals("eventName", log.getName());
    assertEquals(1, log.getTransmissionTargetTokens().size());
    assertTrue(log.getTransmissionTargetTokens().contains("child"));
    /* Verify properties. */
    assertNull(log.getProperties());
    List<TypedProperty> typedProperties = new ArrayList<>();
    typedProperties.add(typedProperty("a", "11"));
    typedProperties.add(typedProperty("b", "22"));
    typedProperties.add(typedProperty("c", "3"));
    typedProperties.add(typedProperty("d", "444"));
    typedProperties.add(typedProperty("e", "555"));
    typedProperties.add(typedProperty("f", "6666"));
    typedProperties.add(typedProperty("g", "7777"));
    assertUnorderedListEquals(typedProperties, log.getTypedProperties());
}
Also used : EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) CommonSchemaEventLog(com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) LinkedHashMap(java.util.LinkedHashMap) DateTimeTypedProperty(com.microsoft.appcenter.ingestion.models.properties.DateTimeTypedProperty) DoubleTypedProperty(com.microsoft.appcenter.ingestion.models.properties.DoubleTypedProperty) BooleanTypedProperty(com.microsoft.appcenter.ingestion.models.properties.BooleanTypedProperty) StringTypedProperty(com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty) LongTypedProperty(com.microsoft.appcenter.ingestion.models.properties.LongTypedProperty) TypedProperty(com.microsoft.appcenter.ingestion.models.properties.TypedProperty) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with TypedProperty

use of com.microsoft.appcenter.ingestion.models.properties.TypedProperty in project mobile-center-sdk-android by Microsoft.

the class PropertyConfiguratorTest method setCommonEventPropertiesWithNullMapProperties.

@Test
public void setCommonEventPropertiesWithNullMapProperties() {
    /* Create transmission target and add property. */
    AnalyticsTransmissionTarget target = Analytics.getTransmissionTarget("test");
    target.getPropertyConfigurator().setEventProperty("key", "value");
    /* Track event without property. */
    target.trackEvent("eventName", (Map<String, String>) null);
    /* Check event. */
    ArgumentCaptor<EventLog> eventLogArg = ArgumentCaptor.forClass(EventLog.class);
    verify(mChannel).enqueue(eventLogArg.capture(), anyString(), eq(DEFAULTS));
    EventLog log = eventLogArg.getValue();
    assertNotNull(log);
    assertEquals(Collections.singleton("test"), log.getTransmissionTargetTokens());
    assertEquals("eventName", log.getName());
    assertNull(log.getProperties());
    List<TypedProperty> typedProperties = new ArrayList<>();
    typedProperties.add(typedProperty("key", "value"));
    assertEquals(typedProperties, log.getTypedProperties());
}
Also used : EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) CommonSchemaEventLog(com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) DateTimeTypedProperty(com.microsoft.appcenter.ingestion.models.properties.DateTimeTypedProperty) DoubleTypedProperty(com.microsoft.appcenter.ingestion.models.properties.DoubleTypedProperty) BooleanTypedProperty(com.microsoft.appcenter.ingestion.models.properties.BooleanTypedProperty) StringTypedProperty(com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty) LongTypedProperty(com.microsoft.appcenter.ingestion.models.properties.LongTypedProperty) TypedProperty(com.microsoft.appcenter.ingestion.models.properties.TypedProperty) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with TypedProperty

use of com.microsoft.appcenter.ingestion.models.properties.TypedProperty in project mobile-center-sdk-android by Microsoft.

the class PropertyConfiguratorTest method trackEventWithCommonTypedProperties.

@Test
public void trackEventWithCommonTypedProperties() {
    /* Create transmission target. */
    AnalyticsTransmissionTarget target = Analytics.getTransmissionTarget("test");
    /* Set common properties for various types. Some of which are invalid. */
    target.getPropertyConfigurator().setEventProperty("myString", "hello");
    target.getPropertyConfigurator().setEventProperty("myNullString", (String) null);
    target.getPropertyConfigurator().setEventProperty("myTrue", true);
    target.getPropertyConfigurator().setEventProperty("myFalse", false);
    target.getPropertyConfigurator().setEventProperty("myLong", Long.MAX_VALUE);
    target.getPropertyConfigurator().setEventProperty("myDate", new Date(456));
    target.getPropertyConfigurator().setEventProperty("myNullDate", (Date) null);
    target.getPropertyConfigurator().setEventProperty("myDouble", -3.14E3);
    target.getPropertyConfigurator().setEventProperty("myNan", Double.NaN);
    target.getPropertyConfigurator().setEventProperty("myInfinite", Double.POSITIVE_INFINITY);
    target.getPropertyConfigurator().setEventProperty("myRemoved", "to be removed");
    target.getPropertyConfigurator().removeEventProperty("myRemoved");
    /* Track event with just a name. */
    target.trackEvent("eventName");
    /* Check what event was sent. */
    ArgumentCaptor<EventLog> eventLogArg = ArgumentCaptor.forClass(EventLog.class);
    verify(mChannel).enqueue(eventLogArg.capture(), anyString(), eq(DEFAULTS));
    EventLog log = eventLogArg.getValue();
    assertNotNull(log);
    assertEquals(Collections.singleton("test"), log.getTransmissionTargetTokens());
    assertEquals("eventName", log.getName());
    assertNull(log.getProperties());
    /* Check typed properties. */
    List<TypedProperty> typedProperties = new ArrayList<>();
    typedProperties.add(typedProperty("myString", "hello"));
    typedProperties.add(typedProperty("myTrue", true));
    typedProperties.add(typedProperty("myFalse", false));
    typedProperties.add(typedProperty("myLong", Long.MAX_VALUE));
    typedProperties.add(typedProperty("myDate", new Date(456)));
    typedProperties.add(typedProperty("myDouble", -3.14E3));
    assertUnorderedListEquals(typedProperties, log.getTypedProperties());
}
Also used : EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) CommonSchemaEventLog(com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog) ArrayList(java.util.ArrayList) Date(java.util.Date) DateTimeTypedProperty(com.microsoft.appcenter.ingestion.models.properties.DateTimeTypedProperty) DoubleTypedProperty(com.microsoft.appcenter.ingestion.models.properties.DoubleTypedProperty) BooleanTypedProperty(com.microsoft.appcenter.ingestion.models.properties.BooleanTypedProperty) StringTypedProperty(com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty) LongTypedProperty(com.microsoft.appcenter.ingestion.models.properties.LongTypedProperty) TypedProperty(com.microsoft.appcenter.ingestion.models.properties.TypedProperty) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 9 with TypedProperty

use of com.microsoft.appcenter.ingestion.models.properties.TypedProperty in project mobile-center-sdk-android by Microsoft.

the class AnalyticsValidator method validateProperties.

/**
 * Validates typed properties.
 *
 * @param properties Typed properties collection to validate.
 */
private static void validateProperties(List<TypedProperty> properties) {
    if (properties == null) {
        return;
    }
    int count = 0;
    boolean maxCountReached = false;
    String message;
    for (ListIterator<TypedProperty> iterator = properties.listIterator(); iterator.hasNext(); ) {
        boolean copyNeededOnModification = true;
        TypedProperty property = iterator.next();
        String key = property.getName();
        if (count >= MAX_PROPERTY_COUNT) {
            if (!maxCountReached) {
                message = String.format("Typed properties cannot contain more than %s items. Skipping other properties.", MAX_PROPERTY_COUNT);
                AppCenterLog.warn(LOG_TAG, message);
                maxCountReached = true;
            }
            iterator.remove();
            continue;
        }
        if (key == null || key.isEmpty()) {
            AppCenterLog.warn(LOG_TAG, "A typed property key cannot be null or empty. Property will be skipped.");
            iterator.remove();
            continue;
        }
        if (key.length() > MAX_PROPERTY_ITEM_LENGTH) {
            message = String.format("Typed property '%s' : property key length cannot be longer than %s characters. Property key will be truncated.", key, MAX_PROPERTY_ITEM_LENGTH);
            AppCenterLog.warn(LOG_TAG, message);
            key = key.substring(0, MAX_PROPERTY_ITEM_LENGTH);
            property = copyProperty(property, key);
            iterator.set(property);
            copyNeededOnModification = false;
        }
        if (property instanceof StringTypedProperty) {
            StringTypedProperty stringTypedProperty = (StringTypedProperty) property;
            String value = stringTypedProperty.getValue();
            if (value == null) {
                message = String.format("Typed property '%s' : property value cannot be null. Property '%s' will be skipped.", key, key);
                AppCenterLog.warn(LOG_TAG, message);
                iterator.remove();
                continue;
            }
            if (value.length() > MAX_PROPERTY_ITEM_LENGTH) {
                message = String.format("A String property '%s' : property value cannot be longer than %s characters. Property value will be truncated.", key, MAX_PROPERTY_ITEM_LENGTH);
                AppCenterLog.warn(LOG_TAG, message);
                value = value.substring(0, MAX_PROPERTY_ITEM_LENGTH);
                if (copyNeededOnModification) {
                    stringTypedProperty = new StringTypedProperty();
                    stringTypedProperty.setName(key);
                    stringTypedProperty.setValue(value);
                    iterator.set(stringTypedProperty);
                } else {
                    stringTypedProperty.setValue(value);
                }
            }
        }
        count++;
    }
}
Also used : StringTypedProperty(com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty) DoubleTypedProperty(com.microsoft.appcenter.ingestion.models.properties.DoubleTypedProperty) LongTypedProperty(com.microsoft.appcenter.ingestion.models.properties.LongTypedProperty) BooleanTypedProperty(com.microsoft.appcenter.ingestion.models.properties.BooleanTypedProperty) StringTypedProperty(com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty) TypedProperty(com.microsoft.appcenter.ingestion.models.properties.TypedProperty) DateTimeTypedProperty(com.microsoft.appcenter.ingestion.models.properties.DateTimeTypedProperty)

Example 10 with TypedProperty

use of com.microsoft.appcenter.ingestion.models.properties.TypedProperty in project mobile-center-sdk-android by Microsoft.

the class AnalyticsValidator method copyProperty.

private static TypedProperty copyProperty(TypedProperty property, String newKey) {
    String type = property.getType();
    TypedProperty copy;
    if (BooleanTypedProperty.TYPE.equals(type)) {
        BooleanTypedProperty typedCopy = new BooleanTypedProperty();
        typedCopy.setValue(((BooleanTypedProperty) property).getValue());
        copy = typedCopy;
    } else if (DateTimeTypedProperty.TYPE.equals(type)) {
        DateTimeTypedProperty typedCopy = new DateTimeTypedProperty();
        typedCopy.setValue(((DateTimeTypedProperty) property).getValue());
        copy = typedCopy;
    } else if (DoubleTypedProperty.TYPE.equals(type)) {
        DoubleTypedProperty typedCopy = new DoubleTypedProperty();
        typedCopy.setValue(((DoubleTypedProperty) property).getValue());
        copy = typedCopy;
    } else if (LongTypedProperty.TYPE.equals(type)) {
        LongTypedProperty typedCopy = new LongTypedProperty();
        typedCopy.setValue(((LongTypedProperty) property).getValue());
        copy = typedCopy;
    } else {
        /* SDK invariant: unknown property type is not possible with public APIs. */
        StringTypedProperty typedCopy = new StringTypedProperty();
        typedCopy.setValue(((StringTypedProperty) property).getValue());
        copy = typedCopy;
    }
    copy.setName(newKey);
    return copy;
}
Also used : DateTimeTypedProperty(com.microsoft.appcenter.ingestion.models.properties.DateTimeTypedProperty) DoubleTypedProperty(com.microsoft.appcenter.ingestion.models.properties.DoubleTypedProperty) LongTypedProperty(com.microsoft.appcenter.ingestion.models.properties.LongTypedProperty) BooleanTypedProperty(com.microsoft.appcenter.ingestion.models.properties.BooleanTypedProperty) StringTypedProperty(com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty) DoubleTypedProperty(com.microsoft.appcenter.ingestion.models.properties.DoubleTypedProperty) LongTypedProperty(com.microsoft.appcenter.ingestion.models.properties.LongTypedProperty) BooleanTypedProperty(com.microsoft.appcenter.ingestion.models.properties.BooleanTypedProperty) StringTypedProperty(com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty) TypedProperty(com.microsoft.appcenter.ingestion.models.properties.TypedProperty) DateTimeTypedProperty(com.microsoft.appcenter.ingestion.models.properties.DateTimeTypedProperty)

Aggregations

TypedProperty (com.microsoft.appcenter.ingestion.models.properties.TypedProperty)37 StringTypedProperty (com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty)35 BooleanTypedProperty (com.microsoft.appcenter.ingestion.models.properties.BooleanTypedProperty)31 DateTimeTypedProperty (com.microsoft.appcenter.ingestion.models.properties.DateTimeTypedProperty)31 DoubleTypedProperty (com.microsoft.appcenter.ingestion.models.properties.DoubleTypedProperty)31 LongTypedProperty (com.microsoft.appcenter.ingestion.models.properties.LongTypedProperty)31 ArrayList (java.util.ArrayList)31 Test (org.junit.Test)31 JSONObject (org.json.JSONObject)14 EventLog (com.microsoft.appcenter.analytics.ingestion.models.EventLog)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 CommonSchemaEventLog (com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog)6 TestUtils.generateString (com.microsoft.appcenter.test.TestUtils.generateString)5 Date (java.util.Date)4 HashMap (java.util.HashMap)3 Matchers.anyString (org.mockito.Matchers.anyString)3 Map (java.util.Map)2 JSONException (org.json.JSONException)2 Context (android.content.Context)1 EventProperties (com.microsoft.appcenter.analytics.EventProperties)1