Skip to content

Commit

Permalink
add more testings
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-jy committed Jun 11, 2024
1 parent 2c8fbee commit 81dbac2
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 3 deletions.
36 changes: 36 additions & 0 deletions src/test/java/net/snowflake/client/core/QueryContextCacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;

import org.junit.Test;

import java.sql.Timestamp;

public class QueryContextCacheTest {
private QueryContextCache qcc = null;
private long BASE_READ_TIMESTAMP = 1668727958;
Expand Down Expand Up @@ -217,6 +220,39 @@ public void testSerializeRequestAndDeserializeResponseDataWithNullContext() thro

qcc.deserializeQueryContextDTO(requestData);
assertCacheDataWithContext(null);

QueryContextCache mockQcc = spy(qcc);
mockQcc.deserializeQueryContextDTO(null);
verify(mockQcc).clearCache();
verify(mockQcc, times(2)).logCacheEntries();
}

@Test
public void testQueryContextEntroDTO() throws Exception {
QueryContextEntryDTO queryContextEntryDTO = new QueryContextEntryDTO();
OpaqueContextDTO opaqueContextDTO = mock(OpaqueContextDTO.class);
queryContextEntryDTO.setId(1);
assertEquals(queryContextEntryDTO.getId(),1);
queryContextEntryDTO.setTimestamp(2);
assertEquals(queryContextEntryDTO.getTimestamp(),2);
queryContextEntryDTO.setPriority(3);
assertEquals(queryContextEntryDTO.getPriority(),3);
queryContextEntryDTO.setContext(opaqueContextDTO);
assertEquals(queryContextEntryDTO.getContext(),opaqueContextDTO);
}

@Test
public void testQueryExecDTO() throws Exception {
QueryContextEntryDTO queryContextEntryDTO = new QueryContextEntryDTO();
OpaqueContextDTO opaqueContextDTO = mock(OpaqueContextDTO.class);
queryContextEntryDTO.setId(1);
assertEquals(queryContextEntryDTO.getId(),1);
queryContextEntryDTO.setTimestamp(2);
assertEquals(queryContextEntryDTO.getTimestamp(),2);
queryContextEntryDTO.setPriority(3);
assertEquals(queryContextEntryDTO.getPriority(),3);
queryContextEntryDTO.setContext(opaqueContextDTO);
assertEquals(queryContextEntryDTO.getContext(),opaqueContextDTO);
}

