Search in sources :

Example 1 with SchemaDirectory

use of com.linkedin.data.schema.resolver.SchemaDirectory in project rest.li by linkedin.

the class TestDataSchemaParser method testCustomSourceSchemaDirectory.

@Test(dataProvider = "inputFiles")
public void testCustomSourceSchemaDirectory(String pegasusFilename, String[] expectedSchemas) throws Exception {
    String tempDirectoryPath = _tempDir.getAbsolutePath();
    String jarFile = tempDirectoryPath + FS + "test.jar";
    SchemaDirectory customSchemaDirectory = () -> "custom";
    String pegasusFile = TEST_RESOURCES_DIR + FS + pegasusFilename;
    String pegasusFileInJar = customSchemaDirectory.getName() + "/" + pegasusFilename;
    createTempJarFile(Collections.singletonMap(pegasusFile, pegasusFileInJar), jarFile);
    // Load with default parser, this will return zero scheams.
    DataSchemaParser parser = new DataSchemaParser.Builder(tempDirectoryPath).build();
    DataSchemaParser.ParseResult parseResult = parser.parseSources(new String[] { jarFile });
    assertEquals(parseResult.getSchemaAndLocations().size(), 0);
    // Now create a parser with custom directory as source
    parser = new DataSchemaParser.Builder(tempDirectoryPath).setSourceDirectories(Collections.singletonList(customSchemaDirectory)).build();
    parseResult = parser.parseSources(new String[] { jarFile });
    assertEquals(parseResult.getSchemaAndLocations().size(), expectedSchemas.length);
    Set<String> schemaNames = parseResult.getSchemaAndLocations().keySet().stream().map(DataSchema::getUnionMemberKey).collect(Collectors.toSet());
    for (String schema : expectedSchemas) {
        assertTrue(schemaNames.contains(schema));
    }
    parseResult.getSchemaAndLocations().values().forEach(loc -> assertEquals(loc.getSourceFile().getAbsolutePath(), jarFile));
}
Also used : SchemaDirectory(com.linkedin.data.schema.resolver.SchemaDirectory) FileFormatDataSchemaParser(com.linkedin.pegasus.generator.FileFormatDataSchemaParser) Test(org.testng.annotations.Test)

Example 2 with SchemaDirectory

use of com.linkedin.data.schema.resolver.SchemaDirectory in project rest.li by linkedin.

the class TestDataSchemaParser method testCustomResolverSchemaDirectory.

@Test
public void testCustomResolverSchemaDirectory() throws Exception {
    String tempDirectoryPath = _tempDir.getAbsolutePath();
    String jarFile = tempDirectoryPath + FS + "test.jar";
    String schemaDir = TEST_RESOURCES_DIR + FS + "extensionSchemas";
    SchemaDirectory customSchemaDirectory = () -> "custom";
    Map<String, String> entryToFileMap = new HashMap<>();
    // FooExtensions is in "extensions" directory and references "Foo" from "custom" directory.
    entryToFileMap.put(schemaDir + FS + "pegasus/Foo.pdl", "custom/Foo.pdl");
    entryToFileMap.put(schemaDir + FS + "extensions/FooExtensions.pdl", "extensions/FooExtensions.pdl");
    createTempJarFile(entryToFileMap, jarFile);
    List<SchemaDirectory> resolverDirectories = Arrays.asList(SchemaDirectoryName.EXTENSIONS, customSchemaDirectory);
    List<SchemaDirectory> sourceDirectories = Collections.singletonList(SchemaDirectoryName.EXTENSIONS);
    DataSchemaParser parser = new DataSchemaParser.Builder(jarFile).setResolverDirectories(resolverDirectories).setSourceDirectories(sourceDirectories).build();
    DataSchemaParser.ParseResult parseResult = parser.parseSources(new String[] { jarFile });
    parseResult = parser.parseSources(new String[] { jarFile });
    // Foo and FooExtensions
    assertEquals(parseResult.getSchemaAndLocations().size(), 2);
    Set<String> schemaNames = parseResult.getSchemaAndLocations().keySet().stream().map(DataSchema::getUnionMemberKey).collect(Collectors.toSet());
    assertTrue(schemaNames.contains("FooExtensions"));
    assertTrue(schemaNames.contains("Foo"));
    parseResult.getSchemaAndLocations().values().forEach(loc -> assertEquals(loc.getSourceFile().getAbsolutePath(), jarFile));
}
Also used : SchemaDirectory(com.linkedin.data.schema.resolver.SchemaDirectory) HashMap(java.util.HashMap) FileFormatDataSchemaParser(com.linkedin.pegasus.generator.FileFormatDataSchemaParser) Test(org.testng.annotations.Test)

