use of com.microsoft.appcenter.ingestion.models.properties.TypedProperty in project mobile-center-sdk-android by Microsoft.
the class PropertyConfiguratorTest method eventPropertiesCascading.
@Test
public void eventPropertiesCascading() {
/* Create transmission target hierarchy. */
AnalyticsTransmissionTarget grandParent = Analytics.getTransmissionTarget("grandParent");
AnalyticsTransmissionTarget parent = grandParent.getTransmissionTarget("parent");
AnalyticsTransmissionTarget child = parent.getTransmissionTarget("child");
/* Set common properties across hierarchy with some overrides. */
grandParent.getPropertyConfigurator().setEventProperty("a", "1");
grandParent.getPropertyConfigurator().setEventProperty("b", "2");
grandParent.getPropertyConfigurator().setEventProperty("c", "3");
/* Override some. */
parent.getPropertyConfigurator().setEventProperty("a", "11");
parent.getPropertyConfigurator().setEventProperty("b", "22");
/* And a new one. */
parent.getPropertyConfigurator().setEventProperty("d", "44");
/* Just to show we still get value from grandParent if we remove an override. */
parent.getPropertyConfigurator().setEventProperty("c", "33");
parent.getPropertyConfigurator().removeEventProperty("c");
/* Overrides in child. */
child.getPropertyConfigurator().setEventProperty("d", "444");
/* New in child. */
child.getPropertyConfigurator().setEventProperty("e", "555");
child.getPropertyConfigurator().setEventProperty("f", "666");
/* Track event in child. Override properties in trackEvent. */
Map<String, String> properties = new LinkedHashMap<>();
properties.put("f", "6666");
properties.put("g", "7777");
child.trackEvent("eventName", properties);
/* Verify log that was sent. */
ArgumentCaptor<EventLog> logArgumentCaptor = ArgumentCaptor.forClass(EventLog.class);
verify(mChannel).enqueue(logArgumentCaptor.capture(), anyString(), eq(DEFAULTS));
EventLog log = logArgumentCaptor.getValue();
assertNotNull(log);
assertEquals("eventName", log.getName());
assertEquals(1, log.getTransmissionTargetTokens().size());
assertTrue(log.getTransmissionTargetTokens().contains("child"));
/* Verify properties. */
assertNull(log.getProperties());
List<TypedProperty> typedProperties = new ArrayList<>();
typedProperties.add(typedProperty("a", "11"));
typedProperties.add(typedProperty("b", "22"));
typedProperties.add(typedProperty("c", "3"));
typedProperties.add(typedProperty("d", "444"));
typedProperties.add(typedProperty("e", "555"));
typedProperties.add(typedProperty("f", "6666"));
typedProperties.add(typedProperty("g", "7777"));
assertUnorderedListEquals(typedProperties, log.getTypedProperties());
}
use of com.microsoft.appcenter.ingestion.models.properties.TypedProperty in project mobile-center-sdk-android by Microsoft.
the class PropertyConfiguratorTest method setCommonEventPropertiesWithNullMapProperties.
@Test
public void setCommonEventPropertiesWithNullMapProperties() {
/* Create transmission target and add property. */
AnalyticsTransmissionTarget target = Analytics.getTransmissionTarget("test");
target.getPropertyConfigurator().setEventProperty("key", "value");
/* Track event without property. */
target.trackEvent("eventName", (Map<String, String>) null);
/* Check event. */
ArgumentCaptor<EventLog> eventLogArg = ArgumentCaptor.forClass(EventLog.class);
verify(mChannel).enqueue(eventLogArg.capture(), anyString(), eq(DEFAULTS));
EventLog log = eventLogArg.getValue();
assertNotNull(log);
assertEquals(Collections.singleton("test"), log.getTransmissionTargetTokens());
assertEquals("eventName", log.getName());
assertNull(log.getProperties());
List<TypedProperty> typedProperties = new ArrayList<>();
typedProperties.add(typedProperty("key", "value"));
assertEquals(typedProperties, log.getTypedProperties());
}
use of com.microsoft.appcenter.ingestion.models.properties.TypedProperty in project mobile-center-sdk-android by Microsoft.
the class PropertyConfiguratorTest method trackEventWithCommonTypedProperties.
@Test
public void trackEventWithCommonTypedProperties() {
/* Create transmission target. */
AnalyticsTransmissionTarget target = Analytics.getTransmissionTarget("test");
/* Set common properties for various types. Some of which are invalid. */
target.getPropertyConfigurator().setEventProperty("myString", "hello");
target.getPropertyConfigurator().setEventProperty("myNullString", (String) null);
target.getPropertyConfigurator().setEventProperty("myTrue", true);
target.getPropertyConfigurator().setEventProperty("myFalse", false);
target.getPropertyConfigurator().setEventProperty("myLong", Long.MAX_VALUE);
target.getPropertyConfigurator().setEventProperty("myDate", new Date(456));
target.getPropertyConfigurator().setEventProperty("myNullDate", (Date) null);
target.getPropertyConfigurator().setEventProperty("myDouble", -3.14E3);
target.getPropertyConfigurator().setEventProperty("myNan", Double.NaN);
target.getPropertyConfigurator().setEventProperty("myInfinite", Double.POSITIVE_INFINITY);
target.getPropertyConfigurator().setEventProperty("myRemoved", "to be removed");
target.getPropertyConfigurator().removeEventProperty("myRemoved");
/* Track event with just a name. */
target.trackEvent("eventName");
/* Check what event was sent. */
ArgumentCaptor<EventLog> eventLogArg = ArgumentCaptor.forClass(EventLog.class);
verify(mChannel).enqueue(eventLogArg.capture(), anyString(), eq(DEFAULTS));
EventLog log = eventLogArg.getValue();
assertNotNull(log);
assertEquals(Collections.singleton("test"), log.getTransmissionTargetTokens());
assertEquals("eventName", log.getName());
assertNull(log.getProperties());
/* Check typed properties. */
List<TypedProperty> typedProperties = new ArrayList<>();
typedProperties.add(typedProperty("myString", "hello"));
typedProperties.add(typedProperty("myTrue", true));
typedProperties.add(typedProperty("myFalse", false));
typedProperties.add(typedProperty("myLong", Long.MAX_VALUE));
typedProperties.add(typedProperty("myDate", new Date(456)));
typedProperties.add(typedProperty("myDouble", -3.14E3));
assertUnorderedListEquals(typedProperties, log.getTypedProperties());
}
use of com.microsoft.appcenter.ingestion.models.properties.TypedProperty 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++;
}
}
use of com.microsoft.appcenter.ingestion.models.properties.TypedProperty 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;
}
Aggregations