Skip to content

Commit

Permalink
separated tests of different connections
Browse files Browse the repository at this point in the history
  • Loading branch information
alexradzin committed Nov 1, 2023
1 parent cd9b0b2 commit 3d8f4e4
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.firebolt.jdbc.annotation.NotImplemented;
import com.firebolt.jdbc.client.FireboltObjectMapper;
import com.firebolt.jdbc.client.HttpClientConfig;
import com.firebolt.jdbc.client.account.FireboltAccountRetriever;
import com.firebolt.jdbc.client.authentication.FireboltAuthenticationClient;
import com.firebolt.jdbc.client.query.StatementClientImpl;
import com.firebolt.jdbc.connection.settings.FireboltProperties;
Expand All @@ -15,10 +14,7 @@
import com.firebolt.jdbc.exception.FireboltUnsupportedOperationException;
import com.firebolt.jdbc.metadata.FireboltDatabaseMetadata;
import com.firebolt.jdbc.metadata.FireboltSystemEngineDatabaseMetadata;
import com.firebolt.jdbc.service.FireboltAccountIdService;
import com.firebolt.jdbc.service.FireboltAuthenticationService;
import com.firebolt.jdbc.service.FireboltEngineInformationSchemaService;
import com.firebolt.jdbc.service.FireboltGatewayUrlService;
import com.firebolt.jdbc.service.FireboltStatementService;
import com.firebolt.jdbc.statement.FireboltStatement;
import com.firebolt.jdbc.statement.preparedstatement.FireboltPreparedStatement;
Expand Down Expand Up @@ -221,9 +217,9 @@ public boolean isClosed() {
public DatabaseMetaData getMetaData() throws SQLException {
this.validateConnectionIsNotClose();
if (!loginProperties.isSystemEngine()) {
return new FireboltDatabaseMetadata(this.httpConnectionUrl, this);
return new FireboltDatabaseMetadata(httpConnectionUrl, this);
} else {
return new FireboltSystemEngineDatabaseMetadata(this.httpConnectionUrl, this);
return new FireboltSystemEngineDatabaseMetadata(httpConnectionUrl, this);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public class FireboltConnectionUserPasswordAuthentication extends FireboltConnec
protected void authenticate() throws SQLException {
String accessToken = getAccessToken(loginProperties).orElse(StringUtils.EMPTY);
FireboltProperties propertiesWithAccessToken = loginProperties.toBuilder().accessToken(accessToken).build();
String endpoint = fireboltEngineService.getEngine(propertiesWithAccessToken).getEndpoint();
this.sessionProperties = loginProperties.toBuilder().host(endpoint).build();
Engine engine = fireboltEngineService.getEngine(propertiesWithAccessToken);
this.sessionProperties = loginProperties.toBuilder().host(engine.getEndpoint()).database(engine.getDatabase()).engine(engine.getName()).build();
}

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

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Properties;

import static java.lang.String.format;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

public class FireboltConnectionServiceSecretAuthenticationTest extends FireboltConnectionTest {
private static final String SYSTEM_ENGINE_URL = "jdbc:firebolt:db?env=dev&account=dev";

@Test
void shouldNotValidateConnectionWhenCallingIsValidWhenUsingSystemEngine() throws SQLException {
Properties propertiesWithSystemEngine = new Properties(connectionProperties);
try (FireboltConnection fireboltConnection = createConnection(SYSTEM_ENGINE_URL, propertiesWithSystemEngine)) {
fireboltConnection.isValid(500);
verifyNoInteractions(fireboltStatementService);
}
}

@Test
void shouldNotGetEngineUrlOrDefaultEngineUrlWhenUsingSystemEngine() throws SQLException {
connectionProperties.put("database", "my_db");
when(fireboltGatewayUrlService.getUrl(any(), any())).thenReturn("http://my_endpoint");

try (FireboltConnection connection = createConnection(SYSTEM_ENGINE_URL, connectionProperties)) {
verify(fireboltEngineService, times(0)).getEngine(argThat(props -> "my_db".equals(props.getDatabase())));
assertEquals("my_endpoint", connection.getSessionProperties().getHost());
}
}

@Test
void noEngineAndDb() throws SQLException {
when(fireboltGatewayUrlService.getUrl(any(), any())).thenReturn("http://my_endpoint");

try (FireboltConnection connection = createConnection("jdbc:firebolt:?env=dev&account=dev", connectionProperties)) {
assertEquals("my_endpoint", connection.getSessionProperties().getHost());
assertNull(connection.getSessionProperties().getEngine());
assertTrue(connection.getSessionProperties().isSystemEngine());
}
}

@ParameterizedTest(name = "{0}")
@CsvSource({
"regular engine,&engine=eng,false",
"system engine,'',true" // system engine is readonly
})
void getMetadata(String testName, String engineParameter, boolean readOnly) throws SQLException {
try (FireboltConnection connection = createConnection(format("jdbc:firebolt:db?env=dev&account=dev%s", engineParameter), connectionProperties)) {
DatabaseMetaData dbmd = connection.getMetaData();
assertEquals(readOnly, dbmd.isReadOnly());
}
}

protected FireboltConnection createConnection(String url, Properties props) throws SQLException {
return new FireboltConnectionServiceSecretAuthentication(url, props, fireboltAuthenticationService, fireboltGatewayUrlService, fireboltStatementService, fireboltEngineService, fireboltAccountIdService);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,30 +75,28 @@
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class FireboltConnectionTest {
abstract class FireboltConnectionTest {

private static final String URL = "jdbc:firebolt:db?env=dev&engine=eng&account=dev";

private static final String SYSTEM_ENGINE_URL = "jdbc:firebolt:db?env=dev&account=dev";

private static final String LOCAL_URL = "jdbc:firebolt:local_dev_db?account=dev&ssl=false&max_query_size=10000000&use_standard_sql=1&mask_internal_errors=0&firebolt_enable_beta_functions=1&firebolt_case_insensitive_identifiers=1&rest_api_pull_timeout_sec=3600&rest_api_pull_interval_millisec=5000&rest_api_retry_times=10&host=localhost";
private final FireboltConnectionTokens fireboltConnectionTokens = FireboltConnectionTokens.builder().build();
@Captor
private ArgumentCaptor<FireboltProperties> propertiesArgumentCaptor;
@Captor
private ArgumentCaptor<StatementInfoWrapper> queryInfoWrapperArgumentCaptor;
@Mock
private FireboltAuthenticationService fireboltAuthenticationService;
protected FireboltAuthenticationService fireboltAuthenticationService;
@Mock
private FireboltGatewayUrlService fireboltGatewayUrlService;
protected FireboltGatewayUrlService fireboltGatewayUrlService;

@Mock
private FireboltEngineInformationSchemaService fireboltEngineService;
protected FireboltEngineInformationSchemaService fireboltEngineService;
@Mock
private FireboltStatementService fireboltStatementService;
protected FireboltStatementService fireboltStatementService;
@Mock
private FireboltAccountIdService fireboltAccountIdService;
private Properties connectionProperties = new Properties();
protected FireboltAccountIdService fireboltAccountIdService;
protected Properties connectionProperties = new Properties();
private static Connection connection;

private static Stream<Arguments> unsupported() {
Expand Down Expand Up @@ -324,15 +322,6 @@ void shouldValidateConnectionWhenCallingIsValid() throws SQLException {
}
}

@Test
void shouldNotValidateConnectionWhenCallingIsValidWhenUsingSystemEngine() throws SQLException {
Properties propertiesWithSystemEngine = new Properties(connectionProperties);
try (FireboltConnection fireboltConnection = createConnection(SYSTEM_ENGINE_URL, propertiesWithSystemEngine)) {
fireboltConnection.isValid(500);
verifyNoInteractions(fireboltStatementService);
}
}

@Test
void shouldIgnore429WhenValidatingConnection() throws SQLException {
when(fireboltStatementService.execute(any(), any(), anyInt(), anyInt(), anyBoolean(), anyBoolean(), any()))
Expand Down Expand Up @@ -600,29 +589,5 @@ void shouldGetEngineUrlWhenEngineIsProvided() throws SQLException {
}
}

@Test
void shouldNotGetEngineUrlOrDefaultEngineUrlWhenUsingSystemEngine() throws SQLException {
connectionProperties.put("database", "my_db");
when(fireboltGatewayUrlService.getUrl(any(), any())).thenReturn("http://my_endpoint");

try (FireboltConnection connection = createConnection(SYSTEM_ENGINE_URL, connectionProperties)) {
verify(fireboltEngineService, times(0)).getEngine(argThat(props -> "my_db".equals(props.getDatabase())));
assertEquals("my_endpoint", connection.getSessionProperties().getHost());
}
}

@Test
void noEngineAndDb() throws SQLException {
when(fireboltGatewayUrlService.getUrl(any(), any())).thenReturn("http://my_endpoint");

try (FireboltConnection connection = createConnection("jdbc:firebolt:?env=dev&account=dev", connectionProperties)) {
assertEquals("my_endpoint", connection.getSessionProperties().getHost());
assertNull(connection.getSessionProperties().getEngine());
assertTrue(connection.getSessionProperties().isSystemEngine());
}
}

private FireboltConnection createConnection(String url, Properties props) throws SQLException {
return new FireboltConnectionServiceSecretAuthentication(url, props, fireboltAuthenticationService, fireboltGatewayUrlService, fireboltStatementService, fireboltEngineService, fireboltAccountIdService);
}
protected abstract FireboltConnection createConnection(String url, Properties props) throws SQLException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.firebolt.jdbc.connection;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Properties;

import static java.lang.String.format;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;

public class FireboltConnectionUserPasswordAuthenticationTest extends FireboltConnectionTest {
private static final String SYSTEM_ENGINE_URL = "jdbc:firebolt:db?env=dev&account=dev&engine=system";
@Test
void shouldNotValidateConnectionWhenCallingIsValidWhenUsingSystemEngine() throws SQLException {
Properties propertiesWithSystemEngine = new Properties(connectionProperties);
try (FireboltConnection fireboltConnection = createConnection(SYSTEM_ENGINE_URL, propertiesWithSystemEngine)) {
fireboltConnection.isValid(500);
verifyNoInteractions(fireboltStatementService);
}
}

@Test
void shouldNotGetEngineUrlOrDefaultEngineUrlWhenUsingSystemEngine() throws SQLException {
connectionProperties.put("database", "my_db");
try (FireboltConnection connection = createConnection(SYSTEM_ENGINE_URL, connectionProperties)) {
verify(fireboltEngineService, times(1)).getEngine(argThat(props -> "my_db".equals(props.getDatabase()) && "system".equals(props.getEngine())));
assertEquals("endpoint", connection.getSessionProperties().getHost());
}
}


@Test
void noEngineAndDb() throws SQLException {
try (FireboltConnection connection = createConnection("jdbc:firebolt:?env=dev&account=dev", connectionProperties)) {
assertEquals("endpoint", connection.getSessionProperties().getHost());
assertNotNull(connection.getSessionProperties().getEngine());
assertFalse(connection.getSessionProperties().isSystemEngine());
}
}

@ParameterizedTest
@CsvSource({
"eng,false",
"system,true" // system engine is readonly
})
void getMetadata(String engine, boolean readOnly) throws SQLException {
try (FireboltConnection connection = createConnection(format("jdbc:firebolt:db?env=dev&engine=%s&account=dev", engine), connectionProperties)) {
DatabaseMetaData dbmd = connection.getMetaData();
assertEquals(readOnly, dbmd.isReadOnly());
}
}

protected FireboltConnection createConnection(String url, Properties props) throws SQLException {
return new FireboltConnectionUserPasswordAuthentication(url, props, fireboltAuthenticationService, fireboltStatementService, fireboltEngineService);
}
}

0 comments on commit 3d8f4e4

Please sign in to comment.