use of com.microsoft.appcenter.ingestion.models.properties.DoubleTypedProperty 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;
}
use of com.microsoft.appcenter.ingestion.models.properties.DoubleTypedProperty in project mobile-center-sdk-android by Microsoft.
the class CommonSchemaDataUtilsAndroidTest method noNestingAccident.
@Test
public void noNestingAccident() 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);
DoubleTypedProperty b = new DoubleTypedProperty();
b.setName("b.c");
b.setValue(2.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.2);
JSONObject expectedData = new JSONObject();
expectedData.put("a", aData);
expectedData.put("b", bData);
assertEquals(expectedData.toString(), log.getData().getProperties().toString());
/* Check metadata. a.b */
JSONObject aFields = new JSONObject();
aFields.put("b", DATA_TYPE_INT64);
JSONObject aMetadata = new JSONObject();
aMetadata.put(METADATA_FIELDS, aFields);
/* b.c */
JSONObject bFields = new JSONObject();
bFields.put("c", DATA_TYPE_DOUBLE);
JSONObject bMetadata = new JSONObject();
bMetadata.put(METADATA_FIELDS, bFields);
/* f at root. */
JSONObject rootFields = new JSONObject();
rootFields.put("a", aMetadata);
rootFields.put("b", bMetadata);
/* Check. */
JSONObject expectedMetadata = new JSONObject();
expectedMetadata.put(METADATA_FIELDS, rootFields);
assertEquals(expectedMetadata.toString(), log.getExt().getMetadata().getMetadata().toString());
}
Aggregations