Skip to content

Commit

Permalink
Moved throw assertions to relevant lines
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-astachowski committed Nov 12, 2024
1 parent d35574f commit 8fb4e63
Show file tree
Hide file tree
Showing 12 changed files with 289 additions and 345 deletions.
54 changes: 21 additions & 33 deletions src/test/java/net/snowflake/client/config/SFPermissionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,13 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.stream.Stream;
import net.snowflake.client.annotations.DontRunOnWindows;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.CsvSource;

public class SFPermissionsTest {

static class PermissionProvider implements ArgumentsProvider {

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) throws Exception {
return Stream.of(
Arguments.of("rwx------", false),
Arguments.of("rw-------", false),
Arguments.of("r-x------", false),
Arguments.of("r--------", false),
Arguments.of("rwxrwx---", true),
Arguments.of("rwxrw----", true),
Arguments.of("rwxr-x---", false),
Arguments.of("rwxr-----", false),
Arguments.of("rwx-wx---", true),
Arguments.of("rwx-w----", true),
Arguments.of("rwx--x---", false),
Arguments.of("rwx---rwx", true),
Arguments.of("rwx---rw-", true),
Arguments.of("rwx---r-x", false),
Arguments.of("rwx---r--", false),
Arguments.of("rwx----wx", true),
Arguments.of("rwx----w-", true),
Arguments.of("rwx-----x", false));
}
}

Path configFilePath = Paths.get("config.json");
String configJson = "{\"common\":{\"log_level\":\"debug\",\"log_path\":\"logs\"}}";

Expand All @@ -59,7 +28,26 @@ public void cleanupConfigFile() throws IOException {
}

@ParameterizedTest
@ArgumentsSource(PermissionProvider.class)
@CsvSource({
"rwx------,false",
"rw-------,false",
"r-x------,false",
"r--------,false",
"rwxrwx---,true",
"rwxrw----,true",
"rwxr-x---,false",
"rwxr-----,false",
"rwx-wx---,true",
"rwx-w----,true",
"rwx--x---,false",
"rwx---rwx,true",
"rwx---rw-,true",
"rwx---r-x,false",
"rwx---r--,false",
"rwx----wx,true",
"rwx----w-,true",
"rwx-----x,false"
})
@DontRunOnWindows
public void testLogDirectoryPermissions(String permission, boolean isSucceed) throws IOException {
// TODO: SNOW-1503722 Change to check for thrown exceptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,21 @@ public class DatabaseMetaDataResultSetLatestIT extends BaseJDBCTest {

@Test
public void testGetObjectNotSupported() throws SQLException {
assertThrows(
SnowflakeLoggedFeatureNotSupportedException.class,
() -> {
try (Connection con = getConnection();
Statement st = con.createStatement()) {
Object[][] rows = {{1.2F}};
List<String> columnNames = Arrays.asList("float");
List<String> columnTypeNames = Arrays.asList("FLOAT");
List<Integer> columnTypes = Arrays.asList(Types.FLOAT);
try (ResultSet resultSet =
new SnowflakeDatabaseMetaDataResultSet(
columnNames, columnTypeNames, columnTypes, rows, st)) {
resultSet.next();
assertEquals(1.2F, resultSet.getObject(1));
}
}
});
try (Connection con = getConnection();
Statement st = con.createStatement()) {
Object[][] rows = {{1.2F}};
List<String> columnNames = Arrays.asList("float");
List<String> columnTypeNames = Arrays.asList("FLOAT");
List<Integer> columnTypes = Arrays.asList(Types.FLOAT);
try (ResultSet resultSet =
new SnowflakeDatabaseMetaDataResultSet(
columnNames, columnTypeNames, columnTypes, rows, st)) {
resultSet.next();
assertThrows(
SnowflakeLoggedFeatureNotSupportedException.class,
() -> assertEquals(1.2F, resultSet.getObject(1)));
}
}
}

/** Added in > 3.17.0 */
Expand Down
136 changes: 64 additions & 72 deletions src/test/java/net/snowflake/client/jdbc/RestRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -458,42 +458,38 @@ public CloseableHttpResponse answer(InvocationOnMock invocation) throws Throwabl
}

