-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b4f74e0
commit 71ed9f0
Showing
6 changed files
with
280 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
...le-core/src/test/java/org/apache/xtable/catalog/glue/TestGlueCatalogConversionSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.xtable.catalog.glue; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import org.apache.xtable.conversion.SourceTable; | ||
import org.apache.xtable.exception.CatalogSyncException; | ||
import org.apache.xtable.model.catalog.CatalogTableIdentifier; | ||
import org.apache.xtable.model.storage.TableFormat; | ||
|
||
import software.amazon.awssdk.services.glue.GlueClient; | ||
import software.amazon.awssdk.services.glue.model.EntityNotFoundException; | ||
import software.amazon.awssdk.services.glue.model.GetTableRequest; | ||
import software.amazon.awssdk.services.glue.model.GetTableResponse; | ||
import software.amazon.awssdk.services.glue.model.GlueException; | ||
import software.amazon.awssdk.services.glue.model.StorageDescriptor; | ||
import software.amazon.awssdk.services.glue.model.Table; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class TestGlueCatalogConversionSource { | ||
|
||
@Mock private GlueCatalogConfig mockCatalogConfig; | ||
@Mock private GlueClient mockGlueClient; | ||
private GlueCatalogConversionSource catalogConversionSource; | ||
private static final String GLUE_DB = "glue_db"; | ||
private static final String GLUE_TABLE = "glue_tbl"; | ||
private static final String TABLE_BASE_PATH = "/var/data/table"; | ||
private static final String GLUE_CATALOG_ID = "aws-account-id"; | ||
private static final CatalogTableIdentifier tableIdentifier = | ||
CatalogTableIdentifier.builder().databaseName(GLUE_DB).tableName(GLUE_TABLE).build(); | ||
private static final GetTableRequest getTableRequest = | ||
GetTableRequest.builder() | ||
.catalogId(GLUE_CATALOG_ID) | ||
.databaseName(GLUE_DB) | ||
.name(GLUE_TABLE) | ||
.build(); | ||
|
||
@BeforeEach | ||
void init() { | ||
when(mockCatalogConfig.getCatalogId()).thenReturn(GLUE_CATALOG_ID); | ||
catalogConversionSource = new GlueCatalogConversionSource(mockCatalogConfig, mockGlueClient); | ||
} | ||
|
||
@Test | ||
void testGetSourceTable_errorGettingTableFromGlue() { | ||
// error getting table from glue | ||
when(mockGlueClient.getTable(getTableRequest)) | ||
.thenThrow(GlueException.builder().message("something went wrong").build()); | ||
assertThrows( | ||
CatalogSyncException.class, () -> catalogConversionSource.getSourceTable(tableIdentifier)); | ||
|
||
verify(mockGlueClient, times(1)).getTable(getTableRequest); | ||
} | ||
|
||
@Test | ||
void testGetSourceTable_tableNotFoundInGlue() { | ||
// table not found in glue | ||
when(mockGlueClient.getTable(getTableRequest)) | ||
.thenThrow(EntityNotFoundException.builder().message("table not found").build()); | ||
assertThrows( | ||
CatalogSyncException.class, () -> catalogConversionSource.getSourceTable(tableIdentifier)); | ||
|
||
verify(mockGlueClient, times(1)).getTable(getTableRequest); | ||
} | ||
|
||
@Test | ||
void testGetSourceTable_tableFormatNotPresent() { | ||
// table format not present in table properties | ||
when(mockGlueClient.getTable(getTableRequest)) | ||
.thenReturn(GetTableResponse.builder().table(Table.builder().build()).build()); | ||
IllegalStateException exception = | ||
assertThrows( | ||
IllegalStateException.class, | ||
() -> catalogConversionSource.getSourceTable(tableIdentifier)); | ||
assertEquals( | ||
"TableFormat is null or empty for table: glue_db.glue_tbl", exception.getMessage()); | ||
|
||
verify(mockGlueClient, times(1)).getTable(getTableRequest); | ||
} | ||
|
||
@ParameterizedTest | ||
@CsvSource(value = {"ICEBERG", "HUDI", "DELTA"}) | ||
void testGetSourceTable(String tableFormat) { | ||
StorageDescriptor sd = StorageDescriptor.builder().location(TABLE_BASE_PATH).build(); | ||
Map<String, String> tableParams = new HashMap<>(); | ||
if (tableFormat.equals(TableFormat.ICEBERG)) { | ||
tableParams.put("write.data.path", String.format("%s/iceberg", TABLE_BASE_PATH)); | ||
tableParams.put("table_type", tableFormat); | ||
} else { | ||
tableParams.put("spark.sql.sources.provider", tableFormat); | ||
} | ||
|
||
String dataPath = | ||
tableFormat.equals(TableFormat.ICEBERG) | ||
? String.format("%s/iceberg", TABLE_BASE_PATH) | ||
: TABLE_BASE_PATH; | ||
SourceTable expected = | ||
newSourceTable(GLUE_TABLE, TABLE_BASE_PATH, dataPath, tableFormat, tableParams); | ||
when(mockGlueClient.getTable(getTableRequest)) | ||
.thenReturn( | ||
GetTableResponse.builder() | ||
.table(newGlueTable(GLUE_DB, GLUE_TABLE, tableParams, sd)) | ||
.build()); | ||
SourceTable output = catalogConversionSource.getSourceTable(tableIdentifier); | ||
assertEquals(expected, output); | ||
|
||
verify(mockGlueClient, times(1)).getTable(getTableRequest); | ||
} | ||
|
||
private Table newGlueTable( | ||
String dbName, String tableName, Map<String, String> params, StorageDescriptor sd) { | ||
return Table.builder() | ||
.databaseName(dbName) | ||
.name(tableName) | ||
.parameters(params) | ||
.storageDescriptor(sd) | ||
.build(); | ||
} | ||
|
||
private SourceTable newSourceTable( | ||
String tblName, | ||
String basePath, | ||
String dataPath, | ||
String tblFormat, | ||
Map<String, String> params) { | ||
Properties tblProperties = new Properties(); | ||
tblProperties.putAll(params); | ||
return SourceTable.builder() | ||
.name(tblName) | ||
.basePath(basePath) | ||
.dataPath(dataPath) | ||
.formatName(tblFormat) | ||
.additionalProperties(tblProperties) | ||
.build(); | ||
} | ||
} |
Oops, something went wrong.