Skip to content

Commit

Permalink
improved FireboltProperties: removed of() factory method, added const…
Browse files Browse the repository at this point in the history
…ructors (#320)
  • Loading branch information
Alexander Radzin authored Jan 11, 2024
1 parent 72e31d0 commit 47803d5
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private static int getUrlVersion(String url, Properties connectionSettings) {
if (allSettings.containsKey("client_id") && allSettings.containsKey("client_secret") && !allSettings.containsKey("user") && !allSettings.containsKey("password")) {
return 2;
}
FireboltProperties props = FireboltProperties.of(propertiesFromUrl, connectionSettings);
FireboltProperties props = new FireboltProperties(new Properties[] {propertiesFromUrl, connectionSettings});
String principal = props.getPrincipal();
if (principal != null && principal.contains("@")) {
return 1;
Expand Down Expand Up @@ -330,8 +330,7 @@ protected FireboltProperties extractFireboltProperties(String jdbcUri, Propertie
}

private static FireboltProperties createFireboltProperties(String jdbcUri, Properties connectionProperties) {
Properties propertiesFromUrl = UrlUtil.extractProperties(jdbcUri);
return FireboltProperties.of(propertiesFromUrl, connectionProperties);
return new FireboltProperties(new Properties[] {UrlUtil.extractProperties(jdbcUri), connectionProperties});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.firebolt.jdbc.connection.settings;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.CustomLog;
import lombok.EqualsAndHashCode;
Expand Down Expand Up @@ -28,6 +29,7 @@

@Getter
@ToString
@AllArgsConstructor
@EqualsAndHashCode
@Builder(toBuilder = true)
@CustomLog
Expand All @@ -45,86 +47,74 @@ public class FireboltProperties {
return keys;
}).flatMap(List::stream).collect(Collectors.toSet());

final int keepAliveTimeoutMillis;
final int maxConnectionsTotal;
final int maxRetries;
final int bufferSize;
final int clientBufferSize;
final int socketTimeoutMillis;
final int connectionTimeoutMillis;
final Integer port;
final String host;
String database;
final String path;
final boolean ssl;
final String sslCertificatePath;
final String sslMode;
final boolean compress;
final String principal;
final String secret;
String engine;
final String account;
final String accountId;
final Integer tcpKeepIdle;
final Integer tcpKeepCount;
final Integer tcpKeepInterval;
final boolean logResultSet;
boolean systemEngine;
final String environment;
final String userDrivers;
final String userClients;
final String accessToken;

private final int keepAliveTimeoutMillis;
private final int maxConnectionsTotal;
private final int maxRetries;
private final int bufferSize;
private final int socketTimeoutMillis;
private final int connectionTimeoutMillis;
private final Integer port;
private final String host;
private String database; // updatable using use statement
private final String path;
private final boolean ssl;
private final String sslCertificatePath;
private final String sslMode;
private final boolean compress;
private final String principal;
private final String secret;
private String engine; // updatable using use statement
private final String account;
private final String accountId;
private final int tcpKeepIdle;
private final int tcpKeepCount;
private final int tcpKeepInterval;
private final boolean logResultSet;
private boolean systemEngine;
private final String environment;
private final String userDrivers;
private final String userClients;
private final String accessToken;
@Builder.Default
final Map<String, String> additionalProperties = new HashMap<>();

public static FireboltProperties of(Properties... properties) {
Properties mergedProperties = mergeProperties(properties);
boolean ssl = getSetting(mergedProperties, FireboltSessionProperty.SSL);
String sslRootCertificate = getSetting(mergedProperties, FireboltSessionProperty.SSL_CERTIFICATE_PATH);
String sslMode = getSetting(mergedProperties, FireboltSessionProperty.SSL_MODE);
String principal = getSetting(mergedProperties, FireboltSessionProperty.CLIENT_ID);
String secret = getSetting(mergedProperties, FireboltSessionProperty.CLIENT_SECRET);
String path = getSetting(mergedProperties, FireboltSessionProperty.PATH);
String database = getDatabase(mergedProperties, path);
String engine = getEngine(mergedProperties);
boolean isSystemEngine = isSystemEngine(engine);
boolean compress = ((Boolean) getSetting(mergedProperties, FireboltSessionProperty.COMPRESS))
&& !isSystemEngine;
String account = getSetting(mergedProperties, FireboltSessionProperty.ACCOUNT);
int keepAliveMillis = getSetting(mergedProperties, FireboltSessionProperty.KEEP_ALIVE_TIMEOUT_MILLIS);
int maxTotal = getSetting(mergedProperties, FireboltSessionProperty.MAX_CONNECTIONS_TOTAL);
int maxRetries = getSetting(mergedProperties, FireboltSessionProperty.MAX_RETRIES);
int bufferSize = getSetting(mergedProperties, FireboltSessionProperty.BUFFER_SIZE);
int socketTimeout = getSetting(mergedProperties, FireboltSessionProperty.SOCKET_TIMEOUT_MILLIS);
int connectionTimeout = getSetting(mergedProperties, FireboltSessionProperty.CONNECTION_TIMEOUT_MILLIS);
int tcpKeepInterval = getSetting(mergedProperties, FireboltSessionProperty.TCP_KEEP_INTERVAL);
int tcpKeepIdle = getSetting(mergedProperties, FireboltSessionProperty.TCP_KEEP_IDLE);
int tcpKeepCount = getSetting(mergedProperties, FireboltSessionProperty.TCP_KEEP_COUNT);
boolean logResultSet = getSetting(mergedProperties, FireboltSessionProperty.LOG_RESULT_SET);
String configuredEnvironment = getSetting(mergedProperties, FireboltSessionProperty.ENVIRONMENT);
String driverVersions = getSetting(mergedProperties, FireboltSessionProperty.USER_DRIVERS);
String clientVersions = getSetting(mergedProperties, FireboltSessionProperty.USER_CLIENTS);
Map<String, String> additionalProperties = new HashMap<>();

String environment = getEnvironment(configuredEnvironment, mergedProperties);
String host = getHost(configuredEnvironment, mergedProperties);
Integer port = getPort(mergedProperties, ssl);
String accessToken = getSetting(mergedProperties, FireboltSessionProperty.ACCESS_TOKEN);

Map<String, String> additionalProperties = new HashMap<>(getFireboltCustomProperties(mergedProperties)); // wrap with HashMap to make these properties updatable
public FireboltProperties(Properties[] allProperties) {
this(mergeProperties(allProperties));
}

return FireboltProperties.builder().ssl(ssl).sslCertificatePath(sslRootCertificate).sslMode(sslMode).path(path)
.port(port).database(database).compress(compress).principal(principal).secret(secret).host(host)
.additionalProperties(additionalProperties).account(account).engine(engine)
.keepAliveTimeoutMillis(keepAliveMillis).maxConnectionsTotal(maxTotal).maxRetries(maxRetries)
.bufferSize(bufferSize).socketTimeoutMillis(socketTimeout).connectionTimeoutMillis(connectionTimeout)
.tcpKeepInterval(tcpKeepInterval).tcpKeepCount(tcpKeepCount).tcpKeepIdle(tcpKeepIdle)
.logResultSet(logResultSet).systemEngine(isSystemEngine)
.environment(environment)
.userDrivers(driverVersions)
.userClients(clientVersions)
.accessToken(accessToken)
.build();
public FireboltProperties(Properties properties) {
ssl = getSetting(properties, FireboltSessionProperty.SSL);
sslCertificatePath = getSetting(properties, FireboltSessionProperty.SSL_CERTIFICATE_PATH);
sslMode = getSetting(properties, FireboltSessionProperty.SSL_MODE);
principal = getSetting(properties, FireboltSessionProperty.CLIENT_ID);
secret = getSetting(properties, FireboltSessionProperty.CLIENT_SECRET);
path = getSetting(properties, FireboltSessionProperty.PATH);
database = getDatabase(properties, path);
engine = getEngine(properties);
systemEngine = isSystemEngine(engine);
compress = ((Boolean) getSetting(properties, FireboltSessionProperty.COMPRESS)) && !systemEngine;
account = getSetting(properties, FireboltSessionProperty.ACCOUNT);
accountId = getSetting(properties, FireboltSessionProperty.ACCOUNT_ID);
keepAliveTimeoutMillis = getSetting(properties, FireboltSessionProperty.KEEP_ALIVE_TIMEOUT_MILLIS);
maxConnectionsTotal = getSetting(properties, FireboltSessionProperty.MAX_CONNECTIONS_TOTAL);
maxRetries = getSetting(properties, FireboltSessionProperty.MAX_RETRIES);
bufferSize = getSetting(properties, FireboltSessionProperty.BUFFER_SIZE);
socketTimeoutMillis = getSetting(properties, FireboltSessionProperty.SOCKET_TIMEOUT_MILLIS);
connectionTimeoutMillis = getSetting(properties, FireboltSessionProperty.CONNECTION_TIMEOUT_MILLIS);
tcpKeepInterval = getSetting(properties, FireboltSessionProperty.TCP_KEEP_INTERVAL);
tcpKeepIdle = getSetting(properties, FireboltSessionProperty.TCP_KEEP_IDLE);
tcpKeepCount = getSetting(properties, FireboltSessionProperty.TCP_KEEP_COUNT);
logResultSet = getSetting(properties, FireboltSessionProperty.LOG_RESULT_SET);
String configuredEnvironment = getSetting(properties, FireboltSessionProperty.ENVIRONMENT);
userDrivers = getSetting(properties, FireboltSessionProperty.USER_DRIVERS);
userClients = getSetting(properties, FireboltSessionProperty.USER_CLIENTS);

environment = getEnvironment(configuredEnvironment, properties);
host = getHost(configuredEnvironment, properties);
port = getPort(properties, ssl);
accessToken = getSetting(properties, FireboltSessionProperty.ACCESS_TOKEN);

additionalProperties = new HashMap<>(getFireboltCustomProperties(properties)); // wrap with HashMap to make these properties updatable
}

private static String getEngine(Properties mergedProperties) {
Expand All @@ -133,11 +123,7 @@ private static String getEngine(Properties mergedProperties) {

private static String getHost(String environment, Properties properties ) {
String host = getSetting(properties, FireboltSessionProperty.HOST);
if (StringUtils.isEmpty(host)) {
return format("api.%s.firebolt.io", environment);
} else {
return host;
}
return host == null || host.isEmpty() ? format("api.%s.firebolt.io", environment) : host;
}

/**
Expand Down Expand Up @@ -263,5 +249,4 @@ public String getHttpConnectionUrl() {
String protocol = isSsl() ? "https://" : "http://";
return protocol + hostAndPort;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.junit.jupiter.api.Assertions.assertSame;

import java.lang.reflect.Field;
import java.util.Properties;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -25,7 +26,7 @@ public void resetSingleton() throws ReflectiveOperationException {
@Test
void shouldInitHttpClient() throws Exception {
assertNull(HttpClientConfig.getInstance());
OkHttpClient client = HttpClientConfig.init(FireboltProperties.of());
OkHttpClient client = HttpClientConfig.init(new FireboltProperties(new Properties()));
assertNotNull(client);
assertSame(client, HttpClientConfig.getInstance());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ private FireboltConnection use(String oldDb, String useCommand, Map<String, List
props.setProperty("host", "firebolt1");
props.setProperty("port", "555");
props.setProperty(FireboltSessionProperty.CONNECTION_TIMEOUT_MILLIS.getKey(), "0"); // simplifies mocking
FireboltProperties fireboltProperties = FireboltProperties.of(props);
FireboltProperties fireboltProperties = new FireboltProperties(props);
Call okCall = getMockedCallWithResponse(200, "", responseHeaders);
when(okHttpClient.newCall(any())).thenReturn(okCall);
FireboltAuthenticationService fireboltAuthenticationService = mock(FireboltAuthenticationService.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.MockedConstruction;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

Expand All @@ -37,6 +37,7 @@
import java.sql.Statement;
import java.sql.Types;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -423,15 +424,18 @@ void shouldThrowExceptionIfAbortingWithNullExecutor() throws SQLException, Inter
@Test
void shouldRemoveExpiredToken() throws SQLException {
FireboltProperties fireboltProperties = FireboltProperties.builder().host("host").database("db").port(8080).account("dev").build();
try (MockedStatic<FireboltProperties> mockedFireboltProperties = Mockito.mockStatic(FireboltProperties.class)) {
when(FireboltProperties.of(any())).thenReturn(fireboltProperties);
when(fireboltAuthenticationService.getConnectionTokens("http://host:8080", fireboltProperties))
.thenReturn(FireboltConnectionTokens.builder().build());
String url = fireboltProperties.getHttpConnectionUrl();
try (MockedConstruction<FireboltProperties> mockedFireboltPropertiesConstruction = Mockito.mockConstruction(FireboltProperties.class, (fireboltPropertiesMock, context) -> {
when(fireboltPropertiesMock.getAccount()).thenReturn(fireboltProperties.getAccount());
when(fireboltPropertiesMock.getDatabase()).thenReturn(fireboltProperties.getDatabase());
when(fireboltPropertiesMock.getHttpConnectionUrl()).thenReturn(fireboltProperties.getHttpConnectionUrl());
when(fireboltPropertiesMock.toBuilder()).thenReturn(fireboltProperties.toBuilder());
lenient().when(fireboltAuthenticationService.getConnectionTokens(eq(url), argThat(argument -> true))).thenReturn(FireboltConnectionTokens.builder().build());
lenient().when(fireboltEngineService.getEngine(any())).thenReturn(new Engine("http://hello", null, null, null, null));

})) {
try (FireboltConnection fireboltConnection = createConnection(URL, connectionProperties)) {
fireboltConnection.removeExpiredTokens();
verify(fireboltAuthenticationService).removeConnectionTokens("http://host:8080", fireboltProperties);
verify(fireboltAuthenticationService).removeConnectionTokens(eq(url), argThat(argument -> true));
}
}
}
Expand All @@ -440,13 +444,19 @@ void shouldRemoveExpiredToken() throws SQLException {
void shouldReturnConnectionTokenWhenAvailable() throws SQLException {
String accessToken = "hello";
FireboltProperties fireboltProperties = FireboltProperties.builder().host("host").database("db").port(8080).account("dev").build();
try (MockedStatic<FireboltProperties> mockedFireboltProperties = Mockito.mockStatic(FireboltProperties.class)) {
when(FireboltProperties.of(any())).thenReturn(fireboltProperties);
String url = fireboltProperties.getHttpConnectionUrl();

try (MockedConstruction<FireboltProperties> mockedFireboltPropertiesConstruction = Mockito.mockConstruction(FireboltProperties.class, (fireboltPropertiesMock, context) -> {
when(fireboltPropertiesMock.getAccount()).thenReturn(fireboltProperties.getAccount());
when(fireboltPropertiesMock.getHttpConnectionUrl()).thenReturn(url);
when(fireboltPropertiesMock.getDatabase()).thenReturn(fireboltProperties.getDatabase());
when(fireboltPropertiesMock.toBuilder()).thenReturn(fireboltProperties.toBuilder());
})) {
FireboltConnectionTokens connectionTokens = FireboltConnectionTokens.builder().accessToken(accessToken).build();
when(fireboltAuthenticationService.getConnectionTokens(eq("http://host:8080"), any())).thenReturn(connectionTokens);
when(fireboltAuthenticationService.getConnectionTokens(eq(url), any())).thenReturn(connectionTokens);
lenient().when(fireboltEngineService.getEngine(any())).thenReturn(new Engine("http://engineHost", null, null, null, null));
try (FireboltConnection fireboltConnection = createConnection(URL, connectionProperties)) {
verify(fireboltAuthenticationService).getConnectionTokens("http://host:8080", fireboltProperties);
verify(fireboltAuthenticationService).getConnectionTokens(eq(url), argThat(argument -> Objects.equals(argument.getHttpConnectionUrl(), url)));
assertEquals(accessToken, fireboltConnection.getAccessToken().get());
}
}
Expand All @@ -455,8 +465,10 @@ void shouldReturnConnectionTokenWhenAvailable() throws SQLException {
@Test
void shouldNotReturnConnectionTokenWithLocalDb() throws SQLException {
FireboltProperties fireboltProperties = FireboltProperties.builder().host("localhost").build();
try (MockedStatic<FireboltProperties> mockedFireboltProperties = Mockito.mockStatic(FireboltProperties.class)) {
when(FireboltProperties.of(any())).thenReturn(fireboltProperties);
try (MockedConstruction<FireboltProperties> mockedFireboltPropertiesConstruction = Mockito.mockConstruction(FireboltProperties.class, (fireboltPropertiesMock, context) -> {
when(fireboltPropertiesMock.getHost()).thenReturn(fireboltProperties.getHost());
when(fireboltPropertiesMock.toBuilder()).thenReturn(fireboltProperties.toBuilder());
})) {
try (FireboltConnection fireboltConnection = createConnection(URL, connectionProperties)) {
assertEquals(Optional.empty(), fireboltConnection.getAccessToken());
verifyNoInteractions(fireboltAuthenticationService);
Expand Down Expand Up @@ -494,10 +506,12 @@ void shouldThrowExceptionIfBothAccessTokenAndUserPasswordAreSupplied() {

@Test
void shouldSetNetworkTimeout() throws SQLException {
FireboltProperties fireboltProperties = FireboltProperties.builder().host("localhost").path("/db")
.socketTimeoutMillis(5).port(8080).build();
try (MockedStatic<FireboltProperties> mockedFireboltProperties = Mockito.mockStatic(FireboltProperties.class)) {
when(FireboltProperties.of(any())).thenReturn(fireboltProperties);
FireboltProperties fireboltProperties = FireboltProperties.builder().host("localhost").socketTimeoutMillis(5).build();
try (MockedConstruction<FireboltProperties> mockedFireboltPropertiesConstruction = Mockito.mockConstruction(FireboltProperties.class, (fireboltPropertiesMock, context) -> {
when(fireboltPropertiesMock.getHost()).thenReturn(fireboltProperties.getHost());
when(fireboltPropertiesMock.getSocketTimeoutMillis()).thenReturn(fireboltProperties.getSocketTimeoutMillis());
when(fireboltPropertiesMock.toBuilder()).thenReturn(fireboltProperties.toBuilder());
})) {
try (FireboltConnection fireboltConnection = createConnection(URL, connectionProperties)) {
assertEquals(5, fireboltConnection.getNetworkTimeout());
fireboltConnection.setNetworkTimeout(null, 1);
Expand All @@ -508,11 +522,14 @@ void shouldSetNetworkTimeout() throws SQLException {

@Test
void shouldUseConnectionTimeoutFromProperties() throws SQLException {
FireboltProperties fireboltProperties = FireboltProperties.builder().host("localhost").path("/db").connectionTimeoutMillis(20).port(8080).build();
try (MockedStatic<FireboltProperties> mockedFireboltProperties = Mockito.mockStatic(FireboltProperties.class)) {
when(FireboltProperties.of(any())).thenReturn(fireboltProperties);
FireboltProperties fireboltProperties = FireboltProperties.builder().host("localhost").connectionTimeoutMillis(20).build();
try (MockedConstruction<FireboltProperties> mockedFireboltPropertiesConstruction = Mockito.mockConstruction(FireboltProperties.class, (fireboltPropertiesMock, context) -> {
when(fireboltPropertiesMock.getHost()).thenReturn(fireboltProperties.getHost());
when(fireboltPropertiesMock.getConnectionTimeoutMillis()).thenReturn(fireboltProperties.getConnectionTimeoutMillis());
when(fireboltPropertiesMock.toBuilder()).thenReturn(fireboltProperties.toBuilder());
})) {
try (FireboltConnection fireboltConnection = createConnection(URL, connectionProperties)) {
assertEquals(20, fireboltConnection.getConnectionTimeout());
assertEquals(fireboltProperties.getConnectionTimeoutMillis(), fireboltConnection.getConnectionTimeout());
}
}
}
Expand Down
Loading

0 comments on commit 47803d5

Please sign in to comment.