Search in sources :

Example 6 with DataElement

use of com.linkedin.data.element.DataElement in project rest.li by linkedin.

the class TestDataIterator method testInitialEmptyDataComplexOrder.

@Test(dataProvider = "orders")
public void testInitialEmptyDataComplexOrder(final IterationOrder order) throws IOException {
    String input = "{ \"a\": { \"b\": {} } }";
    Object o = jsonToObject(input);
    DataIterator i = Builder.create(o, null, order).dataIterator();
    DataElement e = i.next();
    if (order == IterationOrder.PRE_ORDER) {
        assertEquals(e.level(), 0, "preorder traversal of " + input + " started with level " + e.level());
    } else {
        assertEquals(e.level(), 2, "postorder traversal of " + input + " started with level " + e.level());
    }
}
Also used : SimpleDataElement(com.linkedin.data.element.SimpleDataElement) DataElement(com.linkedin.data.element.DataElement) ByteString(com.linkedin.data.ByteString) Test(org.testng.annotations.Test)

Example 7 with DataElement

use of com.linkedin.data.element.DataElement in project rest.li by linkedin.

the class TestDataIterator method testToString.

@Test(dataProvider = "orders")
public void testToString(final IterationOrder order) throws IOException {
    String input = "{ \"a\" : { \"a1\" : \"A1\" }, \"b\" : [ 1.0, 2.0 ] }";
    Object o = jsonToObject(input);
    Builder.create(o, null, order).iterate(new Builder.Callback() {

        public void callback(DataElement e) {
            if (e.getName().equals("a1")) {
                assertEquals(e.toString(), "/a/a1:A1");
            } else if (e.getName().equals(1)) {
                assertEquals(e.toString(), "/b/1:2.0");
            }
        }
    });
}
Also used : SimpleDataElement(com.linkedin.data.element.SimpleDataElement) DataElement(com.linkedin.data.element.DataElement) ByteString(com.linkedin.data.ByteString) Test(org.testng.annotations.Test)

Example 8 with DataElement

use of com.linkedin.data.element.DataElement in project rest.li by linkedin.

the class TestDataIterator method testBuilderCreate.

@Test(dataProvider = "orders")
public void testBuilderCreate(final IterationOrder order) throws IOException {
    String input = "{ \"a\" : { \"a1\" : \"A1\" }, \"b\" : [ 1.0, 2.0 ] }";
    Object o = jsonToObject(input);
    Builder.create(o, null, order).iterate(new Builder.Callback() {

        public void callback(DataElement e) {
            if (e.getName().equals("a")) {
                Builder builder = Builder.create(e.getValue(), e.getSchema(), order);
                assertEqualsByName(builder, Arrays.asList((Object) DataElement.ROOT_NAME, "a1"));
                assertEqualsByPath(builder, Arrays.asList("", "/a1"));
                Builder builder2 = Builder.create(e, order);
                assertEqualsByName(builder2, Arrays.asList((Object) "a", "a1"));
                assertEqualsByPath(builder2, Arrays.asList("/a", "/a/a1"));
            } else if (e.getName().equals("b")) {
                Builder builder = Builder.create(e.getValue(), e.getSchema(), order);
                assertEqualsByName(builder, Arrays.asList((Object) DataElement.ROOT_NAME, 0, 1));
                assertEqualsByPath(builder, Arrays.asList("", "/0", "/1"));
                Builder builder2 = Builder.create(e, order);
                assertEqualsByName(builder2, Arrays.asList((Object) "b", 0, 1));
                assertEqualsByPath(builder2, Arrays.asList("/b", "/b/0", "/b/1"));
            }
        }
    });
}
Also used : SimpleDataElement(com.linkedin.data.element.SimpleDataElement) DataElement(com.linkedin.data.element.DataElement) ByteString(com.linkedin.data.ByteString) Test(org.testng.annotations.Test)

Example 9 with DataElement

use of com.linkedin.data.element.DataElement in project rest.li by linkedin.

the class AnyRecordValidator method validate.

