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