Search in sources :

Example 6 with PegasusSchemaParser

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)));
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) SchemaParser(com.linkedin.data.schema.SchemaParser) PdlSchemaParser(com.linkedin.data.schema.grammar.PdlSchemaParser) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 7 with PegasusSchemaParser

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);
    }
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) ArrayList(java.util.ArrayList) SchemaParser(com.linkedin.data.schema.SchemaParser) PdlSchemaParser(com.linkedin.data.schema.grammar.PdlSchemaParser) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 8 with PegasusSchemaParser

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);
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) ArrayList(java.util.ArrayList) SchemaParser(com.linkedin.data.schema.SchemaParser) PdlSchemaParser(com.linkedin.data.schema.grammar.PdlSchemaParser) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 9 with PegasusSchemaParser

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());
        }
    }
}
Also used : PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser)

Example 10 with PegasusSchemaParser

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);
        }
    }
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) SchemaParser(com.linkedin.data.schema.SchemaParser) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) DataSchemaLocation(com.linkedin.data.schema.DataSchemaLocation)

Aggregations

PegasusSchemaParser (com.linkedin.data.schema.PegasusSchemaParser)17 DataSchema (com.linkedin.data.schema.DataSchema)11 SchemaParser (com.linkedin.data.schema.SchemaParser)11 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)7 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)7 PdlSchemaParser (com.linkedin.data.schema.grammar.PdlSchemaParser)6 DataMap (com.linkedin.data.DataMap)5 Schema (org.apache.avro.Schema)4 Test (org.testng.annotations.Test)4 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)3 GenericRecord (org.apache.avro.generic.GenericRecord)3 BeforeTest (org.testng.annotations.BeforeTest)3 DataSchemaLocation (com.linkedin.data.schema.DataSchemaLocation)2 ArrayList (java.util.ArrayList)2 TestUtil.dataSchemaFromString (com.linkedin.data.TestUtil.dataSchemaFromString)1 DataSchemaResolver (com.linkedin.data.schema.DataSchemaResolver)1 DataSchemaTraverse (com.linkedin.data.schema.DataSchemaTraverse)1 SchemaParserFactory (com.linkedin.data.schema.SchemaParserFactory)1 DefaultDataSchemaResolver (com.linkedin.data.schema.resolver.DefaultDataSchemaResolver)1 FileDataSchemaLocation (com.linkedin.data.schema.resolver.FileDataSchemaLocation)1