@Override
public void validate(ValidatorContext context) {
    DataElement dataElement = context.dataElement();
    Object value = dataElement.getValue();
    DataSchema schema = dataElement.getSchema();
    if (schema.getType() != DataSchema.Type.RECORD || (((RecordDataSchema) schema).getFullName()).equals(SCHEMA_NAME) == false) {
        context.addResult(new Message(context.dataElement().path(), "%1$s invoked on schema that is not %2$s", AnyRecordValidator.class.getName(), SCHEMA_NAME));
    } else if (value.getClass() != DataMap.class) {
        context.addResult(new Message(context.dataElement().path(), "%1$s expects data to be a DataMap, data is %2$s", AnyRecordValidator.class.getName(), value));
    } else {
        DataMap dataMap = (DataMap) value;
        if (dataMap.size() != 1) {
            context.addResult(new Message(context.dataElement().path(), "%1$s expects data to be a DataMap with one entry, data is %2$s", AnyRecordValidator.class.getName(), value));
        } else {
            Map.Entry<String, Object> entry = dataMap.entrySet().iterator().next();
            String anySchemaName = entry.getKey();
            Object anyValue = entry.getValue();
            DataSchema anySchema = schemaFromName(context, anySchemaName);
            if (anySchema != null) {
                DataElement anyElement = new SimpleDataElement(anyValue, entry.getKey(), anySchema, dataElement);
                // do we want to have cache for anySchemaName to validator
                // do we care about classMap argument to DataSchemaAnnotationValidator
                DataSchemaAnnotationValidator validator = new DataSchemaAnnotationValidator(anySchema);
                if (validator.isInitOk() == false) {
                    boolean errorIfNotValidated = getParameter(context.validationOptions()).isValidSchema();
                    context.addResult(new Message(context.dataElement().path(), errorIfNotValidated, "%1$s failed to initialize %2$s with %3$s", AnyRecordValidator.class.getName(), DataSchemaAnnotationValidator.class.getSimpleName(), anySchema));
                    addResult(context, errorIfNotValidated, validator.getInitMessages());
                } else {
                    ValidationResult result = ValidateDataAgainstSchema.validate(anyElement, context.validationOptions(), validator);
                    addResult(context, result.getMessages());
                    if (result.hasFix())
                        context.setHasFix(true);
                    if (result.hasFixupReadOnlyError())
                        context.setHasFixupReadOnlyError(true);
                }
            }
        }
    }
}
Also used : RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) SimpleDataElement(com.linkedin.data.element.SimpleDataElement) DataElement(com.linkedin.data.element.DataElement) Message(com.linkedin.data.message.Message) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) SimpleDataElement(com.linkedin.data.element.SimpleDataElement) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) DataMap(com.linkedin.data.DataMap)

Example 10 with DataElement

use of com.linkedin.data.element.DataElement in project rest.li by linkedin.

the class TestValidation method testNonRootStartDataElement.

