1919
2020import java .util .Collection ;
2121import java .util .Collections ;
22+ import java .util .Date ;
2223import java .util .HashSet ;
2324import java .util .List ;
2425import java .util .Set ;
4243import org .springframework .data .neo4j .config .AbstractNeo4jConfig ;
4344import org .springframework .data .neo4j .core .Neo4jOperations ;
4445import org .springframework .data .neo4j .core .convert .Neo4jConversions ;
46+ import org .springframework .data .neo4j .core .support .DateLong ;
4547import org .springframework .data .neo4j .integration .shared .conversion .PersonWithCustomId ;
4648import org .springframework .data .neo4j .integration .shared .conversion .ThingWithCustomTypes ;
4749import org .springframework .data .neo4j .repository .Neo4jRepository ;
@@ -87,7 +89,8 @@ void setupData() {
8789 try (Session session = driver .session ()) {
8890 session .writeTransaction (transaction -> {
8991 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 ();
9194 return null ;
9295 });
9396 }
@@ -118,7 +121,7 @@ void deleteAllByCustomId() {
118121 .limit (2 )
119122 .collect (Collectors .toList ());
120123 try (
121- Session session = driver .session (getSessionConfig ());
124+ Session session = driver .session (getSessionConfig ())
122125 ) {
123126 ids .forEach (id -> session .writeTransaction (createPersonWithCustomId (id )));
124127 }
@@ -135,7 +138,9 @@ void deleteAllByCustomId() {
135138 @ Test
136139 void findByConvertedCustomType (@ Autowired EntityWithCustomTypePropertyRepository repository ) {
137140
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 ();
139144 }
140145
141146 @ Test
@@ -144,6 +149,31 @@ void findByConvertedCustomTypeWithCustomQuery(@Autowired EntityWithCustomTypePro
144149 assertThat (repository .findByCustomTypeCustomQuery (ThingWithCustomTypes .CustomType .of ("XYZ" ))).isNotNull ();
145150 }
146151
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+
147177 @ Test
148178 void findByConvertedCustomTypeWithSpELPropertyAccessQuery (
149179 @ Autowired EntityWithCustomTypePropertyRepository repository ) {
@@ -164,13 +194,27 @@ void findByConvertedDifferentTypeWithSpELObjectQuery(@Autowired EntityWithCustom
164194 assertThat (repository .findByDifferentTypeCustomQuery (ThingWithCustomTypes .DifferentType .of ("XYZ" ))).isNotNull ();
165195 }
166196
197+ static class ThingWithCustomTypesProjection {
198+
199+ Date dateAsLong ;
200+
201+ @ DateLong
202+ Date modified ;
203+ }
204+
167205 interface EntityWithCustomTypePropertyRepository extends Neo4jRepository <ThingWithCustomTypes , Long > {
168206
169207 ThingWithCustomTypes findByCustomType (ThingWithCustomTypes .CustomType customType );
170208
171209 @ Query ("MATCH (c:CustomTypes) WHERE c.customType = $customType return c" )
172210 ThingWithCustomTypes findByCustomTypeCustomQuery (@ Param ("customType" ) ThingWithCustomTypes .CustomType customType );
173211
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+
174218 @ Query ("MATCH (c:CustomTypes) WHERE c.customType = $differentType return c" )
175219 ThingWithCustomTypes findByDifferentTypeCustomQuery (
176220 @ Param ("differentType" ) ThingWithCustomTypes .DifferentType differentType );
0 commit comments