17
17
import java .util .Date ;
18
18
import java .util .List ;
19
19
import java .util .TimeZone ;
20
- import java .util .concurrent .CompletionStage ;
21
20
import 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 ;
27
22
28
23
import org .hibernate .cfg .AvailableSettings ;
29
24
import org .hibernate .cfg .Configuration ;
30
25
31
26
import org .junit .Test ;
32
27
33
28
import 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 ;
34
33
35
-
36
- import static org .hibernate .reactive .util .impl .CompletionStages .loop ;
34
+ import static org .assertj .core .api .Assertions .assertThat ;
37
35
38
36
public class UTCTest extends BaseReactiveTest {
39
37
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
49
39
final Thing thing = new Thing ();
50
40
51
41
@ Override
@@ -62,64 +52,42 @@ protected Configuration constructConfiguration() {
62
52
63
53
@ Test
64
54
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 ();
68
56
testField (
69
57
context ,
70
58
"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 () )
73
61
);
74
62
}
75
63
76
64
@ Test
77
65
public void testCalendar (TestContext context ) {
78
66
thing .calendar = Calendar .getInstance ( TimeZone .getTimeZone ( "UTC" ) );
79
-
80
67
testField (
81
68
context ,
82
69
"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 () )
85
72
);
86
73
}
87
74
88
75
@ Test
89
76
public void testLocalDate (TestContext context ) {
90
77
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 );
98
79
}
99
80
100
81
@ Test
101
82
public void testLocalTime (TestContext context ) {
102
83
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 );
110
85
}
111
86
112
87
@ Test
113
88
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 );
123
91
}
124
92
125
93
@ Test
@@ -131,46 +99,53 @@ public void testOffsetDateTime(TestContext context) {
131
99
testField (
132
100
context ,
133
101
"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 )
139
106
);
140
107
}
141
108
142
109
@ Test
143
110
public void testZonedDateTime (TestContext context ) {
144
111
final ZoneOffset zoneOffset = ZoneOffset .ofHours ( 7 );
145
- thing .zonedDateTime = ZonedDateTime .now ( zoneOffset );
146
-
112
+ thing .zonedDateTime = ZonedDateTime .now ( zoneOffset ).truncatedTo ( ChronoUnit .MILLIS );
147
113
testField (
148
114
context ,
149
115
"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 )
155
119
);
156
120
}
157
121
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 ) {
159
127
test ( context , getMutinySessionFactory ()
160
128
.withTransaction ( session -> session .persist ( thing ) )
161
129
.chain ( () -> getMutinySessionFactory ()
162
130
.withSession ( session -> session .find ( Thing .class , thing .id ) )
163
131
.invoke ( t -> {
164
- context .assertNotNull ( t );
132
+ assertThat ( t )
133
+ .as ( "Entity not found when using id " + thing .id )
134
+ .isNotNull ();
165
135
assertion .accept ( t );
166
136
} )
167
137
)
168
138
.chain ( () -> getMutinySessionFactory ()
169
139
.withSession ( session -> session
170
140
.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
+ } )
174
149
)
175
150
);
176
151
}
0 commit comments