1717import java .util .Date ;
1818import java .util .List ;
1919import java .util .TimeZone ;
20- import java .util .concurrent .CompletionStage ;
2120import java .util .function .Consumer ;
22- import java .util .function .Supplier ;
23- import jakarta .persistence .Column ;
24- import jakarta .persistence .Entity ;
25- import jakarta .persistence .GeneratedValue ;
26- import jakarta .persistence .Id ;
21+ import java .util .function .Function ;
2722
2823import org .hibernate .cfg .AvailableSettings ;
2924import org .hibernate .cfg .Configuration ;
3025
3126import org .junit .Test ;
3227
3328import io .vertx .ext .unit .TestContext ;
29+ import jakarta .persistence .Column ;
30+ import jakarta .persistence .Entity ;
31+ import jakarta .persistence .GeneratedValue ;
32+ import jakarta .persistence .Id ;
3433
35-
36- import static org .hibernate .reactive .util .impl .CompletionStages .loop ;
34+ import static org .assertj .core .api .Assertions .assertThat ;
3735
3836public class UTCTest extends BaseReactiveTest {
3937
40- @ Override
41- public CompletionStage <Void > deleteEntities (Class <?>... entities ) {
42- return getSessionFactory ()
43- .withTransaction ( s -> loop ( entities , entityClass -> s
44- .createQuery ( "from ThingInUTC" , entityClass )
45- .getResultList ()
46- .thenCompose ( list -> loop ( list , entity -> s .remove ( entity ) ) ) ) );
47- }
48-
38+ // Keeps tract of the values we have saved
4939 final Thing thing = new Thing ();
5040
5141 @ Override
@@ -62,64 +52,42 @@ protected Configuration constructConfiguration() {
6252
6353 @ Test
6454 public void testDate (TestContext context ) {
65- thing .date = Calendar .getInstance ( TimeZone .getTimeZone ( "UTC" ) )
66- .getTime ();
67-
55+ thing .date = Calendar .getInstance ( TimeZone .getTimeZone ( "UTC" ) ).getTime ();
6856 testField (
6957 context ,
7058 "date" ,
71- thing ::getDate ,
72- entity -> context . assertEquals ( thing . date , entity . date )
59+ Thing ::getDate ,
60+ entity -> assertThat ( entity . getDate (). toInstant () ). isEqualTo ( thing . getDate (). toInstant () )
7361 );
7462 }
7563
7664 @ Test
7765 public void testCalendar (TestContext context ) {
7866 thing .calendar = Calendar .getInstance ( TimeZone .getTimeZone ( "UTC" ) );
79-
8067 testField (
8168 context ,
8269 "calendar" ,
83- thing ::getCalendar ,
84- entity -> context . assertEquals ( thing . calendar .toInstant (), entity . calendar .toInstant () )
70+ Thing ::getCalendar ,
71+ entity -> assertThat ( entity . getCalendar () .toInstant () ). isEqualTo ( thing . getCalendar () .toInstant () )
8572 );
8673 }
8774
8875 @ Test
8976 public void testLocalDate (TestContext context ) {
9077 thing .localDate = LocalDate .now ();
91-
92- testField (
93- context ,
94- "localDate" ,
95- thing ::getLocalDate ,
96- entity -> context .assertEquals ( thing .localDate , entity .localDate )
97- );
78+ testField ( context , "localDate" , Thing ::getLocalDate );
9879 }
9980
10081 @ Test
10182 public void testLocalTime (TestContext context ) {
10283 thing .localTime = LocalTime .MAX .truncatedTo ( ChronoUnit .SECONDS );
103-
104- testField (
105- context ,
106- "localTime" ,
107- thing ::getLocalTime ,
108- entity -> context .assertEquals ( thing .localTime , entity .localTime )
109- );
84+ testField ( context , "localTime" , Thing ::getLocalTime );
11085 }
11186
11287 @ Test
11388 public void testLocalDateTime (TestContext context ) {
114- thing .localDateTime = LocalDateTime .now ()
115- .truncatedTo ( ChronoUnit .MILLIS );
116-
117- testField (
118- context ,
119- "localDateTime" ,
120- thing ::getLocalDateTime ,
121- entity -> context .assertEquals ( thing .localDateTime , entity .localDateTime )
122- );
89+ thing .localDateTime = LocalDateTime .now ().truncatedTo ( ChronoUnit .MILLIS );
90+ testField ( context , "localDateTime" , Thing ::getLocalDateTime );
12391 }
12492
12593 @ Test
@@ -131,46 +99,53 @@ public void testOffsetDateTime(TestContext context) {
13199 testField (
132100 context ,
133101 "offsetDateTime" ,
134- thing ::getOffsetDateTime ,
135- entity -> {
136- context .assertEquals ( thing .offsetDateTime ,
137- entity .offsetDateTime .toInstant ().atZone ( zoneOffset ).toOffsetDateTime () );
138- }
102+ Thing ::getOffsetDateTime ,
103+ // The value is stored as UTC, so we need to convert it back the original time zone
104+ entity -> assertThat ( entity .getOffsetDateTime ().atZoneSameInstant ( zoneOffset ).toOffsetDateTime () )
105+ .isEqualTo ( thing .offsetDateTime )
139106 );
140107 }
141108
142109 @ Test
143110 public void testZonedDateTime (TestContext context ) {
144111 final ZoneOffset zoneOffset = ZoneOffset .ofHours ( 7 );
145- thing .zonedDateTime = ZonedDateTime .now ( zoneOffset );
146-
112+ thing .zonedDateTime = ZonedDateTime .now ( zoneOffset ).truncatedTo ( ChronoUnit .MILLIS );
147113 testField (
148114 context ,
149115 "zonedDateTime" ,
150- thing ::getZonedDateTime ,
151- // The equals fails on JDK 15+ without the truncated
152- entity -> context .assertEquals (
153- thing .zonedDateTime .truncatedTo ( ChronoUnit .MILLIS ),
154- entity .zonedDateTime .withZoneSameInstant ( zoneOffset ).truncatedTo ( ChronoUnit .MILLIS ) )
116+ Thing ::getZonedDateTime ,
117+ // The value is stored as UTC, so we need to convert it back the original time zone
118+ entity -> assertThat ( entity .getZonedDateTime ().withZoneSameInstant ( zoneOffset ) ).isEqualTo ( thing .zonedDateTime )
155119 );
156120 }
157121
158- private void testField (TestContext context , String columnName , Supplier <?> fieldValue , Consumer <Thing > assertion ) {
122+ private void testField (TestContext context , String columnName , Function <Thing , Object > getFieldValue ) {
123+ testField ( context , columnName , getFieldValue , entity -> assertThat ( getFieldValue .apply ( entity ) ).isEqualTo ( getFieldValue .apply ( thing ) ) );
124+ }
125+
126+ private void testField (TestContext context , String columnName , Function <Thing , ?> fieldValue , Consumer <Thing > assertion ) {
159127 test ( context , getMutinySessionFactory ()
160128 .withTransaction ( session -> session .persist ( thing ) )
161129 .chain ( () -> getMutinySessionFactory ()
162130 .withSession ( session -> session .find ( Thing .class , thing .id ) )
163131 .invoke ( t -> {
164- context .assertNotNull ( t );
132+ assertThat ( t )
133+ .as ( "Entity not found when using id " + thing .id )
134+ .isNotNull ();
165135 assertion .accept ( t );
166136 } )
167137 )
168138 .chain ( () -> getMutinySessionFactory ()
169139 .withSession ( session -> session
170140 .createQuery ( "from ThingInUTC where " + columnName + "=:dt" , Thing .class )
171- .setParameter ( "dt" , fieldValue .get () )
172- .getSingleResult () )
173- .invoke ( assertion )
141+ .setParameter ( "dt" , fieldValue .apply ( thing ) )
142+ .getSingleResultOrNull () )
143+ .invoke ( result -> {
144+ assertThat ( result )
145+ .as ( "No result when querying using filter: " + columnName + " = " + fieldValue .apply ( thing ) )
146+ .isNotNull ();
147+ assertion .accept ( result );
148+ } )
174149 )
175150 );
176151 }
0 commit comments