-
Notifications
You must be signed in to change notification settings - Fork 35
Updated CLI token source parseExpiry method to account for different time format
#116
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
Changes from all commits
4df3d79
00a623a
6bae4d1
5049891
09cd975
37e8929
a342237
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,10 +37,15 @@ public CliTokenSource( | |
| this.getAllEnv = getAllEnv; | ||
| } | ||
|
|
||
| private static LocalDateTime parseExpiry(String expiry) { | ||
| static LocalDateTime parseExpiry(String expiry) { | ||
| String multiplePrecisionPattern = | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We would need to account for multiple precision digits else it would be fragile. There are difference in how Java 8,11 uses the formatter vs higher versions: 17, 20. https://github.com/databricks/databricks-sdk-java/actions/runs/5575864416/jobs/10186346317?pr=116 We could also truncate the digits to milli or micro seconds but it doesn't account for digits lower than them, will have loss of data and more maintenance. It is better to do pattern matching for all 1-9 digits. |
||
| "[SSSSSSSSS][SSSSSSSS][SSSSSSS][SSSSSS][SSSSS][SSSS][SSS][SS][S]"; | ||
| List<String> datePatterns = | ||
| Arrays.asList( | ||
| "yyyy-MM-dd HH:mm:ss.SSSSSS", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX"); | ||
| "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; | ||
| for (String pattern : datePatterns) { | ||
| try { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package com.databricks.sdk.core; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.time.format.DateTimeParseException; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class CliTokenSourceTest { | ||
|
|
||
| @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); | ||
| } | ||
|
|
||
| @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")); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Making package private