|
19 | 19 | */ |
20 | 20 | package com.amazonaws.athena.connectors.oracle; |
21 | 21 |
|
22 | | -import com.amazonaws.athena.connectors.jdbc.connection.DatabaseConnectionConfig; |
23 | | -import com.amazonaws.athena.connectors.jdbc.connection.DatabaseConnectionInfo; |
24 | | -import com.amazonaws.athena.connector.credentials.DefaultCredentials; |
25 | 22 | import com.amazonaws.athena.connector.credentials.CredentialsProvider; |
| 23 | +import com.amazonaws.athena.connector.credentials.DefaultCredentials; |
26 | 24 | import com.amazonaws.athena.connector.credentials.StaticCredentialsProvider; |
27 | | -import org.junit.Assert; |
| 25 | +import com.amazonaws.athena.connectors.jdbc.connection.DatabaseConnectionConfig; |
| 26 | +import com.amazonaws.athena.connectors.jdbc.connection.DatabaseConnectionInfo; |
28 | 27 | import org.junit.Test; |
29 | 28 |
|
30 | 29 | import java.sql.Connection; |
31 | 30 | import java.sql.Driver; |
32 | 31 | import java.sql.DriverManager; |
33 | 32 | import java.sql.SQLException; |
| 33 | +import java.util.Properties; |
34 | 34 |
|
35 | | -public class OracleJdbcConnectionFactoryTest { |
| 35 | +import static com.github.stefanbirkner.systemlambda.SystemLambda.withEnvironmentVariable; |
| 36 | +import static org.junit.Assert.assertEquals; |
| 37 | +import static org.junit.Assert.fail; |
| 38 | +import static org.mockito.ArgumentMatchers.any; |
| 39 | +import static org.mockito.ArgumentMatchers.anyString; |
| 40 | +import static org.mockito.Mockito.mock; |
| 41 | +import static org.mockito.Mockito.when; |
| 42 | + |
| 43 | +public class OracleJdbcConnectionFactoryTest |
| 44 | +{ |
| 45 | + private static final String CONNECTION_STRING = "oracle://jdbc:oracle:thin:username/password@//test.oracle.com:1521/orcl"; |
36 | 46 |
|
37 | 47 | @Test(expected = RuntimeException.class) |
38 | | - public void getConnectionTest() throws SQLException { |
| 48 | + public void testGetConnection() throws SQLException |
| 49 | + { |
39 | 50 | DefaultCredentials expectedCredential = new DefaultCredentials("test", "test"); |
40 | 51 | CredentialsProvider credentialsProvider = new StaticCredentialsProvider(expectedCredential); |
41 | 52 | DatabaseConnectionConfig databaseConnectionConfig = new DatabaseConnectionConfig("testCatalog", OracleConstants.ORACLE_NAME, |
42 | | - "oracle://jdbc:oracle:thin:username/password@//127.0.0.1:1521/orcl", "test"); |
43 | | - DatabaseConnectionInfo DatabaseConnectionInfo = new DatabaseConnectionInfo(OracleConstants.ORACLE_DRIVER_CLASS,OracleConstants.ORACLE_DEFAULT_PORT); |
44 | | - Connection connection = new OracleJdbcConnectionFactory(databaseConnectionConfig, DatabaseConnectionInfo).getConnection(credentialsProvider); |
| 53 | + CONNECTION_STRING, "test"); |
| 54 | + DatabaseConnectionInfo databaseConnectionInfo = new DatabaseConnectionInfo(OracleConstants.ORACLE_DRIVER_CLASS, OracleConstants.ORACLE_DEFAULT_PORT); |
| 55 | + Connection connection = new OracleJdbcConnectionFactory(databaseConnectionConfig, databaseConnectionInfo).getConnection(credentialsProvider); |
45 | 56 | String originalURL = connection.getMetaData().getURL(); |
46 | 57 | Driver drv = DriverManager.getDriver(originalURL); |
47 | 58 | String driverClass = drv.getClass().getName(); |
48 | | - Assert.assertEquals("oracle.jdbc.OracleDriver", driverClass); |
| 59 | + assertEquals("oracle.jdbc.OracleDriver", driverClass); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testGetConnection_withSsl() { |
| 64 | + Driver mockDriver = mock(Driver.class); |
| 65 | + try { |
| 66 | + withEnvironmentVariable("is_fips_enabled", "true").execute(() -> { |
| 67 | + DefaultCredentials expectedCredential = new DefaultCredentials("test", "test"); |
| 68 | + CredentialsProvider credentialsProvider = new StaticCredentialsProvider(expectedCredential); |
| 69 | + |
| 70 | + DatabaseConnectionConfig databaseConnectionConfig = new DatabaseConnectionConfig( |
| 71 | + "testCatalog", |
| 72 | + OracleConstants.ORACLE_NAME, |
| 73 | + "oracle://jdbc:oracle:thin:username/password@tcps://test.oracle.com:1521/orcl", |
| 74 | + "test" |
| 75 | + ); |
| 76 | + |
| 77 | + DatabaseConnectionInfo databaseConnectionInfo = new DatabaseConnectionInfo( |
| 78 | + OracleConstants.ORACLE_DRIVER_CLASS, |
| 79 | + OracleConstants.ORACLE_DEFAULT_PORT |
| 80 | + ); |
| 81 | + |
| 82 | + Properties[] capturedProps = new Properties[1]; |
| 83 | + |
| 84 | + when(mockDriver.acceptsURL(anyString())).thenReturn(true); |
| 85 | + when(mockDriver.connect(anyString(), any(Properties.class))) |
| 86 | + .thenAnswer(invocation -> { |
| 87 | + capturedProps[0] = invocation.getArgument(1); |
| 88 | + return mock(Connection.class); |
| 89 | + }); |
| 90 | + |
| 91 | + DriverManager.registerDriver(mockDriver); |
| 92 | + |
| 93 | + new OracleJdbcConnectionFactory(databaseConnectionConfig, databaseConnectionInfo) |
| 94 | + .getConnection(credentialsProvider); |
| 95 | + |
| 96 | + Properties sslProps = capturedProps[0]; |
| 97 | + assertEquals("JKS", sslProps.getProperty("javax.net.ssl.trustStoreType")); |
| 98 | + assertEquals("changeit", sslProps.getProperty("javax.net.ssl.trustStorePassword")); |
| 99 | + assertEquals("true", sslProps.getProperty("oracle.net.ssl_server_dn_match")); |
| 100 | + assertEquals( |
| 101 | + "(TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA)", |
| 102 | + sslProps.getProperty("oracle.net.ssl_cipher_suites") |
| 103 | + ); |
| 104 | + }); |
| 105 | + } |
| 106 | + catch (Exception e) { |
| 107 | + fail("Unexpected exception: " + e.getMessage()); |
| 108 | + } |
| 109 | + finally { |
| 110 | + try { |
| 111 | + DriverManager.deregisterDriver(mockDriver); |
| 112 | + } catch (SQLException ex) { |
| 113 | + fail("Failed to deregister mock JDBC driver: " + ex.getMessage()); |
| 114 | + } |
| 115 | + } |
49 | 116 | } |
50 | 117 |
|
| 118 | + @Test(expected = RuntimeException.class) |
| 119 | + public void testGetConnection_withNullCredentialsProvider_throwsRuntimeException() |
| 120 | + { |
| 121 | + DatabaseConnectionConfig config = new DatabaseConnectionConfig( |
| 122 | + "testCatalog", OracleConstants.ORACLE_NAME, |
| 123 | + CONNECTION_STRING, "test"); |
| 124 | + DatabaseConnectionInfo info = new DatabaseConnectionInfo(OracleConstants.ORACLE_DRIVER_CLASS, OracleConstants.ORACLE_DEFAULT_PORT); |
| 125 | + |
| 126 | + new OracleJdbcConnectionFactory(config, info).getConnection(null); |
| 127 | + } |
| 128 | + |
| 129 | + @Test(expected = RuntimeException.class) |
| 130 | + public void testGetConnection_whenDriverFailsToConnect_throwsRuntimeException() throws Exception |
| 131 | + { |
| 132 | + DefaultCredentials credentials = new DefaultCredentials("user", "password"); |
| 133 | + CredentialsProvider credentialsProvider = new StaticCredentialsProvider(credentials); |
| 134 | + |
| 135 | + DatabaseConnectionConfig config = new DatabaseConnectionConfig("catalog", OracleConstants.ORACLE_NAME, CONNECTION_STRING, "test"); |
| 136 | + DatabaseConnectionInfo info = new DatabaseConnectionInfo(OracleConstants.ORACLE_DRIVER_CLASS, OracleConstants.ORACLE_DEFAULT_PORT); |
| 137 | + |
| 138 | + Driver mockDriver = mock(Driver.class); |
| 139 | + when(mockDriver.acceptsURL(anyString())).thenReturn(true); |
| 140 | + when(mockDriver.connect(anyString(), any(Properties.class))) |
| 141 | + .thenThrow(new SQLException("DB down", "08001", 1234)); |
| 142 | + |
| 143 | + DriverManager.registerDriver(mockDriver); |
| 144 | + |
| 145 | + try { |
| 146 | + new OracleJdbcConnectionFactory(config, info).getConnection(credentialsProvider); |
| 147 | + } |
| 148 | + finally { |
| 149 | + DriverManager.deregisterDriver(mockDriver); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @Test(expected = RuntimeException.class) |
| 154 | + public void getConnection_whenDriverClassNotFound_throwsRuntimeException() |
| 155 | + { |
| 156 | + DefaultCredentials credentials = new DefaultCredentials("user", "password"); |
| 157 | + CredentialsProvider credentialsProvider = new StaticCredentialsProvider(credentials); |
| 158 | + |
| 159 | + DatabaseConnectionConfig config = new DatabaseConnectionConfig("catalog", OracleConstants.ORACLE_NAME, CONNECTION_STRING, "test"); |
| 160 | + |
| 161 | + // Provide an invalid/non-existent driver class name to trigger ClassNotFoundException |
| 162 | + DatabaseConnectionInfo info = new DatabaseConnectionInfo("non.existent.DriverClass", OracleConstants.ORACLE_DEFAULT_PORT); |
| 163 | + |
| 164 | + new OracleJdbcConnectionFactory(config, info).getConnection(credentialsProvider); |
| 165 | + } |
51 | 166 | } |
0 commit comments