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());
}
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));
}
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. */
}
}
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());
}
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"));
}
Aggregations