private void assertCacheData() {
Expand Down
69 changes: 69 additions & 0 deletions src/test/java/net/snowflake/client/core/SQLInputOutputTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package net.snowflake.client.core;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.mock;

import net.snowflake.client.core.json.Converters;
import net.snowflake.client.jdbc.BaseJDBCTest;
import net.snowflake.common.core.SqlState;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.mockito.MockedStatic;
import org.mockito.MockedStatic.Verification;
import org.mockito.Mockito;

import java.sql.SQLData;
import java.sql.SQLException;

public class SQLInputOutputTest {

@Test
public void testBaseSQLUnSupportedException() {
BaseSqlInput sqlInput = new ArrowSqlInput(null,null,null,null);
expectSnowflakeLoggedFeatureNotSupportedException(sqlInput::readCharacterStream);
expectSnowflakeLoggedFeatureNotSupportedException(sqlInput::readAsciiStream);
expectSnowflakeLoggedFeatureNotSupportedException(sqlInput::readBinaryStream);
expectSnowflakeLoggedFeatureNotSupportedException(sqlInput::readRef);
expectSnowflakeLoggedFeatureNotSupportedException(sqlInput::readBlob);
expectSnowflakeLoggedFeatureNotSupportedException(sqlInput::readClob);
expectSnowflakeLoggedFeatureNotSupportedException(sqlInput::readArray);
expectSnowflakeLoggedFeatureNotSupportedException(sqlInput::readURL);
expectSnowflakeLoggedFeatureNotSupportedException(sqlInput::readNClob);
expectSnowflakeLoggedFeatureNotSupportedException(sqlInput::readNString);
expectSnowflakeLoggedFeatureNotSupportedException(sqlInput::readSQLXML);
expectSnowflakeLoggedFeatureNotSupportedException(sqlInput::readRowId);
}

@Test
public void testJsonSqlOutPutUnSupportedTest() {
JsonSqlOutput sqloutput = new JsonSqlOutput(mock(SQLData.class),mock(SFBaseSession.class));
expectSnowflakeLoggedFeatureNotSupportedException(()->sqloutput.writeRef(null));
expectSnowflakeLoggedFeatureNotSupportedException(()->sqloutput.writeBlob(null));
expectSnowflakeLoggedFeatureNotSupportedException(()->sqloutput.writeClob(null));
expectSnowflakeLoggedFeatureNotSupportedException(()->sqloutput.writeStruct(null));
expectSnowflakeLoggedFeatureNotSupportedException(()->sqloutput.writeArray(null));
expectSnowflakeLoggedFeatureNotSupportedException(()->sqloutput.writeURL(null));
expectSnowflakeLoggedFeatureNotSupportedException(()->sqloutput.writeNString(null));
expectSnowflakeLoggedFeatureNotSupportedException(()->sqloutput.writeNClob(null));
expectSnowflakeLoggedFeatureNotSupportedException(()->sqloutput.writeRowId(null));
expectSnowflakeLoggedFeatureNotSupportedException(()->sqloutput.writeSQLXML(null));
}

private interface MethodRaisesSQLException {
void run() throws SQLException;
}
private void expectSnowflakeLoggedFeatureNotSupportedException(MethodRaisesSQLException f) {
try {
f.run();
fail("must raise exception");
} catch (SQLException ex) {
assertEquals(SqlState.FEATURE_NOT_SUPPORTED, "0A000");
}
}
}


14 changes: 14 additions & 0 deletions src/test/java/net/snowflake/client/jdbc/BaseJDBCTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import net.snowflake.client.AbstractDriverIT;
import net.snowflake.client.core.SFException;
import net.snowflake.common.core.SqlState;

public class BaseJDBCTest extends AbstractDriverIT {
Expand All @@ -45,6 +46,10 @@ protected interface MethodRaisesSQLException {
void run() throws SQLException;
}

protected interface MethodRaisesSFException {
void run() throws SFException, SQLException;
}

protected interface MethodRaisesSQLClientInfoException {
void run() throws SQLClientInfoException;
}
Expand Down Expand Up @@ -94,6 +99,15 @@ protected void expectFeatureNotSupportedException(MethodRaisesSQLException f) {
}
}

protected void expectFeatureSFException(MethodRaisesSFException f) {
try {
f.run();
fail("must raise exception");
} catch (SFException | SQLException ex ) {
assertTrue(ex instanceof SFException);
}
}

protected void expectSQLClientInfoException(MethodRaisesSQLClientInfoException f) {
try {
f.run();
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/net/snowflake/client/jdbc/ConnectionIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;

Expand Down Expand Up @@ -44,6 +45,7 @@
import net.snowflake.client.RunningOnGithubAction;
import net.snowflake.client.TestUtil;
import net.snowflake.client.category.TestCategoryConnection;
import net.snowflake.client.core.SFBaseSession;
import net.snowflake.client.core.SFSession;
import net.snowflake.common.core.SqlState;
import org.apache.commons.codec.binary.Base64;
Expand Down Expand Up @@ -1028,6 +1030,16 @@ public void testFailOverOrgAccount() throws SQLException {
}
}

@Test
public void testSFBaseSession() throws SQLException {
try(Connection con = getConnection()) {
assertNull(con.unwrap(SFBaseSession.class).getServerUrl());
expectFeatureSFException(() ->
con.unwrap(SFBaseSession.class).addProperty("user","a"));

}
}

private class ConcurrentConnections implements Runnable {

ConcurrentConnections() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package net.snowflake.client.jdbc;

import org.junit.Test;

import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.mockito.Mockito.mock;


public class SnowflakeColumnMetadataTest {

@Test
public void testSnowflakeColumnMetaData() {
SnowflakeColumnMetadata metadata = new SnowflakeColumnMetadata("mock",1,true,2,3,4,"fake",true,null,null,null,null,true);
List<FieldMetadata> list = mock(List.class);
metadata.setName("test");
metadata.setType(10);
metadata.setNullable(false);
metadata.setLength(12);
metadata.setPrecision(13);
metadata.setScale(14);
metadata.setTypeName("type");
metadata.setFixed(false);
metadata.setFields(list);
metadata.setAutoIncrement(false);

assertEquals(metadata.getName(),"test");
assertEquals(metadata.getType(),10);
assertFalse(metadata.isNullable());
assertEquals(metadata.getLength(),12);
assertEquals(metadata.getPrecision(),13);
assertEquals(metadata.getScale(),14);
assertEquals(metadata.getTypeName(),"type");
assertFalse(metadata.isFixed());
assertEquals(metadata.getFields(),list);
assertFalse(metadata.isAutoIncrement());
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package net.snowflake.client.jdbc.cloud.storage;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import java.sql.Connection;
import java.sql.SQLException;

import com.microsoft.azure.storage.blob.ListBlobItem;
import net.snowflake.client.ConditionalIgnoreRule;
import net.snowflake.client.RunningOnGithubAction;
import net.snowflake.client.core.SFSession;
Expand All @@ -17,7 +19,6 @@
import org.junit.Test;

public class SnowflakeAzureClientLatestIT extends BaseJDBCTest {

@Test
@ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class)
public void testAzureClientSetupInvalidEncryptionKeySize() throws SQLException {
Expand All @@ -37,4 +38,14 @@ public void testAzureClientSetupInvalidEncryptionKeySize() throws SQLException {
}
}
}

@Test
public void testCloudExceptionTest() {
Iterable<ListBlobItem> mockList = null;
AzureObjectSummariesIterator iterator = new AzureObjectSummariesIterator(mockList);
AzureObjectSummariesIterator spyIterator = spy(iterator);
UnsupportedOperationException ex = assertThrows(UnsupportedOperationException.class, () -> spyIterator.remove());
assertEquals(ex.getMessage(), "remove() method not supported");

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,7 @@ public void testLogicalConnectionAlreadyClosed() throws SQLException {
expectConnectionAlreadyClosedException(() -> logicalConnection.setSchema("fakedb"));
expectConnectionAlreadyClosedException(
() -> logicalConnection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED));
expectConnectionAlreadyClosedException(
() -> logicalConnection.createArrayOf("faketype",null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.*;


import java.sql.CallableStatement;
import java.sql.Clob;
Expand All @@ -27,6 +30,7 @@
import net.snowflake.client.jdbc.SnowflakeDriver;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.mockito.Mockito;

@Category(TestCategoryConnection.class)
public class LogicalConnectionLatestIT extends BaseJDBCTest {
Expand Down Expand Up @@ -370,6 +374,77 @@ public void testDatabaseMetaData() throws SQLException {
}
}

@Test
public void testExceptions() throws SQLException {
Connection connection = mock(Connection.class);
SnowflakePooledConnection snowflakePooledConnection = mock(SnowflakePooledConnection.class);
when(snowflakePooledConnection.getPhysicalConnection()).thenReturn(connection);
SQLException sqlException = new SQLException("mocking error");
when(connection.createStatement()).thenThrow(sqlException);
when(connection.createStatement(1,2,3)).thenThrow(sqlException);

when(connection.prepareStatement("mocksql")).thenThrow(sqlException);
when(connection.prepareCall("mocksql")).thenThrow(sqlException);
when(connection.prepareCall("mocksql",1,2,3)).thenThrow(sqlException);
when(connection.nativeSQL("mocksql")).thenThrow(sqlException);
when(connection.getAutoCommit()).thenThrow(sqlException);
when(connection.getMetaData()).thenThrow(sqlException);
when(connection.isReadOnly()).thenThrow(sqlException);
when(connection.getCatalog()).thenThrow(sqlException);
when(connection.getTransactionIsolation()).thenThrow(sqlException);
when(connection.getWarnings()).thenThrow(sqlException);
when(connection.prepareCall("mocksql",1,2)).thenThrow(sqlException);
when(connection.getTypeMap()).thenThrow(sqlException);
when(connection.getHoldability()).thenThrow(sqlException);
when(connection.createClob()).thenThrow(sqlException);
when(connection.getClientInfo("mocksql")).thenThrow(sqlException);
when(connection.getClientInfo()).thenThrow(sqlException);
when(connection.createArrayOf("mock",null)).thenThrow(sqlException);
when(connection.getSchema()).thenThrow(sqlException);
when(connection.getNetworkTimeout()).thenThrow(sqlException);
when(connection.isWrapperFor(Connection.class)).thenThrow(sqlException);

doThrow(sqlException).when(connection).setAutoCommit(false);
doThrow(sqlException).when(connection).commit();
doThrow(sqlException).when(connection).rollback();
doThrow(sqlException).when(connection).setReadOnly(false);
doThrow(sqlException).when(connection).clearWarnings();
doThrow(sqlException).when(connection).setSchema(null);
doThrow(sqlException).when(connection).abort(null);
doThrow(sqlException).when(connection).setNetworkTimeout(null,1);

LogicalConnection logicalConnection = new LogicalConnection(snowflakePooledConnection);

assertThrows(SQLException.class, logicalConnection::createStatement);
assertThrows(SQLException.class, ()->logicalConnection.createStatement(1,2,3));
assertThrows(SQLException.class, ()->logicalConnection.nativeSQL("mocksql"));
assertThrows(SQLException.class, logicalConnection::getAutoCommit);
assertThrows(SQLException.class, logicalConnection::getMetaData);
assertThrows(SQLException.class, logicalConnection::isReadOnly);
assertThrows(SQLException.class, logicalConnection::getCatalog);
assertThrows(SQLException.class, logicalConnection::getTransactionIsolation);
assertThrows(SQLException.class, logicalConnection::getWarnings);
assertThrows(SQLException.class, ()->logicalConnection.prepareCall("mocksql"));
assertThrows(SQLException.class, logicalConnection::getTypeMap);
assertThrows(SQLException.class, logicalConnection::getHoldability);
assertThrows(SQLException.class, logicalConnection::createClob);
assertThrows(SQLException.class, ()->logicalConnection.getClientInfo("mocksql"));
assertThrows(SQLException.class, logicalConnection::getClientInfo);
assertThrows(SQLException.class, ()->logicalConnection.createArrayOf("mock",null));
assertThrows(SQLException.class, logicalConnection::getSchema);
assertThrows(SQLException.class, logicalConnection::getNetworkTimeout);
assertThrows(SQLException.class, ()->logicalConnection.isWrapperFor(Connection.class));
assertThrows(SQLException.class, ()->logicalConnection.setAutoCommit(false));
assertThrows(SQLException.class, logicalConnection::rollback);
assertThrows(SQLException.class, ()->logicalConnection.setReadOnly(false));
assertThrows(SQLException.class, logicalConnection::clearWarnings);
assertThrows(SQLException.class, ()->logicalConnection.setSchema(null));
assertThrows(SQLException.class, ()->logicalConnection.abort(null));
assertThrows(SQLException.class, ()->logicalConnection.setNetworkTimeout(null,1));

verify(snowflakePooledConnection,times(26)).fireConnectionErrorEvent(sqlException);
}

private SnowflakeConnectionPoolDataSource setProperties(
SnowflakeConnectionPoolDataSource poolDataSource) {
poolDataSource.setUrl(properties.get("uri"));
Expand Down

0 comments on commit 81dbac2

Please sign in to comment.