Skip to content

Commit 5c9c336

Browse files
committed
add Temporal types Instant, LocalTime, OffsetTime, Year, YearMonth and ZonedDateTime
1 parent 2d8a668 commit 5c9c336

2 files changed

Lines changed: 121 additions & 10 deletions

File tree

src/main/java/it/aboutbits/springboot/testing/validation/source/FutureValueSource.java

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@
22

33
import it.aboutbits.springboot.testing.validation.core.ValueSource;
44

5+
import java.time.Instant;
56
import java.time.LocalDate;
67
import java.time.LocalDateTime;
8+
import java.time.LocalTime;
9+
import java.time.Month;
710
import java.time.OffsetDateTime;
11+
import java.time.OffsetTime;
12+
import java.time.Year;
13+
import java.time.YearMonth;
814
import java.time.ZoneOffset;
15+
import java.time.ZonedDateTime;
916
import java.util.HashMap;
1017
import java.util.Map;
1118
import java.util.function.Function;
@@ -14,10 +21,17 @@
1421
public class FutureValueSource implements ValueSource {
1522
private static final Map<Class<?>, Function<Object[], Stream<?>>> TYPE_SOURCES = new HashMap<>();
1623

24+
// https://jakarta.ee/specifications/bean-validation/3.0/apidocs/jakarta/validation/constraints/future
1725
static {
26+
TYPE_SOURCES.put(Instant.class, FutureValueSource::getInstantStream);
27+
TYPE_SOURCES.put(LocalTime.class, FutureValueSource::getLocalTimeStream);
1828
TYPE_SOURCES.put(LocalDate.class, FutureValueSource::getLocalDateStream);
19-
TYPE_SOURCES.put(LocalDateTime.class, FutureValueSource::getLocalDatetimeStream);
29+
TYPE_SOURCES.put(LocalDateTime.class, FutureValueSource::getLocalDateTimeStream);
30+
TYPE_SOURCES.put(OffsetTime.class, FutureValueSource::getOffsetTimeStream);
2031
TYPE_SOURCES.put(OffsetDateTime.class, FutureValueSource::getOffsetDateTimeStream);
32+
TYPE_SOURCES.put(Year.class, FutureValueSource::getYearStream);
33+
TYPE_SOURCES.put(YearMonth.class, FutureValueSource::getYearMonthStream);
34+
TYPE_SOURCES.put(ZonedDateTime.class, FutureValueSource::getZonedDateTimeStream);
2135
}
2236

2337
@SuppressWarnings("unused")
@@ -36,24 +50,66 @@ public <T> Stream<T> values(Class<T> propertyClass, Object... args) {
3650
throw new IllegalArgumentException("Property class not supported!");
3751
}
3852

53+
private static Stream<Instant> getInstantStream(Object[] args) {
54+
var currentInstant = Instant.now();
55+
var largestInstant = Instant.MAX;
56+
57+
return Stream.of(currentInstant.plusSeconds(1L), largestInstant);
58+
}
59+
60+
private static Stream<LocalTime> getLocalTimeStream(Object[] args) {
61+
var currentLocalTime = LocalTime.now();
62+
var largestLocalTime = LocalTime.MAX;
63+
64+
return Stream.of(currentLocalTime.plusSeconds(1L), largestLocalTime);
65+
}
66+
3967
private static Stream<LocalDate> getLocalDateStream(Object[] args) {
4068
var currentDate = LocalDate.now();
4169
var largestDate = LocalDate.MAX;
4270

43-
return Stream.of(currentDate.plusDays(1), largestDate);
71+
return Stream.of(currentDate.plusDays(1L), largestDate);
4472
}
4573

46-
private static Stream<LocalDateTime> getLocalDatetimeStream(Object[] args) {
74+
private static Stream<LocalDateTime> getLocalDateTimeStream(Object[] args) {
4775
var currentDateTime = LocalDateTime.now();
4876
var largestDateTime = LocalDateTime.MAX;
4977

50-
return Stream.of(currentDateTime.plusSeconds(1), largestDateTime);
78+
return Stream.of(currentDateTime.plusSeconds(1L), largestDateTime);
79+
}
80+
81+
private static Stream<OffsetTime> getOffsetTimeStream(Object[] args) {
82+
var currentOffsetTime = OffsetTime.now(ZoneOffset.UTC);
83+
var largestOffsetTime = OffsetTime.MAX;
84+
85+
return Stream.of(currentOffsetTime.plusSeconds(1L), largestOffsetTime);
5186
}
5287

5388
private static Stream<OffsetDateTime> getOffsetDateTimeStream(Object[] args) {
5489
var currentOffsetDateTime = OffsetDateTime.now(ZoneOffset.UTC);
5590
var largestOffsetDateTime = OffsetDateTime.MAX;
5691

57-
return Stream.of(currentOffsetDateTime.plusSeconds(1), largestOffsetDateTime);
92+
return Stream.of(currentOffsetDateTime.plusSeconds(1L), largestOffsetDateTime);
93+
}
94+
95+
private static Stream<Year> getYearStream(Object[] args) {
96+
var currentYear = Year.now();
97+
var largestYear = Year.of(Year.MAX_VALUE);
98+
99+
return Stream.of(currentYear.plusYears(1L), largestYear);
100+
}
101+
102+
private static Stream<YearMonth> getYearMonthStream(Object[] args) {
103+
var currentYearMonth = YearMonth.now();
104+
var largestYearMonth = YearMonth.of(Year.MAX_VALUE, Month.DECEMBER);
105+
106+
return Stream.of(currentYearMonth.plusMonths(1L), largestYearMonth);
107+
}
108+
109+
private static Stream<ZonedDateTime> getZonedDateTimeStream(Object[] args) {
110+
var currentZonedDateTime = ZonedDateTime.now(ZoneOffset.UTC);
111+
var largestZonedDateTime = LocalDateTime.MAX.atZone(ZoneOffset.MIN);
112+
113+
return Stream.of(currentZonedDateTime.plusSeconds(1L), largestZonedDateTime);
58114
}
59115
}

src/main/java/it/aboutbits/springboot/testing/validation/source/PastValueSource.java

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@
22

33
import it.aboutbits.springboot.testing.validation.core.ValueSource;
44

5+
import java.time.Instant;
56
import java.time.LocalDate;
67
import java.time.LocalDateTime;
8+
import java.time.LocalTime;
9+
import java.time.Month;
710
import java.time.OffsetDateTime;
11+
import java.time.OffsetTime;
12+
import java.time.Year;
13+
import java.time.YearMonth;
814
import java.time.ZoneOffset;
15+
import java.time.ZonedDateTime;
916
import java.util.HashMap;
1017
import java.util.Map;
1118
import java.util.function.Function;
@@ -14,10 +21,17 @@
1421
public class PastValueSource implements ValueSource {
1522
private static final Map<Class<?>, Function<Object[], Stream<?>>> TYPE_SOURCES = new HashMap<>();
1623

24+
// https://jakarta.ee/specifications/bean-validation/3.0/apidocs/jakarta/validation/constraints/past
1725
static {
26+
TYPE_SOURCES.put(Instant.class, PastValueSource::getInstantStream);
27+
TYPE_SOURCES.put(LocalTime.class, PastValueSource::getLocalTimeStream);
1828
TYPE_SOURCES.put(LocalDate.class, PastValueSource::getLocalDateStream);
19-
TYPE_SOURCES.put(LocalDateTime.class, PastValueSource::getLocalDatetimeStream);
29+
TYPE_SOURCES.put(LocalDateTime.class, PastValueSource::getLocalDateTimeStream);
30+
TYPE_SOURCES.put(OffsetTime.class, PastValueSource::getOffsetTimeStream);
2031
TYPE_SOURCES.put(OffsetDateTime.class, PastValueSource::getOffsetDateTimeStream);
32+
TYPE_SOURCES.put(Year.class, PastValueSource::getYearStream);
33+
TYPE_SOURCES.put(YearMonth.class, PastValueSource::getYearMonthStream);
34+
TYPE_SOURCES.put(ZonedDateTime.class, PastValueSource::getZonedDateTimeStream);
2135
}
2236

2337
@SuppressWarnings("unused")
@@ -36,25 +50,66 @@ public <T> Stream<T> values(Class<T> propertyClass, Object... args) {
3650
throw new IllegalArgumentException("Property class not supported!");
3751
}
3852

53+
private static Stream<Instant> getInstantStream(Object[] args) {
54+
var currentInstant = Instant.now();
55+
var smallestInstant = Instant.MIN;
56+
57+
return Stream.of(smallestInstant, currentInstant.minusSeconds(1L));
58+
}
59+
60+
private static Stream<LocalTime> getLocalTimeStream(Object[] args) {
61+
var currentLocalTime = LocalTime.now();
62+
var smallestLocalTime = LocalTime.MIN;
63+
64+
return Stream.of(smallestLocalTime, currentLocalTime.minusSeconds(1L));
65+
}
3966

4067
private static Stream<LocalDate> getLocalDateStream(Object[] args) {
4168
var currentDate = LocalDate.now();
4269
var smallestDate = LocalDate.MIN;
4370

44-
return Stream.of(smallestDate, currentDate.minusDays(1));
71+
return Stream.of(smallestDate, currentDate.minusDays(1L));
4572
}
4673

47-
private static Stream<LocalDateTime> getLocalDatetimeStream(Object[] args) {
74+
private static Stream<LocalDateTime> getLocalDateTimeStream(Object[] args) {
4875
var currentDateTime = LocalDateTime.now();
4976
var smallestDateTime = LocalDateTime.MIN;
5077

51-
return Stream.of(smallestDateTime, currentDateTime.minusSeconds(1));
78+
return Stream.of(smallestDateTime, currentDateTime.minusSeconds(1L));
79+
}
80+
81+
private static Stream<OffsetTime> getOffsetTimeStream(Object[] args) {
82+
var currentOffsetTime = OffsetTime.now(ZoneOffset.UTC);
83+
var smallestOffsetTime = OffsetTime.MIN;
84+
85+
return Stream.of(smallestOffsetTime, currentOffsetTime.minusSeconds(1L));
5286
}
5387

5488
private static Stream<OffsetDateTime> getOffsetDateTimeStream(Object[] args) {
5589
var currentOffsetDateTime = OffsetDateTime.now(ZoneOffset.UTC);
5690
var smallestOffsetDateTime = OffsetDateTime.MIN;
5791

58-
return Stream.of(smallestOffsetDateTime, currentOffsetDateTime.minusSeconds(1));
92+
return Stream.of(smallestOffsetDateTime, currentOffsetDateTime.minusSeconds(1L));
93+
}
94+
95+
private static Stream<Year> getYearStream(Object[] args) {
96+
var currentYear = Year.now();
97+
var smallestYear = Year.of(Year.MIN_VALUE);
98+
99+
return Stream.of(smallestYear, currentYear.minusYears(1L));
100+
}
101+
102+
private static Stream<YearMonth> getYearMonthStream(Object[] args) {
103+
var currentYearMonth = YearMonth.now();
104+
var smallestYearMonth = YearMonth.of(Year.MIN_VALUE, Month.JANUARY);
105+
106+
return Stream.of(smallestYearMonth, currentYearMonth.minusMonths(1L));
107+
}
108+
109+
private static Stream<ZonedDateTime> getZonedDateTimeStream(Object[] args) {
110+
var currentZonedDateTime = ZonedDateTime.now(ZoneOffset.UTC);
111+
var smallestZonedDateTime = LocalDateTime.MIN.atZone(ZoneOffset.MAX);
112+
113+
return Stream.of(smallestZonedDateTime, currentZonedDateTime.minusSeconds(1L));
59114
}
60115
}

0 commit comments

Comments
 (0)