use of com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty in project mobile-center-sdk-android by Microsoft.
the class EventProperties method set.
/**
* Set the specified property value with the specified key.
* If the properties previously contained a property for the key, the old
* value is replaced.
*
* @param key key with which the specified value is to be set.
* @param value value to be set with the specified key.
* @return this instance.
*/
public EventProperties set(String key, String value) {
if (isValidKey(key) && isValidValue(value)) {
StringTypedProperty property = new StringTypedProperty();
property.setName(key);
property.setValue(value);
mProperties.put(key, property);
}
return this;
}
use of com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty 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.StringTypedProperty 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;
}
use of com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty in project mobile-center-sdk-android by Microsoft.
the class AnalyticsTest method trackEventFromAppWithMapProperties.
@Test
public void trackEventFromAppWithMapProperties() {
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);
/* Send event with non-empty Map properties. */
Analytics.trackEvent("eventName", new HashMap<String, String>() {
{
put("name", "value");
}
});
StringTypedProperty stringProperty = new StringTypedProperty();
stringProperty.setName("name");
stringProperty.setValue("value");
verify(channel).enqueue(argumentCaptor.capture(), anyString(), eq(DEFAULTS));
assertNotNull(argumentCaptor.getValue());
assertEquals("eventName", argumentCaptor.getValue().getName());
assertEquals(Collections.<TypedProperty>singletonList(stringProperty), argumentCaptor.getValue().getTypedProperties());
}
use of com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty in project mobile-center-sdk-android by Microsoft.
the class CommonSchemaDataUtilsAndroidTest method noMetadataCleanupOnNestingString.
@Test
public void noMetadataCleanupOnNestingString() 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);
StringTypedProperty b = new StringTypedProperty();
b.setName("b.c");
b.setValue("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");
JSONObject expectedData = new JSONObject();
expectedData.put("a", aData);
expectedData.put("b", bData);
assertEquals(expectedData.toString(), log.getData().getProperties().toString());
/* Check metadata. a.b only. */
JSONObject aFields = new JSONObject();
aFields.put("b", DATA_TYPE_INT64);
JSONObject aMetadata = new JSONObject();
aMetadata.put(METADATA_FIELDS, aFields);
JSONObject rootFields = new JSONObject();
rootFields.put("a", aMetadata);
JSONObject expectedMetadata = new JSONObject();
expectedMetadata.put(METADATA_FIELDS, rootFields);
assertEquals(expectedMetadata.toString(), log.getExt().getMetadata().getMetadata().toString());
}
Aggregations