use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class TestDataSchemaResolver method testClasspathResourceDataSchemaResolver.
@Test
public void testClasspathResourceDataSchemaResolver() {
final ClasspathResourceDataSchemaResolver resolver = new ClasspathResourceDataSchemaResolver(SchemaParserFactory.instance());
final PegasusSchemaParser parser = new SchemaParser(resolver);
final String existingSchemaName = "com.linkedin.data.schema.ValidationDemo";
final String nonExistSchemaName = "Non-Existing Schema";
final DataSchema existSchema = parser.lookupName(existingSchemaName);
assertNotNull(existSchema);
assertTrue(existSchema instanceof RecordDataSchema);
assertEquals(((RecordDataSchema) existSchema).getFullName(), existingSchemaName);
final DataSchema nonExistSchema = parser.lookupName(nonExistSchemaName);
assertNull(nonExistSchema);
assertTrue(parser.errorMessage().contains(nonExistSchemaName));
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class TestDataSchemaResolver method testClassNameDataSchemaResolver.
@Test
public void testClassNameDataSchemaResolver() {
@SuppressWarnings("deprecation") final ClassNameDataSchemaResolver resolver = new ClassNameDataSchemaResolver();
final PegasusSchemaParser parser = new SchemaParser(resolver);
final Class<? extends RecordTemplate> testClass = ClassNameFooRecord.class;
final String nonExistSchemaName = "Non-Existing Schema";
final DataSchema existSchema = parser.lookupName(testClass.getName());
assertNotNull(existSchema);
assertTrue(existSchema instanceof RecordDataSchema);
assertEquals(((RecordDataSchema) existSchema).getFullName(), testClass.getCanonicalName());
assertFalse(resolver.isBadLocation(new ClassNameDataSchemaLocation(testClass.getName())));
final DataSchema nonExistSchema = parser.lookupName(nonExistSchemaName);
assertNull(nonExistSchema);
assertTrue(parser.errorMessage().contains(nonExistSchemaName));
assertTrue(resolver.isBadLocation(new ClassNameDataSchemaLocation(nonExistSchemaName)));
}
use of com.linkedin.data.schema.DataSchema 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.schema.DataSchema in project rest.li by linkedin.
the class TestDataElement method testDataElement.
@Test(dataProvider = "dataElementFactories")
public void testDataElement(DataElementFactory factory) throws IOException {
RecordDataSchema fooSchema = (RecordDataSchema) TestUtil.dataSchemaFromString(fooSchemaText);
ArrayDataSchema arraySchema = (ArrayDataSchema) fooSchema.getField("array").getType();
String fooText = "{\n" + " \"int\" : 34,\n" + " \"string\" : \"abc\",\n" + " \"array\" : [\n" + " { \"int\" : 56 },\n" + " { \"string\" : \"xyz\" },\n" + " { \"array\" : [\n" + " { \"int\" : 78 }\n" + " ] }\n" + " ]\n" + "}\n";
DataMap foo = TestUtil.dataMapFromString(fooText);
DataElement root = factory.create(foo, DataElement.ROOT_NAME, fooSchema, null);
DataElement int1 = factory.create(foo.get("int"), "int", fooSchema.getField("int").getType(), root);
DataElement string1 = factory.create(foo.get("string"), "string", fooSchema.getField("string").getType(), root);
DataElement array1 = factory.create(foo.get("array"), "array", fooSchema.getField("array").getType(), root);
DataElement foo20 = factory.create(array1.getChild(0), 0, arraySchema.getItems(), array1);
DataElement foo21 = factory.create(array1.getChild(1), 1, arraySchema.getItems(), array1);
DataElement foo22 = factory.create(array1.getChild(2), 2, arraySchema.getItems(), array1);
DataElement int20 = factory.create(foo20.getChild("int"), "int", fooSchema.getField("int").getType(), foo20);
DataElement string21 = factory.create(foo21.getChild("string"), "string", fooSchema.getField("string").getType(), foo21);
DataElement array22 = factory.create(foo22.getChild("array"), "array", fooSchema.getField("array").getType(), foo22);
DataElement foo30 = factory.create(array22.getChild(0), 0, arraySchema.getItems(), array22);
DataElement int30 = factory.create(foo30.getChild("int"), "int", fooSchema.getField("int").getType(), foo30);
// test path
Object[][] testPathInput = { { root, foo, fooSchema, new Object[] {} }, { int1, foo.get("int"), DataSchemaConstants.INTEGER_DATA_SCHEMA, new Object[] { "int" } }, { string1, foo.get("string"), DataSchemaConstants.STRING_DATA_SCHEMA, new Object[] { "string" } }, { array1, foo.get("array"), arraySchema, new Object[] { "array" } }, { foo20, ((DataList) foo.get("array")).get(0), fooSchema, new Object[] { "array", 0 } }, { foo21, ((DataList) foo.get("array")).get(1), fooSchema, new Object[] { "array", 1 } }, { foo22, ((DataList) foo.get("array")).get(2), fooSchema, new Object[] { "array", 2 } }, { int20, ((DataMap) ((DataList) foo.get("array")).get(0)).get("int"), DataSchemaConstants.INTEGER_DATA_SCHEMA, new Object[] { "array", 0, "int" } }, { string21, ((DataMap) ((DataList) foo.get("array")).get(1)).get("string"), DataSchemaConstants.STRING_DATA_SCHEMA, new Object[] { "array", 1, "string" } }, { array22, ((DataMap) ((DataList) foo.get("array")).get(2)).get("array"), arraySchema, new Object[] { "array", 2, "array" } }, { foo30, ((DataList) ((DataMap) ((DataList) foo.get("array")).get(2)).get("array")).get(0), fooSchema, new Object[] { "array", 2, "array", 0 } }, { int30, ((DataMap) ((DataList) ((DataMap) ((DataList) foo.get("array")).get(2)).get("array")).get(0)).get("int"), DataSchemaConstants.INTEGER_DATA_SCHEMA, new Object[] { "array", 2, "array", 0, "int" } } };
ArrayList<Object> pathAsList = new ArrayList<Object>();
for (Object[] row : testPathInput) {
DataElement element = (DataElement) row[0];
// test value
Object expectedValue = row[1];
assertSame(expectedValue, element.getValue());
// test schema
DataSchema expectedSchema = (DataSchema) row[2];
assertSame(expectedSchema, element.getSchema());
// test name
Object[] expectedPath = (Object[]) row[3];
Object expectedName = expectedPath.length == 0 ? DataElement.ROOT_NAME : expectedPath[expectedPath.length - 1];
assertEquals(expectedName, element.getName());
// test path
Object[] path = element.path();
element.pathAsList(pathAsList);
StringBuilder builder = new StringBuilder();
StringBuilder builder2 = new StringBuilder();
assertEquals(expectedPath.length, path.length);
assertEquals(expectedPath.length, pathAsList.size());
for (int i = 0; i < expectedPath.length; i++) {
assertEquals(path[i], expectedPath[i]);
assertEquals(pathAsList.get(i), expectedPath[i]);
builder.append('*').append(expectedPath[i]);
builder2.append(DataElement.SEPARATOR).append(expectedPath[i]);
}
assertEquals(builder.toString(), element.pathAsString('*'));
assertEquals(builder2.toString(), element.pathAsString());
// test copyChain
DataElement copy = element.copyChain();
assertElementChainEquals(copy, element.copyChain(), null);
// test DataElementUtil.element
DataElement elementFromUtil = DataElementUtil.element(root, path);
assertElementChainEquals(elementFromUtil, element, root);
elementFromUtil = DataElementUtil.element(root, pathAsList);
assertElementChainEquals(elementFromUtil, element, root);
elementFromUtil = DataElementUtil.element(root, element.pathAsString());
assertElementChainEquals(elementFromUtil, element, root);
elementFromUtil = DataElementUtil.element(root, element.pathAsString('*'), '*');
assertElementChainEquals(elementFromUtil, element, root);
elementFromUtil = DataElementUtil.element(root.getValue(), root.getSchema(), path);
assertElementChainEquals(elementFromUtil, element, root);
elementFromUtil = DataElementUtil.element(root.getValue(), root.getSchema(), pathAsList);
assertElementChainEquals(elementFromUtil, element, root);
elementFromUtil = DataElementUtil.element(root.getValue(), root.getSchema(), element.pathAsString());
assertElementChainEquals(elementFromUtil, element, root);
elementFromUtil = DataElementUtil.element(root.getValue(), root.getSchema(), element.pathAsString('*'), '*');
assertElementChainEquals(elementFromUtil, element, root);
}
}
use of com.linkedin.data.schema.DataSchema in project rest.li by linkedin.
the class TestDataIterator method testDataSchemaTypeEquals.
@Test(dataProvider = "orders")
public void testDataSchemaTypeEquals(IterationOrder order) throws IOException {
String input = "{\n" + " \"a\" : 222,\n" + " \"b\" : \"foo\",\n" + " \"c\" : {\n" + " \"a\" : 333,\n" + " \"e\" : {\n" + " \"b\" : \"bar\",\n" + " \"d\" : [ 0, 1, 2 ]\n" + " }\n" + " },\n" + " \"d\" : [ 10, 11, 12],\n" + " \"f\" : {\n" + " \"k1\" : \"v1\",\n" + " \"k2\" : \"v2\"\n" + " },\n" + " \"u\" : {\n" + " \"int\" : 123\n" + " }\n" + "}";
String schemaText = "{\n" + " \"name\" : \"foo\",\n" + " \"type\" : \"record\",\n" + " \"fields\" : [\n" + " { \"name\" : \"a\", \"type\" : \"int\" },\n" + " { \"name\" : \"b\", \"type\" : \"string\" },\n" + " { \"name\" : \"c\", \"type\" : \"foo\" },\n" + " { \"name\" : \"d\", \"type\" : { \"type\" : \"array\", \"items\" : \"int\" } },\n" + " { \"name\" : \"e\", \"type\" : \"foo\" },\n" + " { \"name\" : \"f\", \"type\" : { \"type\" : \"map\", \"values\" : \"string\" } },\n" + " { \"name\" : \"u\", \"type\" : [ \"int\", \"double\", \"string\" ] }\n" + " ]\n" + "}\n";
DataSchema schema = DataTemplateUtil.parseSchema(schemaText);
Object[][] tests = { { DataSchema.Type.INT, Arrays.asList(222, 333, 0, 1, 2, 10, 11, 12, 123) }, { DataSchema.Type.STRING, Arrays.asList("foo", "bar", "v1", "v2") } };
Object o = jsonToObject(input);
for (Object[] row : tests) {
Builder builder = Builder.create(o, schema, order).filterBy(dataSchemaTypeEquals((DataSchema.Type) row[0]));
@SuppressWarnings("unchecked") List<Object> expected = (List<Object>) row[1];
assertEqualsByValue(builder, expected);
}
}
Aggregations