Search in sources :

Example 21 with StringTypedProperty

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

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

the class CommonSchemaDataUtilsAndroidTest method typedProperty.

private static StringTypedProperty typedProperty(String key, String value) {
    StringTypedProperty stringTypedProperty = new StringTypedProperty();
    stringTypedProperty.setName(key);
    stringTypedProperty.setValue(value);
    return stringTypedProperty;
}
Also used : StringTypedProperty(com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty)

Example 23 with StringTypedProperty

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

the class CommonSchemaDataUtilsAndroidTest method baseDataMissing.

@Test
public void baseDataMissing() {
    /* When using invalid base data. */
    MockCommonSchemaLog log = new MockCommonSchemaLog();
    log.setExt(new Extensions());
    List<TypedProperty> properties = new ArrayList<>();
    StringTypedProperty a = new StringTypedProperty();
    a.setName("baseType");
    a.setValue("Some.Type");
    properties.add(a);
    CommonSchemaDataUtils.addCommonSchemaData(properties, log);
    /* Check data and metadata is missing type. */
    assertEquals(0, log.getData().getProperties().length());
    assertNull(log.getExt().getMetadata());
}
Also used : 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)

Example 24 with StringTypedProperty

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

the class CommonSchemaDataUtilsAndroidTest method baseTypeMissing.

@Test
public void baseTypeMissing() {
    /* When using invalid base data. */
    MockCommonSchemaLog log = new MockCommonSchemaLog();
    log.setExt(new Extensions());
    List<TypedProperty> properties = new ArrayList<>();
    StringTypedProperty a = new StringTypedProperty();
    a.setName("baseData.test");
    a.setValue("test");
    properties.add(a);
    CommonSchemaDataUtils.addCommonSchemaData(properties, log);
    /* Check data and metadata is missing type. */
    assertEquals(0, log.getData().getProperties().length());
    assertNull(log.getExt().getMetadata());
}
Also used : 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)

Example 25 with StringTypedProperty

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

the class CommonSchemaDataUtilsAndroidTest method overrideMetadataToNull.

@Test
public void overrideMetadataToNull() throws JSONException {
    MockCommonSchemaLog log = new MockCommonSchemaLog();
    log.setExt(new Extensions());
    List<TypedProperty> properties = new ArrayList<>();
    LongTypedProperty a = new LongTypedProperty();
    a.setName("a.b.c");
    a.setValue(1);
    properties.add(a);
    StringTypedProperty b = new StringTypedProperty();
    b.setName("a.b");
    b.setValue("2");
    properties.add(b);
    CommonSchemaDataUtils.addCommonSchemaData(properties, log);
    /* Check data. */
    JSONObject aData = new JSONObject();
    aData.put("b", "2");
    JSONObject expectedData = new JSONObject();
    expectedData.put("a", aData);
    assertEquals(expectedData.toString(), log.getData().getProperties().toString());
    /* Check metadata is null */
    assertNull(log.getExt().getMetadata());
}
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