Search in sources :

Example 96 with DataSchema

use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.

the class TestConversions method testConvertDataMapToDataSchema.

@Test
public void testConvertDataMapToDataSchema() throws IOException {
    for (String good : goodInputs) {
        NamedDataSchema dataSchema = (NamedDataSchema) TestUtil.dataSchemaFromString(good);
        DataMap mapFromString = TestUtil.dataMapFromString(good);
        PegasusSchemaParser parser = new SchemaParser();
        DataSchema schemaFromMap = Conversions.dataMapToDataSchema(mapFromString, parser);
        assertEquals(schemaFromMap, dataSchema);
    }
    for (String bad : badInputs) {
        DataMap mapFromString = TestUtil.dataMapFromString(bad);
        PegasusSchemaParser parser = new SchemaParser();
        DataSchema schemaFromMap = Conversions.dataMapToDataSchema(mapFromString, parser);
        assertNull(schemaFromMap);
        assertTrue(parser.hasError());
    }
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) SchemaParser(com.linkedin.data.schema.SchemaParser) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 97 with DataSchema

use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.

the class TestValidation method testJsonValidation.

@Test
public void testJsonValidation() throws IOException {
    Object[][] input = { { "{ \"type\" : \"record\", \"name\" : \"Foo\", \"fields\" : [ { \"name\" : \"intField\", \"type\" : \"int\" } ] }", new Object[] { new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.NORMAL), "{ \"intField\" : " + Integer.MAX_VALUE + " }" }, new Object[] { new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.OFF), "{ \"intField\" : " + Integer.MAX_VALUE + " }" }, new Object[] { new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.NORMAL), "{ \"intField\" : " + Integer.MIN_VALUE + " }" }, new Object[] { new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.OFF), "{ \"intField\" : " + Integer.MIN_VALUE + " }" }, new Object[] { new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.OFF), "{ \"intField\" : " + ((long) Integer.MAX_VALUE + 1) + " }", "ERROR :: /intField :: 2147483648 is not backed by a Integer" } }, { "{ \"type\" : \"record\", \"name\" : \"Foo\", \"fields\" : [ { \"name\" : \"longField\", \"type\" : \"long\" } ] }", new Object[] { new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.NORMAL), "{ \"longField\" : " + Long.MAX_VALUE + " }" }, new Object[] { new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.OFF), "{ \"longField\" : " + Long.MAX_VALUE + " }" }, new Object[] { new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.NORMAL), "{ \"longField\" : " + Long.MIN_VALUE + " }" }, new Object[] { new ValidationOptions(RequiredMode.MUST_BE_PRESENT, CoercionMode.OFF), "{ \"longField\" : " + Long.MIN_VALUE + " }" } } };
    for (Object[] row : input) {
        String schemaText = (String) row[0];
        for (int i = 1; i < row.length; i++) {
            Object[] test = (Object[]) row[i];
            ValidationOptions options = (ValidationOptions) test[0];
            String dataText = (String) test[1];
            String expectedResult = test.length > 2 ? (String) test[2] : null;
            DataSchema schema = dataSchemaFromString(schemaText);
            DataMap dataMap = dataMapFromString(dataText);
            ValidationResult result = ValidateDataAgainstSchema.validate(dataMap, schema, options);
            if (expectedResult == null) {
                Assert.assertTrue(result.isValid());
                Assert.assertEquals(result.getMessages().size(), 0);
            } else {
                Assert.assertTrue(result.toString().contains(expectedResult));
            }
        }
    }
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) 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)

Example 98 with DataSchema

use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.

the class TestValidation method testUnrecognizedFieldTrimming.

