Skip to content
Merged
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 @@ -37,10 +37,15 @@ public CliTokenSource(
this.getAllEnv = getAllEnv;
}

private static LocalDateTime parseExpiry(String expiry) {
static LocalDateTime parseExpiry(String expiry) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making package private

String multiplePrecisionPattern =
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 {
Expand Down
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"));
}
}
}