@Test
public void testNonRootStartDataElement() throws IOException {
    String schemaText = "{\n" + "  \"name\" : \"Foo\",\n" + "  \"type\" : \"record\",\n" + "  \"fields\" : [\n" + "    { \"name\" : \"intField\", \"type\" : \"int\", \"optional\" : true },\n" + "    { \"name\" : \"stringField\", \"type\" : \"string\", \"optional\" : true },\n" + "    { \"name\" : \"arrayField\", \"type\" : { \"type\" : \"array\", \"items\" : \"Foo\" }, \"optional\" : true },\n" + "    { \"name\" : \"mapField\", \"type\" : { \"type\" : \"map\", \"values\" : \"Foo\" }, \"optional\" : true },\n" + "    { \"name\" : \"unionField\", \"type\" : [ \"int\", \"string\", \"Foo\" ], \"optional\" : true },\n" + "    { \"name\" : \"fooField\", \"type\" : \"Foo\", \"optional\" : true }\n" + "  ]\n" + "}\n";
    String[] empty = {};
    Object[][] input = { { "{ \"intField\" : \"bad\", \"fooField\" : { \"intField\" : 32 } }", "/fooField", empty, new String[] { "ERROR" } }, { "{ \"intField\" : 32, \"fooField\" : { \"intField\" : \"bad\" } }", "/fooField", new String[] { "ERROR", "/fooField/intField" }, empty }, { "{\n" + "  \"stringField\" : 32,\n" + "  \"arrayField\" : [ { \"intField\" : \"bad0\" }, { \"intField\" : \"bad1\" } ]\n" + "}\n", "/arrayField/0", new String[] { "ERROR", "/arrayField/0/intField" }, new String[] { "/stringField", "/arrayField/1/intField" } }, { "{\n" + "  \"stringField\" : 32,\n" + "  \"mapField\" : { \"m0\" : { \"intField\" : \"bad0\" }, \"m1\" : { \"intField\" : \"bad1\" } }\n" + "}\n", "/mapField/m1", new String[] { "ERROR", "/mapField/m1/intField" }, new String[] { "/stringField", "/mapField/m0/intField" } }, { "{\n" + "  \"stringField\" : 32,\n" + "  \"arrayField\" : [\n" + "    { \"unionField\" : { \"Foo\" : { \"intField\" : \"bad0\" } } },\n" + "    { \"unionField\" : { \"int\" : \"bad1\" } }\n" + "  ]\n" + "}\n", "/arrayField/0/unionField", new String[] { "ERROR", "/arrayField/0/unionField/Foo/intField" }, new String[] { "/stringField", "/arrayField/1/unionField/int" } }, { "{\n" + "  \"stringField\" : 32,\n" + "  \"fooField\" : {\n" + "    \"stringField\" : 45,\n" + "    \"fooField\" : { \"intField\" : \"bad1\" } }\n" + "  }\n" + "}\n", "/fooField/fooField", new String[] { "ERROR", "/fooField/fooField/intField" }, new String[] { "/stringField", "/fooField/stringField" } } };
    DataSchema schema = dataSchemaFromString(schemaText);
    for (Object[] row : input) {
        String dataString = (String) row[0];
        String startPath = (String) row[1];
        String[] expectedStrings = (String[]) row[2];
        String[] notExpectedStrings = (String[]) row[3];
        DataMap map = dataMapFromString(dataString);
        DataElement startElement = DataElementUtil.element(map, schema, startPath);
        Assert.assertNotSame(startElement, null);
        ValidationResult result = validate(startElement, new ValidationOptions());
        String message = result.getMessages().toString();
        for (String expected : expectedStrings) {
            Assert.assertTrue(message.contains(expected), message + " does not contain " + expected);
        }
        for (String notExpected : notExpectedStrings) {
            Assert.assertFalse(message.contains(notExpected), message + " contains " + notExpected);
        }
    }
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataElement(com.linkedin.data.element.DataElement) TestUtil.dataMapFromString(com.linkedin.data.TestUtil.dataMapFromString) ByteString(com.linkedin.data.ByteString) TestUtil.dataSchemaFromString(com.linkedin.data.TestUtil.dataSchemaFromString) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Aggregations

DataElement (com.linkedin.data.element.DataElement)20 SimpleDataElement (com.linkedin.data.element.SimpleDataElement)11 ByteString (com.linkedin.data.ByteString)7 Test (org.testng.annotations.Test)7 DataMap (com.linkedin.data.DataMap)5 Message (com.linkedin.data.message.Message)5 DataSchema (com.linkedin.data.schema.DataSchema)4 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)4 ArrayList (java.util.ArrayList)4 ValidationResult (com.linkedin.data.schema.validation.ValidationResult)3 TestUtil.dataMapFromString (com.linkedin.data.TestUtil.dataMapFromString)2 TestUtil.dataSchemaFromString (com.linkedin.data.TestUtil.dataSchemaFromString)2 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)2 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)2 DataComplex (com.linkedin.data.DataComplex)1 TestUtil.asList (com.linkedin.data.TestUtil.asList)1 Predicate (com.linkedin.data.it.Predicate)1 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)1 CoercionMode (com.linkedin.data.schema.validation.CoercionMode)1 RequiredMode (com.linkedin.data.schema.validation.RequiredMode)1