Search in sources :

Example 26 with TypedProperty

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

the class AnalyticsValidatorForEventLogTest method shouldFilterTooManyProperties.

@Test
public void shouldFilterTooManyProperties() {
    final String validEventName = "eventName";
    mEventLog.setName(validEventName);
    final String validPropertyItem = "valid";
    List<TypedProperty> properties = new ArrayList<>();
    for (int i = 0; i < 30; i++) {
        StringTypedProperty property = new StringTypedProperty();
        property.setName(validPropertyItem + i);
        property.setValue(validPropertyItem);
        properties.add(property);
    }
    mEventLog.setTypedProperties(properties);
    assertFalse(mAnalyticsValidator.shouldFilter(mEventLog));
    assertEquals(validEventName, mEventLog.getName());
    assertEquals(MAX_PROPERTY_COUNT, mEventLog.getTypedProperties().size());
}
Also used : ArrayList(java.util.ArrayList) TestUtils.generateString(com.microsoft.appcenter.test.TestUtils.generateString) 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)

Example 27 with TypedProperty

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

the class EventLogFactoryTest method convertEventWithoutProperties.

@Test
@PrepareForTest({ PartAUtils.class, CommonSchemaDataUtils.class })
public void convertEventWithoutProperties() {
    /* Mock utilities. */
    mockStatic(PartAUtils.class);
    mockStatic(CommonSchemaDataUtils.class);
    /* Create event log. */
    EventLog log = new EventLog();
    log.setName("test");
    /* Old properties are ignored. */
    Map<String, String> oldProperties = new HashMap<>();
    oldProperties.put("ignored", "ignored");
    log.setProperties(oldProperties);
    /* Set typed properties. */
    List<TypedProperty> properties = new ArrayList<>();
    StringTypedProperty stringTypedProperty = new StringTypedProperty();
    stringTypedProperty.setName("a");
    stringTypedProperty.setValue("b");
    properties.add(stringTypedProperty);
    log.setTypedProperties(properties);
    /* With 2 targets. */
    log.addTransmissionTarget("t1");
    log.addTransmissionTarget("t2");
    /* And with a tag. */
    Object tag = new Object();
    log.setTag(tag);
    /* When we convert logs. */
    Collection<CommonSchemaLog> convertedLogs = new EventLogFactory().toCommonSchemaLogs(log);
    /* Check number of logs: 1 per target. */
    assertNotNull(convertedLogs);
    assertEquals(2, convertedLogs.size());
    /* For each target. */
    for (CommonSchemaLog commonSchemaLog : convertedLogs) {
        /* Check name was added. */
        verifyStatic();
        PartAUtils.setName(same(commonSchemaLog), eq("test"));
        /* Check tag was added. */
        assertSame(tag, commonSchemaLog.getTag());
    }
    /* Check Part A was added with target tokens. */
    verifyStatic();
    PartAUtils.addPartAFromLog(eq(log), notNull(CommonSchemaLog.class), eq("t1"));
    verifyStatic();
    PartAUtils.addPartAFromLog(eq(log), notNull(CommonSchemaLog.class), eq("t2"));
    /* Check data was added with typed properties (and thus not old ones). */
    verifyStatic(times(2));
    CommonSchemaDataUtils.addCommonSchemaData(eq(properties), notNull(CommonSchemaLog.class));
}
Also used : HashMap(java.util.HashMap) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) ArrayList(java.util.ArrayList) StringTypedProperty(com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty) CommonSchemaEventLogFactory(com.microsoft.appcenter.analytics.ingestion.models.one.json.CommonSchemaEventLogFactory) StringTypedProperty(com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty) TypedProperty(com.microsoft.appcenter.ingestion.models.properties.TypedProperty) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 28 with TypedProperty

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

the class CommonSchemaDataUtils method addCommonSchemaData.

/**
 * Adds part B and part C properties to a log and Part A metadata.
 *
 * @param properties custom properties as source of data.
 * @param dest       destination common schema log.
 */
