17
17
18
18
import static org .assertj .core .api .Assertions .assertThat ;
19
19
20
+ import java .nio .charset .Charset ;
21
+ import java .nio .charset .StandardCharsets ;
20
22
import java .time .OffsetDateTime ;
23
+ import java .time .OffsetTime ;
24
+ import java .util .List ;
21
25
22
26
import org .junit .jupiter .api .Test ;
23
27
28
+ import org .springframework .beans .BeansException ;
24
29
import org .springframework .beans .factory .annotation .Autowired ;
30
+ import org .springframework .beans .factory .config .BeanPostProcessor ;
25
31
import org .springframework .boot .SpringBootConfiguration ;
26
32
import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
27
33
import org .springframework .boot .autoconfigure .data .redis .RedisProperties ;
28
34
import org .springframework .boot .test .autoconfigure .data .redis .DataRedisTest ;
29
35
import org .springframework .context .annotation .Bean ;
36
+ import org .springframework .core .convert .converter .Converter ;
30
37
import org .springframework .data .annotation .Id ;
31
38
import org .springframework .data .redis .connection .RedisConfiguration ;
32
39
import org .springframework .data .redis .core .RedisHash ;
40
+ import org .springframework .data .redis .core .convert .RedisCustomConversions ;
33
41
import org .springframework .data .redis .repository .configuration .EnableRedisRepositories ;
34
42
import org .springframework .data .repository .CrudRepository ;
35
43
import org .springframework .lang .NonNull ;
55
63
* @see org.springframework.test.context.ContextConfiguration
56
64
* @see io.vmware.spring.data.redis.tests.AbstractRedisIntegrationTests
57
65
* @see io.vmware.spring.data.redis.tests.repository.RedisRepositoryWithEntityHavingOffsetDateTimePropertyIntegrationTests.RedisTestConfiguration
66
+ * @see <a href="https://github.com/spring-projects/spring-data-redis/issues/2677">Jsr310Converters does not contain converters for OffsetTime and OffsetDateTime</a>
58
67
* @since 0.1.0
59
68
*/
60
69
@ Getter
@@ -72,8 +81,9 @@ public class RedisRepositoryWithEntityHavingOffsetDateTimePropertyIntegrationTes
72
81
void userRepositorySaveAndFindSuccessful () {
73
82
74
83
OffsetDateTime lastAccessed = OffsetDateTime .now ();
84
+ OffsetTime timeToLive = OffsetTime .now ().plusMinutes (5 );
75
85
76
- User jonDoe = User .as ("Jon Doe" ).lastAccessed (lastAccessed );
86
+ User jonDoe = User .as ("Jon Doe" ).lastAccessed (lastAccessed ). timeToLive ( timeToLive ) ;
77
87
78
88
getUserRepository ().save (jonDoe );
79
89
@@ -83,6 +93,7 @@ void userRepositorySaveAndFindSuccessful() {
83
93
assertThat (loadedUser ).isNotSameAs (jonDoe );
84
94
assertThat (loadedUser .getName ()).isEqualTo ("Jon Doe" );
85
95
assertThat (loadedUser .getLastAccessed ()).isEqualTo (lastAccessed );
96
+ assertThat (loadedUser .getTimeToLive ()).isEqualTo (timeToLive );
86
97
}
87
98
88
99
@ SpringBootConfiguration
@@ -94,6 +105,78 @@ static class RedisTestConfiguration {
94
105
RedisConfiguration redisConfiguration (RedisProperties redisProperties ) {
95
106
return redisStandaloneConfiguration (redisProperties );
96
107
}
108
+
109
+ @ Bean
110
+ BeanPostProcessor redisCustomConversionsBeanPostProcess () {
111
+
112
+ return new BeanPostProcessor () {
113
+
114
+ @ Override
115
+ public Object postProcessBeforeInitialization (@ NonNull Object bean , @ NonNull String beanName ) throws BeansException {
116
+
117
+ return bean instanceof RedisCustomConversions
118
+ ? new RedisCustomConversions (ApplicationConverters .list ())
119
+ : bean ;
120
+ }
121
+ };
122
+ }
123
+ }
124
+
125
+ static abstract class ApplicationConverters {
126
+
127
+ static List <?> list () {
128
+ return List .of (
129
+ new BytesToOffsetDateTimeConverter (),
130
+ new BytesToOffsetTimeConverter (),
131
+ new OffsetDateTimeToBytesConverter (),
132
+ new OffsetTimeToBytesConverter ()
133
+ );
134
+ }
135
+
136
+ static class StringBasedConverter {
137
+
138
+ static final Charset CHARSET = StandardCharsets .UTF_8 ;
139
+
140
+ byte [] fromString (String source ) {
141
+ return source .getBytes (CHARSET );
142
+ }
143
+
144
+ String toString (byte [] bytes ) {
145
+ return new String (bytes , CHARSET );
146
+ }
147
+ }
148
+
149
+ static class BytesToOffsetTimeConverter extends StringBasedConverter implements Converter <byte [], OffsetTime > {
150
+
151
+ @ Override
152
+ public OffsetTime convert (@ NonNull byte [] source ) {
153
+ return OffsetTime .parse (toString (source ));
154
+ }
155
+ }
156
+
157
+ static class BytesToOffsetDateTimeConverter extends StringBasedConverter implements Converter <byte [], OffsetDateTime > {
158
+
159
+ @ Override
160
+ public OffsetDateTime convert (@ NonNull byte [] source ) {
161
+ return OffsetDateTime .parse (toString (source ));
162
+ }
163
+ }
164
+
165
+ static class OffsetDateTimeToBytesConverter extends StringBasedConverter implements Converter <OffsetDateTime , byte []> {
166
+
167
+ @ Override
168
+ public byte [] convert (OffsetDateTime source ) {
169
+ return fromString (source .toString ());
170
+ }
171
+ }
172
+
173
+ static class OffsetTimeToBytesConverter extends StringBasedConverter implements Converter <OffsetTime , byte []> {
174
+
175
+ @ Override
176
+ public byte [] convert (OffsetTime source ) {
177
+ return fromString (source .toString ());
178
+ }
179
+ }
97
180
}
98
181
99
182
@ Getter
@@ -107,11 +190,18 @@ static class User {
107
190
108
191
private OffsetDateTime lastAccessed ;
109
192
193
+ private OffsetTime timeToLive ;
194
+
110
195
public @ NonNull User lastAccessed (@ Nullable OffsetDateTime dateTime ) {
111
196
this .lastAccessed = dateTime ;
112
197
return this ;
113
198
}
114
199
200
+ public @ NonNull User timeToLive (@ Nullable OffsetTime time ) {
201
+ this .timeToLive = time ;
202
+ return this ;
203
+ }
204
+
115
205
@ Override
116
206
public String toString () {
117
207
return getName ();
0 commit comments