diff --git a/databricks-sdk-java/src/main/java/com/databricks/sdk/core/CliTokenSource.java b/databricks-sdk-java/src/main/java/com/databricks/sdk/core/CliTokenSource.java index 8a7328904..17e409d93 100644 --- a/databricks-sdk-java/src/main/java/com/databricks/sdk/core/CliTokenSource.java +++ b/databricks-sdk-java/src/main/java/com/databricks/sdk/core/CliTokenSource.java @@ -8,7 +8,10 @@ import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; import java.io.InputStream; +import java.time.Instant; import java.time.LocalDateTime; +import java.time.OffsetDateTime; +import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.util.Arrays; @@ -36,26 +39,45 @@ public CliTokenSource( this.env = env; } - static LocalDateTime parseExpiry(String expiry) { + /** + * Parses an expiry time string and returns the corresponding {@link Instant}. The method attempts + * to parse the input in the following order: 1. RFC 3339/ISO 8601 format with offset (e.g. + * "2024-03-20T10:30:00Z") - If the timestamp is compliant with ISO 8601 and RFC 3339, the + * timezone indicator (Z or +/-HH:mm) is used as part of the conversion process to UTC 2. Local + * date-time format "yyyy-MM-dd HH:mm:ss" (e.g. "2024-03-20 10:30:00") 3. Local date-time format + * with optional fractional seconds of varying precision (e.g. "2024-03-20 10:30:00.123") + * + *

For timestamps without a timezone indicator (formats 2 and 3), the system's default time + * zone is assumed and used for the conversion to UTC. + * + * @param expiry expiry time string in one of the supported formats + * @return the parsed {@link Instant} + * @throws DateTimeParseException if the input string cannot be parsed in any of the supported + * formats + */ + static Instant parseExpiry(String expiry) { + DateTimeParseException parseException; + try { + return OffsetDateTime.parse(expiry).toInstant(); + } catch (DateTimeParseException e) { + parseException = e; + } + String multiplePrecisionPattern = "[SSSSSSSSS][SSSSSSSS][SSSSSSS][SSSSSS][SSSSS][SSSS][SSS][SS][S]"; List datePatterns = - Arrays.asList( - "yyyy-MM-dd HH:mm:ss", - "yyyy-MM-dd HH:mm:ss." + multiplePrecisionPattern, - "yyyy-MM-dd'T'HH:mm:ss." + multiplePrecisionPattern + "XXX", - "yyyy-MM-dd'T'HH:mm:ss." + multiplePrecisionPattern + "'Z'"); - DateTimeParseException lastException = null; + Arrays.asList("yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss." + multiplePrecisionPattern); for (String pattern : datePatterns) { try { DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern); LocalDateTime dateTime = LocalDateTime.parse(expiry, formatter); - return dateTime; + return dateTime.atZone(ZoneId.systemDefault()).toInstant(); } catch (DateTimeParseException e) { - lastException = e; + parseException.addSuppressed(e); } } - throw lastException; + + throw parseException; } private String getProcessStream(InputStream stream) throws IOException { @@ -83,7 +105,7 @@ protected Token refresh() { String tokenType = jsonNode.get(tokenTypeField).asText(); String accessToken = jsonNode.get(accessTokenField).asText(); String expiry = jsonNode.get(expiryField).asText(); - LocalDateTime expiresOn = parseExpiry(expiry); + Instant expiresOn = parseExpiry(expiry); return new Token(accessToken, tokenType, expiresOn); } catch (DatabricksException e) { throw e; diff --git a/databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/DatabricksOAuthTokenSource.java b/databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/DatabricksOAuthTokenSource.java index f16ae2aed..484e0712e 100644 --- a/databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/DatabricksOAuthTokenSource.java +++ b/databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/DatabricksOAuthTokenSource.java @@ -3,7 +3,7 @@ import com.databricks.sdk.core.DatabricksException; import com.databricks.sdk.core.http.HttpClient; import com.google.common.base.Strings; -import java.time.LocalDateTime; +import java.time.Instant; import java.util.HashMap; import java.util.Map; import java.util.Objects; @@ -166,7 +166,7 @@ public Token refresh() { throw e; } - LocalDateTime expiry = LocalDateTime.now().plusSeconds(response.getExpiresIn()); + Instant expiry = Instant.now().plusSeconds(response.getExpiresIn()); return new Token( response.getAccessToken(), response.getTokenType(), response.getRefreshToken(), expiry); } diff --git a/databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/EndpointTokenSource.java b/databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/EndpointTokenSource.java index 3ca75c441..ed08f57d6 100644 --- a/databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/EndpointTokenSource.java +++ b/databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/EndpointTokenSource.java @@ -2,7 +2,7 @@ import com.databricks.sdk.core.DatabricksException; import com.databricks.sdk.core.http.HttpClient; -import java.time.LocalDateTime; +import java.time.Instant; import java.util.HashMap; import java.util.Map; import java.util.Objects; @@ -87,7 +87,7 @@ protected Token refresh() { throw e; } - LocalDateTime expiry = LocalDateTime.now().plusSeconds(oauthResponse.getExpiresIn()); + Instant expiry = Instant.now().plusSeconds(oauthResponse.getExpiresIn()); return new Token( oauthResponse.getAccessToken(), oauthResponse.getTokenType(), diff --git a/databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/OidcTokenSource.java b/databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/OidcTokenSource.java index 719544ebf..b15f55ded 100644 --- a/databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/OidcTokenSource.java +++ b/databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/OidcTokenSource.java @@ -8,7 +8,7 @@ import com.google.common.base.Strings; import com.google.common.collect.ImmutableMap; import java.io.IOException; -import java.time.LocalDateTime; +import java.time.Instant; /** * {@code OidcTokenSource} is responsible for obtaining OAuth tokens using the OpenID Connect (OIDC) @@ -77,7 +77,7 @@ protected Token refresh() { if (resp.getErrorCode() != null) { throw new IllegalArgumentException(resp.getErrorCode() + ": " + resp.getErrorSummary()); } - LocalDateTime expiry = LocalDateTime.now().plusSeconds(resp.getExpiresIn()); + Instant expiry = Instant.now().plusSeconds(resp.getExpiresIn()); return new Token(resp.getAccessToken(), resp.getTokenType(), resp.getRefreshToken(), expiry); } } diff --git a/databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/RefreshableTokenSource.java b/databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/RefreshableTokenSource.java index e93f91ae5..750aae967 100644 --- a/databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/RefreshableTokenSource.java +++ b/databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/RefreshableTokenSource.java @@ -5,8 +5,7 @@ import com.databricks.sdk.core.http.FormRequest; import com.databricks.sdk.core.http.HttpClient; import com.databricks.sdk.core.http.Request; -import java.time.LocalDateTime; -import java.time.temporal.ChronoUnit; +import java.time.Instant; import java.util.Base64; import java.util.Map; import org.apache.http.HttpHeaders; @@ -70,7 +69,7 @@ protected static Token retrieveToken( if (resp.getErrorCode() != null) { throw new IllegalArgumentException(resp.getErrorCode() + ": " + resp.getErrorSummary()); } - LocalDateTime expiry = LocalDateTime.now().plus(resp.getExpiresIn(), ChronoUnit.SECONDS); + Instant expiry = Instant.now().plusSeconds(resp.getExpiresIn()); return new Token(resp.getAccessToken(), resp.getTokenType(), resp.getRefreshToken(), expiry); } catch (Exception e) { throw new DatabricksException("Failed to refresh credentials: " + e.getMessage(), e); diff --git a/databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/Token.java b/databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/Token.java index f0fd72f68..ac6fbc3ac 100644 --- a/databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/Token.java +++ b/databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/Token.java @@ -4,8 +4,7 @@ import com.databricks.sdk.core.utils.SystemClockSupplier; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; -import java.time.LocalDateTime; -import java.time.temporal.ChronoUnit; +import java.time.Instant; import java.util.Objects; public class Token { @@ -19,21 +18,20 @@ public class Token { * The expiry time of the token. * *

OAuth token responses include the duration of the lifetime of the access token. When the - * token is retrieved, this is converted to a LocalDateTime tracking the expiry time of the token - * with respect to the current clock. + * token is retrieved, this is converted to an Instant tracking the expiry time of the token with + * respect to the current clock. */ - @JsonProperty private LocalDateTime expiry; + @JsonProperty private Instant expiry; private final ClockSupplier clockSupplier; /** Constructor for non-refreshable tokens (e.g. M2M). */ - public Token(String accessToken, String tokenType, LocalDateTime expiry) { + public Token(String accessToken, String tokenType, Instant expiry) { this(accessToken, tokenType, null, expiry, new SystemClockSupplier()); } /** Constructor for non-refreshable tokens (e.g. M2M) with ClockSupplier */ - public Token( - String accessToken, String tokenType, LocalDateTime expiry, ClockSupplier clockSupplier) { + public Token(String accessToken, String tokenType, Instant expiry, ClockSupplier clockSupplier) { this(accessToken, tokenType, null, expiry, clockSupplier); } @@ -43,7 +41,7 @@ public Token( @JsonProperty("accessToken") String accessToken, @JsonProperty("tokenType") String tokenType, @JsonProperty("refreshToken") String refreshToken, - @JsonProperty("expiry") LocalDateTime expiry) { + @JsonProperty("expiry") Instant expiry) { this(accessToken, tokenType, refreshToken, expiry, new SystemClockSupplier()); } @@ -52,7 +50,7 @@ public Token( String accessToken, String tokenType, String refreshToken, - LocalDateTime expiry, + Instant expiry, ClockSupplier clockSupplier) { Objects.requireNonNull(accessToken, "accessToken must be defined"); Objects.requireNonNull(tokenType, "tokenType must be defined"); @@ -71,8 +69,8 @@ public boolean isExpired() { } // Azure Databricks rejects tokens that expire in 30 seconds or less, // so we refresh the token 40 seconds before it expires. - LocalDateTime potentiallyExpired = expiry.minus(40, ChronoUnit.SECONDS); - LocalDateTime now = LocalDateTime.now(clockSupplier.getClock()); + Instant potentiallyExpired = expiry.minusSeconds(40); + Instant now = Instant.now(clockSupplier.getClock()); return potentiallyExpired.isBefore(now); } diff --git a/databricks-sdk-java/src/main/java/com/databricks/sdk/core/utils/SystemClockSupplier.java b/databricks-sdk-java/src/main/java/com/databricks/sdk/core/utils/SystemClockSupplier.java index edac0cd94..c79e6884d 100644 --- a/databricks-sdk-java/src/main/java/com/databricks/sdk/core/utils/SystemClockSupplier.java +++ b/databricks-sdk-java/src/main/java/com/databricks/sdk/core/utils/SystemClockSupplier.java @@ -5,6 +5,6 @@ public class SystemClockSupplier implements ClockSupplier { @Override public Clock getClock() { - return Clock.systemDefaultZone(); + return Clock.systemUTC(); } } diff --git a/databricks-sdk-java/src/test/java/com/databricks/sdk/core/AzureCliCredentialsProviderTest.java b/databricks-sdk-java/src/test/java/com/databricks/sdk/core/AzureCliCredentialsProviderTest.java index 0212b7652..4e8a57b06 100644 --- a/databricks-sdk-java/src/test/java/com/databricks/sdk/core/AzureCliCredentialsProviderTest.java +++ b/databricks-sdk-java/src/test/java/com/databricks/sdk/core/AzureCliCredentialsProviderTest.java @@ -7,7 +7,6 @@ import com.databricks.sdk.core.oauth.Token; import com.databricks.sdk.core.oauth.TokenSource; -import java.time.LocalDateTime; import java.util.Arrays; import java.util.List; import org.junit.jupiter.api.Test; @@ -25,7 +24,7 @@ class AzureCliCredentialsProviderTest { private static CliTokenSource mockTokenSource() { CliTokenSource tokenSource = Mockito.mock(CliTokenSource.class); Mockito.when(tokenSource.getToken()) - .thenReturn(new Token(TOKEN, TOKEN_TYPE, LocalDateTime.now())); + .thenReturn(new Token(TOKEN, TOKEN_TYPE, java.time.Instant.now())); return tokenSource; } diff --git a/databricks-sdk-java/src/test/java/com/databricks/sdk/core/CliTokenSourceTest.java b/databricks-sdk-java/src/test/java/com/databricks/sdk/core/CliTokenSourceTest.java index a551b72d3..aa98730cb 100644 --- a/databricks-sdk-java/src/test/java/com/databricks/sdk/core/CliTokenSourceTest.java +++ b/databricks-sdk-java/src/test/java/com/databricks/sdk/core/CliTokenSourceTest.java @@ -1,6 +1,7 @@ package com.databricks.sdk.core; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mockConstruction; @@ -14,7 +15,9 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.time.Duration; +import java.time.Instant; import java.time.LocalDateTime; +import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; @@ -27,7 +30,6 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; -import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -157,30 +159,58 @@ public void testRefreshWithExpiry( } } - @Test - public void testParseExpiryWithoutTruncate() { - LocalDateTime parsedDateTime = CliTokenSource.parseExpiry("2023-07-17T09:02:22.330612218Z"); - assertEquals(LocalDateTime.of(2023, 7, 17, 9, 2, 22, 330612218), parsedDateTime); + private static Stream expiryProvider() { + return Stream.of( + Arguments.of( + "2023-07-17T09:02:22.330612218Z", + Instant.parse("2023-07-17T09:02:22.330612218Z"), + "9-digit nanos"), + Arguments.of( + "2023-07-17T09:02:22.33061221Z", + Instant.parse("2023-07-17T09:02:22.330612210Z"), + "8-digit nanos"), + Arguments.of( + "2023-07-17T09:02:22.330612Z", + Instant.parse("2023-07-17T09:02:22.330612000Z"), + "6-digit nanos"), + Arguments.of( + "2023-07-17T10:02:22.330612218+01:00", + Instant.parse("2023-07-17T09:02:22.330612218Z"), + "+01:00 offset, 9-digit nanos"), + Arguments.of( + "2023-07-17T04:02:22.330612218-05:00", + Instant.parse("2023-07-17T09:02:22.330612218Z"), + "-05:00 offset, 9-digit nanos"), + Arguments.of( + "2023-07-17T10:02:22.330612+01:00", + Instant.parse("2023-07-17T09:02:22.330612000Z"), + "+01:00 offset, 6-digit nanos"), + Arguments.of("2023-07-17T09:02:22.33061221987Z", null, "Invalid: >9 nanos"), + Arguments.of("17-07-2023 09:02:22", null, "Invalid date format"), + Arguments.of( + "2023-07-17 09:02:22.330612218", + LocalDateTime.parse("2023-07-17T09:02:22.330612218") + .atZone(ZoneId.systemDefault()) + .toInstant(), + "Space separator, 9-digit nanos"), + Arguments.of( + "2023-07-17 09:02:22.330612", + LocalDateTime.parse("2023-07-17T09:02:22.330612") + .atZone(ZoneId.systemDefault()) + .toInstant(), + "Space separator, 6-digit nanos"), + Arguments.of( + "2023-07-17 09:02:22.33061221987", null, "Space separator, Invalid: >9 nanos")); } - @Test - public void testParseExpiryWithTruncate() { - LocalDateTime parsedDateTime = CliTokenSource.parseExpiry("2023-07-17T09:02:22.33061221Z"); - assertEquals(LocalDateTime.of(2023, 7, 17, 9, 2, 22, 330612210), parsedDateTime); - } - - @Test - public void testParseExpiryWithTruncateAndLessNanoSecondDigits() { - LocalDateTime parsedDateTime = CliTokenSource.parseExpiry("2023-07-17T09:02:22.330612Z"); - assertEquals(LocalDateTime.of(2023, 7, 17, 9, 2, 22, 330612000), parsedDateTime); - } - - @Test - public void testParseExpiryWithMoreThanNineNanoSecondDigits() { - try { - CliTokenSource.parseExpiry("2023-07-17T09:02:22.33061221987Z"); - } catch (DateTimeParseException e) { - assert (e.getMessage().contains("could not be parsed")); + @ParameterizedTest(name = "{2}") + @MethodSource("expiryProvider") + public void testParseExpiry(String input, Instant expectedInstant, String description) { + if (expectedInstant == null) { + assertThrows(DateTimeParseException.class, () -> CliTokenSource.parseExpiry(input)); + } else { + Instant parsedInstant = CliTokenSource.parseExpiry(input); + assertEquals(expectedInstant, parsedInstant); } } } diff --git a/databricks-sdk-java/src/test/java/com/databricks/sdk/core/DatabricksConfigTest.java b/databricks-sdk-java/src/test/java/com/databricks/sdk/core/DatabricksConfigTest.java index 38b6fcd9c..b3ac333a3 100644 --- a/databricks-sdk-java/src/test/java/com/databricks/sdk/core/DatabricksConfigTest.java +++ b/databricks-sdk-java/src/test/java/com/databricks/sdk/core/DatabricksConfigTest.java @@ -12,7 +12,6 @@ import com.databricks.sdk.core.oauth.TokenSource; import com.databricks.sdk.core.utils.Environment; import java.io.IOException; -import java.time.LocalDateTime; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -232,7 +231,7 @@ public void testGetTokenSourceWithOAuth() { HttpClient httpClient = mock(HttpClient.class); TokenSource mockTokenSource = mock(TokenSource.class); when(mockTokenSource.getToken()) - .thenReturn(new Token("test-token", "Bearer", LocalDateTime.now().plusHours(1))); + .thenReturn(new Token("test-token", "Bearer", java.time.Instant.now().plusSeconds(3600))); OAuthHeaderFactory mockHeaderFactory = OAuthHeaderFactory.fromTokenSource(mockTokenSource); CredentialsProvider mockProvider = mock(CredentialsProvider.class); when(mockProvider.authType()).thenReturn("test"); diff --git a/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/DataPlaneTokenSourceTest.java b/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/DataPlaneTokenSourceTest.java index 5887c4ee1..1fb96a559 100644 --- a/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/DataPlaneTokenSourceTest.java +++ b/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/DataPlaneTokenSourceTest.java @@ -8,7 +8,7 @@ import com.databricks.sdk.core.http.Response; import java.io.IOException; import java.net.URL; -import java.time.LocalDateTime; +import java.time.Instant; import java.util.stream.Stream; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -29,8 +29,7 @@ public class DataPlaneTokenSourceTest { private static Stream provideDataPlaneTokenScenarios() throws Exception { // Mock DatabricksOAuthTokenSource for control plane token - Token cpToken = - new Token(TEST_CP_TOKEN, TEST_TOKEN_TYPE, null, LocalDateTime.now().plusSeconds(600)); + Token cpToken = new Token(TEST_CP_TOKEN, TEST_TOKEN_TYPE, null, Instant.now().plusSeconds(600)); DatabricksOAuthTokenSource mockCpTokenSource = mock(DatabricksOAuthTokenSource.class); when(mockCpTokenSource.getToken()).thenReturn(cpToken); @@ -81,9 +80,8 @@ private static Stream provideDataPlaneTokenScenarios() throws Excepti "dp-access-token1", TEST_TOKEN_TYPE, TEST_REFRESH_TOKEN, - LocalDateTime.now().plusSeconds(TEST_EXPIRES_IN)), - null // No exception - ), + Instant.now().plusSeconds(TEST_EXPIRES_IN)), + null), Arguments.of( "Success: endpoint2/auth2 (different cache key)", TEST_ENDPOINT_2, @@ -95,7 +93,7 @@ private static Stream provideDataPlaneTokenScenarios() throws Excepti "dp-access-token2", TEST_TOKEN_TYPE, TEST_REFRESH_TOKEN, - LocalDateTime.now().plusSeconds(TEST_EXPIRES_IN)), + Instant.now().plusSeconds(TEST_EXPIRES_IN)), null), Arguments.of( "Error response from endpoint", @@ -203,7 +201,7 @@ void testDataPlaneTokenSource( @Test void testEndpointTokenSourceCaching() throws Exception { Token cpToken = - new Token(TEST_CP_TOKEN, TEST_TOKEN_TYPE, null, LocalDateTime.now().plusSeconds(3600)); + new Token(TEST_CP_TOKEN, TEST_TOKEN_TYPE, null, Instant.now().plusSeconds(3600)); DatabricksOAuthTokenSource mockCpTokenSource = mock(DatabricksOAuthTokenSource.class); when(mockCpTokenSource.getToken()).thenReturn(cpToken); diff --git a/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/EndpointTokenSourceTest.java b/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/EndpointTokenSourceTest.java index a3af2254f..5fdac9f2d 100644 --- a/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/EndpointTokenSourceTest.java +++ b/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/EndpointTokenSourceTest.java @@ -9,7 +9,7 @@ import com.databricks.sdk.core.http.Response; import java.io.IOException; import java.net.URL; -import java.time.LocalDateTime; +import java.time.Instant; import java.util.stream.Stream; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -48,7 +48,7 @@ private static Stream provideEndpointTokenScenarios() throws Exceptio String malformedJson = "{not valid json}"; // Mock DatabricksOAuthTokenSource for control plane token - Token cpToken = new Token(TEST_CP_TOKEN, TEST_TOKEN_TYPE, LocalDateTime.now().plusMinutes(10)); + Token cpToken = new Token(TEST_CP_TOKEN, TEST_TOKEN_TYPE, Instant.now().plusSeconds(600)); DatabricksOAuthTokenSource mockCpTokenSource = mock(DatabricksOAuthTokenSource.class); when(mockCpTokenSource.getToken()).thenReturn(cpToken); diff --git a/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/ExternalBrowserCredentialsProviderTest.java b/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/ExternalBrowserCredentialsProviderTest.java index 1714b731c..932690bd7 100644 --- a/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/ExternalBrowserCredentialsProviderTest.java +++ b/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/ExternalBrowserCredentialsProviderTest.java @@ -13,7 +13,7 @@ import com.databricks.sdk.core.http.Response; import java.io.IOException; import java.net.URL; -import java.time.LocalDateTime; +import java.time.Instant; import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -210,7 +210,7 @@ void sessionCredentials() throws IOException { "originalAccessToken", "originalTokenType", "originalRefreshToken", - LocalDateTime.MAX)) + Instant.MAX)) .build(); Token token = sessionCredentials.refresh(); @@ -234,7 +234,7 @@ void cacheWithValidTokenTest() throws IOException { .execute(any(Request.class)); // Create an valid token with valid refresh token - LocalDateTime futureTime = LocalDateTime.now().plusHours(1); + Instant futureTime = Instant.now().plusSeconds(3600); Token validToken = new Token("valid_access_token", "Bearer", "valid_refresh_token", futureTime); // Create mock token cache that returns the valid token @@ -314,7 +314,7 @@ void cacheWithInvalidAccessTokenValidRefreshTest() throws IOException { .execute(any(Request.class)); // Create an expired token with valid refresh token - LocalDateTime pastTime = LocalDateTime.now().minusHours(1); + Instant pastTime = Instant.now().minusSeconds(3600); Token expiredToken = new Token("expired_access_token", "Bearer", "valid_refresh_token", pastTime); @@ -392,7 +392,7 @@ void cacheWithInvalidAccessTokenRefreshFailingTest() throws IOException { .execute(any(Request.class)); // Create an expired token with invalid refresh token - LocalDateTime pastTime = LocalDateTime.now().minusHours(1); + Instant pastTime = Instant.now().minusSeconds(3600); Token expiredToken = new Token("expired_access_token", "Bearer", "invalid_refresh_token", pastTime); @@ -406,7 +406,7 @@ void cacheWithInvalidAccessTokenRefreshFailingTest() throws IOException { "browser_access_token", "Bearer", "browser_refresh_token", - LocalDateTime.now().plusHours(1)); + Instant.now().plusSeconds(3600)); SessionCredentials browserAuthCreds = new SessionCredentials.Builder() @@ -460,7 +460,7 @@ void cacheWithInvalidAccessTokenRefreshFailingTest() throws IOException { @Test void cacheWithInvalidTokensTest() throws IOException { // Create completely invalid token (no refresh token) - LocalDateTime pastTime = LocalDateTime.now().minusHours(1); + Instant pastTime = Instant.now().minusSeconds(3600); Token invalidToken = new Token("expired_access_token", "Bearer", null, pastTime); // Create mock token cache that returns the invalid token @@ -473,7 +473,7 @@ void cacheWithInvalidTokensTest() throws IOException { "browser_access_token", "Bearer", "browser_refresh_token", - LocalDateTime.now().plusHours(1)); + Instant.now().plusSeconds(3600)); SessionCredentials browserAuthCreds = new SessionCredentials.Builder() diff --git a/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/FileTokenCacheTest.java b/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/FileTokenCacheTest.java index ede6cfd11..303f6de66 100644 --- a/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/FileTokenCacheTest.java +++ b/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/FileTokenCacheTest.java @@ -5,7 +5,7 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.time.LocalDateTime; +import java.time.Instant; import java.util.Arrays; import java.util.List; import org.junit.jupiter.api.AfterEach; @@ -42,7 +42,7 @@ void testEmptyCache() { @Test void testSaveAndLoadToken() { // Given a token - LocalDateTime expiry = LocalDateTime.now().plusHours(1); + Instant expiry = Instant.now().plusSeconds(3600); Token token = new Token("access-token", "Bearer", "refresh-token", expiry); // When saving and loading the token @@ -60,14 +60,14 @@ void testSaveAndLoadToken() { @Test void testTokenExpiry() { // Create an expired token - LocalDateTime pastTime = LocalDateTime.now().minusHours(1); + Instant pastTime = Instant.now().minusSeconds(3600); Token expiredToken = new Token("access-token", "Bearer", "refresh-token", pastTime); // Verify it's marked as expired assertTrue(expiredToken.isExpired(), "Token should be expired"); // Create a valid token - LocalDateTime futureTime = LocalDateTime.now().plusMinutes(30); + Instant futureTime = Instant.now().plusSeconds(1800); Token validToken = new Token("access-token", "Bearer", "refresh-token", futureTime); // Verify it's not marked as expired @@ -86,8 +86,8 @@ void testNullPathRejection() { @Test void testOverwriteToken() { // Given two tokens saved in sequence - Token token1 = new Token("token1", "Bearer", "refresh1", LocalDateTime.now().plusHours(1)); - Token token2 = new Token("token2", "Bearer", "refresh2", LocalDateTime.now().plusHours(2)); + Token token1 = new Token("token1", "Bearer", "refresh1", Instant.now().plusSeconds(3600)); + Token token2 = new Token("token2", "Bearer", "refresh2", Instant.now().plusSeconds(7200)); tokenCache.save(token1); tokenCache.save(token2); @@ -110,7 +110,7 @@ void testWithCustomPath(@TempDir Path tempDir) { // And a token Token testToken = new Token( - "test-access-token", "Bearer", "test-refresh-token", LocalDateTime.now().plusHours(1)); + "test-access-token", "Bearer", "test-refresh-token", Instant.now().plusSeconds(3600)); // When saving and loading cache.save(testToken); diff --git a/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/OAuthHeaderFactoryTest.java b/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/OAuthHeaderFactoryTest.java index d0530b2c1..f0b83153c 100644 --- a/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/OAuthHeaderFactoryTest.java +++ b/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/OAuthHeaderFactoryTest.java @@ -3,7 +3,7 @@ import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; -import java.time.LocalDateTime; +import java.time.Instant; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -25,7 +25,7 @@ public class OAuthHeaderFactoryTest { @Mock private TokenSource tokenSource; private static Stream provideTokenSourceTestCases() { - LocalDateTime expiry = LocalDateTime.now().plusHours(1); + Instant expiry = Instant.now().plusSeconds(3600); Token token = new Token(TOKEN_VALUE, TOKEN_TYPE, expiry); return Stream.of( @@ -57,7 +57,7 @@ public void testFromTokenSourceFactoryMethod( } private static Stream provideSuppliersTestCases() { - LocalDateTime expiry = LocalDateTime.now().plusHours(1); + Instant expiry = Instant.now().plusSeconds(3600); Token token = new Token(TOKEN_VALUE, TOKEN_TYPE, expiry); Map standardHeaders = new HashMap<>(); diff --git a/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/TokenSourceCredentialsProviderTest.java b/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/TokenSourceCredentialsProviderTest.java index 14eb3fa40..8d2d68fd4 100644 --- a/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/TokenSourceCredentialsProviderTest.java +++ b/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/TokenSourceCredentialsProviderTest.java @@ -6,7 +6,7 @@ import com.databricks.sdk.core.DatabricksConfig; import com.databricks.sdk.core.DatabricksException; import com.databricks.sdk.core.HeaderFactory; -import java.time.LocalDateTime; +import java.time.Instant; import java.util.Map; import java.util.stream.Stream; import org.junit.jupiter.params.ParameterizedTest; @@ -19,7 +19,7 @@ class TokenSourceCredentialsProviderTest { private static final String TEST_AUTH_TYPE = "test-auth-type"; private static final String TEST_ACCESS_TOKEN_VALUE = "test-access-token"; private static final Token TEST_TOKEN = - new Token(TEST_ACCESS_TOKEN_VALUE, "Bearer", LocalDateTime.now().plusHours(1)); + new Token(TEST_ACCESS_TOKEN_VALUE, "Bearer", Instant.now().plusSeconds(3600)); /** Tests token retrieval scenarios */ @ParameterizedTest(name = "{0}") diff --git a/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/TokenTest.java b/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/TokenTest.java index 2d87a32c2..16b726222 100644 --- a/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/TokenTest.java +++ b/databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/TokenTest.java @@ -4,7 +4,6 @@ import com.databricks.sdk.core.utils.FakeClockSupplier; import java.time.Instant; -import java.time.LocalDateTime; import java.time.ZoneId; import org.junit.jupiter.api.Test; @@ -13,20 +12,20 @@ class TokenTest { private static final String accessToken = "testAccessToken"; private static final String refreshToken = "testRefreshToken"; private static final String tokenType = "testTokenType"; - private final LocalDateTime currentLocalDateTime; + private final Instant currentInstant; private final FakeClockSupplier fakeClockSupplier; TokenTest() { Instant instant = Instant.parse("2023-10-18T12:00:00.00Z"); ZoneId zoneId = ZoneId.of("UTC"); fakeClockSupplier = new FakeClockSupplier(instant, zoneId); - currentLocalDateTime = LocalDateTime.now(fakeClockSupplier.getClock()); + currentInstant = Instant.now(fakeClockSupplier.getClock()); } @Test void createNonRefreshableToken() { Token token = - new Token(accessToken, tokenType, currentLocalDateTime.plusMinutes(5), fakeClockSupplier); + new Token(accessToken, tokenType, currentInstant.plusSeconds(300), fakeClockSupplier); assertEquals(accessToken, token.getAccessToken()); assertEquals(tokenType, token.getTokenType()); assertNull(token.getRefreshToken()); @@ -40,7 +39,7 @@ void createRefreshableToken() { accessToken, tokenType, refreshToken, - currentLocalDateTime.plusMinutes(5), + currentInstant.plusSeconds(300), fakeClockSupplier); assertEquals(accessToken, token.getAccessToken()); assertEquals(tokenType, token.getTokenType()); @@ -51,7 +50,7 @@ void createRefreshableToken() { @Test void tokenExpiryMoreThan40Seconds() { Token token = - new Token(accessToken, tokenType, currentLocalDateTime.plusSeconds(50), fakeClockSupplier); + new Token(accessToken, tokenType, currentInstant.plusSeconds(50), fakeClockSupplier); assertFalse(token.isExpired()); assertTrue(token.isValid()); } @@ -59,7 +58,7 @@ void tokenExpiryMoreThan40Seconds() { @Test void tokenExpiryLessThan40Seconds() { Token token = - new Token(accessToken, tokenType, currentLocalDateTime.plusSeconds(30), fakeClockSupplier); + new Token(accessToken, tokenType, currentInstant.plusSeconds(30), fakeClockSupplier); assertTrue(token.isExpired()); assertFalse(token.isValid()); } @@ -67,7 +66,7 @@ void tokenExpiryLessThan40Seconds() { @Test void expiredToken() { Token token = - new Token(accessToken, tokenType, currentLocalDateTime.minusSeconds(10), fakeClockSupplier); + new Token(accessToken, tokenType, currentInstant.minusSeconds(10), fakeClockSupplier); assertTrue(token.isExpired()); assertFalse(token.isValid()); }