use of com.microsoft.appcenter.ingestion.models.properties.DateTimeTypedProperty in project mobile-center-sdk-android by Microsoft.
the class CommonSchemaDataUtilsAndroidTest method dateTimeTypedProperty.
@Test
public void dateTimeTypedProperty() throws JSONException {
MockCommonSchemaLog log = new MockCommonSchemaLog();
List<TypedProperty> properties = new ArrayList<>();
DateTimeTypedProperty property = new DateTimeTypedProperty();
property.setName("a");
property.setValue(new Date(100));
properties.add(property);
CommonSchemaDataUtils.addCommonSchemaData(properties, log);
/* Check data. */
assertEquals(1, log.getData().getProperties().length());
assertEquals(new Date(100), JSONDateUtils.toDate(log.getData().getProperties().getString("a")));
/* Check metadata. */
JSONObject expectedMetadata = new JSONObject();
JSONObject a = new JSONObject();
a.put("a", DATA_TYPE_DATETIME);
expectedMetadata.put(METADATA_FIELDS, a);
assertNotNull(log.getExt());
assertNotNull(log.getExt().getMetadata());
assertEquals(expectedMetadata.toString(), log.getExt().getMetadata().getMetadata().toString());
}
use of com.microsoft.appcenter.ingestion.models.properties.DateTimeTypedProperty in project mobile-center-sdk-android by Microsoft.
the class AnalyticsTest method trackEventFromAppWithEventProperties.
@Test
public void trackEventFromAppWithEventProperties() {
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);
/* Prepare typed properties. */
Date date = new Date();
StringTypedProperty stringTypedProperty = new StringTypedProperty();
stringTypedProperty.setName("n0");
stringTypedProperty.setValue("value");
DateTimeTypedProperty dateTimeTypedProperty = new DateTimeTypedProperty();
dateTimeTypedProperty.setName("n1");
dateTimeTypedProperty.setValue(date);
LongTypedProperty longTypedProperty = new LongTypedProperty();
longTypedProperty.setName("n2");
longTypedProperty.setValue(0);
DoubleTypedProperty doubleTypedProperty = new DoubleTypedProperty();
doubleTypedProperty.setName("n3");
doubleTypedProperty.setValue(0);
BooleanTypedProperty booleanTypedProperty = new BooleanTypedProperty();
booleanTypedProperty.setName("n4");
booleanTypedProperty.setValue(true);
/* Send event with non-empty EventProperties. */
EventProperties eventProperties = new EventProperties();
eventProperties.set("n0", "value");
eventProperties.set("n1", date);
eventProperties.set("n2", 0L);
eventProperties.set("n3", 0d);
eventProperties.set("n4", true);
Analytics.trackEvent("eventName", eventProperties);
verify(channel).enqueue(argumentCaptor.capture(), anyString(), eq(DEFAULTS));
assertNotNull(argumentCaptor.getValue());
assertEquals("eventName", argumentCaptor.getValue().getName());
assertEquals(stringTypedProperty, argumentCaptor.getValue().getTypedProperties().get(0));
assertEquals(dateTimeTypedProperty, argumentCaptor.getValue().getTypedProperties().get(1));
assertEquals(longTypedProperty, argumentCaptor.getValue().getTypedProperties().get(2));
assertEquals(doubleTypedProperty, argumentCaptor.getValue().getTypedProperties().get(3));
assertEquals(booleanTypedProperty, argumentCaptor.getValue().getTypedProperties().get(4));
}
use of com.microsoft.appcenter.ingestion.models.properties.DateTimeTypedProperty in project mobile-center-sdk-android by Microsoft.
the class PropertyConfiguratorTest method typedProperty.
@SuppressWarnings("SameParameterValue")
private static DateTimeTypedProperty typedProperty(String name, Date value) {
DateTimeTypedProperty typedProperty = new DateTimeTypedProperty();
typedProperty.setName(name);
typedProperty.setValue(value);
return typedProperty;
}
use of com.microsoft.appcenter.ingestion.models.properties.DateTimeTypedProperty 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;
}
Aggregations