Search in sources :

Example 6 with StringTypedProperty

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

the class EventProperties method set.

/**
 * Set the specified property value with the specified key.
 * If the properties previously contained a property for the key, the old
 * value is replaced.
 *
 * @param key   key with which the specified value is to be set.
 * @param value value to be set with the specified key.
 * @return this instance.
 */
public EventProperties set(String key, String value) {
    if (isValidKey(key) && isValidValue(value)) {
        StringTypedProperty property = new StringTypedProperty();
        property.setName(key);
        property.setValue(value);
        mProperties.put(key, property);
    }
    return this;
}
Also used : StringTypedProperty(com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty)

Example 7 with StringTypedProperty

use of com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty 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 8 with StringTypedProperty

use of com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty 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)

Example 9 with StringTypedProperty

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

the class AnalyticsTest method trackEventFromAppWithMapProperties.

@Test
public void trackEventFromAppWithMapProperties() {
    Analytics analytics = Analytics.getInstance();
    Channel channel = mock(Channel.class);
    ArgumentCaptor<EventLog> argumentCaptor = ArgumentCaptor.forClass(EventLog.class);
    analytics.onStarting(mAppCenterHandler);
    analytics.onStarted(mock(Context.class), channel, "", null, true);
    /* Send event with non-empty Map properties. */
    Analytics.trackEvent("eventName", new HashMap<String, String>() {

        {
            put("name", "value");
        }
    });
    StringTypedProperty stringProperty = new StringTypedProperty();
    stringProperty.setName("name");
    stringProperty.setValue("value");
    verify(channel).enqueue(argumentCaptor.capture(), anyString(), eq(DEFAULTS));
    assertNotNull(argumentCaptor.getValue());
    assertEquals("eventName", argumentCaptor.getValue().getName());
    assertEquals(Collections.<TypedProperty>singletonList(stringProperty), argumentCaptor.getValue().getTypedProperties());
}
Also used : Context(android.content.Context) UserIdContext(com.microsoft.appcenter.utils.context.UserIdContext) Channel(com.microsoft.appcenter.channel.Channel) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) CommonSchemaEventLog(com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog) Matchers.anyString(org.mockito.Matchers.anyString) StringTypedProperty(com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty) Test(org.junit.Test)

Example 10 with StringTypedProperty

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

the class CommonSchemaDataUtilsAndroidTest method noMetadataCleanupOnNestingString.

@Test
public void noMetadataCleanupOnNestingString() throws JSONException {
    MockCommonSchemaLog log = new MockCommonSchemaLog();
    log.setExt(new Extensions());
    List<TypedProperty> properties = new ArrayList<>();
    LongTypedProperty a = new LongTypedProperty();
    a.setName("a.b");
    a.setValue(1);
    properties.add(a);
    StringTypedProperty b = new StringTypedProperty();
    b.setName("b.c");
    b.setValue("2");
    properties.add(b);
    CommonSchemaDataUtils.addCommonSchemaData(properties, log);
    /* Check data. */
    JSONObject aData = new JSONObject();
    aData.put("b", 1);
    JSONObject bData = new JSONObject();
    bData.put("c", "2");
    JSONObject expectedData = new JSONObject();
    expectedData.put("a", aData);
    expectedData.put("b", bData);
    assertEquals(expectedData.toString(), log.getData().getProperties().toString());
    /* Check metadata. a.b only. */
    JSONObject aFields = new JSONObject();
    aFields.put("b", DATA_TYPE_INT64);
    JSONObject aMetadata = new JSONObject();
    aMetadata.put(METADATA_FIELDS, aFields);
    JSONObject rootFields = new JSONObject();
    rootFields.put("a", aMetadata);
    JSONObject expectedMetadata = new JSONObject();
    expectedMetadata.put(METADATA_FIELDS, rootFields);
    assertEquals(expectedMetadata.toString(), log.getExt().getMetadata().getMetadata().toString());
}
Also used : LongTypedProperty(com.microsoft.appcenter.ingestion.models.properties.LongTypedProperty) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) 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) Test(org.junit.Test)

Aggregations

StringTypedProperty (com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty)25 TypedProperty (com.microsoft.appcenter.ingestion.models.properties.TypedProperty)17 Test (org.junit.Test)17 BooleanTypedProperty (com.microsoft.appcenter.ingestion.models.properties.BooleanTypedProperty)16 DateTimeTypedProperty (com.microsoft.appcenter.ingestion.models.properties.DateTimeTypedProperty)16 DoubleTypedProperty (com.microsoft.appcenter.ingestion.models.properties.DoubleTypedProperty)16 LongTypedProperty (com.microsoft.appcenter.ingestion.models.properties.LongTypedProperty)16 ArrayList (java.util.ArrayList)16 EventLog (com.microsoft.appcenter.analytics.ingestion.models.EventLog)5 TestUtils.generateString (com.microsoft.appcenter.test.TestUtils.generateString)5 JSONObject (org.json.JSONObject)5 Context (android.content.Context)3 CommonSchemaEventLog (com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog)3 Channel (com.microsoft.appcenter.channel.Channel)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 Matchers.anyString (org.mockito.Matchers.anyString)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 UserIdContext (com.microsoft.appcenter.utils.context.UserIdContext)2 PageLog (com.microsoft.appcenter.analytics.ingestion.models.PageLog)1