19
19
20
20
import java .util .Collection ;
21
21
import java .util .Collections ;
22
+ import java .util .Date ;
22
23
import java .util .HashSet ;
23
24
import java .util .List ;
24
25
import java .util .Set ;
42
43
import org .springframework .data .neo4j .config .AbstractNeo4jConfig ;
43
44
import org .springframework .data .neo4j .core .Neo4jOperations ;
44
45
import org .springframework .data .neo4j .core .convert .Neo4jConversions ;
46
+ import org .springframework .data .neo4j .core .support .DateLong ;
45
47
import org .springframework .data .neo4j .integration .shared .conversion .PersonWithCustomId ;
46
48
import org .springframework .data .neo4j .integration .shared .conversion .ThingWithCustomTypes ;
47
49
import org .springframework .data .neo4j .repository .Neo4jRepository ;
@@ -87,7 +89,8 @@ void setupData() {
87
89
try (Session session = driver .session ()) {
88
90
session .writeTransaction (transaction -> {
89
91
transaction .run ("MATCH (n) detach delete n" ).consume ();
90
- transaction .run ("CREATE (:CustomTypes{customType:'XYZ'})" ).consume ();
92
+ transaction .run ("CREATE (:CustomTypes{customType:'XYZ', dateAsLong: 1630311077418})" ).consume ();
93
+ transaction .run ("CREATE (:CustomTypes{customType:'ABC'})" ).consume ();
91
94
return null ;
92
95
});
93
96
}
@@ -118,7 +121,7 @@ void deleteAllByCustomId() {
118
121
.limit (2 )
119
122
.collect (Collectors .toList ());
120
123
try (
121
- Session session = driver .session (getSessionConfig ());
124
+ Session session = driver .session (getSessionConfig ())
122
125
) {
123
126
ids .forEach (id -> session .writeTransaction (createPersonWithCustomId (id )));
124
127
}
@@ -135,7 +138,9 @@ void deleteAllByCustomId() {
135
138
@ Test
136
139
void findByConvertedCustomType (@ Autowired EntityWithCustomTypePropertyRepository repository ) {
137
140
138
- assertThat (repository .findByCustomType (ThingWithCustomTypes .CustomType .of ("XYZ" ))).isNotNull ();
141
+ ThingWithCustomTypes xyz = repository .findByCustomType (ThingWithCustomTypes .CustomType .of ("XYZ" ));
142
+ assertThat (xyz ).isNotNull ();
143
+ assertThat (xyz .getDateAsLong ()).isNotNull ();
139
144
}
140
145
141
146
@ Test
@@ -144,6 +149,31 @@ void findByConvertedCustomTypeWithCustomQuery(@Autowired EntityWithCustomTypePro
144
149
assertThat (repository .findByCustomTypeCustomQuery (ThingWithCustomTypes .CustomType .of ("XYZ" ))).isNotNull ();
145
150
}
146
151
152
+ @ Test // GH-2365
153
+ void customConverterShouldBeApplied (@ Autowired EntityWithCustomTypePropertyRepository repository ) {
154
+
155
+ ThingWithCustomTypes xyz = repository .defaultAttributeWithCoalesce (ThingWithCustomTypes .CustomType .of ("XYZ" ));
156
+ assertThat (xyz ).isNotNull ();
157
+ assertThat (xyz .getDateAsLong ()).isInSameDayAs ("2021-08-30" );
158
+ }
159
+
160
+ @ Test // GH-2365
161
+ void customConverterShouldBeAppliedWithCoalesce (@ Autowired EntityWithCustomTypePropertyRepository repository ) {
162
+
163
+ ThingWithCustomTypes abc = repository .defaultAttributeWithCoalesce (ThingWithCustomTypes .CustomType .of ("ABC" ));
164
+ assertThat (abc ).isNotNull ();
165
+ assertThat (abc .getDateAsLong ()).isInSameDayAs ("2021-09-21" );
166
+ }
167
+
168
+ @ Test // GH-2365
169
+ void converterAndProjection (@ Autowired EntityWithCustomTypePropertyRepository repository ) {
170
+
171
+ ThingWithCustomTypesProjection projection = repository .converterOnProjection (ThingWithCustomTypes .CustomType .of ("XYZ" ));
172
+ assertThat (projection ).isNotNull ();
173
+ assertThat (projection .dateAsLong ).isInSameDayAs ("2021-08-30" );
174
+ assertThat (projection .modified ).isInSameDayAs ("2021-09-21" );
175
+ }
176
+
147
177
@ Test
148
178
void findByConvertedCustomTypeWithSpELPropertyAccessQuery (
149
179
@ Autowired EntityWithCustomTypePropertyRepository repository ) {
@@ -164,13 +194,27 @@ void findByConvertedDifferentTypeWithSpELObjectQuery(@Autowired EntityWithCustom
164
194
assertThat (repository .findByDifferentTypeCustomQuery (ThingWithCustomTypes .DifferentType .of ("XYZ" ))).isNotNull ();
165
195
}
166
196
197
+ static class ThingWithCustomTypesProjection {
198
+
199
+ Date dateAsLong ;
200
+
201
+ @ DateLong
202
+ Date modified ;
203
+ }
204
+
167
205
interface EntityWithCustomTypePropertyRepository extends Neo4jRepository <ThingWithCustomTypes , Long > {
168
206
169
207
ThingWithCustomTypes findByCustomType (ThingWithCustomTypes .CustomType customType );
170
208
171
209
@ Query ("MATCH (c:CustomTypes) WHERE c.customType = $customType return c" )
172
210
ThingWithCustomTypes findByCustomTypeCustomQuery (@ Param ("customType" ) ThingWithCustomTypes .CustomType customType );
173
211
212
+ @ Query ("MATCH (c:CustomTypes) WHERE c.customType = $customType return c, 1632200400000 as modified" )
213
+ ThingWithCustomTypesProjection converterOnProjection (@ Param ("customType" ) ThingWithCustomTypes .CustomType customType );
214
+
215
+ @ Query ("MATCH (thingWithCustomTypes:`CustomTypes`) WHERE thingWithCustomTypes.customType = $0 RETURN thingWithCustomTypes{.customType, dateAsLong: COALESCE(thingWithCustomTypes.dateAsLong, 1632200400000), .dateAsString, .id, __nodeLabels__: labels(thingWithCustomTypes), __internalNeo4jId__: id(thingWithCustomTypes)}" )
216
+ ThingWithCustomTypes defaultAttributeWithCoalesce (@ Param ("customType" ) ThingWithCustomTypes .CustomType customType );
217
+
174
218
@ Query ("MATCH (c:CustomTypes) WHERE c.customType = $differentType return c" )
175
219
ThingWithCustomTypes findByDifferentTypeCustomQuery (
176
220
@ Param ("differentType" ) ThingWithCustomTypes .DifferentType differentType );
0 commit comments