Search in sources :

Example 11 with DoubleTypedProperty

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

the class CommonSchemaDataUtils method validateProperty.

/**
 * Validate typed property.
 *
 * @param property typed property.
 * @return property value.
 * @throws IllegalArgumentException if the property is invalid.
 * @throws JSONException            if JSON date formatting fails (never happens).
 */
private static Object validateProperty(TypedProperty property) throws IllegalArgumentException, JSONException {
    /* Validate key not null. */
    String key = property.getName();
    if (key == null) {
        throw new IllegalArgumentException("Property key cannot be null.");
    }
    /* Validate baseType. */
    if (key.equals(BASE_TYPE) && !(property instanceof StringTypedProperty)) {
        throw new IllegalArgumentException("baseType must be a string.");
    }
    if (key.startsWith(BASE_TYPE + ".")) {
        throw new IllegalArgumentException("baseType must be a string.");
    }
    /* Validate baseData is an object, meaning it has at least 1 dot. */
    if (key.equals(BASE_DATA)) {
        throw new IllegalArgumentException("baseData must be an object.");
    }
    /* Get value from property. */
    Object value;
    if (property instanceof StringTypedProperty) {
        StringTypedProperty stringTypedProperty = (StringTypedProperty) property;
        value = stringTypedProperty.getValue();
    } else if (property instanceof LongTypedProperty) {
        LongTypedProperty longTypedProperty = (LongTypedProperty) property;
        value = longTypedProperty.getValue();
    } else if (property instanceof DoubleTypedProperty) {
        DoubleTypedProperty doubleTypedProperty = (DoubleTypedProperty) property;
        value = doubleTypedProperty.getValue();
    } else if (property instanceof DateTimeTypedProperty) {
        value = JSONDateUtils.toString(((DateTimeTypedProperty) property).getValue());
    } else if (property instanceof BooleanTypedProperty) {
        BooleanTypedProperty booleanTypedProperty = (BooleanTypedProperty) property;
        value = booleanTypedProperty.getValue();
    } else {
        throw new IllegalArgumentException("Unsupported property type: " + property.getType());
    }
    /* Validate value not null. */
    if (value == null) {
        throw new IllegalArgumentException("Value of property with key '" + key + "' cannot be null.");
    }
    return value;
}
Also used : DoubleTypedProperty(com.microsoft.appcenter.ingestion.models.properties.DoubleTypedProperty) DateTimeTypedProperty(com.microsoft.appcenter.ingestion.models.properties.DateTimeTypedProperty) LongTypedProperty(com.microsoft.appcenter.ingestion.models.properties.LongTypedProperty) BooleanTypedProperty(com.microsoft.appcenter.ingestion.models.properties.BooleanTypedProperty) JSONObject(org.json.JSONObject) StringTypedProperty(com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty)

Example 12 with DoubleTypedProperty

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

the class CommonSchemaDataUtilsAndroidTest method noNestingAccident.

@Test
public void noNestingAccident() 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);
    DoubleTypedProperty b = new DoubleTypedProperty();
    b.setName("b.c");
    b.setValue(2.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.2);
    JSONObject expectedData = new JSONObject();
    expectedData.put("a", aData);
    expectedData.put("b", bData);
    assertEquals(expectedData.toString(), log.getData().getProperties().toString());
    /* Check metadata. a.b */
    JSONObject aFields = new JSONObject();
    aFields.put("b", DATA_TYPE_INT64);
    JSONObject aMetadata = new JSONObject();
    aMetadata.put(METADATA_FIELDS, aFields);
    /* b.c */
    JSONObject bFields = new JSONObject();
    bFields.put("c", DATA_TYPE_DOUBLE);
    JSONObject bMetadata = new JSONObject();
    bMetadata.put(METADATA_FIELDS, bFields);
    /* f at root. */
    JSONObject rootFields = new JSONObject();
    rootFields.put("a", aMetadata);
    rootFields.put("b", bMetadata);
    /* Check. */
    JSONObject expectedMetadata = new JSONObject();
    expectedMetadata.put(METADATA_FIELDS, rootFields);
    assertEquals(expectedMetadata.toString(), log.getExt().getMetadata().getMetadata().toString());
}
Also used : DoubleTypedProperty(com.microsoft.appcenter.ingestion.models.properties.DoubleTypedProperty) LongTypedProperty(com.microsoft.appcenter.ingestion.models.properties.LongTypedProperty) 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)

Aggregations

DoubleTypedProperty (com.microsoft.appcenter.ingestion.models.properties.DoubleTypedProperty)12 BooleanTypedProperty (com.microsoft.appcenter.ingestion.models.properties.BooleanTypedProperty)9 DateTimeTypedProperty (com.microsoft.appcenter.ingestion.models.properties.DateTimeTypedProperty)9 LongTypedProperty (com.microsoft.appcenter.ingestion.models.properties.LongTypedProperty)9 StringTypedProperty (com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty)9 Test (org.junit.Test)8 TypedProperty (com.microsoft.appcenter.ingestion.models.properties.TypedProperty)6 ArrayList (java.util.ArrayList)6 JSONObject (org.json.JSONObject)5 Date (java.util.Date)3 EventLog (com.microsoft.appcenter.analytics.ingestion.models.EventLog)2 Context (android.content.Context)1 PageLog (com.microsoft.appcenter.analytics.ingestion.models.PageLog)1 StartSessionLog (com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog)1 EventLogFactory (com.microsoft.appcenter.analytics.ingestion.models.json.EventLogFactory)1 PageLogFactory (com.microsoft.appcenter.analytics.ingestion.models.json.PageLogFactory)1 StartSessionLogFactory (com.microsoft.appcenter.analytics.ingestion.models.json.StartSessionLogFactory)1 CommonSchemaEventLog (com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog)1 Channel (com.microsoft.appcenter.channel.Channel)1 Device (com.microsoft.appcenter.ingestion.models.Device)1