Skip to content

Commit

Permalink
feat(impl): [eclipse-tractusx#639] Support any ISO date
Browse files Browse the repository at this point in the history
  • Loading branch information
dsmf committed Jul 23, 2024
1 parent a99eb75 commit 87953c1
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ _**For better traceability add the corresponding GitHub issue number in each cha

### Changed

- The date search operators `AFTER_LOCAL_DATE` and `BEFORE_LOCAL_DATE` for fields `createdOn` and `validUntil` support any ISO date time now (relates to #639 and #750).
- Improved documentation for `GET /irs/policies/paged` endpoint. #639

## [5.4.0] - 2024-07-22
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

/**
* Date utilities.
Expand All @@ -34,11 +36,19 @@ private DateUtils() {
}

public static boolean isDateBefore(final OffsetDateTime dateTime, final String referenceDateString) {
return dateTime.isBefore(toOffsetDateTimeAtStartOfDay(referenceDateString));
if (isDateWithoutTime(referenceDateString)) {
return dateTime.isBefore(toOffsetDateTimeAtStartOfDay(referenceDateString));
} else {
return dateTime.isBefore(OffsetDateTime.parse(referenceDateString));
}
}

public static boolean isDateAfter(final OffsetDateTime dateTime, final String referenceDateString) {
return dateTime.isAfter(toOffsetDateTimeAtEndOfDay(referenceDateString));
if (isDateWithoutTime(referenceDateString)) {
return dateTime.isAfter(toOffsetDateTimeAtEndOfDay(referenceDateString));
} else {
return dateTime.isAfter(OffsetDateTime.parse(referenceDateString));
}
}

public static OffsetDateTime toOffsetDateTimeAtStartOfDay(final String dateString) {
Expand All @@ -48,4 +58,23 @@ public static OffsetDateTime toOffsetDateTimeAtStartOfDay(final String dateStrin
public static OffsetDateTime toOffsetDateTimeAtEndOfDay(final String dateString) {
return LocalDate.parse(dateString).atTime(LocalTime.MAX).atOffset(ZoneOffset.UTC);
}

public static boolean isDateWithoutTime(final String referenceDateString) {
try {
DateTimeFormatter.ofPattern("yyyy-MM-dd").parse(referenceDateString);
return true;
} catch (DateTimeParseException e) {
try {
OffsetDateTime.parse(referenceDateString, DateTimeFormatter.ISO_DATE);
return true;
} catch (DateTimeParseException e2) {
try {
OffsetDateTime.parse(referenceDateString, DateTimeFormatter.ISO_DATE_TIME);
return false;
} catch (DateTimeParseException e3) {
throw new IllegalArgumentException("Invalid date format: " + referenceDateString);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
package org.eclipse.tractusx.irs.policystore.common;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.stream.Stream;

import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -40,11 +43,26 @@ void testIsDateBefore(final OffsetDateTime dateTime, final String referenceDateS
}

static Stream<Arguments> provideDatesForIsDateBefore() {
final OffsetDateTime referenceDateTime = LocalDate.parse("2024-07-05").atStartOfDay().atOffset(ZoneOffset.UTC);
return Stream.of( //
Arguments.of(referenceDateTime, "2024-07-04", false),
Arguments.of(referenceDateTime, "2024-07-05", false),
Arguments.of(referenceDateTime, "2024-07-06", true));
Arguments.of(atStartOfDay("2024-07-05"), "2024-07-04", false), //
Arguments.of(atStartOfDay("2024-07-05"), "2024-07-05", false), //
Arguments.of(atStartOfDay("2024-07-05"), "2024-07-06", true), //
Arguments.of(OffsetDateTime.parse("2024-07-23T15:30:00Z"), "2024-07-23T15:30:00Z", false), //
Arguments.of(OffsetDateTime.parse("2024-07-23T15:30:00Z"), "2024-07-23T15:30:01Z", true), //
Arguments.of(OffsetDateTime.parse("2023-12-01T08:45:00+05:30"), "2023-12-01T08:45:00+05:30", false), //
Arguments.of(OffsetDateTime.parse("2023-12-01T08:45:00+05:30"), "2023-12-01T08:46:01+05:30", true), //
Arguments.of(OffsetDateTime.parse("2022-11-15T22:15:30-04:00"), "2022-11-15T22:15:30-04:00", false), //
Arguments.of(OffsetDateTime.parse("2022-11-15T22:15:30-04:00"), "2022-11-15T22:16:01-04:00", true), //
Arguments.of(OffsetDateTime.parse("2021-06-30T14:00:00.123Z"), "2021-06-30T14:00:00.123Z", false), //
Arguments.of(OffsetDateTime.parse("2021-06-30T14:00:00.123Z"), "2021-06-30T14:00:00.124Z", true), //
Arguments.of(OffsetDateTime.parse("2021-06-29T00:00Z"), "2021-06-29T00:00Z", false), //
Arguments.of(OffsetDateTime.parse("2021-06-29T00:00Z"), "2021-06-30T00:01Z", true) //
);
}

@NotNull
private static OffsetDateTime atStartOfDay(final String date) {
return LocalDate.parse(date).atStartOfDay().atOffset(ZoneOffset.UTC);
}

@ParameterizedTest
Expand All @@ -54,14 +72,48 @@ void testIsDateAfter(final OffsetDateTime dateTime, final String dateString, fin
}

static Stream<Arguments> provideDatesForIsDateAfter() {
final OffsetDateTime referenceDateTime = LocalDate.parse("2023-07-05")
.atTime(LocalTime.MAX)
.atOffset(ZoneOffset.UTC);

return Stream.of( //
Arguments.of(referenceDateTime, "2023-07-04", true),
Arguments.of(referenceDateTime, "2023-07-05", false),
Arguments.of(referenceDateTime, "2023-07-06", false));
Arguments.of(atStartOfDay("2024-07-05"), "2024-07-04", true), //
Arguments.of(atStartOfDay("2024-07-05"), "2024-07-05", false), //
Arguments.of(atStartOfDay("2024-07-05"), "2024-07-06", false), //
Arguments.of(OffsetDateTime.parse("2024-07-23T15:30:00Z"), "2024-07-23T15:30:00Z", false), //
Arguments.of(OffsetDateTime.parse("2024-07-23T15:30:00Z"), "2024-07-23T15:29:59Z", true), //
Arguments.of(OffsetDateTime.parse("2023-12-01T08:45:00+05:30"), "2023-12-01T08:45:00+05:30", false), //
Arguments.of(OffsetDateTime.parse("2023-12-01T08:45:00+05:30"), "2023-12-01T08:44:59+05:30", true), //
Arguments.of(OffsetDateTime.parse("2022-11-15T22:15:30-04:00"), "2022-11-15T22:15:30-04:00", false), //
Arguments.of(OffsetDateTime.parse("2022-11-15T22:15:30-04:00"), "2022-11-15T22:15:29-04:00", true), //
Arguments.of(OffsetDateTime.parse("2021-06-30T14:00:00.123Z"), "2021-06-30T14:00:00.123Z", false), //
Arguments.of(OffsetDateTime.parse("2021-06-30T14:00:00.123Z"), "2021-06-30T14:00:00.122Z", true), //
Arguments.of(OffsetDateTime.parse("2021-06-29T00:00Z"), "2021-06-29T00:00Z", false), //
Arguments.of(OffsetDateTime.parse("2021-06-29T00:00Z"), "2021-06-28T00:01Z", true) //
);
}

@NotNull
private static OffsetDateTime atEndOfDay(final String dateStr) {
return LocalDate.parse(dateStr).atTime(LocalTime.MAX).atOffset(ZoneOffset.UTC);
}

@Test
public void testIsDateWithoutTimeWithDateOnly() {
assertThat(DateUtils.isDateWithoutTime("2023-07-23")).isTrue();
}

@Test
public void testIsDateWithoutTimeWithDateTime() {
assertThat(DateUtils.isDateWithoutTime("2023-07-23T10:15:30+01:00")).isFalse();
}

@Test
public void testIsDateWithoutTimeWithInvalidDate() {
assertThatThrownBy(() -> DateUtils.isDateWithoutTime("invalid-date")).isInstanceOf(
IllegalArgumentException.class).hasMessageContaining("Invalid date format: invalid-date");
}

@Test
public void testIsDateWithoutTimeWithDifferentFormatDateTime() {
assertThat(DateUtils.isDateWithoutTime("2023-07-23T10:15:30Z")).isFalse();
}

}

0 comments on commit 87953c1

Please sign in to comment.