Skip to content

Commit 5b4d411

Browse files
committed
refactor date parsing to use ISO_DATE_TIME
1 parent f73cf11 commit 5b4d411

2 files changed

Lines changed: 22 additions & 12 deletions

File tree

camunda-carbon-reductor-c8/src/main/java/de/envite/greenbpm/carbonreductorconnector/adapter/in/zeebe/variable/CarbonReductorVariableMapper.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,16 @@
1414
import java.time.format.DateTimeParseException;
1515
import java.util.Optional;
1616

17-
import static java.lang.String.format;
18-
import static java.time.format.DateTimeFormatter.ofPattern;
17+
import static java.time.format.DateTimeFormatter.ISO_DATE_TIME;
1918

2019
@Component
2120
public class CarbonReductorVariableMapper {
2221

23-
// Sample Date: 2023-09-08T12:58:20.766Z[GMT]
24-
private static final String DATE_TIME_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSX'['z']'";
25-
2622
private static OffsetDateTime parseDateString(String value) {
2723
try {
28-
return OffsetDateTime.parse(value, ofPattern(DATE_TIME_PATTERN));
24+
return OffsetDateTime.parse(value, ISO_DATE_TIME);
2925
} catch (DateTimeParseException exception) {
30-
throw new IllegalArgumentException(
31-
format("Milestone: Unknown date time format (Expected %s)",
32-
DATE_TIME_PATTERN.replace("'", "")
33-
)
34-
);
26+
throw new IllegalArgumentException("Milestone: Unknown date time format. Expected ISO 8601, e.g. '2024-01-01T11:45:12.123Z[GMT]'.");
3527
}
3628
}
3729

camunda-carbon-reductor-c8/src/test/java/de/envite/greenbpm/carbonreductorconnector/adapter/in/zeebe/variable/CarbonReductorVariableMapperTest.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
import org.assertj.core.api.SoftAssertions;
77
import org.junit.jupiter.api.Nested;
88
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.params.ParameterizedTest;
10+
import org.junit.jupiter.params.provider.ValueSource;
911

1012
import java.time.*;
1113

1214
import static de.envite.greenbpm.carbonreductorconnector.adapter.in.zeebe.test.utils.TestDataGenerator.*;
15+
import static org.assertj.core.api.AssertionsForClassTypes.assertThatNoException;
1316
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
1417

1518
class CarbonReductorVariableMapperTest {
@@ -42,14 +45,29 @@ void should_map_all_fields() {
4245
softAssertions.assertAll();
4346
}
4447

48+
@ParameterizedTest
49+
@ValueSource(strings = {
50+
"2026-02-06T11:40:39.188Z[GMT]",
51+
"2026-02-06T11:20:15.65Z[GMT]",
52+
"2026-02-06T11:40:39.1Z[GMT]",
53+
"2026-02-06T11:40:39Z[GMT]",
54+
"2026-02-06T11:40:39.188Z"
55+
})
56+
void should_parse_valid_iso_date_times(String isodatetime) {
57+
CarbonReductorInputVariable inputVariables = createInputVariables();
58+
inputVariables.setMilestone(isodatetime);
59+
60+
assertThatNoException().isThrownBy(() -> classUnderTest.mapToDomain(inputVariables));
61+
}
62+
4563
@Test
4664
void should_throw_on_invalid_date() {
4765
CarbonReductorInputVariable inputVariables = createInputVariables();
4866
inputVariables.setMilestone("2020-07-31T14:27:30@Europe/Berlin");
4967

5068
assertThatThrownBy(() -> classUnderTest.mapToDomain(inputVariables))
5169
.isInstanceOf(IllegalArgumentException.class)
52-
.hasMessageStartingWith("Milestone: Unknown date time format (Expected yyyy-MM-ddTHH:mm:ss.SSSX[z])");
70+
.hasMessageMatching("Milestone: Unknown date time format. Expected ISO 8601, e.g. '2024-01-01T11:45:12.123Z[GMT]'.");
5371
}
5472
}
5573

0 commit comments

Comments
 (0)