diff --git a/src/main/java/com/firebolt/jdbc/connection/FireboltConnection.java b/src/main/java/com/firebolt/jdbc/connection/FireboltConnection.java index 0cffbc22b..0334dbdb1 100644 --- a/src/main/java/com/firebolt/jdbc/connection/FireboltConnection.java +++ b/src/main/java/com/firebolt/jdbc/connection/FireboltConnection.java @@ -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; @@ -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; @@ -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); } } diff --git a/src/main/java/com/firebolt/jdbc/connection/FireboltConnectionUserPasswordAuthentication.java b/src/main/java/com/firebolt/jdbc/connection/FireboltConnectionUserPasswordAuthentication.java index f909189c1..15d44a281 100644 --- a/src/main/java/com/firebolt/jdbc/connection/FireboltConnectionUserPasswordAuthentication.java +++ b/src/main/java/com/firebolt/jdbc/connection/FireboltConnectionUserPasswordAuthentication.java @@ -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 diff --git a/src/test/java/com/firebolt/jdbc/connection/FireboltConnectionServiceSecretAuthenticationTest.java b/src/test/java/com/firebolt/jdbc/connection/FireboltConnectionServiceSecretAuthenticationTest.java new file mode 100644 index 000000000..e31201303 --- /dev/null +++ b/src/test/java/com/firebolt/jdbc/connection/FireboltConnectionServiceSecretAuthenticationTest.java @@ -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); + } +} diff --git a/src/test/java/com/firebolt/jdbc/connection/FireboltConnectionTest.java b/src/test/java/com/firebolt/jdbc/connection/FireboltConnectionTest.java index 53dca0b74..a1deaccd8 100644 --- a/src/test/java/com/firebolt/jdbc/connection/FireboltConnectionTest.java +++ b/src/test/java/com/firebolt/jdbc/connection/FireboltConnectionTest.java @@ -75,12 +75,10 @@ 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 @@ -88,17 +86,17 @@ class FireboltConnectionTest { @Captor private ArgumentCaptor 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 unsupported() { @@ -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())) @@ -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; } diff --git a/src/test/java/com/firebolt/jdbc/connection/FireboltConnectionUserPasswordAuthenticationTest.java b/src/test/java/com/firebolt/jdbc/connection/FireboltConnectionUserPasswordAuthenticationTest.java new file mode 100644 index 000000000..70921deda --- /dev/null +++ b/src/test/java/com/firebolt/jdbc/connection/FireboltConnectionUserPasswordAuthenticationTest.java @@ -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); + } +}