use of com.linkedin.data.schema.PegasusSchemaParser 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.PegasusSchemaParser in project rest.li by linkedin.
the class TestDataSchemaResolver method testClasspathResourceDataSchemaResolverMultipleSchemaDirectories.
@Test
public void testClasspathResourceDataSchemaResolverMultipleSchemaDirectories() {
// Tests for data schemas
ClasspathResourceDataSchemaResolver resolver = new ClasspathResourceDataSchemaResolver(Thread.currentThread().getContextClassLoader(), Arrays.asList(SchemaDirectoryName.EXTENSIONS, SchemaDirectoryName.PEGASUS));
PegasusSchemaParser parser = new SchemaParser(resolver);
final List<String> expectedSchemas = new ArrayList<>();
Collections.addAll(expectedSchemas, "com.linkedin.data.schema.ValidationDemo", "com.linkedin.restli.example.Album", "com.linkedin.restli.example.AlbumExtensions", "com.linkedin.restli.example.FruitsPdl", "com.linkedin.data.schema.RecordWithPdlReference");
for (String expectedSchemaName : expectedSchemas) {
final DataSchema existSchema = parser.lookupName(expectedSchemaName);
assertNotNull(existSchema, "Failed parsing : " + expectedSchemaName);
assertEquals(((NamedDataSchema) existSchema).getFullName(), expectedSchemaName);
}
}
use of com.linkedin.data.schema.PegasusSchemaParser in project rest.li by linkedin.
the class TestDataSchemaResolver method testClasspathResourceDataSchemaResolver.
@Test
public void testClasspathResourceDataSchemaResolver() {
// Tests for data schemas
ClasspathResourceDataSchemaResolver resolver = new ClasspathResourceDataSchemaResolver();
PegasusSchemaParser parser = new SchemaParser(resolver);
final List<String> existingSchemas = new ArrayList<>();
Collections.addAll(existingSchemas, "com.linkedin.data.schema.ValidationDemo", "com.linkedin.restli.example.Album", "com.linkedin.restli.example.FruitsPdl", "com.linkedin.data.schema.RecordWithPdlReference");
final String nonExistSchemaName = "Non-Existing Schema";
for (String existingSchemaName : existingSchemas) {
final DataSchema existSchema = parser.lookupName(existingSchemaName);
assertNotNull(existSchema, "Failed parsing : " + existingSchemaName);
assertEquals(((NamedDataSchema) existSchema).getFullName(), existingSchemaName);
}
final DataSchema nonExistSchema = parser.lookupName(nonExistSchemaName);
assertNull(nonExistSchema);
}
use of com.linkedin.data.schema.PegasusSchemaParser in project rest.li by linkedin.
the class FileFormatDataSchemaParser method parseSchemaStream.
/**
* Parse a source file to obtain the data schemas contained within.
* This method will cause the {@link DataSchemaResolver} to resolve any referenced named and unnamed schemas,
* as well as registering named schemas in its bindings.
*
* @param schemaInputStream provides the source data.
* @return the top-level data schemas within the source file.
* @throws IOException if there is a file access error.
*/
private List<DataSchema> parseSchemaStream(InputStream schemaInputStream, DataSchemaLocation schemaLocation, DataSchemaParser.ParseResult result) throws IOException {
PegasusSchemaParser parser = _schemaParserFactory.create(_schemaResolver);
try {
parser.setLocation(schemaLocation);
parser.parse(schemaInputStream);
if (parser.hasError()) {
return Collections.emptyList();
}
return parser.topLevelDataSchemas();
} finally {
schemaInputStream.close();
if (parser.hasError()) {
result._messageBuilder.append(schemaLocation.toString()).append(",").append(parser.errorMessage());
}
}
}
use of com.linkedin.data.schema.PegasusSchemaParser in project rest.li by linkedin.
the class TestDataSchemaResolver method lookup.
public void lookup(DataSchemaResolver resolver, String[][] lookups, char separator, boolean debug) {
PegasusSchemaParser parser = new SchemaParser(resolver);
for (String[] entry : lookups) {
String name = entry[0];
String expectFound = entry[1];
String expected = entry[2];
DataSchema schema = parser.lookupName(name);
if (debug) {
out.println("----" + name + "-----");
}
String errorMessage = parser.errorMessage();
if (debug && errorMessage.isEmpty() == false) {
out.println(errorMessage);
}
if (expectFound == ERROR) {
assertTrue(parser.hasError());
assertTrue(expected == null || errorMessage.contains(expected));
} else if (expectFound == FOUND) {
assertTrue(schema != null);
String schemaText = schema.toString();
if (debug) {
out.println(schemaText);
}
assertFalse(parser.hasError());
assertTrue(schema instanceof NamedDataSchema);
NamedDataSchema namedSchema = (NamedDataSchema) schema;
assertEquals(namedSchema.getFullName(), name);
assertTrue(schemaText.contains(expected));
assertTrue(resolver.bindings().containsKey(name));
assertSame(resolver.bindings().get(name), namedSchema);
String location = entry[3];
DataSchemaLocation namedSchemalocation = resolver.nameToDataSchemaLocations().get(name);
String locationNorm;
if (namedSchemalocation.toString().contains(".jar")) {
locationNorm = location.replace(separator, '/');
} else {
locationNorm = location.replace('/', separator);
}
assertNotNull(namedSchemalocation);
assertEquals(namedSchemalocation.toString().indexOf(locationNorm), namedSchemalocation.toString().length() - locationNorm.length());
assertTrue(resolver.locationResolved(namedSchemalocation));
} else if (expectFound == NOT_FOUND) {
assertTrue(schema == null);
assertFalse(parser.hasError());
assertTrue(expected == null || errorMessage.contains(expected));
assertFalse(resolver.bindings().containsKey(name));
} else {
assertTrue(false);
}
}
}
Aggregations