15
15
*/
16
16
package org .springframework .data .neo4j .integration .imperative ;
17
17
18
- import static java .util .Collections .*;
19
- import static org .assertj .core .api .Assertions .*;
18
+ import static java .util .Collections .singletonList ;
19
+ import static java .util .Collections .singletonMap ;
20
+ import static org .assertj .core .api .Assertions .assertThat ;
20
21
21
22
import java .util .Arrays ;
22
23
import java .util .Collection ;
23
24
import java .util .HashMap ;
24
25
import java .util .List ;
25
26
import java .util .Map ;
26
27
import java .util .Optional ;
28
+ import java .util .concurrent .atomic .AtomicLong ;
27
29
import java .util .function .Function ;
30
+ import java .util .stream .Collectors ;
31
+ import java .util .stream .Stream ;
28
32
29
33
import org .junit .jupiter .api .BeforeEach ;
30
34
import org .junit .jupiter .api .Test ;
38
42
import org .neo4j .driver .Session ;
39
43
import org .neo4j .driver .SessionConfig ;
40
44
import org .neo4j .driver .Transaction ;
45
+ import org .neo4j .driver .TransactionWork ;
41
46
import org .neo4j .driver .Value ;
42
47
import org .neo4j .driver .Values ;
48
+ import org .neo4j .driver .summary .ResultSummary ;
43
49
import org .springframework .beans .factory .annotation .Autowired ;
44
50
import org .springframework .context .annotation .Bean ;
45
51
import org .springframework .context .annotation .Configuration ;
46
52
import org .springframework .data .neo4j .config .AbstractNeo4jConfig ;
47
53
import org .springframework .data .neo4j .core .Neo4jOperations ;
54
+ import org .springframework .data .neo4j .core .convert .Neo4jConversions ;
48
55
import org .springframework .data .neo4j .integration .shared .PersonWithAllConstructor ;
56
+ import org .springframework .data .neo4j .integration .shared .PersonWithCustomId ;
49
57
import org .springframework .data .neo4j .integration .shared .ThingWithGeneratedId ;
50
58
import org .springframework .data .neo4j .test .Neo4jExtension .Neo4jConnectionSupport ;
51
59
import org .springframework .data .neo4j .test .Neo4jIntegrationTest ;
54
62
/**
55
63
* @author Gerrit Meier
56
64
* @author Michael J. Simons
65
+ * @author Rosetta Roberts
57
66
*/
58
67
@ Neo4jIntegrationTest
59
68
class Neo4jOperationsIT {
@@ -65,6 +74,7 @@ class Neo4jOperationsIT {
65
74
private final Driver driver ;
66
75
private final Neo4jOperations neo4jOperations ;
67
76
77
+ private final AtomicLong customIdValueGenerator = new AtomicLong ();
68
78
private Long person1Id ;
69
79
private Long person2Id ;
70
80
@@ -81,23 +91,25 @@ class Neo4jOperationsIT {
81
91
* @return The session config used for verification methods.
82
92
*/
83
93
SessionConfig getSessionConfig () {
84
-
85
94
return SessionConfig .defaultConfig ();
86
95
}
87
96
88
97
@ BeforeEach
89
98
void setupData () {
90
99
91
- Transaction transaction = driver .session (getSessionConfig ()).beginTransaction ();
92
- transaction .run ("MATCH (n) detach delete n" );
100
+ try (
101
+ Session session = driver .session (getSessionConfig ());
102
+ Transaction transaction = session .beginTransaction ();
103
+ ) {
104
+ transaction .run ("MATCH (n) detach delete n" );
93
105
94
- person1Id = transaction .run ("CREATE (n:PersonWithAllConstructor) SET n.name = $name RETURN id(n)" ,
95
- Values .parameters ("name" , TEST_PERSON1_NAME )).next ().get (0 ).asLong ();
96
- person2Id = transaction .run ("CREATE (n:PersonWithAllConstructor) SET n.name = $name RETURN id(n)" ,
97
- Values .parameters ("name" , TEST_PERSON2_NAME )).next ().get (0 ).asLong ();
106
+ person1Id = transaction .run ("CREATE (n:PersonWithAllConstructor) SET n.name = $name RETURN id(n) AS id " ,
107
+ Values .parameters ("name" , TEST_PERSON1_NAME )).single ().get ("id" ).asLong ();
108
+ person2Id = transaction .run ("CREATE (n:PersonWithAllConstructor) SET n.name = $name RETURN id(n) AS id " ,
109
+ Values .parameters ("name" , TEST_PERSON2_NAME )).single ().get ("id" ).asLong ();
98
110
99
- transaction .commit ();
100
- transaction . close ();
111
+ transaction .commit ();
112
+ }
101
113
}
102
114
103
115
@ Test
@@ -157,8 +169,9 @@ void findAllWithStatementAndParameters() {
157
169
Statement statement = Cypher .match (node ).where (node .property ("name" ).isEqualTo (Cypher .parameter ("name" )))
158
170
.returning (node ).build ();
159
171
160
- List <PersonWithAllConstructor > people = neo4jOperations .findAll (statement , singletonMap ("name" , TEST_PERSON1_NAME ),
161
- PersonWithAllConstructor .class );
172
+ List <PersonWithAllConstructor > people = neo4jOperations
173
+ .findAll (statement , singletonMap ("name" , TEST_PERSON1_NAME ),
174
+ PersonWithAllConstructor .class );
162
175
163
176
assertThat (people ).hasSize (1 );
164
177
}
@@ -276,15 +289,67 @@ void deleteAllById() {
276
289
}
277
290
}
278
291
292
+ TransactionWork <ResultSummary > createPersonWithCustomId (PersonWithCustomId .PersonId assignedId ) {
293
+
294
+ return tx -> tx .run ("CREATE (n:PersonWithCustomId) SET n.id = $id " ,
295
+ Values .parameters ("id" , assignedId .getId ())).consume ();
296
+ }
297
+
298
+ @ Test
299
+ void deleteByCustomId () {
300
+
301
+ PersonWithCustomId .PersonId id = new PersonWithCustomId .PersonId (customIdValueGenerator .incrementAndGet ());
302
+ try (Session session = driver .session (getSessionConfig ())) {
303
+ session .writeTransaction (createPersonWithCustomId (id ));
304
+ }
305
+
306
+ assertThat (neo4jOperations .count (PersonWithCustomId .class )).isEqualTo (1L );
307
+ neo4jOperations .deleteById (id , PersonWithCustomId .class );
308
+
309
+ try (Session session = driver .session (getSessionConfig ())) {
310
+ Result result = session .run ("MATCH (p:PersonWithCustomId) return count(p) as count" );
311
+ assertThat (result .single ().get ("count" ).asLong ()).isEqualTo (0 );
312
+ }
313
+ }
314
+
315
+ @ Test
316
+ void deleteAllByCustomId () {
317
+
318
+ List <PersonWithCustomId .PersonId > ids = Stream .generate (customIdValueGenerator ::incrementAndGet )
319
+ .map (PersonWithCustomId .PersonId ::new )
320
+ .limit (2 )
321
+ .collect (Collectors .toList ());
322
+ try (
323
+ Session session = driver .session (getSessionConfig ());
324
+ ) {
325
+ ids .forEach (id -> session .writeTransaction (createPersonWithCustomId (id )));
326
+ }
327
+
328
+ assertThat (neo4jOperations .count (PersonWithCustomId .class )).isEqualTo (2L );
329
+ neo4jOperations .deleteAllById (ids , PersonWithCustomId .class );
330
+
331
+ try (Session session = driver .session (getSessionConfig ())) {
332
+ Result result = session .run ("MATCH (p:PersonWithCustomId) return count(p) as count" );
333
+ assertThat (result .single ().get ("count" ).asLong ()).isEqualTo (0 );
334
+ }
335
+ }
336
+
279
337
@ Configuration
280
338
@ EnableTransactionManagement
281
339
static class Config extends AbstractNeo4jConfig {
282
340
283
341
@ Bean
342
+ @ Override
284
343
public Driver driver () {
285
344
return neo4jConnectionSupport .getDriver ();
286
345
}
287
346
347
+ @ Bean
348
+ @ Override
349
+ public Neo4jConversions neo4jConversions () {
350
+ return new Neo4jConversions (singletonList (new PersonWithCustomId .CustomPersonIdConverter ()));
351
+ }
352
+
288
353
@ Override // needed here because there is no implicit registration of entities upfront some methods under test
289
354
protected Collection <String > getMappingBasePackages () {
290
355
return singletonList (PersonWithAllConstructor .class .getPackage ().getName ());
0 commit comments