Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNOW-1016470: Increase code coverage to at least 90% in JDBC #2069

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -170,6 +171,25 @@ public void testSessionUtilExternalBrowser() throws Throwable {
sub.authenticate();
MatcherAssert.assertThat(
"", sub.getToken(), equalTo(FakeSessionUtilExternalBrowser.MOCK_SAML_TOKEN));

sub = FakeSessionUtilExternalBrowser.createInstance(loginInput, false);
Mockito.when(loginInput.getDisableConsoleLogin()).thenReturn(false);
sub.authenticate();
MatcherAssert.assertThat(
"", sub.getToken(), equalTo(FakeSessionUtilExternalBrowser.MOCK_SAML_TOKEN));

sub = FakeSessionUtilExternalBrowser.createInstance(loginInput, false);
Mockito.when(loginInput.getDisableConsoleLogin())
.thenAnswer(
invocation -> {
throw new SocketTimeoutException("Test exception");
});
try {
sub.authenticate();
fail("should have failed with an exception.");
} catch (SFException ex) {
assertTrue(ex.getMessage().contains("External browser authentication failed"));
}
}
}

Expand Down Expand Up @@ -286,4 +306,43 @@ public void testExternalBrowserTimeout() throws Exception {
assertTrue(e.getMessage().contains("External browser authentication failed"));
}
}

// @Test
// public void testSessionUtilExternalBrowserWithConsoleLogin() throws Throwable {
// SFLoginInput loginInput = mock(SFLoginInput.class);
// when(loginInput.getServerUrl()).thenReturn("https://testaccount.snowflakecomputing.com/");
// when(loginInput.getAuthenticator())
// .thenReturn(ClientAuthnDTO.AuthenticatorType.EXTERNALBROWSER.name());
// when(loginInput.getAccountName()).thenReturn("testaccount");
// when(loginInput.getUserName()).thenReturn("testuser");
// when(loginInput.getDisableConsoleLogin()).thenReturn(false);
//
// try (MockedStatic<HttpUtil> mockedHttpUtil = mockStatic(HttpUtil.class)) {
// mockedHttpUtil
// .when(
// () ->
// HttpUtil.executeGeneralRequest(
// Mockito.any(HttpRequestBase.class),
// Mockito.anyInt(),
// Mockito.anyInt(),
// Mockito.anyInt(),
// Mockito.anyInt(),
// Mockito.nullable(HttpClientSettingsKey.class)))
// .thenReturn(
// "{\"success\":\"true\",\"data\":{\"proofKey\":\""
// + MOCK_PROOF_KEY
// + "\","
// + " \"ssoUrl\":\""
// + MOCK_SSO_URL
// + "\"}}");
//
// SessionUtilExternalBrowser sub = FakeSessionUtilExternalBrowser.createInstance(loginInput);
// sub.authenticate();
// assertThat("", sub.getToken(), equalTo(FakeSessionUtilExternalBrowser.MOCK_SAML_TOKEN));
//
// sub = FakeSessionUtilExternalBrowser.createInstance(loginInput, true);
// sub.authenticate();
// assertThat("", sub.getToken(), equalTo(FakeSessionUtilExternalBrowser.MOCK_SAML_TOKEN));
// }
// }
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.snowflake.client.jdbc.diagnostic;

import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -353,4 +354,17 @@ public void restoreJvmArguments() {
System.getProperty(HTTPS_PROXY_PORT, oldJvmHttpsProxyPort);
}
}

/** Test added in version > 3.16.1 */
@Test
public void testRunDiagnosticContextMethods() {
Map<SFSessionProperty, Object> connectionPropertiesMap = new HashMap<>();
File allowlistFile = new File("src/test/resources/allowlist.json");

DiagnosticContext diagnosticContext =
new DiagnosticContext(allowlistFile.getAbsolutePath(), connectionPropertiesMap);
diagnosticContext.runDiagnostics();
diagnosticContext.logEnvironmentInfo();
assertNotNull(diagnosticContext.getEndpoints());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import java.util.concurrent.TimeUnit;
import net.snowflake.client.annotations.RunOnTestaccountNotOnGithubActions;
import net.snowflake.client.category.TestTags;
import net.snowflake.client.core.SFException;
import net.snowflake.client.core.SFSession;
import net.snowflake.client.jdbc.BaseJDBCTest;
import net.snowflake.client.jdbc.ErrorCode;
import net.snowflake.client.jdbc.SnowflakeConnectionV1;
import net.snowflake.client.jdbc.SnowflakeLoggedFeatureNotSupportedException;
import net.snowflake.client.jdbc.SnowflakeSQLLoggedException;
Expand Down Expand Up @@ -403,4 +405,61 @@ public void testSqlFeatureNotSupportedExceptionIBTelemetry() throws SQLException
SQLFeatureNotSupportedException.class, () -> statement.execute("select 1", new int[] {}));
}
}

// Test for creation of Exception for the Code coverage purpose.
@Test
public void testSFExceptionLog() {
TelemetryService service = TelemetryService.getInstance();
try {
throw new SFException(ErrorCode.MISSING_SERVER_URL);
} catch (SFException ex) {
// example for an exception metric
// this metric will be delivered to snowflake and wavefront
TelemetryEvent.LogBuilder logBuilder = new TelemetryEvent.LogBuilder();
TelemetryEvent log = logBuilder.withException(ex).build();
assertThat("check log value", log.get("Value").equals("This is an example log"));
service.report(log);
}
}

@Test
public void testCreateLogAndMetricBuilderWithException() {
TelemetryService service = TelemetryService.getInstance();
try {
throw new Exception();
} catch (Exception ex) {
// example for an exception log
// this log will be delivered to snowflake
TelemetryEvent.LogBuilder logBuilder = new TelemetryEvent.LogBuilder();
TelemetryEvent log = logBuilder.withException(ex).withTag("domain", "test").build();
assertThat("check log value", log.get("Value").equals("This is an example log"));
service.report(log);

// example for an exception metric
// this metric will be delivered to snowflake and wavefront
TelemetryEvent.MetricBuilder mBuilder = new TelemetryEvent.MetricBuilder();
TelemetryEvent metric = mBuilder.withException(ex).withTag("domain", "test").build();
assertThat("check log value", log.get("Value").equals("This is an example log"));
service.report(metric);
}
}

// Test for creation of Exception for the Code coverage purpose.
@SuppressWarnings("deprecation")
@Test
public void testWithNameWithValueMethod() {
try {
throw new SFException(ErrorCode.BAD_RESPONSE);
} catch (SFException ex) {
// this log will be delivered to snowflake
TelemetryService service = TelemetryService.getInstance();
TelemetryEvent.LogBuilder logBuilder = new TelemetryEvent.LogBuilder();
TelemetryEvent log =
logBuilder.withName("ExampleLog").withValue("This is an example log").build();
assertThat("check log value", log.get("Value").equals("This is an example log"));
service.report(log);

log.getDeployment();
}
}
}
Loading