|
1 | | -package com.amazonaws.athena.connectors.cloudera;/*- |
| 1 | +/*- |
2 | 2 | * #%L |
3 | 3 | * athena-cloudera-impala |
4 | 4 | * %% |
|
17 | 17 | * limitations under the License. |
18 | 18 | * #L% |
19 | 19 | */ |
20 | | - |
21 | | -import com.amazonaws.athena.connector.credentials.DefaultCredentials; |
| 20 | +package com.amazonaws.athena.connectors.cloudera; |
22 | 21 | import com.amazonaws.athena.connector.credentials.CredentialsProvider; |
| 22 | +import com.amazonaws.athena.connector.credentials.DefaultCredentials; |
23 | 23 | import com.amazonaws.athena.connector.credentials.StaticCredentialsProvider; |
24 | | -import com.amazonaws.athena.connectors.jdbc.connection.*; |
| 24 | +import com.amazonaws.athena.connectors.jdbc.connection.DatabaseConnectionConfig; |
| 25 | +import com.amazonaws.athena.connectors.jdbc.connection.DatabaseConnectionInfo; |
25 | 26 | import com.google.common.collect.ImmutableMap; |
26 | | -import org.junit.Assert; |
27 | 27 | import org.junit.Test; |
28 | 28 |
|
29 | 29 | import java.sql.Connection; |
|
32 | 32 | import java.sql.SQLException; |
33 | 33 | import java.util.Map; |
34 | 34 |
|
35 | | -public class ImpalaJdbcConnectionFactoryTest { |
| 35 | +import static org.junit.Assert.assertEquals; |
| 36 | + |
| 37 | +public class ImpalaJdbcConnectionFactoryTest |
| 38 | +{ |
| 39 | + private static final String TEST_CATALOG = "testCatalog"; |
| 40 | + private static final String TEST_DEFAULT_DATABASE = "default"; |
| 41 | + private static final Map<String, String> JDBC_PROPERTIES = ImmutableMap.of("databaseTerm", "SCHEMA"); |
| 42 | + |
36 | 43 | @Test(expected = RuntimeException.class) |
37 | | - public void getConnectionTest() throws ClassNotFoundException, SQLException { |
| 44 | + public void testGetConnection() throws SQLException |
| 45 | + { |
| 46 | + String impalaConnectionString = "impala://jdbc:impala://localhost:10000/athena;AuthMech=3;UID=hive;PWD=''"; |
| 47 | + |
38 | 48 | DefaultCredentials expectedCredential = new DefaultCredentials("impala", "impala"); |
39 | 49 | CredentialsProvider credentialsProvider = new StaticCredentialsProvider(expectedCredential); |
40 | | - DatabaseConnectionConfig databaseConnectionConfig = new DatabaseConnectionConfig("testCatalog", ImpalaConstants.IMPALA_NAME, |
41 | | - "impala://jdbc:impala://23.21.178.97:10000/athena;AuthMech=3;UID=hive;PWD=''", "impala"); |
42 | | - Map<String, String> JDBC_PROPERTIES = ImmutableMap.of("databaseTerm", "SCHEMA"); |
43 | | - DatabaseConnectionInfo DatabaseConnectionInfo = new DatabaseConnectionInfo(ImpalaConstants.IMPALA_DRIVER_CLASS, ImpalaConstants.IMPALA_DEFAULT_PORT); |
44 | | - Connection connection = new ImpalaJdbcConnectionFactory(databaseConnectionConfig, JDBC_PROPERTIES,DatabaseConnectionInfo).getConnection(credentialsProvider); |
| 50 | + DatabaseConnectionConfig databaseConnectionConfig = new DatabaseConnectionConfig(TEST_CATALOG, ImpalaConstants.IMPALA_NAME, |
| 51 | + impalaConnectionString, "impala"); |
| 52 | + DatabaseConnectionInfo databaseConnectionInfo = new DatabaseConnectionInfo(ImpalaConstants.IMPALA_DRIVER_CLASS, ImpalaConstants.IMPALA_DEFAULT_PORT); |
| 53 | + Connection connection = new ImpalaJdbcConnectionFactory(databaseConnectionConfig, JDBC_PROPERTIES, databaseConnectionInfo).getConnection(credentialsProvider); |
45 | 54 | String originalURL = connection.getMetaData().getURL(); |
46 | 55 | Driver drv = DriverManager.getDriver(originalURL); |
47 | 56 | String driverClass = drv.getClass().getName(); |
48 | | - Assert.assertEquals("com.cloudera.impala.jdbc.Driver", driverClass); |
| 57 | + assertEquals("com.cloudera.impala.jdbc.Driver", driverClass); |
| 58 | + } |
| 59 | + |
| 60 | + @Test(expected = RuntimeException.class) |
| 61 | + public void testGetConnection_withNullCredentials() throws Exception |
| 62 | + { |
| 63 | + String localJdbcString = String.format("jdbc:impala://localhost:10000/%s", TEST_DEFAULT_DATABASE); |
| 64 | + |
| 65 | + // Setup the connection config with a test JDBC string |
| 66 | + DatabaseConnectionConfig databaseConnectionConfig = new DatabaseConnectionConfig( |
| 67 | + TEST_CATALOG, |
| 68 | + ImpalaConstants.IMPALA_NAME, |
| 69 | + localJdbcString, |
| 70 | + TEST_DEFAULT_DATABASE |
| 71 | + ); |
| 72 | + |
| 73 | + // Setup other required parameters |
| 74 | + ImpalaJdbcConnectionFactory connectionFactory = getImpalaJdbcConnectionFactory(databaseConnectionConfig); |
| 75 | + |
| 76 | + // Attempt to get connection with null credentials - this should use the original JDBC string |
| 77 | + Connection connection = connectionFactory.getConnection(null); |
| 78 | + |
| 79 | + // Verify the connection URL matches the original JDBC string |
| 80 | + assertEquals(localJdbcString, connection.getMetaData().getURL()); |
| 81 | + } |
| 82 | + |
| 83 | + private static ImpalaJdbcConnectionFactory getImpalaJdbcConnectionFactory(DatabaseConnectionConfig databaseConnectionConfig) |
| 84 | + { |
| 85 | + DatabaseConnectionInfo databaseConnectionInfo = new DatabaseConnectionInfo( |
| 86 | + ImpalaConstants.IMPALA_DRIVER_CLASS, |
| 87 | + ImpalaConstants.IMPALA_DEFAULT_PORT |
| 88 | + ); |
| 89 | + |
| 90 | + // Create the connection factory and attempt to get a connection with null credentials |
| 91 | + return new ImpalaJdbcConnectionFactory( |
| 92 | + databaseConnectionConfig, |
| 93 | + JDBC_PROPERTIES, |
| 94 | + databaseConnectionInfo |
| 95 | + ); |
49 | 96 | } |
50 | 97 | } |
0 commit comments