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));
}
}
}
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);
}
}
}
use of com.linkedin.data.it.Predicate in project rest.li by linkedin.
the class FilterSchemaGenerator method main.
public static void main(String[] args) {
CommandLine cl = null;
try {
final CommandLineParser parser = new GnuParser();
cl = parser.parse(_options, args);
} catch (ParseException e) {
_log.error("Invalid arguments: " + e.getMessage());
reportInvalidArguments();
}
final String[] directoryArgs = cl.getArgs();
if (directoryArgs.length != 2) {
reportInvalidArguments();
}
final File sourceDirectory = new File(directoryArgs[0]);
if (!sourceDirectory.exists()) {
_log.error(sourceDirectory.getPath() + " does not exist");
System.exit(1);
}
if (!sourceDirectory.isDirectory()) {
_log.error(sourceDirectory.getPath() + " is not a directory");
System.exit(1);
}
final URI sourceDirectoryURI = sourceDirectory.toURI();
final File outputDirectory = new File(directoryArgs[1]);
if (outputDirectory.exists() && !sourceDirectory.isDirectory()) {
_log.error(outputDirectory.getPath() + " is not a directory");
System.exit(1);
}
final boolean isAvroMode = cl.hasOption('a');
final String predicateExpression = cl.getOptionValue('e');
final Predicate predicate = PredicateExpressionParser.parse(predicateExpression);
final Collection<File> sourceFiles = FileUtil.listFiles(sourceDirectory, null);
int exitCode = 0;
for (File sourceFile : sourceFiles) {
try {
final ValidationOptions val = new ValidationOptions();
val.setAvroUnionMode(isAvroMode);
final SchemaParser schemaParser = new SchemaParser();
schemaParser.setValidationOptions(val);
schemaParser.parse(new FileInputStream(sourceFile));
if (schemaParser.hasError()) {
_log.error("Error parsing " + sourceFile.getPath() + ": " + schemaParser.errorMessageBuilder());
exitCode = 1;
continue;
}
final DataSchema originalSchema = schemaParser.topLevelDataSchemas().get(0);
if (!(originalSchema instanceof NamedDataSchema)) {
_log.error(sourceFile.getPath() + " does not contain valid NamedDataSchema");
exitCode = 1;
continue;
}
final SchemaParser filterParser = new SchemaParser();
filterParser.setValidationOptions(val);
final NamedDataSchema filteredSchema = Filters.removeByPredicate((NamedDataSchema) originalSchema, predicate, filterParser);
if (filterParser.hasError()) {
_log.error("Error applying predicate: " + filterParser.errorMessageBuilder());
exitCode = 1;
continue;
}
final String relativePath = sourceDirectoryURI.relativize(sourceFile.toURI()).getPath();
final String outputFilePath = outputDirectory.getPath() + File.separator + relativePath;
final File outputFile = new File(outputFilePath);
final File outputFileParent = outputFile.getParentFile();
outputFileParent.mkdirs();
if (!outputFileParent.exists()) {
_log.error("Unable to write filtered schema to " + outputFileParent.getPath());
exitCode = 1;
continue;
}
FileOutputStream fout = new FileOutputStream(outputFile);
String schemaJson = SchemaToJsonEncoder.schemaToJson(filteredSchema, JsonBuilder.Pretty.INDENTED);
fout.write(schemaJson.getBytes(RestConstants.DEFAULT_CHARSET));
fout.close();
} catch (IOException e) {
_log.error(e.getMessage());
exitCode = 1;
}
}
System.exit(exitCode);
}
use of com.linkedin.data.it.Predicate in project rest.li by linkedin.
the class TestPredicateExpressionParser method testOrOr.
@Test
public void testOrOr() {
final Predicate parsed = PredicateExpressionParser.parse("com.linkedin.data.it.AlwaysTruePredicate | com.linkedin.data.it.AlwaysTruePredicate | com.linkedin.data.it.AlwaysFalsePredicate");
Assert.assertEquals(parsed.getClass(), OrPredicate.class);
final List<Predicate> children = ((OrPredicate) parsed).getChildPredicates();
Assert.assertEquals(children.get(0).getClass(), AlwaysTruePredicate.class);
Assert.assertEquals(children.get(1).getClass(), AlwaysTruePredicate.class);
Assert.assertEquals(children.get(2).getClass(), AlwaysFalsePredicate.class);
}
use of com.linkedin.data.it.Predicate in project rest.li by linkedin.
the class TestPredicateExpressionParser method testNotParen.
@Test
public void testNotParen() {
final Predicate parsed = PredicateExpressionParser.parse("!(com.linkedin.data.it.AlwaysTruePredicate)");
Assert.assertEquals(parsed.getClass(), NotPredicate.class);
Assert.assertEquals(((NotPredicate) parsed).getChildPredicate().getClass(), AlwaysTruePredicate.class);
}
Aggregations