@Test
public void testUnrecognizedFieldTrimming() throws IOException {
    ValidationOptions options = new ValidationOptions(RequiredMode.CAN_BE_ABSENT_IF_HAS_DEFAULT, CoercionMode.NORMAL, UnrecognizedFieldMode.TRIM);
    String schemaText = "{\n" + "  \"name\" : \"Foo\",\n" + "  \"type\" : \"record\",\n" + "  \"fields\" : [\n" + "    { \"name\" : \"primitive\", \"type\" : \"int\", \"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\" : \"recordField\", \"type\" : \"Foo\", \"optional\" : true }\n" + "  ]\n" + "}\n";
    DataSchema schema = dataSchemaFromString(schemaText);
    String dataString = "{\n" + "  \"primitive\" : 1,\n" + "  \"unrecognizedPrimitive\": -1,\n" + "  \"arrayField\" : [ { \"primitive\": 2, \"unrecognizedInArray\": -2 } ],\n" + "  \"mapField\" : { \"key\": { \"primitive\": 3, \"unrecognizedInMap\": -3 } },\n" + "  \"unionField\" : { \"Foo\": { \"primitive\": 4, \"unrecognizedInMap\": -4 } },\n" + "  \"recordField\" : {\n" + "    \"primitive\" : 5,\n" + "    \"unrecognizedPrimitive\": -5\n" + "  },\n" + "  \"unrecognizedMap\": { \"key\": -100},\n" + "  \"unrecognizedArray\": [ -101 ]\n" + "}";
    String trimmedDataString = "{\n" + "  \"primitive\" : 1,\n" + "  \"arrayField\" : [ { \"primitive\": 2 } ],\n" + "  \"mapField\" : { \"key\": { \"primitive\": 3 } },\n" + "  \"unionField\" : { \"Foo\": { \"primitive\": 4 } },\n" + "  \"recordField\" : {\n" + "    \"primitive\" : 5\n" + "  }\n" + "}";
    // mutable
    DataMap toValidate = dataMapFromString(dataString);
    ValidationResult result = validate(toValidate, schema, options);
    Assert.assertTrue(result.isValid(), MessageUtil.messagesToString(result.getMessages()));
    Assert.assertEquals(result.getFixed(), dataMapFromString(trimmedDataString));
    Assert.assertSame(toValidate, result.getFixed());
    // read-only
    DataMap readOnlyToValidate = dataMapFromString(dataString);
    readOnlyToValidate.makeReadOnly();
    ValidationResult readOnlyResult = validate(readOnlyToValidate, schema, options);
    Assert.assertTrue(readOnlyResult.hasFixupReadOnlyError());
    String message = readOnlyResult.getMessages().toString();
    Assert.assertEquals(readOnlyResult.getMessages().size(), 7);
    String[] expectedStrings = new String[] { "/unrecognizedMap", "/unrecognizedArray", "/recordField/unrecognizedPrimitive", "/unionField/Foo/unrecognizedInMap", "/unrecognizedPrimitive", "/arrayField/0/unrecognizedInArray", "/mapField/key/unrecognizedInMap" };
    for (String expected : expectedStrings) {
        Assert.assertTrue(message.contains(expected), message + " does not contain " + expected);
    }
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) 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)

Example 99 with DataSchema

use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.

the class TestValidation method testTrimUnrecognizedFieldsWithAvroUnionDefault.

@Test
public void testTrimUnrecognizedFieldsWithAvroUnionDefault() throws IOException {
    ValidationOptions options = new ValidationOptions(RequiredMode.CAN_BE_ABSENT_IF_HAS_DEFAULT, CoercionMode.NORMAL, UnrecognizedFieldMode.TRIM);
    options.setAvroUnionMode(true);
    String schemaText = "{\n" + "  \"name\" : \"Foo\",\n" + "  \"type\" : \"record\",\n" + "  \"fields\" : [\n" + "    { \"name\" : \"primitive\", \"type\" : \"int\", \"optional\" : true },\n" + "    { \"name\" : \"unionField\", \"type\" : [ \"Foo\", \"int\", \"string\" ], \"optional\" : true }\n" + "  ]\n" + "}\n";
    DataSchema schema = dataSchemaFromString(schemaText);
    String dataString = "{\n" + "  \"unionField\" : { \"primitive\": 4, \"unrecognizedInMap\": -4 }\n" + "}";
    String trimmedDataString = "{\n" + "  \"unionField\" : { \"primitive\": 4 }\n" + "}";
    DataMap toValidate = dataMapFromString(dataString);
    ValidationResult result = validate(toValidate, schema, options);
    Assert.assertTrue(result.isValid(), MessageUtil.messagesToString(result.getMessages()));
    Assert.assertEquals(result.getFixed(), dataMapFromString(trimmedDataString));
    Assert.assertSame(toValidate, result.getFixed());
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) 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)

Example 100 with DataSchema

use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.

the class DataSchemaAnnotationValidator method validateSchema.

private final void validateSchema(ValidatorContext context, DataSchema schema) {
    if (schema.getType() == DataSchema.Type.TYPEREF) {
        DataSchema refSchema = ((TyperefDataSchema) schema).getRef();
        validateSchema(context, refSchema);
        getAndInvokeValidatorList(context, schema);
    } else {
        getAndInvokeValidatorList(context, schema);
    }
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema)

Aggregations

DataSchema (com.linkedin.data.schema.DataSchema)131 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)82 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)53 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)48 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)44 DataMap (com.linkedin.data.DataMap)43 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)41 MapDataSchema (com.linkedin.data.schema.MapDataSchema)40 Test (org.testng.annotations.Test)37 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)36 FixedDataSchema (com.linkedin.data.schema.FixedDataSchema)24 ByteString (com.linkedin.data.ByteString)15 TestUtil.dataMapFromString (com.linkedin.data.TestUtil.dataMapFromString)15 TestUtil.dataSchemaFromString (com.linkedin.data.TestUtil.dataSchemaFromString)14 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)14 ArrayList (java.util.ArrayList)12 DataList (com.linkedin.data.DataList)11 ComplexDataSchema (com.linkedin.data.schema.ComplexDataSchema)9 SchemaParser (com.linkedin.data.schema.SchemaParser)9 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)9