Skip to content

Commit 113bdb3

Browse files
authored
Updated CLI token source parseExpiry method to account for different time format (#116)
## Changes <!-- Summary of your changes that are easy to understand --> Add functionality to check for different date format for expiry parsing in CLI token source. Also add pattern matching for flexibility between different java versions ## Tests <!-- How is this tested? --> Added unit tests, since `LocalDateTime` support upto nano second precision -- tested for less and more digits as well.
1 parent 979b85c commit 113bdb3

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

databricks-sdk-java/src/main/java/com/databricks/sdk/core/CliTokenSource.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,15 @@ public CliTokenSource(
3737
this.getAllEnv = getAllEnv;
3838
}
3939

40-
private static LocalDateTime parseExpiry(String expiry) {
40+
static LocalDateTime parseExpiry(String expiry) {
41+
String multiplePrecisionPattern =
42+
"[SSSSSSSSS][SSSSSSSS][SSSSSSS][SSSSSS][SSSSS][SSSS][SSS][SS][S]";
4143
List<String> datePatterns =
4244
Arrays.asList(
43-
"yyyy-MM-dd HH:mm:ss.SSSSSS", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX");
45+
"yyyy-MM-dd HH:mm:ss",
46+
"yyyy-MM-dd HH:mm:ss." + multiplePrecisionPattern,
47+
"yyyy-MM-dd'T'HH:mm:ss." + multiplePrecisionPattern + "XXX",
48+
"yyyy-MM-dd'T'HH:mm:ss." + multiplePrecisionPattern + "'Z'");
4449
DateTimeParseException lastException = null;
4550
for (String pattern : datePatterns) {
4651
try {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.databricks.sdk.core;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.time.LocalDateTime;
6+
import java.time.format.DateTimeParseException;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class CliTokenSourceTest {
10+
11+
@Test
12+
public void testParseExpiryWithoutTruncate() {
13+
LocalDateTime parsedDateTime = CliTokenSource.parseExpiry("2023-07-17T09:02:22.330612218Z");
14+
assertEquals(LocalDateTime.of(2023, 7, 17, 9, 2, 22, 330612218), parsedDateTime);
15+
}
16+
17+
@Test
18+
public void testParseExpiryWithTruncate() {
19+
LocalDateTime parsedDateTime = CliTokenSource.parseExpiry("2023-07-17T09:02:22.33061221Z");
20+
assertEquals(LocalDateTime.of(2023, 7, 17, 9, 2, 22, 330612210), parsedDateTime);
21+
}
22+
23+
@Test
24+
public void testParseExpiryWithTruncateAndLessNanoSecondDigits() {
25+
LocalDateTime parsedDateTime = CliTokenSource.parseExpiry("2023-07-17T09:02:22.330612Z");
26+
assertEquals(LocalDateTime.of(2023, 7, 17, 9, 2, 22, 330612000), parsedDateTime);
27+
}
28+
29+
@Test
30+
public void testParseExpiryWithMoreThanNineNanoSecondDigits() {
31+
try {
32+
CliTokenSource.parseExpiry("2023-07-17T09:02:22.33061221987Z");
33+
} catch (DateTimeParseException e) {
34+
assert (e.getMessage().contains("could not be parsed"));
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)