diff --git a/build.gradle b/build.gradle index 59b1f0d11..dbb2ad408 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ plugins { } group 'com.firebolt' -version = '2.4.0' +version = '2.4.1-SNAPSHOT' repositories { mavenCentral() diff --git a/lombok.config b/lombok.config index 1e69cb834..86058d25d 100644 --- a/lombok.config +++ b/lombok.config @@ -1,4 +1,4 @@ lombok.anyConstructor.addConstructorProperties = true config.stopBubbling = true lombok.addLombokGeneratedAnnotation = true -lombok.log.custom.declaration = com.firebolt.jdbc.log.FireboltLogger com.firebolt.jdbc.LoggerUtil.getLogger(NAME) \ No newline at end of file +lombok.log.custom.declaration = com.firebolt.jdbc.log.FireboltLogger com.firebolt.jdbc.util.LoggerUtil.getLogger(NAME) \ No newline at end of file diff --git a/src/integrationTest/java/integration/tests/DatabaseMetaDataTest.java b/src/integrationTest/java/integration/tests/DatabaseMetaDataTest.java index d5647275a..15ddfa075 100644 --- a/src/integrationTest/java/integration/tests/DatabaseMetaDataTest.java +++ b/src/integrationTest/java/integration/tests/DatabaseMetaDataTest.java @@ -110,7 +110,7 @@ void shouldReturnColumns() throws SQLException { contains(schemaName, schemaName, schemaName, schemaName, schemaName)); assertThat(result.get(REMARKS), contains(null, null, null, null, null)); assertThat(result.get(NULLABLE), contains("0", "1", "1", "1", "0")); - assertThat(result.get(DECIMAL_DIGITS), contains(null, null, null, null, null)); + assertThat(result.get(DECIMAL_DIGITS), contains("0", "0", "0", "0", "0")); assertThat(result.get(SQL_DATETIME_SUB), contains(null, null, null, null, null)); assertThat(result.get(NUM_PREC_RADIX), contains("10", "10", "10", "10", "10")); assertThat(result.get(IS_GENERATEDCOLUMN), contains("NO", "NO", "NO", "NO", "NO")); @@ -121,9 +121,9 @@ void shouldReturnColumns() throws SQLException { assertThat(result.get(SCOPE_SCHEMA), contains(null, null, null, null, null)); assertThat(result.get(ORDINAL_POSITION), contains("1", "2", "3", "4", "5")); assertThat(result.get(TYPE_NAME), - contains("bigint", "timestamp", "string", "boolean", "integer")); + contains("bigint", "timestamp", "text", "boolean", "integer")); assertThat(result.get(DATA_TYPE), contains("-5", "93", "12", "16", "4")); - assertThat(result.get(COLUMN_SIZE), contains("20", "19", "0", "1", "11")); + assertThat(result.get(COLUMN_SIZE), contains("0", "6", "0", "1", "0")); } } diff --git a/src/main/java/com/firebolt/FireboltDriver.java b/src/main/java/com/firebolt/FireboltDriver.java index 2139fd22c..e079e62e6 100644 --- a/src/main/java/com/firebolt/FireboltDriver.java +++ b/src/main/java/com/firebolt/FireboltDriver.java @@ -6,8 +6,8 @@ import org.apache.commons.lang3.StringUtils; -import com.firebolt.jdbc.PropertyUtil; -import com.firebolt.jdbc.VersionUtil; +import com.firebolt.jdbc.util.PropertyUtil; +import com.firebolt.jdbc.util.VersionUtil; import com.firebolt.jdbc.connection.FireboltConnection; import com.firebolt.jdbc.exception.FireboltSQLFeatureNotSupportedException; diff --git a/src/main/java/com/firebolt/jdbc/client/FireboltClient.java b/src/main/java/com/firebolt/jdbc/client/FireboltClient.java index f1764fce0..5694fbaf1 100644 --- a/src/main/java/com/firebolt/jdbc/client/FireboltClient.java +++ b/src/main/java/com/firebolt/jdbc/client/FireboltClient.java @@ -15,7 +15,7 @@ import org.apache.commons.lang3.tuple.Pair; import com.fasterxml.jackson.databind.ObjectMapper; -import com.firebolt.jdbc.CloseableUtil; +import com.firebolt.jdbc.util.CloseableUtil; import com.firebolt.jdbc.connection.FireboltConnection; import com.firebolt.jdbc.exception.FireboltException; import com.firebolt.jdbc.resultset.compress.LZ4InputStream; diff --git a/src/main/java/com/firebolt/jdbc/client/UsageTrackerUtil.java b/src/main/java/com/firebolt/jdbc/client/UsageTrackerUtil.java index ac62787bd..0c2548860 100644 --- a/src/main/java/com/firebolt/jdbc/client/UsageTrackerUtil.java +++ b/src/main/java/com/firebolt/jdbc/client/UsageTrackerUtil.java @@ -5,7 +5,7 @@ import org.apache.commons.lang3.StringUtils; -import com.firebolt.jdbc.VersionUtil; +import com.firebolt.jdbc.util.VersionUtil; import com.google.common.collect.ImmutableMap; import lombok.CustomLog; diff --git a/src/main/java/com/firebolt/jdbc/client/query/StatementClientImpl.java b/src/main/java/com/firebolt/jdbc/client/query/StatementClientImpl.java index 01c59327e..1ad04a9b5 100644 --- a/src/main/java/com/firebolt/jdbc/client/query/StatementClientImpl.java +++ b/src/main/java/com/firebolt/jdbc/client/query/StatementClientImpl.java @@ -10,8 +10,8 @@ import org.apache.commons.lang3.tuple.Pair; import com.fasterxml.jackson.databind.ObjectMapper; -import com.firebolt.jdbc.CloseableUtil; -import com.firebolt.jdbc.PropertyUtil; +import com.firebolt.jdbc.util.CloseableUtil; +import com.firebolt.jdbc.util.PropertyUtil; import com.firebolt.jdbc.client.FireboltClient; import com.firebolt.jdbc.connection.FireboltConnection; import com.firebolt.jdbc.connection.FireboltConnectionTokens; diff --git a/src/main/java/com/firebolt/jdbc/connection/FireboltConnection.java b/src/main/java/com/firebolt/jdbc/connection/FireboltConnection.java index a219029f5..21a84a25d 100644 --- a/src/main/java/com/firebolt/jdbc/connection/FireboltConnection.java +++ b/src/main/java/com/firebolt/jdbc/connection/FireboltConnection.java @@ -14,7 +14,7 @@ import org.apache.commons.lang3.tuple.Pair; import com.fasterxml.jackson.databind.ObjectMapper; -import com.firebolt.jdbc.PropertyUtil; +import com.firebolt.jdbc.util.PropertyUtil; import com.firebolt.jdbc.annotation.ExcludeFromJacocoGeneratedReport; import com.firebolt.jdbc.annotation.NotImplemented; import com.firebolt.jdbc.client.FireboltObjectMapper; diff --git a/src/main/java/com/firebolt/jdbc/metadata/FireboltDatabaseMetadata.java b/src/main/java/com/firebolt/jdbc/metadata/FireboltDatabaseMetadata.java index 818f915c0..1a3533ca9 100644 --- a/src/main/java/com/firebolt/jdbc/metadata/FireboltDatabaseMetadata.java +++ b/src/main/java/com/firebolt/jdbc/metadata/FireboltDatabaseMetadata.java @@ -12,7 +12,7 @@ import org.apache.commons.lang3.StringUtils; import com.firebolt.jdbc.QueryResult; -import com.firebolt.jdbc.VersionUtil; +import com.firebolt.jdbc.util.VersionUtil; import com.firebolt.jdbc.annotation.ExcludeFromJacocoGeneratedReport; import com.firebolt.jdbc.annotation.NotImplemented; import com.firebolt.jdbc.connection.FireboltConnection; diff --git a/src/main/java/com/firebolt/jdbc/resultset/FireboltResultSet.java b/src/main/java/com/firebolt/jdbc/resultset/FireboltResultSet.java index 2dda5081b..37edae5eb 100644 --- a/src/main/java/com/firebolt/jdbc/resultset/FireboltResultSet.java +++ b/src/main/java/com/firebolt/jdbc/resultset/FireboltResultSet.java @@ -15,7 +15,7 @@ import org.apache.commons.lang3.exception.ExceptionUtils; import org.apache.commons.text.StringEscapeUtils; -import com.firebolt.jdbc.LoggerUtil; +import com.firebolt.jdbc.util.LoggerUtil; import com.firebolt.jdbc.QueryResult; import com.firebolt.jdbc.annotation.ExcludeFromJacocoGeneratedReport; import com.firebolt.jdbc.annotation.NotImplemented; @@ -123,7 +123,7 @@ private BufferedReader createStreamReader(InputStream is, Integer bufferSize, bo } else { inputStreamReader = new InputStreamReader(is, StandardCharsets.UTF_8); } - return bufferSize != null ? new BufferedReader(inputStreamReader, bufferSize) + return bufferSize != null && bufferSize != 0 ? new BufferedReader(inputStreamReader, bufferSize) : new BufferedReader(inputStreamReader); } diff --git a/src/main/java/com/firebolt/jdbc/service/FireboltStatementService.java b/src/main/java/com/firebolt/jdbc/service/FireboltStatementService.java index f8737a8ec..401935847 100644 --- a/src/main/java/com/firebolt/jdbc/service/FireboltStatementService.java +++ b/src/main/java/com/firebolt/jdbc/service/FireboltStatementService.java @@ -3,12 +3,21 @@ import static com.firebolt.jdbc.exception.ExceptionType.INVALID_REQUEST; import java.io.InputStream; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Optional; import com.firebolt.jdbc.client.query.StatementClient; import com.firebolt.jdbc.connection.settings.FireboltProperties; import com.firebolt.jdbc.exception.FireboltException; +import com.firebolt.jdbc.resultset.FireboltResultSet; +import com.firebolt.jdbc.statement.FireboltStatement; import com.firebolt.jdbc.statement.StatementInfoWrapper; +import com.firebolt.jdbc.statement.StatementType; +import com.firebolt.jdbc.statement.rawstatement.QueryRawStatement; +import com.firebolt.jdbc.util.CloseableUtil; +import com.firebolt.jdbc.util.InputStreamUtil; import lombok.CustomLog; import lombok.NonNull; import lombok.RequiredArgsConstructor; @@ -17,24 +26,36 @@ @CustomLog public class FireboltStatementService { + private static final String UNKNOWN_TABLE_NAME = "unknown"; private final StatementClient statementClient; private final boolean systemEngine; /** * Executes statement * - * @param statementInfoWrapper the statement - * @param connectionProperties the connection properties + * @param statementInfoWrapper the statement info + * @param properties the connection properties * @param queryTimeout query timeout * @param maxRows max rows * @param standardSql indicates if standard sql should be used + * @param statement the statement * @return an InputStream with the result */ - public InputStream execute(@NonNull StatementInfoWrapper statementInfoWrapper, - @NonNull FireboltProperties connectionProperties, int queryTimeout, int maxRows, boolean standardSql) - throws FireboltException { - return statementClient.postSqlStatement(statementInfoWrapper, connectionProperties, systemEngine, queryTimeout, + public Optional execute(StatementInfoWrapper statementInfoWrapper, + FireboltProperties properties, int queryTimeout, int maxRows, boolean standardSql, + FireboltStatement statement) + throws SQLException { + InputStream is = statementClient.postSqlStatement(statementInfoWrapper, properties, systemEngine, queryTimeout, maxRows, standardSql); + if (statementInfoWrapper.getType() == StatementType.QUERY) { + return Optional.of(createResultSet(is, (QueryRawStatement) statementInfoWrapper.getInitialStatement(), properties, statement)); + } else { + // If the statement is not a query, read all bytes from the input stream and close it. + // This is needed otherwise the stream with the server will be closed after having received the first chunk of data (resulting in incomplete inserts). + InputStreamUtil.readAllBytes(is); + CloseableUtil.close(is); + } + return Optional.empty(); } public void abortStatement(@NonNull String statementId, @NonNull FireboltProperties properties) @@ -53,4 +74,12 @@ public void abortStatementHttpRequest(@NonNull String statementId) throws Firebo public boolean isStatementRunning(String statementId) { return statementClient.isStatementRunning(statementId); } + + private FireboltResultSet createResultSet(InputStream inputStream, QueryRawStatement initialQuery, FireboltProperties properties, FireboltStatement statement) + throws SQLException { + return new FireboltResultSet(inputStream, Optional.ofNullable(initialQuery.getTable()).orElse(UNKNOWN_TABLE_NAME), + Optional.ofNullable(initialQuery.getDatabase()).orElse(properties.getDatabase()), + properties.getBufferSize(), properties.isCompress(), statement, + properties.isLogResultSet()); + } } diff --git a/src/main/java/com/firebolt/jdbc/statement/FireboltStatement.java b/src/main/java/com/firebolt/jdbc/statement/FireboltStatement.java index a1d294df6..b7c7672cc 100644 --- a/src/main/java/com/firebolt/jdbc/statement/FireboltStatement.java +++ b/src/main/java/com/firebolt/jdbc/statement/FireboltStatement.java @@ -7,8 +7,6 @@ import org.apache.commons.lang3.StringUtils; -import com.firebolt.jdbc.CloseableUtil; -import com.firebolt.jdbc.PropertyUtil; import com.firebolt.jdbc.annotation.ExcludeFromJacocoGeneratedReport; import com.firebolt.jdbc.annotation.NotImplemented; import com.firebolt.jdbc.connection.FireboltConnection; @@ -16,9 +14,9 @@ import com.firebolt.jdbc.exception.FireboltException; import com.firebolt.jdbc.exception.FireboltSQLFeatureNotSupportedException; import com.firebolt.jdbc.exception.FireboltUnsupportedOperationException; -import com.firebolt.jdbc.resultset.FireboltResultSet; import com.firebolt.jdbc.service.FireboltStatementService; -import com.firebolt.jdbc.statement.rawstatement.QueryRawStatement; +import com.firebolt.jdbc.util.CloseableUtil; +import com.firebolt.jdbc.util.PropertyUtil; import lombok.Builder; import lombok.CustomLog; @@ -111,15 +109,13 @@ private Optional execute(StatementInfoWrapper statementInfoWrapper, b this.connection.addProperty(statementInfoWrapper.getParam()); log.debug("The property from the query {} was stored", runningStatementId); } else { - inputStream = statementService.execute(statementInfoWrapper, this.sessionProperties, - this.queryTimeout, this.maxRows, isStandardSql); - if (statementInfoWrapper.getType() == StatementType.QUERY) { - resultSet = getResultSet(inputStream, - (QueryRawStatement) statementInfoWrapper.getInitialStatement()); + Optional currentRs = statementService.execute(statementInfoWrapper, + this.sessionProperties, this.queryTimeout, this.maxRows, isStandardSql, this); + if (currentRs.isPresent()) { + resultSet = currentRs.get(); currentUpdateCount = -1; // Always -1 when returning a ResultSet } else { currentUpdateCount = 0; - CloseableUtil.close(inputStream); } log.info("The query with the id {} was executed with success", runningStatementId); } @@ -152,14 +148,6 @@ private boolean isStatementNotCancelled(StatementInfoWrapper statementInfoWrappe } } - private FireboltResultSet getResultSet(InputStream inputStream, QueryRawStatement initialQuery) - throws SQLException { - return new FireboltResultSet(inputStream, Optional.ofNullable(initialQuery.getTable()).orElse("unknown"), - Optional.ofNullable(initialQuery.getDatabase()).orElse(this.sessionProperties.getDatabase()), - this.sessionProperties.getBufferSize(), this.sessionProperties.isCompress(), this, - this.sessionProperties.isLogResultSet()); - } - private void closeAllResults() { synchronized (this) { if (this.firstUnclosedStatementResult != null) { @@ -496,6 +484,7 @@ public void clearBatch() throws SQLException { // Batch are not supported by the driver throw new FireboltUnsupportedOperationException(); } + @Override @NotImplemented @ExcludeFromJacocoGeneratedReport @@ -503,7 +492,7 @@ public int[] executeBatch() throws SQLException { // Batch are not supported by the driver throw new FireboltUnsupportedOperationException(); } - + @Override @NotImplemented @ExcludeFromJacocoGeneratedReport diff --git a/src/main/java/com/firebolt/jdbc/CloseableUtil.java b/src/main/java/com/firebolt/jdbc/util/CloseableUtil.java similarity index 94% rename from src/main/java/com/firebolt/jdbc/CloseableUtil.java rename to src/main/java/com/firebolt/jdbc/util/CloseableUtil.java index d522b7eb9..660576645 100644 --- a/src/main/java/com/firebolt/jdbc/CloseableUtil.java +++ b/src/main/java/com/firebolt/jdbc/util/CloseableUtil.java @@ -1,4 +1,4 @@ -package com.firebolt.jdbc; +package com.firebolt.jdbc.util; import java.io.Closeable; import java.io.IOException; diff --git a/src/main/java/com/firebolt/jdbc/util/InputStreamUtil.java b/src/main/java/com/firebolt/jdbc/util/InputStreamUtil.java new file mode 100644 index 000000000..3c86b9e16 --- /dev/null +++ b/src/main/java/com/firebolt/jdbc/util/InputStreamUtil.java @@ -0,0 +1,30 @@ +package com.firebolt.jdbc.util; + +import lombok.CustomLog; +import lombok.experimental.UtilityClass; + +import javax.annotation.Nullable; +import java.io.IOException; +import java.io.InputStream; + +@UtilityClass +@CustomLog +public class InputStreamUtil { + + /** + * Read all bytes from the input stream if the stream is not null + * + * @param is input stream + */ + public void readAllBytes(@Nullable InputStream is) { + if (is != null) { + while (true) { + try { + if (is.read() == -1) break; + } catch (IOException e) { + log.warn("Could not read entire input stream for non query statement", e); + } + } + } + } +} diff --git a/src/main/java/com/firebolt/jdbc/LoggerUtil.java b/src/main/java/com/firebolt/jdbc/util/LoggerUtil.java similarity index 98% rename from src/main/java/com/firebolt/jdbc/LoggerUtil.java rename to src/main/java/com/firebolt/jdbc/util/LoggerUtil.java index 8ab4a31ae..3092885a3 100644 --- a/src/main/java/com/firebolt/jdbc/LoggerUtil.java +++ b/src/main/java/com/firebolt/jdbc/util/LoggerUtil.java @@ -1,4 +1,4 @@ -package com.firebolt.jdbc; +package com.firebolt.jdbc.util; import java.io.*; import java.nio.charset.StandardCharsets; diff --git a/src/main/java/com/firebolt/jdbc/PropertyUtil.java b/src/main/java/com/firebolt/jdbc/util/PropertyUtil.java similarity index 98% rename from src/main/java/com/firebolt/jdbc/PropertyUtil.java rename to src/main/java/com/firebolt/jdbc/util/PropertyUtil.java index e61a6dd61..7b460cfb0 100644 --- a/src/main/java/com/firebolt/jdbc/PropertyUtil.java +++ b/src/main/java/com/firebolt/jdbc/util/PropertyUtil.java @@ -1,4 +1,4 @@ -package com.firebolt.jdbc; +package com.firebolt.jdbc.util; import java.sql.DriverPropertyInfo; import java.util.ArrayList; diff --git a/src/main/java/com/firebolt/jdbc/VersionUtil.java b/src/main/java/com/firebolt/jdbc/util/VersionUtil.java similarity index 98% rename from src/main/java/com/firebolt/jdbc/VersionUtil.java rename to src/main/java/com/firebolt/jdbc/util/VersionUtil.java index e35990117..892f3bfba 100644 --- a/src/main/java/com/firebolt/jdbc/VersionUtil.java +++ b/src/main/java/com/firebolt/jdbc/util/VersionUtil.java @@ -1,4 +1,4 @@ -package com.firebolt.jdbc; +package com.firebolt.jdbc.util; import java.io.IOException; import java.util.Properties; diff --git a/src/test/java/com/firebolt/jdbc/client/UsageTrackerUtilTest.java b/src/test/java/com/firebolt/jdbc/client/UsageTrackerUtilTest.java index d56f72dad..548fbf529 100644 --- a/src/test/java/com/firebolt/jdbc/client/UsageTrackerUtilTest.java +++ b/src/test/java/com/firebolt/jdbc/client/UsageTrackerUtilTest.java @@ -17,7 +17,7 @@ import org.mockito.MockedStatic; import org.mockito.Mockito; -import com.firebolt.jdbc.VersionUtil; +import com.firebolt.jdbc.util.VersionUtil; @SetSystemProperty(key = "java.version", value = "8.0.1") @SetSystemProperty(key = "os.version", value = "10.1") diff --git a/src/test/java/com/firebolt/jdbc/client/account/FireboltAccountClientTest.java b/src/test/java/com/firebolt/jdbc/client/account/FireboltAccountClientTest.java index 8181492a2..f3f2e1659 100644 --- a/src/test/java/com/firebolt/jdbc/client/account/FireboltAccountClientTest.java +++ b/src/test/java/com/firebolt/jdbc/client/account/FireboltAccountClientTest.java @@ -20,7 +20,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; -import com.firebolt.jdbc.VersionUtil; +import com.firebolt.jdbc.util.VersionUtil; import com.firebolt.jdbc.client.account.response.FireboltAccountResponse; import com.firebolt.jdbc.client.account.response.FireboltDefaultDatabaseEngineResponse; import com.firebolt.jdbc.client.account.response.FireboltEngineIdResponse; diff --git a/src/test/java/com/firebolt/jdbc/client/authentication/FireboltAuthenticationClientTest.java b/src/test/java/com/firebolt/jdbc/client/authentication/FireboltAuthenticationClientTest.java index f9e93cc36..3c9ed6ae9 100644 --- a/src/test/java/com/firebolt/jdbc/client/authentication/FireboltAuthenticationClientTest.java +++ b/src/test/java/com/firebolt/jdbc/client/authentication/FireboltAuthenticationClientTest.java @@ -15,7 +15,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; -import com.firebolt.jdbc.VersionUtil; +import com.firebolt.jdbc.util.VersionUtil; import com.firebolt.jdbc.client.authentication.response.FireboltAuthenticationResponse; import com.firebolt.jdbc.connection.FireboltConnection; import com.firebolt.jdbc.exception.FireboltException; diff --git a/src/test/java/com/firebolt/jdbc/client/query/StatementClientImplTest.java b/src/test/java/com/firebolt/jdbc/client/query/StatementClientImplTest.java index 4e13c7c3b..aa449322d 100644 --- a/src/test/java/com/firebolt/jdbc/client/query/StatementClientImplTest.java +++ b/src/test/java/com/firebolt/jdbc/client/query/StatementClientImplTest.java @@ -24,7 +24,7 @@ import org.mockito.junit.jupiter.MockitoExtension; import com.fasterxml.jackson.databind.ObjectMapper; -import com.firebolt.jdbc.VersionUtil; +import com.firebolt.jdbc.util.VersionUtil; import com.firebolt.jdbc.connection.FireboltConnection; import com.firebolt.jdbc.connection.FireboltConnectionTokens; import com.firebolt.jdbc.connection.settings.FireboltProperties; diff --git a/src/test/java/com/firebolt/jdbc/connection/FireboltConnectionTest.java b/src/test/java/com/firebolt/jdbc/connection/FireboltConnectionTest.java index 910250335..6b4081f08 100644 --- a/src/test/java/com/firebolt/jdbc/connection/FireboltConnectionTest.java +++ b/src/test/java/com/firebolt/jdbc/connection/FireboltConnectionTest.java @@ -6,7 +6,6 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.*; -import java.io.ByteArrayInputStream; import java.sql.*; import java.util.Optional; import java.util.Properties; @@ -77,8 +76,8 @@ void shouldNotFetchTokenNorEngineHostForLocalFirebolt() throws SQLException { @Test void shouldPrepareStatement() throws SQLException { - when(fireboltStatementService.execute(any(), any(), anyInt(), anyInt(), anyBoolean())) - .thenReturn(new ByteArrayInputStream("".getBytes())); + when(fireboltStatementService.execute(any(), any(), anyInt(), anyInt(), anyBoolean(), any())) + .thenReturn(Optional.empty()); FireboltConnection fireboltConnection = new FireboltConnection(URL, connectionProperties, fireboltAuthenticationService, fireboltEngineService, fireboltStatementService); PreparedStatement statement = fireboltConnection @@ -89,7 +88,7 @@ void shouldPrepareStatement() throws SQLException { assertNotNull(fireboltConnection); assertNotNull(statement); verify(fireboltStatementService).execute(queryInfoWrapperArgumentCaptor.capture(), any(), anyInt(), anyInt(), - anyBoolean()); + anyBoolean(), any()); assertEquals("INSERT INTO cars(sales, name) VALUES (500, 'Ford')", queryInfoWrapperArgumentCaptor.getValue().getSql()); } @@ -111,13 +110,13 @@ void shouldNotSetNewPropertyWhenConnectionIsNotValidWithTheNewProperty() throws FireboltConnection fireboltConnection = new FireboltConnection(URL, connectionProperties, fireboltAuthenticationService, fireboltEngineService, fireboltStatementService); - when(fireboltStatementService.execute(any(), any(), anyInt(), anyInt(), anyBoolean())) + when(fireboltStatementService.execute(any(), any(), anyInt(), anyInt(), anyBoolean(), any())) .thenThrow(new FireboltException(ExceptionType.TOO_MANY_REQUESTS)); assertThrows(FireboltException.class, () -> fireboltConnection.addProperty(new ImmutablePair<>("custom_1", "1"))); verify(fireboltStatementService).execute(queryInfoWrapperArgumentCaptor.capture(), - propertiesArgumentCaptor.capture(), anyInt(), anyInt(), anyBoolean()); + propertiesArgumentCaptor.capture(), anyInt(), anyInt(), anyBoolean(), any()); assertEquals("1", propertiesArgumentCaptor.getValue().getAdditionalProperties().get("custom_1")); assertEquals("SELECT 1", queryInfoWrapperArgumentCaptor.getValue().getSql()); assertNull(fireboltConnection.getSessionProperties().getAdditionalProperties().get("custom_1")); @@ -125,8 +124,8 @@ void shouldNotSetNewPropertyWhenConnectionIsNotValidWithTheNewProperty() throws @Test void shouldSetNewPropertyWhenConnectionIsValidWithTheNewProperty() throws SQLException { - when(fireboltStatementService.execute(any(), any(), anyInt(), anyInt(), anyBoolean())) - .thenReturn(new ByteArrayInputStream("".getBytes())); + when(fireboltStatementService.execute(any(), any(), anyInt(), anyInt(), anyBoolean(), any())) + .thenReturn(Optional.empty()); FireboltConnection fireboltConnection = new FireboltConnection(URL, connectionProperties, fireboltAuthenticationService, fireboltEngineService, fireboltStatementService); @@ -135,7 +134,7 @@ void shouldSetNewPropertyWhenConnectionIsValidWithTheNewProperty() throws SQLExc fireboltConnection.addProperty(newProperties); verify(fireboltStatementService).execute(queryInfoWrapperArgumentCaptor.capture(), - propertiesArgumentCaptor.capture(), anyInt(), anyInt(), anyBoolean()); + propertiesArgumentCaptor.capture(), anyInt(), anyInt(), anyBoolean(), any()); assertEquals("1", propertiesArgumentCaptor.getValue().getAdditionalProperties().get("custom_1")); assertEquals("1", fireboltConnection.getSessionProperties().getAdditionalProperties().get("custom_1")); assertEquals("SELECT 1", queryInfoWrapperArgumentCaptor.getValue().getSql()); @@ -143,14 +142,14 @@ void shouldSetNewPropertyWhenConnectionIsValidWithTheNewProperty() throws SQLExc @Test void shouldValidateConnectionWhenCallingIsValid() throws SQLException { - when(fireboltStatementService.execute(any(), any(), anyInt(), anyInt(), anyBoolean())) - .thenReturn(new ByteArrayInputStream("".getBytes())); + when(fireboltStatementService.execute(any(), any(), anyInt(), anyInt(), anyBoolean(), any())) + .thenReturn(Optional.empty()); FireboltConnection fireboltConnection = new FireboltConnection(URL, connectionProperties, fireboltAuthenticationService, fireboltEngineService, fireboltStatementService); fireboltConnection.isValid(500); verify(fireboltStatementService).execute(queryInfoWrapperArgumentCaptor.capture(), - propertiesArgumentCaptor.capture(), anyInt(), anyInt(), anyBoolean()); + propertiesArgumentCaptor.capture(), anyInt(), anyInt(), anyBoolean(), any()); assertEquals("SELECT 1", queryInfoWrapperArgumentCaptor.getValue().getSql()); } @@ -167,7 +166,7 @@ void shouldNotValidateConnectionWhenCallingIsValidWhenUsingSystemEngine() throws @Test void shouldIgnore429WhenValidatingConnection() throws SQLException { - when(fireboltStatementService.execute(any(), any(), anyInt(), anyInt(), anyBoolean())) + when(fireboltStatementService.execute(any(), any(), anyInt(), anyInt(), anyBoolean(), any())) .thenThrow(new FireboltException(ExceptionType.TOO_MANY_REQUESTS)); FireboltConnection fireboltConnection = new FireboltConnection(URL, connectionProperties, fireboltAuthenticationService, fireboltEngineService, fireboltStatementService); @@ -176,7 +175,7 @@ void shouldIgnore429WhenValidatingConnection() throws SQLException { @Test void shouldReturnFalseWhenValidatingConnectionThrowsAnException() throws SQLException { - when(fireboltStatementService.execute(any(), any(), anyInt(), anyInt(), anyBoolean())) + when(fireboltStatementService.execute(any(), any(), anyInt(), anyInt(), anyBoolean(), any())) .thenThrow(new FireboltException(ExceptionType.ERROR)); FireboltConnection fireboltConnection = new FireboltConnection(URL, connectionProperties, fireboltAuthenticationService, fireboltEngineService, fireboltStatementService); @@ -200,8 +199,8 @@ void shouldReturnFalseWhenValidatingClosedConnection() throws SQLException { @Test void shouldExtractConnectorOverrides() throws SQLException { - when(fireboltStatementService.execute(any(), any(), anyInt(), anyInt(), anyBoolean())) - .thenReturn(new ByteArrayInputStream("".getBytes())); + when(fireboltStatementService.execute(any(), any(), anyInt(), anyInt(), anyBoolean(), any())) + .thenReturn(Optional.empty()); connectionProperties.put("user_clients", "ConnA:1.0.9,ConnB:2.8.0"); connectionProperties.put("user_drivers", "DriverA:2.0.9,DriverB:3.8.0"); @@ -212,7 +211,7 @@ void shouldExtractConnectorOverrides() throws SQLException { statement.execute(); verify(fireboltStatementService).execute(any(), propertiesArgumentCaptor.capture(), anyInt(), anyInt(), - anyBoolean()); + anyBoolean(), any()); assertNull(propertiesArgumentCaptor.getValue().getAdditionalProperties().get("user_clients")); assertNull(propertiesArgumentCaptor.getValue().getAdditionalProperties().get("user_drivers")); assertNull(fireboltConnectionImpl.getSessionProperties().getAdditionalProperties().get("user_clients")); diff --git a/src/test/java/com/firebolt/jdbc/resultset/FireboltResultSetTest.java b/src/test/java/com/firebolt/jdbc/resultset/FireboltResultSetTest.java index 631981137..a3572df85 100644 --- a/src/test/java/com/firebolt/jdbc/resultset/FireboltResultSetTest.java +++ b/src/test/java/com/firebolt/jdbc/resultset/FireboltResultSetTest.java @@ -26,7 +26,7 @@ import org.mockito.MockedStatic; import org.mockito.junit.jupiter.MockitoExtension; -import com.firebolt.jdbc.LoggerUtil; +import com.firebolt.jdbc.util.LoggerUtil; import com.firebolt.jdbc.exception.FireboltException; import com.firebolt.jdbc.statement.FireboltStatement; diff --git a/src/test/java/com/firebolt/jdbc/service/FireboltStatementServiceTest.java b/src/test/java/com/firebolt/jdbc/service/FireboltStatementServiceTest.java index 5decbd144..f04c05e96 100644 --- a/src/test/java/com/firebolt/jdbc/service/FireboltStatementServiceTest.java +++ b/src/test/java/com/firebolt/jdbc/service/FireboltStatementServiceTest.java @@ -1,17 +1,24 @@ package com.firebolt.jdbc.service; import static org.junit.Assert.assertThrows; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoInteractions; +import static org.mockito.Mockito.*; +import java.sql.SQLException; +import java.util.Optional; + +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; +import org.mockito.MockedConstruction; +import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; import com.firebolt.jdbc.client.query.StatementClient; import com.firebolt.jdbc.connection.settings.FireboltProperties; import com.firebolt.jdbc.exception.FireboltException; +import com.firebolt.jdbc.resultset.FireboltResultSet; +import com.firebolt.jdbc.statement.FireboltStatement; import com.firebolt.jdbc.statement.StatementInfoWrapper; import com.firebolt.jdbc.statement.StatementUtil; @@ -22,31 +29,39 @@ class FireboltStatementServiceTest { private StatementClient statementClient; @Test - void shouldExecuteQueryWithAllRequiredParameters() throws FireboltException { - StatementInfoWrapper statementInfoWrapper = StatementUtil.parseToStatementInfoWrappers("SELECT 1").get(0); - FireboltProperties fireboltProperties = FireboltProperties.builder().database("db").host("firebolt1").ssl(true) - .compress(true).build(); - FireboltStatementService fireboltStatementService = new FireboltStatementService(statementClient, false); - fireboltStatementService.execute(statementInfoWrapper, fireboltProperties, 10, -1, true); - verify(statementClient).postSqlStatement(statementInfoWrapper, fireboltProperties, false, 10, -1, true); + void shouldExecuteQueryAndCreateResultSet() throws SQLException { + try (MockedConstruction mocked = Mockito.mockConstruction(FireboltResultSet.class)) { + + StatementInfoWrapper statementInfoWrapper = StatementUtil.parseToStatementInfoWrappers("SELECT 1").get(0); + FireboltProperties fireboltProperties = FireboltProperties.builder().database("db").host("firebolt1") + .ssl(true).compress(false).build(); + FireboltStatementService fireboltStatementService = new FireboltStatementService(statementClient, false); + + fireboltStatementService.execute(statementInfoWrapper, fireboltProperties, 10, -1, true, + mock(FireboltStatement.class)); + verify(statementClient).postSqlStatement(statementInfoWrapper, fireboltProperties, false, 10, -1, true); + Assertions.assertEquals(1, mocked.constructed().size()); + } } @Test - void shouldExecuteQueryWithLocalHostFormatParameters() throws FireboltException { - StatementInfoWrapper statementInfoWrapper = StatementUtil.parseToStatementInfoWrappers("SELECT 1").get(0); - FireboltProperties fireboltProperties = FireboltProperties.builder().database("db").host("localhost").ssl(true) - .compress(true).build(); - - FireboltStatementService fireboltStatementService = new FireboltStatementService(statementClient, false); - fireboltStatementService.execute(statementInfoWrapper, fireboltProperties, -1, 10, true); - verify(statementClient).postSqlStatement(statementInfoWrapper, fireboltProperties, false, -1, 10, true); - + void shouldExecuteQueryWithLocalHostFormatParameters() throws SQLException { + try (MockedConstruction mocked = Mockito.mockConstruction(FireboltResultSet.class)) { + StatementInfoWrapper statementInfoWrapper = StatementUtil.parseToStatementInfoWrappers("SELECT 1").get(0); + FireboltProperties fireboltProperties = FireboltProperties.builder().database("db").host("localhost") + .ssl(true).compress(false).build(); + FireboltStatementService fireboltStatementService = new FireboltStatementService(statementClient, false); + fireboltStatementService.execute(statementInfoWrapper, fireboltProperties, -1, 10, true, + mock(FireboltStatement.class)); + Assertions.assertEquals(1, mocked.constructed().size()); + verify(statementClient).postSqlStatement(statementInfoWrapper, fireboltProperties, false, -1, 10, true); + } } @Test void shouldCancelQueryWithAllRequiredParams() throws FireboltException { FireboltProperties fireboltProperties = FireboltProperties.builder().database("db").host("http://firebolt1") - .ssl(true).compress(true).build(); + .ssl(true).compress(false).build(); FireboltStatementService fireboltStatementService = new FireboltStatementService(statementClient, false); fireboltStatementService.abortStatement("123", fireboltProperties); @@ -56,7 +71,7 @@ void shouldCancelQueryWithAllRequiredParams() throws FireboltException { @Test void shouldThrowExceptionWhenTryingToCancelQueryWithASystemEngine() throws FireboltException { FireboltProperties fireboltProperties = FireboltProperties.builder().database("db").host("http://firebolt1") - .ssl(true).compress(true).build(); + .ssl(true).compress(false).build(); FireboltStatementService fireboltStatementService = new FireboltStatementService(statementClient, true); assertThrows(FireboltException.class, () -> fireboltStatementService.abortStatement("123", fireboltProperties)); @@ -64,23 +79,46 @@ void shouldThrowExceptionWhenTryingToCancelQueryWithASystemEngine() throws Fireb } @Test - void shouldExecuteQueryWithParametersForSystemEngine() throws FireboltException { - StatementInfoWrapper statementInfoWrapper = StatementUtil.parseToStatementInfoWrappers("SELECT 1").get(0); - FireboltProperties fireboltProperties = FireboltProperties.builder().database("db").host("firebolt1").ssl(true) - .compress(true).build(); - FireboltStatementService fireboltStatementService = new FireboltStatementService(statementClient, true); - fireboltStatementService.execute(statementInfoWrapper, fireboltProperties, 10, 10, true); - verify(statementClient).postSqlStatement(statementInfoWrapper, fireboltProperties, true, 10, 10, true); + void shouldExecuteQueryWithParametersForSystemEngine() throws SQLException { + try (MockedConstruction mocked = Mockito.mockConstruction(FireboltResultSet.class)) { + StatementInfoWrapper statementInfoWrapper = StatementUtil.parseToStatementInfoWrappers("SELECT 1").get(0); + FireboltProperties fireboltProperties = FireboltProperties.builder().database("db").host("firebolt1") + .ssl(true).compress(false).build(); + FireboltStatementService fireboltStatementService = new FireboltStatementService(statementClient, true); + fireboltStatementService.execute(statementInfoWrapper, fireboltProperties, 10, 10, true, + mock(FireboltStatement.class)); + Assertions.assertEquals(1, mocked.constructed().size()); + verify(statementClient).postSqlStatement(statementInfoWrapper, fireboltProperties, true, 10, 10, true); + } } @Test - void shouldIncludeNonStandardSqlQueryParamForNonStandardSql() throws FireboltException { - StatementInfoWrapper statementInfoWrapper = StatementUtil.parseToStatementInfoWrappers("SELECT 1").get(0); + void shouldIncludeNonStandardSqlQueryParamForNonStandardSql() throws SQLException { + try (MockedConstruction mocked = Mockito.mockConstruction(FireboltResultSet.class)) { + + StatementInfoWrapper statementInfoWrapper = StatementUtil.parseToStatementInfoWrappers("SELECT 1").get(0); + FireboltProperties fireboltProperties = FireboltProperties.builder().database("db").host("localhost") + .ssl(true).compress(false).build(); + + FireboltStatementService fireboltStatementService = new FireboltStatementService(statementClient, true); + fireboltStatementService.execute(statementInfoWrapper, fireboltProperties, -1, 0, false, + mock(FireboltStatement.class)); + Assertions.assertEquals(1, mocked.constructed().size()); + verify(statementClient).postSqlStatement(statementInfoWrapper, fireboltProperties, true, -1, 0, false); + } + } + + @Test + void shouldBeEmptyWithNonQueryStatement() throws SQLException { + StatementInfoWrapper statementInfoWrapper = StatementUtil + .parseToStatementInfoWrappers("INSERT INTO ltv SELECT * FROM ltv_external").get(0); FireboltProperties fireboltProperties = FireboltProperties.builder().database("db").host("localhost").ssl(true) - .compress(true).build(); + .compress(false).build(); - FireboltStatementService fireboltStatementService = new FireboltStatementService(statementClient, true); - fireboltStatementService.execute(statementInfoWrapper, fireboltProperties, -1, 0, false); - verify(statementClient).postSqlStatement(statementInfoWrapper, fireboltProperties, true, -1, 0, false); + FireboltStatementService fireboltStatementService = new FireboltStatementService(statementClient, false); + Assertions.assertEquals(Optional.empty(), fireboltStatementService.execute(statementInfoWrapper, + fireboltProperties, -1, 10, true, mock(FireboltStatement.class))); + verify(statementClient).postSqlStatement(statementInfoWrapper, fireboltProperties, false, -1, 10, true); } + } diff --git a/src/test/java/com/firebolt/jdbc/statement/FireboltStatementTest.java b/src/test/java/com/firebolt/jdbc/statement/FireboltStatementTest.java index 706ce5ad9..29d9d2471 100644 --- a/src/test/java/com/firebolt/jdbc/statement/FireboltStatementTest.java +++ b/src/test/java/com/firebolt/jdbc/statement/FireboltStatementTest.java @@ -5,18 +5,19 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.*; -import java.io.IOException; -import java.io.InputStream; import java.lang.reflect.Field; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.HashMap; +import java.util.Optional; import org.apache.commons.lang3.tuple.ImmutablePair; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.*; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import com.firebolt.jdbc.connection.FireboltConnection; @@ -42,121 +43,83 @@ class FireboltStatementTest { private FireboltConnection fireboltConnection; @Test - void shouldExecuteQueryAndCreateResultSet() throws SQLException { - try (MockedConstruction mocked = Mockito.mockConstruction(FireboltResultSet.class)) { - FireboltProperties fireboltProperties = FireboltProperties.builder().additionalProperties(new HashMap<>()) - .build(); - FireboltStatement fireboltStatement = FireboltStatement.builder().statementService(fireboltStatementService) - .sessionProperties(fireboltProperties).build(); - - when(fireboltStatementService.execute(any(), any(), anyInt(), anyInt(), anyBoolean())) - .thenReturn(mock(InputStream.class)); - fireboltStatement.executeQuery("show database"); - assertTrue(fireboltProperties.getAdditionalProperties().isEmpty()); - verify(fireboltStatementService).execute(queryInfoWrapperArgumentCaptor.capture(), eq(fireboltProperties), - anyInt(), anyInt(), anyBoolean()); - assertEquals(1, mocked.constructed().size()); - assertEquals(-1, fireboltStatement.getUpdateCount()); - assertEquals("show database", queryInfoWrapperArgumentCaptor.getValue().getSql()); - assertEquals(StatementType.QUERY, queryInfoWrapperArgumentCaptor.getValue().getType()); - } - } + void shouldExtractAdditionalPropertiesAndNotExecuteQueryWhenSetParamIsUsed() throws SQLException { + FireboltConnection connection = mock(FireboltConnection.class); + FireboltProperties fireboltProperties = FireboltProperties.builder().additionalProperties(new HashMap<>()) + .build(); + FireboltStatement fireboltStatement = FireboltStatement.builder().statementService(fireboltStatementService) + .sessionProperties(fireboltProperties).connection(connection).build(); - @Test - void shouldExtractAdditionalProperties() throws SQLException { - try (MockedConstruction mockedResultSet = Mockito - .mockConstruction(FireboltResultSet.class)) { - FireboltConnection connection = mock(FireboltConnection.class); - FireboltProperties fireboltProperties = FireboltProperties.builder().additionalProperties(new HashMap<>()) - .build(); - - FireboltStatement fireboltStatement = FireboltStatement.builder().statementService(fireboltStatementService) - .sessionProperties(fireboltProperties).connection(connection).build(); - - fireboltStatement.execute("set custom_1 = 1"); - verifyNoMoreInteractions(fireboltStatementService); - verify(connection).addProperty(new ImmutablePair<>("custom_1", "1")); - assertEquals(0, mockedResultSet.constructed().size()); - } + fireboltStatement.execute("set custom_1 = 1"); + verifyNoMoreInteractions(fireboltStatementService); + verify(connection).addProperty(new ImmutablePair<>("custom_1", "1")); } @SneakyThrows @Test - void shouldCancelByQueryWhenAggressiveCancelIsEnabled() throws SQLException, IOException, NoSuchFieldException { - try (MockedConstruction mockedResultSet = Mockito - .mockConstruction(FireboltResultSet.class)) { - - FireboltProperties fireboltProperties = FireboltProperties.builder().database("db").aggressiveCancel(true) - .additionalProperties(new HashMap<>()).build(); - - FireboltStatement fireboltStatement = FireboltStatement.builder().statementService(fireboltStatementService) - .sessionProperties(fireboltProperties).connection(fireboltConnection).build(); - - Field runningStatementField = FireboltStatement.class.getDeclaredField("runningStatementId"); - runningStatementField.setAccessible(true); - runningStatementField.set(fireboltStatement, "1234"); - fireboltStatement.cancel(); - verify(fireboltStatementService).execute(queryInfoWrapperArgumentCaptor.capture(), - fireboltPropertiesArgumentCaptor.capture(), anyInt(), anyInt(), booleanArgumentCaptor.capture()); - assertEquals("KILL QUERY ON CLUSTER sql_cluster WHERE initial_query_id='1234'", - queryInfoWrapperArgumentCaptor.getValue().getSql()); - assertFalse(booleanArgumentCaptor.getValue()); - } + void shouldCancelByQueryWhenAggressiveCancelIsEnabled() { + FireboltProperties fireboltProperties = FireboltProperties.builder().database("db").aggressiveCancel(true) + .additionalProperties(new HashMap<>()).build(); + + FireboltStatement fireboltStatement = FireboltStatement.builder().statementService(fireboltStatementService) + .sessionProperties(fireboltProperties).connection(fireboltConnection).build(); + + Field runningStatementField = FireboltStatement.class.getDeclaredField("runningStatementId"); + runningStatementField.setAccessible(true); + runningStatementField.set(fireboltStatement, "1234"); + fireboltStatement.cancel(); + verify(fireboltStatementService).execute(queryInfoWrapperArgumentCaptor.capture(), + fireboltPropertiesArgumentCaptor.capture(), anyInt(), anyInt(), booleanArgumentCaptor.capture(), any()); + assertEquals("KILL QUERY ON CLUSTER sql_cluster WHERE initial_query_id='1234'", + queryInfoWrapperArgumentCaptor.getValue().getSql()); + assertFalse(booleanArgumentCaptor.getValue()); } @SneakyThrows @Test - void shouldCancelByApiCallWhenAggressiveCancelIsDisabled() throws SQLException, IOException { - try (MockedConstruction mockedResultSet = Mockito - .mockConstruction(FireboltResultSet.class)) { - - FireboltProperties fireboltProperties = FireboltProperties.builder().database("db") - .additionalProperties(new HashMap<>()).build(); - - FireboltStatement fireboltStatement = FireboltStatement.builder().statementService(fireboltStatementService) - .sessionProperties(fireboltProperties).connection(fireboltConnection).build(); - - Field runningStatementField = FireboltStatement.class.getDeclaredField("runningStatementId"); - runningStatementField.setAccessible(true); - runningStatementField.set(fireboltStatement, "1234"); - fireboltStatement.cancel(); - verify(fireboltStatementService).abortStatement(any(), eq(fireboltProperties)); - } + void shouldCancelByApiCallWhenAggressiveCancelIsDisabled() { + FireboltProperties fireboltProperties = FireboltProperties.builder().database("db") + .additionalProperties(new HashMap<>()).build(); + + FireboltStatement fireboltStatement = FireboltStatement.builder().statementService(fireboltStatementService) + .sessionProperties(fireboltProperties).connection(fireboltConnection).build(); + + Field runningStatementField = FireboltStatement.class.getDeclaredField("runningStatementId"); + runningStatementField.setAccessible(true); + runningStatementField.set(fireboltStatement, "1234"); + fireboltStatement.cancel(); + verify(fireboltStatementService).abortStatement(any(), eq(fireboltProperties)); } @Test - void shouldCloseInputStreamOnClose() throws SQLException, IOException { - try (MockedConstruction mockedResultSet = Mockito - .mockConstruction(FireboltResultSet.class)) { - FireboltProperties fireboltProperties = FireboltProperties.builder().additionalProperties(new HashMap<>()) - .build(); - FireboltConnection connection = mock(FireboltConnection.class); - FireboltStatement fireboltStatement = FireboltStatement.builder().statementService(fireboltStatementService) - .sessionProperties(fireboltProperties).connection(connection).build(); - - when(fireboltStatementService.execute(any(), any(), anyInt(), anyInt(), anyBoolean())) - .thenReturn(mock(InputStream.class)); - - fireboltStatement.executeQuery("show database"); - fireboltStatement.close(); - verify(mockedResultSet.constructed().get(0)).close(); - verify(connection).removeClosedStatement(fireboltStatement); - } + void shouldCloseInputStreamOnClose() throws SQLException { + ResultSet rs = mock(ResultSet.class); + FireboltProperties fireboltProperties = FireboltProperties.builder().additionalProperties(new HashMap<>()) + .build(); + FireboltConnection connection = mock(FireboltConnection.class); + FireboltStatement fireboltStatement = FireboltStatement.builder().statementService(fireboltStatementService) + .sessionProperties(fireboltProperties).connection(connection).build(); + + when(fireboltStatementService.execute(any(), any(), anyInt(), anyInt(), anyBoolean(), any())) + .thenReturn(Optional.empty()); + when(fireboltStatementService.execute(any(), any(), anyInt(), anyInt(), anyBoolean(), any())) + .thenReturn(Optional.of(rs)); + fireboltStatement.executeQuery("show database"); + fireboltStatement.close(); + verify(rs).close(); + verify(connection).removeClosedStatement(fireboltStatement); } @Test void shouldThrowAnExceptionWhenExecutingQueryOnANonQueryStatement() { - try (MockedConstruction mockedResultSet = Mockito - .mockConstruction(FireboltResultSet.class)) { - FireboltConnection connection = mock(FireboltConnection.class); - FireboltProperties fireboltProperties = FireboltProperties.builder().additionalProperties(new HashMap<>()) - .build(); + FireboltConnection connection = mock(FireboltConnection.class); + FireboltProperties fireboltProperties = FireboltProperties.builder().additionalProperties(new HashMap<>()) + .build(); - FireboltStatement fireboltStatement = FireboltStatement.builder().statementService(fireboltStatementService) - .sessionProperties(fireboltProperties).connection(connection).build(); + FireboltStatement fireboltStatement = FireboltStatement.builder().statementService(fireboltStatementService) + .sessionProperties(fireboltProperties).connection(connection).build(); - assertThrows(FireboltException.class, () -> fireboltStatement.executeQuery("set custom_1 = 1")); - } + assertThrows(FireboltException.class, () -> fireboltStatement.executeQuery("set custom_1 = 1")); } @Test @@ -167,90 +130,98 @@ void shouldExecuteIfUpdateStatementWouldNotReturnAResultSet() throws SQLExceptio try (FireboltStatement fireboltStatement = FireboltStatement.builder() .statementService(fireboltStatementService).connection(fireboltConnection) .sessionProperties(fireboltProperties).build()) { - when(fireboltStatementService.execute(any(), any(), anyInt(), anyInt(), anyBoolean())) - .thenReturn(mock(InputStream.class)); + when(fireboltStatementService.execute(any(), any(), anyInt(), anyInt(), anyBoolean(), any())) + .thenReturn(Optional.empty()); assertEquals(0, fireboltStatement.executeUpdate("INSERT INTO cars(sales, name) VALUES (500, 'Ford')")); verify(fireboltStatementService).execute(queryInfoWrapperArgumentCaptor.capture(), eq(fireboltProperties), - anyInt(), anyInt(), anyBoolean()); + anyInt(), anyInt(), anyBoolean(), any()); assertEquals("INSERT INTO cars(sales, name) VALUES (500, 'Ford')", queryInfoWrapperArgumentCaptor.getValue().getSql()); + assertEquals(0, fireboltStatement.getUpdateCount()); } } @Test void shouldCloseCurrentAndGetMoreResultsForMultiStatementQuery() throws SQLException { - try (MockedConstruction mockedResultSet = Mockito - .mockConstruction(FireboltResultSet.class)) { - FireboltConnection connection = mock(FireboltConnection.class); - FireboltProperties fireboltProperties = FireboltProperties.builder().additionalProperties(new HashMap<>()) - .build(); - FireboltStatement fireboltStatement = FireboltStatement.builder().statementService(fireboltStatementService) - .sessionProperties(fireboltProperties).connection(connection).build(); - fireboltStatement.execute("SELECT 1; SELECT 2;"); - ResultSet rs = fireboltStatement.getResultSet(); - assertEquals(mockedResultSet.constructed().get(0), fireboltStatement.getResultSet()); - fireboltStatement.getMoreResults(); - verify(rs).close(); - assertEquals(mockedResultSet.constructed().get(1), fireboltStatement.getResultSet()); - rs = fireboltStatement.getResultSet(); - fireboltStatement.getMoreResults(); - verify(rs).close(); - assertNull(fireboltStatement.getResultSet()); - } + ResultSet rs = mock(FireboltResultSet.class); + ResultSet rs2 = mock(FireboltResultSet.class); + FireboltConnection connection = mock(FireboltConnection.class); + FireboltProperties fireboltProperties = FireboltProperties.builder().additionalProperties(new HashMap<>()) + .build(); + FireboltStatement fireboltStatement = FireboltStatement.builder().statementService(fireboltStatementService) + .sessionProperties(fireboltProperties).connection(connection).build(); + + when(fireboltStatementService.execute(any(), any(), anyInt(), anyInt(), anyBoolean(), any())) + .thenReturn(Optional.of(rs)).thenReturn(Optional.of(rs2)); + fireboltStatement.execute("SELECT 1; SELECT 2;"); + assertEquals(rs, fireboltStatement.getResultSet()); + assertEquals(-1, fireboltStatement.getUpdateCount()); + fireboltStatement.getMoreResults(); + verify(rs).close(); + assertEquals(rs2, fireboltStatement.getResultSet()); + rs = fireboltStatement.getResultSet(); + fireboltStatement.getMoreResults(); + verify(rs).close(); + assertEquals(-1, fireboltStatement.getUpdateCount()); + assertNull(fireboltStatement.getResultSet()); } @Test void shouldCloseCurrentAndGetMoreResultWhenCallingGetMoreResultsWithCloseCurrentFlag() throws SQLException { - try (MockedConstruction mockedResultSet = Mockito - .mockConstruction(FireboltResultSet.class)) { - FireboltConnection connection = mock(FireboltConnection.class); - FireboltProperties fireboltProperties = FireboltProperties.builder().additionalProperties(new HashMap<>()) - .build(); - FireboltStatement fireboltStatement = FireboltStatement.builder().statementService(fireboltStatementService) - .sessionProperties(fireboltProperties).connection(connection).build(); - fireboltStatement.execute("SELECT 1; SELECT 2;"); - ResultSet resultSet = fireboltStatement.getResultSet(); - fireboltStatement.getMoreResults(CLOSE_CURRENT_RESULT); - verify(resultSet).close(); - } + FireboltConnection connection = mock(FireboltConnection.class); + FireboltProperties fireboltProperties = FireboltProperties.builder().additionalProperties(new HashMap<>()) + .build(); + FireboltStatement fireboltStatement = FireboltStatement.builder().statementService(fireboltStatementService) + .sessionProperties(fireboltProperties).connection(connection).build(); + when(fireboltStatementService.execute(any(), any(), anyInt(), anyInt(), anyBoolean(), any())) + .thenReturn(Optional.of(mock(FireboltResultSet.class))); + fireboltStatement.execute("SELECT 1; SELECT 2;"); + ResultSet resultSet = fireboltStatement.getResultSet(); + fireboltStatement.getMoreResults(CLOSE_CURRENT_RESULT); + verify(resultSet).close(); } @Test void shouldKeepCurrentAndGetMoreResultWhenCallingGetMoreResultsWithKeepCurrentResultFlag() throws SQLException { - try (MockedConstruction mockedResultSet = Mockito - .mockConstruction(FireboltResultSet.class)) { - FireboltConnection connection = mock(FireboltConnection.class); - FireboltProperties fireboltProperties = FireboltProperties.builder().additionalProperties(new HashMap<>()) - .build(); - FireboltStatement fireboltStatement = FireboltStatement.builder().statementService(fireboltStatementService) - .sessionProperties(fireboltProperties).connection(connection).build(); - fireboltStatement.execute("SELECT 1; SELECT 2;"); - ResultSet resultSet = fireboltStatement.getResultSet(); - fireboltStatement.getMoreResults(Statement.KEEP_CURRENT_RESULT); - verify(resultSet, never()).close(); - } + FireboltConnection connection = mock(FireboltConnection.class); + FireboltProperties fireboltProperties = FireboltProperties.builder().additionalProperties(new HashMap<>()) + .build(); + FireboltStatement fireboltStatement = FireboltStatement.builder().statementService(fireboltStatementService) + .sessionProperties(fireboltProperties).connection(connection).build(); + when(fireboltStatementService.execute(any(), any(), anyInt(), anyInt(), anyBoolean(), any())) + .thenReturn(Optional.of(mock(ResultSet.class))); + + fireboltStatement.execute("SELECT 1; SELECT 2;"); + ResultSet resultSet = fireboltStatement.getResultSet(); + fireboltStatement.getMoreResults(Statement.KEEP_CURRENT_RESULT); + verify(resultSet, never()).close(); } @Test void shouldCloseUnclosedAndGetMoreResultWhenCallingGetMoreResultsWithCloseAllResultFlag() throws SQLException { - try (MockedConstruction mockedResultSet = Mockito - .mockConstruction(FireboltResultSet.class)) { - FireboltConnection connection = mock(FireboltConnection.class); - FireboltProperties fireboltProperties = FireboltProperties.builder().additionalProperties(new HashMap<>()) - .build(); - FireboltStatement fireboltStatement = FireboltStatement.builder().statementService(fireboltStatementService) - .sessionProperties(fireboltProperties).connection(connection).build(); - fireboltStatement.execute("SELECT 1; SELECT 2; SELECT 3;"); - ResultSet firstRs = fireboltStatement.getResultSet(); - fireboltStatement.getMoreResults(Statement.KEEP_CURRENT_RESULT); - verify(firstRs, never()).close(); - ResultSet secondRs = fireboltStatement.getResultSet(); - fireboltStatement.getMoreResults(Statement.KEEP_CURRENT_RESULT); - verify(secondRs, never()).close(); - fireboltStatement.getMoreResults(Statement.CLOSE_ALL_RESULTS); - verify(firstRs).close(); - verify(secondRs).close(); - } + ResultSet rs = mock(FireboltResultSet.class); + ResultSet rs2 = mock(FireboltResultSet.class); + ResultSet rs3 = mock(FireboltResultSet.class); + FireboltConnection connection = mock(FireboltConnection.class); + FireboltProperties fireboltProperties = FireboltProperties.builder().additionalProperties(new HashMap<>()) + .build(); + FireboltStatement fireboltStatement = FireboltStatement.builder().statementService(fireboltStatementService) + .sessionProperties(fireboltProperties).connection(connection).build(); + + when(fireboltStatementService.execute(any(), any(), anyInt(), anyInt(), anyBoolean(), any())) + .thenReturn(Optional.of(rs)).thenReturn(Optional.of(rs2)).thenReturn(Optional.of(rs3)); + + fireboltStatement.execute("SELECT 1; SELECT 2; SELECT 3;"); + ResultSet firstRs = fireboltStatement.getResultSet(); + fireboltStatement.getMoreResults(Statement.KEEP_CURRENT_RESULT); + verify(firstRs, never()).close(); + ResultSet secondRs = fireboltStatement.getResultSet(); + fireboltStatement.getMoreResults(Statement.KEEP_CURRENT_RESULT); + verify(secondRs, never()).close(); + fireboltStatement.getMoreResults(Statement.CLOSE_ALL_RESULTS); + verify(firstRs).close(); + verify(secondRs).close(); + } } diff --git a/src/test/java/com/firebolt/jdbc/statement/preparedstatement/FireboltPreparedStatementTest.java b/src/test/java/com/firebolt/jdbc/statement/preparedstatement/FireboltPreparedStatementTest.java index d91d106ce..35aa73073 100644 --- a/src/test/java/com/firebolt/jdbc/statement/preparedstatement/FireboltPreparedStatementTest.java +++ b/src/test/java/com/firebolt/jdbc/statement/preparedstatement/FireboltPreparedStatementTest.java @@ -12,6 +12,7 @@ import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; import java.sql.Timestamp; +import java.util.Optional; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -39,10 +40,10 @@ class FireboltPreparedStatementTest { private FireboltProperties properties; @BeforeEach - void beforeEach() throws FireboltException { + void beforeEach() throws SQLException { lenient().when(properties.getBufferSize()).thenReturn(10); - lenient().when(fireboltStatementService.execute(any(), any(), anyInt(), anyInt(), anyBoolean())) - .thenReturn(Mockito.mock(InputStream.class)); + lenient().when(fireboltStatementService.execute(any(), any(), anyInt(), anyInt(), anyBoolean(), any())) + .thenReturn(Optional.empty()); } @Test @@ -55,7 +56,7 @@ void shouldExecute() throws SQLException { statement.setObject(2, "Ford"); statement.execute(); verify(fireboltStatementService).execute(queryInfoWrapperArgumentCaptor.capture(), eq(this.properties), - anyInt(), anyInt(), anyBoolean()); + anyInt(), anyInt(), anyBoolean(), any()); assertEquals("INSERT INTO cars (sales, make) VALUES (500,'Ford')", queryInfoWrapperArgumentCaptor.getValue().getSql()); @@ -75,7 +76,7 @@ void shouldExecuteBatch() throws SQLException { statement.addBatch(); statement.executeBatch(); verify(fireboltStatementService, times(2)).execute(queryInfoWrapperArgumentCaptor.capture(), - eq(this.properties), anyInt(), anyInt(), anyBoolean()); + eq(this.properties), anyInt(), anyInt(), anyBoolean(), any()); assertEquals("INSERT INTO cars (sales, make) VALUES (150,'Ford')", queryInfoWrapperArgumentCaptor.getAllValues().get(0).getSql()); @@ -112,7 +113,7 @@ void shouldExecuteWithSpecialCharactersInQuery() throws SQLException { statement.execute(); verify(fireboltStatementService).execute(queryInfoWrapperArgumentCaptor.capture(), eq(this.properties), - anyInt(), anyInt(), anyBoolean()); + anyInt(), anyInt(), anyBoolean(), any()); assertEquals(expectedSql, queryInfoWrapperArgumentCaptor.getValue().getSql()); } @@ -148,7 +149,7 @@ void shouldSetNull() throws SQLException { statement.execute(); verify(fireboltStatementService).execute(queryInfoWrapperArgumentCaptor.capture(), eq(this.properties), - anyInt(), anyInt(), anyBoolean()); + anyInt(), anyInt(), anyBoolean(), any()); assertEquals("INSERT INTO cars (sales, make) VALUES (\\N,\\N)", queryInfoWrapperArgumentCaptor.getValue().getSql()); } @@ -161,7 +162,7 @@ void shouldSetBoolean() throws SQLException { statement.execute(); verify(fireboltStatementService).execute(queryInfoWrapperArgumentCaptor.capture(), eq(this.properties), - anyInt(), anyInt(), anyBoolean()); + anyInt(), anyInt(), anyBoolean(), any()); assertEquals("INSERT INTO cars(available) VALUES (1)", queryInfoWrapperArgumentCaptor.getValue().getSql()); } @@ -185,7 +186,7 @@ void shouldSetInt() throws SQLException { statement.execute(); verify(fireboltStatementService).execute(queryInfoWrapperArgumentCaptor.capture(), eq(this.properties), - anyInt(), anyInt(), anyBoolean()); + anyInt(), anyInt(), anyBoolean(), any()); } @Test @@ -196,7 +197,7 @@ void shouldSetLong() throws SQLException { statement.execute(); verify(fireboltStatementService).execute(queryInfoWrapperArgumentCaptor.capture(), eq(this.properties), - anyInt(), anyInt(), anyBoolean()); + anyInt(), anyInt(), anyBoolean(), any()); assertEquals("INSERT INTO cars(price) VALUES (50)", queryInfoWrapperArgumentCaptor.getValue().getSql()); } @@ -208,7 +209,7 @@ void shouldSetFloat() throws SQLException { statement.execute(); verify(fireboltStatementService).execute(queryInfoWrapperArgumentCaptor.capture(), eq(this.properties), - anyInt(), anyInt(), anyBoolean()); + anyInt(), anyInt(), anyBoolean(), any()); assertEquals("INSERT INTO cars(price) VALUES (5.5)", queryInfoWrapperArgumentCaptor.getValue().getSql()); } @@ -221,7 +222,7 @@ void shouldSetDouble() throws SQLException { statement.execute(); verify(fireboltStatementService).execute(queryInfoWrapperArgumentCaptor.capture(), eq(this.properties), - anyInt(), anyInt(), anyBoolean()); + anyInt(), anyInt(), anyBoolean(), any()); } @Test @@ -232,7 +233,7 @@ void shouldSetBigDecimal() throws SQLException { statement.execute(); verify(fireboltStatementService).execute(queryInfoWrapperArgumentCaptor.capture(), eq(this.properties), - anyInt(), anyInt(), anyBoolean()); + anyInt(), anyInt(), anyBoolean(), any()); assertEquals("INSERT INTO cars(price) VALUES (555555555555.55555555)", queryInfoWrapperArgumentCaptor.getValue().getSql()); } @@ -246,7 +247,7 @@ void shouldSetDate() throws SQLException { statement.execute(); verify(fireboltStatementService).execute(queryInfoWrapperArgumentCaptor.capture(), eq(this.properties), - anyInt(), anyInt(), anyBoolean()); + anyInt(), anyInt(), anyBoolean(), any()); assertEquals("INSERT INTO cars(release_date) VALUES ('2019-07-31')", queryInfoWrapperArgumentCaptor.getValue().getSql()); } @@ -260,7 +261,7 @@ void shouldSetTimeStamp() throws SQLException { statement.execute(); verify(fireboltStatementService).execute(queryInfoWrapperArgumentCaptor.capture(), eq(this.properties), - anyInt(), anyInt(), anyBoolean()); + anyInt(), anyInt(), anyBoolean(), any()); assertEquals("INSERT INTO cars(release_date) VALUES ('2019-07-31 12:15:13')", queryInfoWrapperArgumentCaptor.getValue().getSql()); @@ -285,7 +286,7 @@ void shouldSetAllObjects() throws SQLException { statement.execute(); verify(fireboltStatementService).execute(queryInfoWrapperArgumentCaptor.capture(), eq(this.properties), - anyInt(), anyInt(), anyBoolean()); + anyInt(), anyInt(), anyBoolean(), any()); assertEquals( "INSERT INTO cars(timestamp, date, float, long, big_decimal, null, boolean, int) VALUES ('2019-07-31 12:15:13','2019-07-31',5.5,5,555555555555.55555555,\\N,1,5)", diff --git a/src/test/java/com/firebolt/jdbc/util/InputStreamUtilTest.java b/src/test/java/com/firebolt/jdbc/util/InputStreamUtilTest.java new file mode 100644 index 000000000..c4f1b782b --- /dev/null +++ b/src/test/java/com/firebolt/jdbc/util/InputStreamUtilTest.java @@ -0,0 +1,25 @@ +package com.firebolt.jdbc.util; + +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +import static org.junit.jupiter.api.Assertions.*; + +class InputStreamUtilTest { + + @Test + void shouldReadAllBytes() throws IOException { + String initialString = "text"; + InputStream targetStream = new ByteArrayInputStream(initialString.getBytes()); + InputStreamUtil.readAllBytes(targetStream); + assertEquals(-1, targetStream.read()); + } + + @Test + void shouldNotThrowExceptionIfStreamIsNull() { + assertDoesNotThrow(() -> InputStreamUtil.readAllBytes(null)); + } +} \ No newline at end of file diff --git a/src/test/java/com/firebolt/jdbc/LoggerUtilTest.java b/src/test/java/com/firebolt/jdbc/util/LoggerUtilTest.java similarity index 93% rename from src/test/java/com/firebolt/jdbc/LoggerUtilTest.java rename to src/test/java/com/firebolt/jdbc/util/LoggerUtilTest.java index c102394ff..ded46dc23 100644 --- a/src/test/java/com/firebolt/jdbc/LoggerUtilTest.java +++ b/src/test/java/com/firebolt/jdbc/util/LoggerUtilTest.java @@ -1,4 +1,4 @@ -package com.firebolt.jdbc; +package com.firebolt.jdbc.util; import static org.junit.jupiter.api.Assertions.assertTrue; diff --git a/src/test/java/com/firebolt/jdbc/PropertyUtilTest.java b/src/test/java/com/firebolt/jdbc/util/PropertyUtilTest.java similarity index 98% rename from src/test/java/com/firebolt/jdbc/PropertyUtilTest.java rename to src/test/java/com/firebolt/jdbc/util/PropertyUtilTest.java index 2fee1e72f..bc2aadfa5 100644 --- a/src/test/java/com/firebolt/jdbc/PropertyUtilTest.java +++ b/src/test/java/com/firebolt/jdbc/util/PropertyUtilTest.java @@ -1,4 +1,4 @@ -package com.firebolt.jdbc; +package com.firebolt.jdbc.util; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mockStatic; diff --git a/src/test/java/com/firebolt/jdbc/StatementUtilTest.java b/src/test/java/com/firebolt/jdbc/util/StatementUtilTest.java similarity index 99% rename from src/test/java/com/firebolt/jdbc/StatementUtilTest.java rename to src/test/java/com/firebolt/jdbc/util/StatementUtilTest.java index 614239ef7..805a2cc7f 100644 --- a/src/test/java/com/firebolt/jdbc/StatementUtilTest.java +++ b/src/test/java/com/firebolt/jdbc/util/StatementUtilTest.java @@ -1,4 +1,4 @@ -package com.firebolt.jdbc; +package com.firebolt.jdbc.util; import static com.firebolt.jdbc.statement.StatementType.PARAM_SETTING; import static com.firebolt.jdbc.statement.StatementUtil.isQuery; diff --git a/src/test/java/com/firebolt/jdbc/VersionUtilTest.java b/src/test/java/com/firebolt/jdbc/util/VersionUtilTest.java similarity index 96% rename from src/test/java/com/firebolt/jdbc/VersionUtilTest.java rename to src/test/java/com/firebolt/jdbc/util/VersionUtilTest.java index ba182680c..156ac732f 100644 --- a/src/test/java/com/firebolt/jdbc/VersionUtilTest.java +++ b/src/test/java/com/firebolt/jdbc/util/VersionUtilTest.java @@ -1,4 +1,4 @@ -package com.firebolt.jdbc; +package com.firebolt.jdbc.util; import static org.junit.jupiter.api.Assertions.assertEquals; diff --git a/src/test/resources/responses/firebolt-response-select-1 b/src/test/resources/responses/firebolt-response-select-1 new file mode 100644 index 000000000..d9aaab679 --- /dev/null +++ b/src/test/resources/responses/firebolt-response-select-1 @@ -0,0 +1,3 @@ +1 +int +1 \ No newline at end of file