Skip to content

Commit f602b63

Browse files
committed
added test for secure connection
1 parent a107d2d commit f602b63

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/DriverProperties.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
*/
1212
public enum DriverProperties {
1313

14+
/**
15+
* Indicates if driver should create a secure connection over SSL/TLS
16+
*/
17+
SECURE_CONNECTION("ssl", "false"),
18+
1419
/**
1520
* query settings to be passed along with query operation.
1621
* {@see com.clickhouse.client.api.query.QuerySettings}

jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcConfiguration.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ static String createConnectionURL(String url, boolean ssl) throws SQLException {
8080
}
8181

8282
try {
83-
System.out.println(url);
8483
URI tmp = URI.create(url);
85-
System.out.println(tmp);
8684
return tmp.getScheme() + "://" + tmp.getAuthority() + tmp.getPath();
8785
} catch (Exception e) {
8886
throw new SQLException("Failed to parse url", e);
@@ -95,7 +93,7 @@ private static String stripUrlPrefix(String url) {
9593
} else if (url.startsWith(PREFIX_CLICKHOUSE_SHORT)) {
9694
return url.substring(PREFIX_CLICKHOUSE_SHORT.length());
9795
} else {
98-
throw new IllegalArgumentException("URL is not supported.");
96+
throw new IllegalArgumentException("Specified URL doesn't have jdbc any of prefixes: [ " + PREFIX_CLICKHOUSE + ", " + PREFIX_CLICKHOUSE_SHORT + " ]");
9997
}
10098
}
10199

@@ -107,7 +105,7 @@ private void initProperties(String url, Properties providedProperties) {
107105
if (entry.getKey() instanceof String && entry.getValue() instanceof String) {
108106
props.put((String) entry.getKey(), (String) entry.getValue());
109107
} else {
110-
throw new RuntimeException("Cannot apply non-String properties");
108+
throw new IllegalArgumentException("Property key and value should be a string");
111109
}
112110
}
113111

jdbc-v2/src/test/java/com/clickhouse/jdbc/ConnectionTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
import java.sql.*;
44
import java.util.Properties;
55

6+
import com.clickhouse.client.ClickHouseNode;
7+
import com.clickhouse.client.ClickHouseProtocol;
68
import com.clickhouse.client.api.ClientConfigProperties;
79
import com.clickhouse.client.api.ServerException;
810
import com.clickhouse.client.api.internal.ServerSettings;
911
import com.clickhouse.jdbc.internal.ClientInfoProperties;
12+
import com.clickhouse.jdbc.internal.DriverProperties;
1013
import org.testng.Assert;
1114
import org.testng.annotations.Test;
1215

@@ -306,4 +309,38 @@ public void testMaxResultRowsProperty() throws Exception {
306309
}
307310
}
308311
}
312+
313+
@Test
314+
public void testSecureConnection() throws Exception {
315+
if (isCloud()) {
316+
return; // this test uses self-signed cert
317+
}
318+
ClickHouseNode secureServer = getSecureServer(ClickHouseProtocol.HTTP);
319+
320+
Properties properties = new Properties();
321+
properties.put(ClientConfigProperties.USER.getKey(), "default");
322+
properties.put(ClientConfigProperties.PASSWORD.getKey(), "");
323+
properties.put(ClientConfigProperties.CA_CERTIFICATE.getKey(), "containers/clickhouse-server/certs/localhost.crt");
324+
325+
try (Connection conn = new ConnectionImpl("jdbc:clickhouse:" + secureServer.getBaseUri(), properties);
326+
Statement stmt = conn.createStatement();
327+
ResultSet rs = stmt.executeQuery("SELECT number FROM system.numbers LIMIT 10")) {
328+
329+
int count = 0;
330+
while (rs.next()) { count ++ ; }
331+
Assert.assertEquals(count, 10);
332+
}
333+
334+
properties.put(DriverProperties.SECURE_CONNECTION.getKey(), "true");
335+
String jdbcUrl = "jdbc:clickhouse://"+secureServer.getHost() + ":" + secureServer.getPort() + "/";
336+
337+
try (Connection conn = new ConnectionImpl(jdbcUrl, properties);
338+
Statement stmt = conn.createStatement();
339+
ResultSet rs = stmt.executeQuery("SELECT number FROM system.numbers LIMIT 10")) {
340+
341+
int count = 0;
342+
while (rs.next()) { count ++ ; }
343+
Assert.assertEquals(count, 10);
344+
}
345+
}
309346
}

jdbc-v2/src/test/java/com/clickhouse/jdbc/internal/JdbcConfigurationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import static org.testng.Assert.assertEquals;
1515

1616
public class JdbcConfigurationTest {
17-
17+
1818
@Test(dataProvider = "testConnectionUrlDataProvider")
1919
public void testConnectionUrl(String jdbcUrl, String connectionUrl, Properties properties) throws Exception {
2020
JdbcConfiguration configuration = new JdbcConfiguration(jdbcUrl, properties);

0 commit comments

Comments
 (0)