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 @@ -14,24 +14,16 @@
import java.time.format.DateTimeParseException;
import java.util.Optional;

import static java.lang.String.format;
import static java.time.format.DateTimeFormatter.ofPattern;
import static java.time.format.DateTimeFormatter.ISO_DATE_TIME;

@Component
public class CarbonReductorVariableMapper {

// Sample Date: 2023-09-08T12:58:20.766Z[GMT]
private static final String DATE_TIME_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSX'['z']'";

private static OffsetDateTime parseDateString(String value) {
try {
return OffsetDateTime.parse(value, ofPattern(DATE_TIME_PATTERN));
return OffsetDateTime.parse(value, ISO_DATE_TIME);
} catch (DateTimeParseException exception) {
throw new IllegalArgumentException(
format("Milestone: Unknown date time format (Expected %s)",
DATE_TIME_PATTERN.replace("'", "")
)
);
throw new IllegalArgumentException("Milestone: Unknown date time format. Expected ISO 8601, e.g. '2024-01-01T11:45:12.123Z[GMT]'.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.time.*;

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

class CarbonReductorVariableMapperTest {
Expand Down Expand Up @@ -42,14 +45,29 @@ void should_map_all_fields() {
softAssertions.assertAll();
}

@ParameterizedTest
@ValueSource(strings = {
"2026-02-06T11:40:39.188Z[GMT]",
"2026-02-06T11:20:15.65Z[GMT]",
"2026-02-06T11:40:39.1Z[GMT]",
"2026-02-06T11:40:39Z[GMT]",
"2026-02-06T11:40:39.188Z"
})
Comment thread
lwluc marked this conversation as resolved.
void should_parse_valid_iso_date_times(String isodatetime) {
CarbonReductorInputVariable inputVariables = createInputVariables();
inputVariables.setMilestone(isodatetime);

assertThatNoException().isThrownBy(() -> classUnderTest.mapToDomain(inputVariables));
}

@Test
void should_throw_on_invalid_date() {
CarbonReductorInputVariable inputVariables = createInputVariables();
inputVariables.setMilestone("2020-07-31T14:27:30@Europe/Berlin");

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

Expand Down