Skip to content

Commit 5a8647f

Browse files
Jithendar12ritiktrianz
authored andcommitted
Add Unit tests for Oracle Connector
1 parent bedaa4c commit 5a8647f

File tree

6 files changed

+509
-58
lines changed

6 files changed

+509
-58
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: 125 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,148 @@
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;
3434

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";
3646

3747
@Test(expected = RuntimeException.class)
38-
public void getConnectionTest() throws SQLException {
48+
public void testGetConnection() throws SQLException
49+
{
3950
DefaultCredentials expectedCredential = new DefaultCredentials("test", "test");
4051
CredentialsProvider credentialsProvider = new StaticCredentialsProvider(expectedCredential);
4152
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);
4556
String originalURL = connection.getMetaData().getURL();
4657
Driver drv = DriverManager.getDriver(originalURL);
4758
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+
}
49116
}
50117

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

0 commit comments

Comments
 (0)