public static void addCommonSchemaData(List<TypedProperty> properties, CommonSchemaLog dest) {
    if (properties == null) {
        return;
    }
    try {
        /* Part B and C are mixed into the same top level data property. */
        Data data = new Data();
        dest.setData(data);
        /* We also build Part A metadata extension at the same time to reflect the data. */
        MetadataExtension metadata = new MetadataExtension();
        for (TypedProperty property : properties) {
            /* Validate property and get type. */
            Object value;
            try {
                value = validateProperty(property);
            } catch (IllegalArgumentException e) {
                AppCenterLog.warn(LOG_TAG, e.getMessage());
                continue;
            }
            /* Get metadata type. */
            Integer metadataType = getMetadataType(property);
            /* Split property name by dot. */
            String key = property.getName();
            String[] keys = key.split("\\.", -1);
            int lastIndex = keys.length - 1;
            /* Handle all intermediate keys. */
            JSONObject destProperties = data.getProperties();
            JSONObject destMetadata = metadata.getMetadata();
            for (int i = 0; i < lastIndex; i++) {
                /* Add data sub object. */
                String subKey = keys[i];
                JSONObject subDataObject = destProperties.optJSONObject(subKey);
                if (subDataObject == null) {
                    if (destProperties.has(subKey)) {
                        AppCenterLog.warn(LOG_TAG, "Property key '" + subKey + "' already has a value, the old value will be overridden.");
                    }
                    /* Add sub data intermediate object. */
                    subDataObject = new JSONObject();
                    destProperties.put(subKey, subDataObject);
                }
                destProperties = subDataObject;
                /* Handle metadata. */
                destMetadata = addIntermediateMetadata(destMetadata, subKey);
            }
            /* Handle the last key for data, the leaf. */
            String lastKey = keys[lastIndex];
            if (destProperties.has(lastKey)) {
                AppCenterLog.warn(LOG_TAG, "Property key '" + lastKey + "' already has a value, the old value will be overridden.");
            }
            destProperties.put(lastKey, value);
            /* Handle the last key for meta-data, the leaf. */
            addLeafMetadata(metadataType, destMetadata, lastKey);
        }
        /* Warn/cleanup if baseData and baseType are not paired. */
        JSONObject dataObject = data.getProperties();
        String baseType = dataObject.optString(BASE_TYPE, null);
        JSONObject baseData = dataObject.optJSONObject(BASE_DATA);
        if (baseType == null && baseData != null) {
            /* Discard unpaired data and metadata. */
            AppCenterLog.warn(LOG_TAG, "baseData was set but baseType is missing.");
            dataObject.remove(BASE_DATA);
            JSONObject baseMetaData = metadata.getMetadata().optJSONObject(METADATA_FIELDS);
            /* baseMetaData is always non null as baseData has at least 1 sub object and not cleaned up yet if empty. */
            baseMetaData.remove(BASE_DATA);
        }
        if (baseType != null && baseData == null) {
            /* Discard unpaired base type. */
            AppCenterLog.warn(LOG_TAG, "baseType was set but baseData is missing.");
            dataObject.remove(BASE_TYPE);
        }
        /* Add metadata extension only if not empty after cleanup. */
        if (!cleanUpEmptyObjectsInMetadata(metadata.getMetadata())) {
            if (dest.getExt() == null) {
                dest.setExt(new Extensions());
            }
            dest.getExt().setMetadata(metadata);
        }
    } catch (JSONException ignore) {
    /* Can only happen with NaN or Infinite but this is already checked before. */
    }
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) 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 29 with TypedProperty

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

the class CommonSchemaDataUtilsAndroidTest method overrideProperty.

@Test
public void overrideProperty() {
    MockCommonSchemaLog log = new MockCommonSchemaLog();
    List<TypedProperty> properties = new ArrayList<>();
    properties.add(typedProperty("a.b", "1"));
    properties.add(typedProperty("a.b.c.d", "2"));
    properties.add(typedProperty("a.b.c", "3"));
    CommonSchemaDataUtils.addCommonSchemaData(properties, log);
    JSONObject b = log.getData().getProperties().optJSONObject("a").optJSONObject("b");
    assertNotNull(b);
    assertEquals("3", b.optString("c", null));
    assertNull(log.getExt());
}
Also used : JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) 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)

Example 30 with TypedProperty

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

the class CommonSchemaDataUtilsAndroidTest method booleanTypedProperty.

@Test
public void booleanTypedProperty() throws JSONException {
    MockCommonSchemaLog log = new MockCommonSchemaLog();
    List<TypedProperty> properties = new ArrayList<>();
    BooleanTypedProperty property = new BooleanTypedProperty();
    property.setName("a");
    property.setValue(true);
    properties.add(property);
    CommonSchemaDataUtils.addCommonSchemaData(properties, log);
    assertEquals(1, log.getData().getProperties().length());
    assertTrue(log.getData().getProperties().getBoolean("a"));
}
Also used : BooleanTypedProperty(com.microsoft.appcenter.ingestion.models.properties.BooleanTypedProperty) ArrayList(java.util.ArrayList) 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

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