Search in sources :

Example 16 with Message

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");
}
Also used : Message(com.linkedin.data.message.Message) DataComplexProcessor(com.linkedin.data.transform.DataComplexProcessor) DataMap(com.linkedin.data.DataMap) DataProcessingException(com.linkedin.data.transform.DataProcessingException) Test(org.testng.annotations.Test)

Example 17 with Message

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);
}
Also used : Message(com.linkedin.data.message.Message)

Example 18 with Message

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++;
    }
}
Also used : Message(com.linkedin.data.message.Message) TestUtil.dataMapFromString(com.linkedin.data.TestUtil.dataMapFromString) ByteString(com.linkedin.data.ByteString) TestUtil.dataSchemaFromString(com.linkedin.data.TestUtil.dataSchemaFromString)

Example 19 with Message

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;
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) Message(com.linkedin.data.message.Message) DataSchemaResolver(com.linkedin.data.schema.DataSchemaResolver)

Example 20 with Message

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;
}
Also used : EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) Message(com.linkedin.data.message.Message) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map) DataMap(com.linkedin.data.DataMap)

Aggregations

Message (com.linkedin.data.message.Message)20 DataElement (com.linkedin.data.element.DataElement)7 ValidationResult (com.linkedin.data.schema.validation.ValidationResult)5 DataMap (com.linkedin.data.DataMap)4 SimpleDataElement (com.linkedin.data.element.SimpleDataElement)4 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)3 TestUtil.dataMapFromString (com.linkedin.data.TestUtil.dataMapFromString)2 TestUtil.dataSchemaFromString (com.linkedin.data.TestUtil.dataSchemaFromString)2 DataSchema (com.linkedin.data.schema.DataSchema)2 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)2 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)2 DataComplexProcessor (com.linkedin.data.transform.DataComplexProcessor)2 DataProcessingException (com.linkedin.data.transform.DataProcessingException)2 MockBadRequest (com.linkedin.restli.server.errors.MockBadRequest)2 MockInputError (com.linkedin.restli.server.errors.MockInputError)2 MockInputErrorArray (com.linkedin.restli.server.errors.MockInputErrorArray)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Test (org.testng.annotations.Test)2 ByteString (com.linkedin.data.ByteString)1