Example 3 with SchemaDirectory

use of com.linkedin.data.schema.resolver.SchemaDirectory in project rest.li by linkedin.

the class TestDataSchemaParser method testSchemaFilesInExtensionPathInFolder.

@Test(dataProvider = "entityRelationshipInputFiles")
public void testSchemaFilesInExtensionPathInFolder(String[] files, String[] expectedExtensions) throws Exception {
    String pegasusWithFS = TEST_RESOURCES_DIR + FS;
    String resolverPath = pegasusWithFS + "extensionSchemas/extensions:" + pegasusWithFS + "extensionSchemas/others:" + pegasusWithFS + "extensionSchemas/pegasus";
    List<SchemaDirectory> resolverDirectories = Arrays.asList(SchemaDirectoryName.EXTENSIONS, SchemaDirectoryName.PEGASUS);
    List<SchemaDirectory> sourceDirectories = Collections.singletonList(SchemaDirectoryName.EXTENSIONS);
    DataSchemaParser parser = new DataSchemaParser.Builder(resolverPath).setResolverDirectories(resolverDirectories).setSourceDirectories(sourceDirectories).build();
    String[] schemaFiles = Arrays.stream(files).map(casename -> TEST_RESOURCES_DIR + FS + "extensionSchemas" + FS + casename).toArray(String[]::new);
    DataSchemaParser.ParseResult parseResult = parser.parseSources(schemaFiles);
    Map<DataSchema, DataSchemaLocation> extensions = parseResult.getExtensionDataSchemaAndLocations();
    assertEquals(extensions.size(), expectedExtensions.length);
    Set<String> actualNames = extensions.keySet().stream().map(dataSchema -> (NamedDataSchema) dataSchema).map(NamedDataSchema::getName).collect(Collectors.toSet());
    assertEquals(actualNames, Arrays.stream(expectedExtensions).collect(Collectors.toSet()));
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) Arrays(java.util.Arrays) FileFormatDataSchemaParser(com.linkedin.pegasus.generator.FileFormatDataSchemaParser) DataProvider(org.testng.annotations.DataProvider) DataSchema(com.linkedin.data.schema.DataSchema) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) AfterMethod(org.testng.annotations.AfterMethod) Assert(org.testng.Assert) Map(java.util.Map) ZipEntry(java.util.zip.ZipEntry) Files(java.nio.file.Files) BeforeMethod(org.testng.annotations.BeforeMethod) FileOutputStream(java.io.FileOutputStream) Set(java.util.Set) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) FileInputStream(java.io.FileInputStream) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) DataSchemaLocation(com.linkedin.data.schema.DataSchemaLocation) SchemaDirectory(com.linkedin.data.schema.resolver.SchemaDirectory) SchemaDirectoryName(com.linkedin.data.schema.resolver.SchemaDirectoryName) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) Collections(java.util.Collections) DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) SchemaDirectory(com.linkedin.data.schema.resolver.SchemaDirectory) FileFormatDataSchemaParser(com.linkedin.pegasus.generator.FileFormatDataSchemaParser) DataSchemaLocation(com.linkedin.data.schema.DataSchemaLocation) Test(org.testng.annotations.Test)

