Skip to content

Commit b7a3775

Browse files
Jithendar12ritiktrianz
authored andcommitted
Add Unit tests for Oracle Connector.
1 parent e302bc0 commit b7a3775

File tree

6 files changed

+490
-60
lines changed

6 files changed

+490
-60
lines changed

athena-oracle/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@
6464
<version>${mockito.version}</version>
6565
<scope>test</scope>
6666
</dependency>
67+
<dependency>
68+
<groupId>com.github.stefanbirkner</groupId>
69+
<artifactId>system-lambda</artifactId>
70+
<version>1.2.1</version>
71+
<scope>test</scope>
72+
</dependency>
73+
6774
</dependencies>
6875
<build>
6976
<plugins>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*-
2+
* #%L
3+
* athena-oracle
4+
* %%
5+
* Copyright (C) 2019 - 2025 Amazon Web Services
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.amazonaws.athena.connectors.oracle;
21+
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
25+
import java.util.HashMap;
26+
import java.util.Map;
27+
28+
import static com.amazonaws.athena.connector.lambda.connection.EnvironmentConstants.DATABASE;
29+
import static com.amazonaws.athena.connector.lambda.connection.EnvironmentConstants.DEFAULT;
30+
import static com.amazonaws.athena.connector.lambda.connection.EnvironmentConstants.ENFORCE_SSL;
31+
import static com.amazonaws.athena.connector.lambda.connection.EnvironmentConstants.HOST;
32+
import static com.amazonaws.athena.connector.lambda.connection.EnvironmentConstants.PORT;
33+
import static com.amazonaws.athena.connector.lambda.connection.EnvironmentConstants.SECRET_NAME;
34+
import static org.junit.Assert.assertEquals;
35+
36+
public class OracleEnvironmentPropertiesTest
37+
{
38+
Map<String, String> connectionProperties;
39+
OracleEnvironmentProperties oracleEnvironmentProperties;
40+
41+
@Before
42+
public void setUp()
43+
{
44+
connectionProperties = new HashMap<>();
45+
connectionProperties.put(HOST, "test.oracle.com");
46+
connectionProperties.put(PORT, "1521");
47+
connectionProperties.put(DATABASE, "orcl");
48+
connectionProperties.put(SECRET_NAME, "oracle-secret");
49+
oracleEnvironmentProperties = new OracleEnvironmentProperties();
50+
}
51+
52+
@Test
53+
public void testOracleConnectionString_withoutSSL()
54+
{
55+
Map<String, String> oracleConnectionProperties = oracleEnvironmentProperties.connectionPropertiesToEnvironment(connectionProperties);
56+
String expectedConnectionString = "oracle://jdbc:oracle:thin:${oracle-secret}@//test.oracle.com:1521/orcl";
57+
assertEquals(expectedConnectionString, oracleConnectionProperties.get(DEFAULT));
58+
}
59+
60+
@Test
61+
public void testOracleConnectionString_withSSL()
62+
{
63+
connectionProperties.put(ENFORCE_SSL, "true");
64+
Map<String, String> oracleConnectionProperties = oracleEnvironmentProperties.connectionPropertiesToEnvironment(connectionProperties);
65+
String expectedConnectionString = "oracle://jdbc:oracle:thin:${oracle-secret}@tcps://test.oracle.com:1521/orcl";
66+
assertEquals(expectedConnectionString, oracleConnectionProperties.get(DEFAULT));
67+
}
68+
}

athena-oracle/src/test/java/com/amazonaws/athena/connectors/oracle/OracleJdbcConnectionFactoryTest.java

Lines changed: 119 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,140 @@
1919
*/
2020
package com.amazonaws.athena.connectors.oracle;
2121

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;
2522
import com.amazonaws.athena.connector.credentials.CredentialsProvider;
23+
import com.amazonaws.athena.connector.credentials.DefaultCredentials;
2624
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;
2827
import org.junit.Test;
2928

3029
import java.sql.Connection;
3130
import java.sql.Driver;
3231
import java.sql.DriverManager;
3332
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";
3450

35-
public class OracleJdbcConnectionFactoryTest {
3651

3752
@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);
4056
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);
4561
String originalURL = connection.getMetaData().getURL();
4662
Driver drv = DriverManager.getDriver(originalURL);
4763
String driverClass = drv.getClass().getName();
48-
Assert.assertEquals("oracle.jdbc.OracleDriver", driverClass);
64+
assertEquals("oracle.jdbc.OracleDriver", driverClass);
4965
}
5066

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+
}
51158
}

0 commit comments

Comments
 (0)