use of com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty 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.StringTypedProperty in project mobile-center-sdk-android by Microsoft.
the class CommonSchemaDataUtilsAndroidTest method typedProperty.
private static StringTypedProperty typedProperty(String key, String value) {
StringTypedProperty stringTypedProperty = new StringTypedProperty();
stringTypedProperty.setName(key);
stringTypedProperty.setValue(value);
return stringTypedProperty;
}
use of com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty in project mobile-center-sdk-android by Microsoft.
the class CommonSchemaDataUtilsAndroidTest method baseDataMissing.
@Test
public void baseDataMissing() {
/* When using invalid base data. */
MockCommonSchemaLog log = new MockCommonSchemaLog();
log.setExt(new Extensions());
List<TypedProperty> properties = new ArrayList<>();
StringTypedProperty a = new StringTypedProperty();
a.setName("baseType");
a.setValue("Some.Type");
properties.add(a);
CommonSchemaDataUtils.addCommonSchemaData(properties, log);
/* Check data and metadata is missing type. */
assertEquals(0, log.getData().getProperties().length());
assertNull(log.getExt().getMetadata());
}
use of com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty in project mobile-center-sdk-android by Microsoft.
the class CommonSchemaDataUtilsAndroidTest method baseTypeMissing.
@Test
public void baseTypeMissing() {
/* When using invalid base data. */
MockCommonSchemaLog log = new MockCommonSchemaLog();
log.setExt(new Extensions());
List<TypedProperty> properties = new ArrayList<>();
StringTypedProperty a = new StringTypedProperty();
a.setName("baseData.test");
a.setValue("test");
properties.add(a);
CommonSchemaDataUtils.addCommonSchemaData(properties, log);
/* Check data and metadata is missing type. */
assertEquals(0, log.getData().getProperties().length());
assertNull(log.getExt().getMetadata());
}
use of com.microsoft.appcenter.ingestion.models.properties.StringTypedProperty in project mobile-center-sdk-android by Microsoft.
the class CommonSchemaDataUtilsAndroidTest method overrideMetadataToNull.
@Test
public void overrideMetadataToNull() throws JSONException {
MockCommonSchemaLog log = new MockCommonSchemaLog();
log.setExt(new Extensions());
List<TypedProperty> properties = new ArrayList<>();
LongTypedProperty a = new LongTypedProperty();
a.setName("a.b.c");
a.setValue(1);
properties.add(a);
StringTypedProperty b = new StringTypedProperty();
b.setName("a.b");
b.setValue("2");
properties.add(b);
CommonSchemaDataUtils.addCommonSchemaData(properties, log);
/* Check data. */
JSONObject aData = new JSONObject();
aData.put("b", "2");
JSONObject expectedData = new JSONObject();
expectedData.put("a", aData);
assertEquals(expectedData.toString(), log.getData().getProperties().toString());
/* Check metadata is null */
assertNull(log.getExt().getMetadata());
}
Aggregations