Skip to content

Commit

Permalink
fix(jdbc): DateTimeFormatter can be reused in the JdbcMapper
Browse files Browse the repository at this point in the history
  • Loading branch information
loicmathieu committed Jul 10, 2023
1 parent 1132b03 commit 9845b83
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
6 changes: 6 additions & 0 deletions core/src/test/resources/flows/valids/execution.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
id: execution-start-date
namespace: io.kestra.tests
tasks:
- id: hello
type: io.kestra.core.tasks.debugs.Return
format: "{{ execution.startDate }}"
12 changes: 5 additions & 7 deletions jdbc/src/main/java/io/kestra/jdbc/JdbcMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import java.time.format.DateTimeFormatter;

public abstract class JdbcMapper {
private static final DateTimeFormatter INSTANT_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
.withZone(ZoneOffset.UTC);
private static final DateTimeFormatter ZONED_DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
private static ObjectMapper MAPPER;

public static ObjectMapper of() {
Expand All @@ -24,19 +27,14 @@ public static ObjectMapper of() {
module.addSerializer(Instant.class, new JsonSerializer<>() {
@Override
public void serialize(Instant instant, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
.withZone(ZoneOffset.UTC)
.format(instant)
);
jsonGenerator.writeString(INSTANT_FORMATTER.format(instant));
}
});

module.addSerializer(ZonedDateTime.class, new JsonSerializer<>() {
@Override
public void serialize(ZonedDateTime instant, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
.format(instant)
);
jsonGenerator.writeString(ZONED_DATE_TIME_FORMATTER.format(instant));
}
});

Expand Down
8 changes: 8 additions & 0 deletions jdbc/src/test/java/io/kestra/jdbc/runner/JdbcRunnerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.matchesPattern;

@MicronautTest(transactional = false)
@TestInstance(TestInstance.Lifecycle.PER_CLASS) // must be per-class to allow calling once init() which took a lot of time
Expand Down Expand Up @@ -241,4 +242,11 @@ public void pauseRunDelay() throws Exception {
public void pauseRunTimeout() throws Exception {
pauseTest.runTimeout(runnerUtils);
}

@Test
void executionDate() throws TimeoutException {
Execution execution = runnerUtils.runOne("io.kestra.tests", "execution-start-date", null, null, Duration.ofSeconds(60));

assertThat((String) execution.getTaskRunList().get(0).getOutputs().get("value"), matchesPattern("^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}Z"));
}
}

0 comments on commit 9845b83

Please sign in to comment.