22
33import it .aboutbits .springboot .testing .validation .core .ValueSource ;
44
5+ import java .time .Instant ;
56import java .time .LocalDate ;
67import java .time .LocalDateTime ;
8+ import java .time .LocalTime ;
9+ import java .time .Month ;
710import java .time .OffsetDateTime ;
11+ import java .time .OffsetTime ;
12+ import java .time .Year ;
13+ import java .time .YearMonth ;
814import java .time .ZoneOffset ;
15+ import java .time .ZonedDateTime ;
916import java .util .HashMap ;
1017import java .util .Map ;
1118import java .util .function .Function ;
1421public 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