use of com.linkedin.data.message.Message in project rest.li by linkedin.
the class TestFilter method testPathIncludedInError.
@Test
public void testPathIncludedInError() throws JsonParseException, IOException, DataProcessingException {
DataMap data = dataMapFromString("{ 'a': { 'x': 'a'}}".replace('\'', '"'));
DataMap filter = dataMapFromString("{ 'a': { 'x': { 'y': 1}}}".replace('\'', '"'));
DataComplexProcessor processor = new DataComplexProcessor(new Filter(), filter, data);
boolean thrown = false;
try {
processor.run(true);
} catch (DataProcessingException e) {
assertEquals(e.getMessages().size(), 1, "expected exactly 1 error");
Message m = e.getMessages().get(0);
assertNotNull(m.getPath(), "path should be set on a message");
assertEquals(m.getPath(), new Object[] { "a", "x" });
thrown = true;
}
assertEquals(thrown, true, "exception should have been thrown");
}
use of com.linkedin.data.message.Message in project rest.li by linkedin.
the class TestValidation method printErrorPaths.
private void printErrorPaths(PrintStream out, Collection<Message> messages) {
StringBuilder sb = new StringBuilder();
sb.append("new String[] { ");
boolean first = true;
for (Message m : messages) {
if (first == false)
sb.append(", ");
sb.append("\"").append(pathAsString(m.getPath())).append("\"");
first = false;
}
sb.append(" },");
out.println(sb);
}
use of com.linkedin.data.message.Message in project rest.li by linkedin.
the class TestValidation method checkMessagesErrorPath.
private void checkMessagesErrorPath(Collection<Message> messages, String[] errorPaths) {
int index = 0;
for (Message m : messages) {
if (index >= errorPaths.length) {
break;
}
String path = pathAsString(m.getPath());
Assert.assertEquals(path, errorPaths[index]);
index++;
}
}
use of com.linkedin.data.message.Message in project rest.li by linkedin.
the class AnyRecordValidator method schemaFromName.
protected DataSchema schemaFromName(ValidatorContext context, String schemaName) {
StringBuilder sb = new StringBuilder();
Parameter parameter = getParameter(context.validationOptions());
DataSchemaResolver resolver = parameter.resolver();
NamedDataSchema schema;
if (resolver == null) {
schema = null;
context.addResult(new Message(context.dataElement().path(schemaName), parameter.isValidSchema(), "%1$s cannot obtain schema for \"%2$s\", no resolver", AnyRecordValidator.class.getName(), schemaName));
} else {
schema = resolver.findDataSchema(schemaName, sb);
if (schema == null) {
context.addResult(new Message(context.dataElement().path(schemaName), parameter.isValidSchema(), "%1$s cannot obtain schema for \"%2$s\" (%3$s)", AnyRecordValidator.class.getName(), schemaName, sb.toString()));
}
}
return schema;
}
use of com.linkedin.data.message.Message in project rest.li by linkedin.
the class PegasusUnionToAvroRecordConvertCallback method modifyFieldDefaultValue.
/**
* Translates the default value specified on a field. The default value translation only happens for fields whose type
* is a union that uses aliases for its members. The modified default value will be for the equivalent Avro record that
* will be generated for this Union type during schema translation.
*
* @param field Reference to the union field whose default value is modified
* @param path The path of the union field whose default value is modified
* @return An instance of {@link DataMap} which is the modified default value or null if the default value doesn't
* have to be translated
*/
private DataMap modifyFieldDefaultValue(RecordDataSchema.Field field, List<String> path) {
DataMap modifiedDefaultValue = null;
// Find if the field's type is a union that uses aliases for its members
DataSchema fieldSchema = field.getType().getDereferencedDataSchema();
boolean unionWithMembersAliased = fieldSchema.getType() == DataSchema.Type.UNION && ((UnionDataSchema) fieldSchema).areMembersAliased();
// If the field is required or the OptionalDefaultMode.TRANSLATE_DEFAULT is used, propagate the default value to the new record
boolean propagateDefault = !field.getOptional() || _options.getOptionalDefaultMode() == OptionalDefaultMode.TRANSLATE_DEFAULT;
Object defaultValue = field.getDefault();
if (unionWithMembersAliased && propagateDefault && defaultValue != null) {
String key;
if (defaultValue == Data.NULL) {
key = DataSchemaConstants.NULL_TYPE;
} else {
DataMap unionMap = (DataMap) defaultValue;
if (unionMap.size() != 1) {
Message message = new Message(path.toArray(), "union default value $1%s has more than one entry", defaultValue);
throw new IllegalArgumentException(message.toString());
}
Map.Entry<String, Object> entry = unionMap.entrySet().iterator().next();
key = entry.getKey();
}
modifiedDefaultValue = (defaultValue == Data.NULL) ? new DataMap() : new DataMap((DataMap) defaultValue);
modifiedDefaultValue.put(DataSchemaConstants.DISCRIMINATOR_FIELD, key);
}
return modifiedDefaultValue;
}
Aggregations