Search in sources :

Example 1 with Predicate

use of com.linkedin.data.it.Predicate in project rest.li by linkedin.

the class TestFilteredSchemaDataTranslation method testFilteredAvroSchemaDataTranslation.

/**
   * Removed derived field from Avro schema.
   */
@Test
public void testFilteredAvroSchemaDataTranslation() throws IOException {
    Object[][] inputs = { { "{ " + "  \"type\" : \"record\", " + "  \"name\" : \"Foo\", " + "  \"fields\" : [ " + "    { \"name\" : \"a\", \"type\" : \"int\" }, " + "    { \"name\" : \"b\", \"type\" : \"int\", \"optional\" : true }, " + "    { \"name\" : \"c\", \"type\" : \"int\", \"optional\" : true, \"derived\" : true } " + "  ] " + "}", Predicates.hasChildWithNameValue("derived", true), "{ " + "  \"type\" : \"record\", " + "  \"name\" : \"Foo\", " + "  \"fields\" : [ " + "    { \"name\" : \"a\", \"type\" : \"int\" }, " + "    { \"name\" : \"b\", \"type\" : [ \"null\", \"int\" ], \"default\" : null } " + "  ] " + "}", // "c" is dropped from output because it is not in the output schema
    "{ \"a\" : 1, \"b\" : 2, \"c\" : 3 }", "{ \"a\" : 1, \"b\" : { \"int\" : 2 } }", // "b" is translated to null and "c" is dropped from output because it is not in the output schema
    "{ \"a\" : 1, \"c\" : 3 }", "{ \"a\" : 1, \"b\" : null }" } };
    for (Object[] row : inputs) {
        int i = 0;
        String schemaText = (String) row[i++];
        Predicate predicate = (Predicate) row[i++];
        String avroSchemaText = (String) row[i++];
        RecordDataSchema schema = (RecordDataSchema) TestUtil.dataSchemaFromString(schemaText);
        NamedDataSchema filteredSchema = Filters.removeByPredicate(schema, predicate, new SchemaParser());
        Schema filteredAvroSchema = SchemaTranslator.dataToAvroSchema(filteredSchema);
        Schema expectedAvroSchema = Schema.parse(avroSchemaText);
        assertEquals(filteredAvroSchema, expectedAvroSchema);
        while (i < row.length) {
            String translationSourceJson = (String) row[i++];
            String translationResultJson = (String) row[i++];
            DataMap dataMap = TestUtil.dataMapFromString(translationSourceJson);
            GenericRecord genericRecord = DataTranslator.dataMapToGenericRecord(dataMap, schema, filteredAvroSchema);
            String avroJson = AvroUtil.jsonFromGenericRecord(genericRecord);
            DataMap avroJsonAsDataMap = TestUtil.dataMapFromString(avroJson);
            assertEquals(avroJsonAsDataMap, TestUtil.dataMapFromString(translationResultJson));
        }
    }
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) Schema(org.apache.avro.Schema) DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) SchemaParser(com.linkedin.data.schema.SchemaParser) GenericRecord(org.apache.avro.generic.GenericRecord) Predicate(com.linkedin.data.it.Predicate) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 2 with Predicate

use of com.linkedin.data.it.Predicate in project rest.li by linkedin.

the class TestFilteredSchemaDataTranslation method testFilteredDataSchemaDataTranslation.

/**
   * Removed field from Pegasus schema.
   */
@Test
public void testFilteredDataSchemaDataTranslation() throws IOException {
    Object[][] inputs = { { "{ " + "  \"type\" : \"record\", " + "  \"name\" : \"Foo\", " + "  \"fields\" : [ " + "    { \"name\" : \"a\", \"type\" : \"int\" }, " + "    { \"name\" : \"b\", \"type\" : [ \"null\", \"int\" ], \"default\" : null }, " + "    { \"name\" : \"removeMe\", \"type\" : \"int\" } " + "  ] " + "}", Predicates.hasChildWithNameValue("name", "removeMe"), "{ " + "  \"type\" : \"record\", " + "  \"name\" : \"Foo\", " + "  \"fields\" : [ " + "    { \"name\" : \"a\", \"type\" : \"int\" }, " + "    { \"name\" : \"b\", \"type\" : \"int\", \"optional\" : true } " + "  ] " + "}", // "removeMe" is dropped from output because it is not in output schema
    "{ \"a\" : 1, \"b\" : { \"int\" : 2 }, \"removeMe\" : 3 }", "{ \"a\" : 1, \"b\" : 2 }", // "b" has null value is dropped from output, "removeMe" is dropped from output because it is not in output schema
    "{ \"a\" : 1, \"b\" : null, \"removeMe\" : 3 }", "{ \"a\" : 1 }" } };
    for (Object[] row : inputs) {
        int i = 0;
        String avroSchemaText = (String) row[i++];
        Predicate predicate = (Predicate) row[i++];
        String schemaText = (String) row[i++];
        Schema avroSchema = Schema.parse(avroSchemaText);
        System.out.println(avroSchema);
        RecordDataSchema schema = (RecordDataSchema) SchemaTranslator.avroToDataSchema(avroSchema);
        RecordDataSchema filteredSchema = (RecordDataSchema) Filters.removeByPredicate(schema, predicate, new SchemaParser());
        DataSchema expectedSchema = TestUtil.dataSchemaFromString(schemaText);
        System.out.println(filteredSchema);
        assertEquals(filteredSchema, expectedSchema);
        while (i < row.length) {
            String translationSourceJson = (String) row[i++];
            String translationExpectedJson = (String) row[i++];
            GenericRecord genericRecord = AvroUtil.genericRecordFromJson(translationSourceJson, avroSchema);
            DataMap dataMap = DataTranslator.genericRecordToDataMap(genericRecord, filteredSchema, avroSchema);
            assertEquals(dataMap, TestUtil.dataMapFromString(translationExpectedJson));
        }
    }
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) Schema(org.apache.avro.Schema) DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) SchemaParser(com.linkedin.data.schema.SchemaParser) GenericRecord(org.apache.avro.generic.GenericRecord) Predicate(com.linkedin.data.it.Predicate) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 3 with Predicate

