Skip to content

Commit 5d61594

Browse files
Jithendar12VenkatasivareddyTR
authored andcommitted
Add unit tests in Synapse Connector.
1 parent d56c3aa commit 5d61594

File tree

5 files changed

+762
-111
lines changed

5 files changed

+762
-111
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*-
2+
* #%L
3+
* athena-synapse
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.synapse;
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.HOST;
31+
import static com.amazonaws.athena.connector.lambda.connection.EnvironmentConstants.PORT;
32+
import static com.amazonaws.athena.connector.lambda.connection.EnvironmentConstants.SECRET_NAME;
33+
import static org.junit.Assert.assertEquals;
34+
35+
public class SynapseEnvironmentPropertiesTest
36+
{
37+
private Map<String, String> connectionProperties;
38+
private SynapseEnvironmentProperties synapseEnvironmentProperties;
39+
40+
@Before
41+
public void setUp()
42+
{
43+
connectionProperties = new HashMap<>();
44+
connectionProperties.put(HOST, "test.sql.azuresynapse.net");
45+
connectionProperties.put(PORT, "1433");
46+
connectionProperties.put(DATABASE, "testdb");
47+
connectionProperties.put(SECRET_NAME, "synapse-secret");
48+
49+
synapseEnvironmentProperties = new SynapseEnvironmentProperties();
50+
}
51+
52+
@Test
53+
public void testSynapseConnectionString()
54+
{
55+
Map<String, String> synapseConnectionProperties = synapseEnvironmentProperties.connectionPropertiesToEnvironment(connectionProperties);
56+
57+
String expectedConnectionString = "synapse://jdbc:sqlserver://test.sql.azuresynapse.net:1433;databaseName=testdb;${synapse-secret}";
58+
assertEquals(expectedConnectionString, synapseConnectionProperties.get(DEFAULT));
59+
}
60+
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/*-
2+
* #%L
3+
* athena-synapse
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.synapse;
21+
22+
import com.amazonaws.athena.connector.credentials.CredentialsProvider;
23+
import com.amazonaws.athena.connector.credentials.DefaultCredentials;
24+
import com.amazonaws.athena.connector.credentials.StaticCredentialsProvider;
25+
import com.amazonaws.athena.connectors.jdbc.connection.DatabaseConnectionConfig;
26+
import com.amazonaws.athena.connectors.jdbc.connection.DatabaseConnectionInfo;
27+
import org.junit.After;
28+
import org.junit.Before;
29+
import org.junit.Test;
30+
31+
import java.sql.Connection;
32+
import java.sql.Driver;
33+
import java.sql.DriverManager;
34+
import java.sql.SQLException;
35+
import java.util.HashMap;
36+
import java.util.Properties;
37+
import java.util.concurrent.atomic.AtomicReference;
38+
39+
import static org.junit.Assert.assertEquals;
40+
import static org.junit.Assert.fail;
41+
import static org.mockito.ArgumentMatchers.any;
42+
import static org.mockito.ArgumentMatchers.anyString;
43+
import static org.mockito.Mockito.mock;
44+
import static org.mockito.Mockito.when;
45+
46+
public class SynapseJdbcConnectionFactoryTest
47+
{
48+
private static final String CATALOG = "testCatalog";
49+
private static final String SYNAPSE = "synapse";
50+
private static final String SECRET_NAME = "testSecret";
51+
private static final String MOCK_JDBC_URL = "jdbc:sqlserver://sql.azuresynapse.net:1433;databaseName=testdb";
52+
private static final int MOCK_PORT = 1433;
53+
private static final String MOCK_DRIVER_CLASS = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
54+
private static final String TEST_USERNAME = "testUser";
55+
private static final String TEST_PASSWORD = "testPassword";
56+
private static final String SECRET_PLACEHOLDER = ";${test}";
57+
private static final String USER_PASSWORD_PARAMS = ";user=" + TEST_USERNAME + ";password=" + TEST_PASSWORD;
58+
private static final String ACTIVE_DIRECTORY_AUTH = ";authentication=ActiveDirectoryServicePrincipal";
59+
private static final String AAD_PRINCIPAL_PARAMS = ";AADSecurePrincipalId=" + TEST_USERNAME + ";AADSecurePrincipalSecret=" + TEST_PASSWORD;
60+
61+
private Driver mockDriver;
62+
private Connection mockConnection;
63+
private AtomicReference<String> actualUrl;
64+
65+
@Before
66+
public void setUp() throws SQLException
67+
{
68+
mockDriver = mock(Driver.class);
69+
mockConnection = mock(Connection.class);
70+
actualUrl = new AtomicReference<>();
71+
72+
when(mockDriver.acceptsURL(any())).thenReturn(true);
73+
when(mockDriver.connect(any(), any(Properties.class))).thenAnswer(invocation -> {
74+
actualUrl.set(invocation.getArgument(0));
75+
return mockConnection;
76+
});
77+
78+
DriverManager.registerDriver(mockDriver);
79+
}
80+
81+
@After
82+
public void tearDown() throws SQLException
83+
{
84+
if (mockDriver != null) {
85+
DriverManager.deregisterDriver(mockDriver);
86+
}
87+
}
88+
89+
private Connection createConnection(String jdbcUrl, CredentialsProvider credentialsProvider)
90+
{
91+
DatabaseConnectionConfig config = new DatabaseConnectionConfig(CATALOG, SYNAPSE, jdbcUrl, SECRET_NAME);
92+
DatabaseConnectionInfo info = new DatabaseConnectionInfo(MOCK_DRIVER_CLASS, MOCK_PORT);
93+
SynapseJdbcConnectionFactory factory = new SynapseJdbcConnectionFactory(config, new HashMap<>(), info);
94+
return factory.getConnection(credentialsProvider);
95+
}
96+
97+
@Test
98+
public void testGetConnection_withCredentials_replacesSecret()
99+
{
100+
Connection connection = createConnection(MOCK_JDBC_URL + SECRET_PLACEHOLDER,
101+
new StaticCredentialsProvider(new DefaultCredentials(TEST_USERNAME, TEST_PASSWORD)));
102+
String expectedUrl = MOCK_JDBC_URL + USER_PASSWORD_PARAMS;
103+
assertEquals(expectedUrl, actualUrl.get());
104+
assertEquals(mockConnection, connection);
105+
}
106+
107+
@Test
108+
public void testGetConnection_withActiveDirectoryServicePrincipal()
109+
{
110+
Connection connection = createConnection(
111+
MOCK_JDBC_URL + ACTIVE_DIRECTORY_AUTH + SECRET_PLACEHOLDER,
112+
new StaticCredentialsProvider(new DefaultCredentials(TEST_USERNAME, TEST_PASSWORD)));
113+
String expectedUrl = MOCK_JDBC_URL + ACTIVE_DIRECTORY_AUTH + AAD_PRINCIPAL_PARAMS;
114+
assertEquals(expectedUrl, actualUrl.get());
115+
assertEquals(mockConnection, connection);
116+
}
117+
118+
@Test
119+
public void testGetConnection_withNullCredentials_doesNotReplaceSecret()
120+
{
121+
Connection connection = createConnection(MOCK_JDBC_URL + USER_PASSWORD_PARAMS, null);
122+
assertEquals(MOCK_JDBC_URL + USER_PASSWORD_PARAMS, actualUrl.get());
123+
assertEquals(mockConnection, connection);
124+
}
125+
126+
@Test(expected = RuntimeException.class)
127+
public void testGetConnection_whenDriverClassNotFound_throwsRuntimeException() throws SQLException
128+
{
129+
DatabaseConnectionConfig config = new DatabaseConnectionConfig(CATALOG, SYNAPSE, MOCK_JDBC_URL, SECRET_NAME);
130+
131+
// Intentionally provide invalid driver class
132+
DatabaseConnectionInfo info = new DatabaseConnectionInfo("invalid.DriverClass", MOCK_PORT);
133+
134+
SynapseJdbcConnectionFactory factory = new SynapseJdbcConnectionFactory(config, new HashMap<>(), info);
135+
try (Connection ignored = factory.getConnection(null)) {
136+
fail("Expected RuntimeException was not thrown");
137+
}
138+
}
139+
140+
@Test(expected = RuntimeException.class)
141+
public void testGetConnection_whenDriverFailsToConnect_throwsRuntimeException() throws SQLException
142+
{
143+
// Override the default mock behavior for this specific test
144+
when(mockDriver.connect(anyString(), any(Properties.class)))
145+
.thenThrow(new SQLException("DB error", "08001", 999));
146+
147+
try (Connection ignored = createConnection(MOCK_JDBC_URL,
148+
new StaticCredentialsProvider(new DefaultCredentials(TEST_USERNAME, TEST_PASSWORD)))) {
149+
fail("Expected RuntimeException was not thrown");
150+
}
151+
}
152+
}

0 commit comments

Comments
 (0)