Example 4 with SchemaDirectory

use of com.linkedin.data.schema.resolver.SchemaDirectory in project rest.li by linkedin.

the class TestDataSchemaParser method testSchemaFilesInExtensionPathInJar.

@Test(dataProvider = "entityRelationshipInputFiles")
public void testSchemaFilesInExtensionPathInJar(String[] files, String[] expectedExtensions) throws Exception {
    String tempDirectoryPath = _tempDir.getAbsolutePath();
    String jarFile = tempDirectoryPath + FS + "test.jar";
    String schemaDir = TEST_RESOURCES_DIR + FS + "extensionSchemas";
    Map<String, String> entryToFileMap = Arrays.stream(files).collect(Collectors.toMap(filename -> schemaDir + FS + filename, filename -> filename));
    createTempJarFile(entryToFileMap, jarFile);
    List<SchemaDirectory> resolverDirectories = Arrays.asList(SchemaDirectoryName.EXTENSIONS, SchemaDirectoryName.PEGASUS);
    List<SchemaDirectory> sourceDirectories = Collections.singletonList(SchemaDirectoryName.EXTENSIONS);
    DataSchemaParser parser = new DataSchemaParser.Builder(jarFile).setResolverDirectories(resolverDirectories).setSourceDirectories(sourceDirectories).build();
    DataSchemaParser.ParseResult parseResult = parser.parseSources(new String[] { jarFile });
    Map<DataSchema, DataSchemaLocation> extensions = parseResult.getExtensionDataSchemaAndLocations();
    assertEquals(extensions.size(), expectedExtensions.length);
    Set<String> actualNames = extensions.keySet().stream().map(dataSchema -> (NamedDataSchema) dataSchema).map(NamedDataSchema::getName).collect(Collectors.toSet());
    assertEquals(actualNames, Arrays.stream(expectedExtensions).collect(Collectors.toSet()));
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) Arrays(java.util.Arrays) FileFormatDataSchemaParser(com.linkedin.pegasus.generator.FileFormatDataSchemaParser) DataProvider(org.testng.annotations.DataProvider) DataSchema(com.linkedin.data.schema.DataSchema) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) AfterMethod(org.testng.annotations.AfterMethod) Assert(org.testng.Assert) Map(java.util.Map) ZipEntry(java.util.zip.ZipEntry) Files(java.nio.file.Files) BeforeMethod(org.testng.annotations.BeforeMethod) FileOutputStream(java.io.FileOutputStream) Set(java.util.Set) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) FileInputStream(java.io.FileInputStream) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) DataSchemaLocation(com.linkedin.data.schema.DataSchemaLocation) SchemaDirectory(com.linkedin.data.schema.resolver.SchemaDirectory) SchemaDirectoryName(com.linkedin.data.schema.resolver.SchemaDirectoryName) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) Collections(java.util.Collections) DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) SchemaDirectory(com.linkedin.data.schema.resolver.SchemaDirectory) FileFormatDataSchemaParser(com.linkedin.pegasus.generator.FileFormatDataSchemaParser) DataSchemaLocation(com.linkedin.data.schema.DataSchemaLocation) Test(org.testng.annotations.Test)

Aggregations

SchemaDirectory (com.linkedin.data.schema.resolver.SchemaDirectory)4 FileFormatDataSchemaParser (com.linkedin.pegasus.generator.FileFormatDataSchemaParser)4 Test (org.testng.annotations.Test)4 HashMap (java.util.HashMap)3 DataSchema (com.linkedin.data.schema.DataSchema)2 DataSchemaLocation (com.linkedin.data.schema.DataSchemaLocation)2 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)2 SchemaDirectoryName (com.linkedin.data.schema.resolver.SchemaDirectoryName)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 Files (java.nio.file.Files)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 ZipEntry (java.util.zip.ZipEntry)2