use of com.linkedin.data.it.Predicate in project rest.li by linkedin.

the class TestSchemaFilter method testInputs.

private static void testInputs(Object[][] inputs, boolean isAvroUnionMode) throws IOException {
    for (Object[] row : inputs) {
        String schemaText = (String) row[0];
        Predicate predicate = (Predicate) row[1];
        String expected = (String) row[2];
        NamedDataSchema schema = dataSchemaFromString(schemaText, isAvroUnionMode);
        DataSchema filteredSchema = null;
        SchemaParser parser = new SchemaParser();
        parser.getValidationOptions().setAvroUnionMode(isAvroUnionMode);
        filteredSchema = Filters.removeByPredicate(schema, predicate, parser);
        if (filteredSchema != null) {
            // Schema string match
            String expectedSchemaText = expected;
            DataSchema expectedSchema = dataSchemaFromString(expectedSchemaText, isAvroUnionMode);
            assertEquals(filteredSchema.toString(), expectedSchema.toString());
            assertEquals(filteredSchema, expectedSchema);
        } else {
            String parserMessage = parser.errorMessage();
            assertTrue(parserMessage.contains(expected), "\nContains :" + expected + "\nActual   :" + parserMessage);
        }
    }
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) SchemaParser(com.linkedin.data.schema.SchemaParser) Predicate(com.linkedin.data.it.Predicate)

Example 4 with Predicate

use of com.linkedin.data.it.Predicate in project rest.li by linkedin.

the class RestUtils method trimRecordTemplate.

/**
   * This method recursively removes all values from a DataMap
   * that do not match some field in the schema via an all positive
   * projection generated from the schema unless whitelisted by the
   * override parameter.
   *
   * Readonly datamaps should not invoke this.
   * An exception will be thrown if this is done.
   *
   * @param dataMap represents the DataMap that will be trimmed.
   * @param schema represents the schema that the datamap should match.
   * @param override represents the MaskTree that defines how fields outside the schema should be preserved.
   * @param failOnMismatch true if an exception should be thrown if there is a data-schema mismatch.
   */
public static void trimRecordTemplate(DataMap dataMap, RecordDataSchema schema, MaskTree override, final boolean failOnMismatch) {
    if (dataMap == null || schema == null) {
        return;
    }
    DataMap overrideResults = RestUtils.projectFields(dataMap, ProjectionMode.AUTOMATIC, override);
    Builder.create(dataMap, schema, IterationOrder.PRE_ORDER).filterBy(new Predicate() {

        @Override
        public boolean evaluate(DataElement element) {
            if (failOnMismatch && element.getSchema() == null) {
                throw new IllegalArgumentException("Schema validation did not succeed: " + element.getValue().toString());
            }
            return element.getSchema() == null;
        }
    }).remove();
    CheckedUtil.putAllWithoutChecking(dataMap, overrideResults);
}
Also used : DataElement(com.linkedin.data.element.DataElement) DataMap(com.linkedin.data.DataMap) Predicate(com.linkedin.data.it.Predicate)

Example 5 with Predicate

use of com.linkedin.data.it.Predicate in project rest.li by linkedin.

the class TestPredicateExpressionParser method testNot.

@Test
public void testNot() {
    final Predicate parsed = PredicateExpressionParser.parse("!com.linkedin.data.it.AlwaysTruePredicate");
    Assert.assertEquals(parsed.getClass(), NotPredicate.class);
    Assert.assertEquals(((NotPredicate) parsed).getChildPredicate().getClass(), AlwaysTruePredicate.class);
}
Also used : NotPredicate(com.linkedin.data.it.NotPredicate) AndPredicate(com.linkedin.data.it.AndPredicate) NotPredicate(com.linkedin.data.it.NotPredicate) AlwaysTruePredicate(com.linkedin.data.it.AlwaysTruePredicate) AlwaysFalsePredicate(com.linkedin.data.it.AlwaysFalsePredicate) OrPredicate(com.linkedin.data.it.OrPredicate) Predicate(com.linkedin.data.it.Predicate) Test(org.testng.annotations.Test)

Aggregations

Predicate (com.linkedin.data.it.Predicate)33 Test (org.testng.annotations.Test)28 AlwaysFalsePredicate (com.linkedin.data.it.AlwaysFalsePredicate)26 AlwaysTruePredicate (com.linkedin.data.it.AlwaysTruePredicate)26 AndPredicate (com.linkedin.data.it.AndPredicate)26 NotPredicate (com.linkedin.data.it.NotPredicate)26 OrPredicate (com.linkedin.data.it.OrPredicate)26 DataSchema (com.linkedin.data.schema.DataSchema)4 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)4 SchemaParser (com.linkedin.data.schema.SchemaParser)4 DataMap (com.linkedin.data.DataMap)3 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)2 Schema (org.apache.avro.Schema)2 GenericRecord (org.apache.avro.generic.GenericRecord)2 DataElement (com.linkedin.data.element.DataElement)1 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1