@Test
public void testMaxRetriesExceeded() {
assertThrows(
SnowflakeSQLException.class,
() -> {
boolean telemetryEnabled = TelemetryService.getInstance().isEnabled();

CloseableHttpClient client = mock(CloseableHttpClient.class);
when(client.execute(any(HttpUriRequest.class)))
.thenAnswer(
new Answer<CloseableHttpResponse>() {
int callCount = 0;

@Override
public CloseableHttpResponse answer(InvocationOnMock invocation)
throws Throwable {
callCount += 1;
if (callCount >= 4) {
return successResponse();
} else {
return socketTimeoutResponse();
}
}
});

try {
TelemetryService.disable();
execute(client, "fakeurl.com/?requestId=abcd-1234", 0, 0, 0, true, false, 1);
fail("testMaxRetries");
} finally {
if (telemetryEnabled) {
TelemetryService.enable();
} else {
TelemetryService.disable();
}
}
});
public void testMaxRetriesExceeded() throws IOException {
boolean telemetryEnabled = TelemetryService.getInstance().isEnabled();

CloseableHttpClient client = mock(CloseableHttpClient.class);
when(client.execute(any(HttpUriRequest.class)))
.thenAnswer(
new Answer<CloseableHttpResponse>() {
int callCount = 0;

@Override
public CloseableHttpResponse answer(InvocationOnMock invocation) throws Throwable {
callCount += 1;
if (callCount >= 4) {
return successResponse();
} else {
return socketTimeoutResponse();
}
}
});

try {
TelemetryService.disable();
assertThrows(
SnowflakeSQLException.class,
() -> execute(client, "fakeurl.com/?requestId=abcd-1234", 0, 0, 0, true, false, 1));
} finally {
if (telemetryEnabled) {
TelemetryService.enable();
} else {
TelemetryService.disable();
}
}
}

@Test
Expand All @@ -520,42 +516,38 @@ public CloseableHttpResponse answer(InvocationOnMock invocationOnMock)
}

@Test
public void testLoginMaxRetries() {
assertThrows(
SnowflakeSQLException.class,
() -> {
boolean telemetryEnabled = TelemetryService.getInstance().isEnabled();

CloseableHttpClient client = mock(CloseableHttpClient.class);
when(client.execute(any(HttpUriRequest.class)))
.thenAnswer(
new Answer<CloseableHttpResponse>() {
int callCount = 0;

@Override
public CloseableHttpResponse answer(InvocationOnMock invocation)
throws Throwable {
callCount += 1;
if (callCount >= 4) {
return retryLoginResponse();
} else {
return socketTimeoutResponse();
}
}
});

try {
TelemetryService.disable();
execute(client, "/session/v1/login-request", 0, 0, 0, true, false, 1);
fail("testMaxRetries");
} finally {
if (telemetryEnabled) {
TelemetryService.enable();
} else {
TelemetryService.disable();
}
}
});
public void testLoginMaxRetries() throws IOException {
boolean telemetryEnabled = TelemetryService.getInstance().isEnabled();

CloseableHttpClient client = mock(CloseableHttpClient.class);
when(client.execute(any(HttpUriRequest.class)))
.thenAnswer(
new Answer<CloseableHttpResponse>() {
int callCount = 0;

@Override
public CloseableHttpResponse answer(InvocationOnMock invocation) throws Throwable {
callCount += 1;
if (callCount >= 4) {
return retryLoginResponse();
} else {
return socketTimeoutResponse();
}
}
});

try {
TelemetryService.disable();
assertThrows(
SnowflakeSQLException.class,
() -> execute(client, "/session/v1/login-request", 0, 0, 0, true, false, 1));
} finally {
if (telemetryEnabled) {
TelemetryService.enable();
} else {
TelemetryService.disable();
}
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,38 +161,36 @@ public void errorInvalidKey() {
@DontRunOnGithubActions
public void errorInterruptedException() throws SQLException {
// Can still retry, no error thrown
try {
spyingClient.handleStorageException(
new InterruptedException(), 0, "upload", sfSession, command, null);
} catch (Exception e) {
fail("Should not have exception here");
}
Mockito.verify(spyingClient, Mockito.never()).renew(Mockito.anyMap());
assertThrows(
SnowflakeSQLException.class,
() -> {
try {
() ->
spyingClient.handleStorageException(
new InterruptedException(), 0, "upload", sfSession, command, null);
} catch (Exception e) {
fail("Should not have exception here");
}
Mockito.verify(spyingClient, Mockito.never()).renew(Mockito.anyMap());
spyingClient.handleStorageException(
new InterruptedException(), 26, "upload", sfSession, command, null);
});
new InterruptedException(), 26, "upload", sfSession, command, null));
}

@Test
@DontRunOnGithubActions
public void errorSocketTimeoutException() {
public void errorSocketTimeoutException() throws SnowflakeSQLException {
// Can still retry, no error thrown
try {
spyingClient.handleStorageException(
new SocketTimeoutException(), 0, "upload", sfSession, command, null);
} catch (Exception e) {
fail("Should not have exception here");
}
Mockito.verify(spyingClient, Mockito.never()).renew(Mockito.anyMap());
assertThrows(
SnowflakeSQLException.class,
() -> {
// Can still retry, no error thrown
try {
() ->
spyingClient.handleStorageException(
new SocketTimeoutException(), 0, "upload", sfSession, command, null);
} catch (Exception e) {
fail("Should not have exception here");
}
Mockito.verify(spyingClient, Mockito.never()).renew(Mockito.anyMap());
spyingClient.handleStorageException(
new SocketTimeoutException(), 26, "upload", sfSession, command, null);
});
new SocketTimeoutException(), 26, "upload", sfSession, command, null));
}

@Test
Expand All @@ -207,30 +205,25 @@ public void errorUnknownException() {

@Test
@DontRunOnGithubActions
public void errorNoSpaceLeftOnDevice() {
public void errorNoSpaceLeftOnDevice() throws IOException {
File destFolder = new File(tmpFolder, "dest");
destFolder.mkdirs();
String destFolderCanonicalPath = destFolder.getCanonicalPath();
String getCommand =
"get @testPutGet_stage/" + TEST_DATA_FILE + " 'file://" + destFolderCanonicalPath + "'";
assertThrows(
SnowflakeSQLException.class,
() -> {
File destFolder = new File(tmpFolder, "dest");
destFolder.mkdirs();
String destFolderCanonicalPath = destFolder.getCanonicalPath();
String getCommand =
"get @testPutGet_stage/"
+ TEST_DATA_FILE
+ " 'file://"
+ destFolderCanonicalPath
+ "'";
spyingClient.handleStorageException(
new StorageException(
"",
Constants.NO_SPACE_LEFT_ON_DEVICE_ERR,
new IOException(Constants.NO_SPACE_LEFT_ON_DEVICE_ERR)),
0,
"download",
null,
getCommand,
null);
});
() ->
spyingClient.handleStorageException(
new StorageException(
"",
Constants.NO_SPACE_LEFT_ON_DEVICE_ERR,
new IOException(Constants.NO_SPACE_LEFT_ON_DEVICE_ERR)),
0,
"download",
null,
getCommand,
null));
}

@AfterEach
Expand Down
12 changes: 4 additions & 8 deletions src/test/java/net/snowflake/client/jdbc/SnowflakeDriverIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -2747,14 +2747,10 @@ public void testPutGetToUnencryptedStage() throws Throwable {

/** Prepare statement will fail if the connection is already closed. */
@Test
public void testNotClosedSession() {
assertThrows(
SQLException.class,
() -> {
Connection connection = getConnection();
connection.close();
connection.prepareStatement("select 1");
});
public void testNotClosedSession() throws SQLException {
Connection connection = getConnection();
connection.close();
assertThrows(SnowflakeSQLException.class, () -> connection.prepareStatement("select 1"));
}

@Test
Expand Down
Loading

0 comments on commit 8fb4e63

Please sign in to comment.