23
23
import java .util .Optional ;
24
24
25
25
// end::faq.template-imperative-pt1[]
26
- import org .junit .jupiter .api .Disabled ;
26
+ import org .junit .jupiter .api .BeforeEach ;
27
27
// tag::faq.template-imperative-pt1[]
28
28
import org .junit .jupiter .api .Test ;
29
+ // end::faq.template-imperative-pt1[]
30
+ import org .neo4j .driver .Driver ;
31
+ // tag::faq.template-imperative-pt1[]
29
32
import org .springframework .beans .factory .annotation .Autowired ;
33
+ // end::faq.template-imperative-pt1[]
34
+ import org .springframework .context .annotation .Bean ;
35
+ import org .springframework .context .annotation .Configuration ;
36
+ import org .springframework .data .neo4j .core .DatabaseSelectionProvider ;
37
+ // tag::faq.template-imperative-pt1[]
30
38
import org .springframework .data .neo4j .core .Neo4jTemplate ;
39
+ // end::faq.template-imperative-pt1[]
40
+ import org .springframework .data .neo4j .core .transaction .Neo4jBookmarkManager ;
41
+ import org .springframework .data .neo4j .core .transaction .Neo4jTransactionManager ;
42
+ // tag::faq.template-imperative-pt1[]
31
43
import org .springframework .data .neo4j .documentation .domain .MovieEntity ;
32
44
import org .springframework .data .neo4j .documentation .domain .PersonEntity ;
33
45
import org .springframework .data .neo4j .documentation .domain .Roles ;
46
+ // end::faq.template-imperative-pt1[]
47
+ import org .springframework .data .neo4j .repository .config .EnableNeo4jRepositories ;
48
+ import org .springframework .data .neo4j .test .BookmarkCapture ;
49
+ import org .springframework .data .neo4j .test .Neo4jExtension ;
50
+ import org .springframework .data .neo4j .test .Neo4jImperativeTestConfiguration ;
51
+ import org .springframework .data .neo4j .test .Neo4jIntegrationTest ;
52
+ import org .springframework .transaction .PlatformTransactionManager ;
53
+ import org .springframework .transaction .annotation .EnableTransactionManagement ;
54
+ // tag::faq.template-imperative-pt1[]
34
55
35
56
// end::faq.template-imperative-pt1[]
36
57
37
58
/**
38
59
* @author Michael J. Simons
39
60
*/
40
- @ Disabled
61
+ @ Neo4jIntegrationTest
41
62
// tag::faq.template-imperative-pt2[]
42
63
public class TemplateExampleTest {
43
64
65
+ // end::faq.template-imperative-pt2[]
66
+
67
+ protected static Neo4jExtension .Neo4jConnectionSupport neo4jConnectionSupport ;
68
+
69
+ @ BeforeEach
70
+ void setup (@ Autowired Driver driver , @ Autowired BookmarkCapture bookmarkCapture ) {
71
+ try (var session = driver .session (bookmarkCapture .createSessionConfig ()); var transaction = session .beginTransaction ()) {
72
+ transaction .run ("MATCH (n) detach delete n" ).consume ();
73
+ transaction .commit ();
74
+ bookmarkCapture .seedWith (session .lastBookmarks ());
75
+ }
76
+ }
77
+
78
+ // tag::faq.template-imperative-pt2[]
44
79
@ Test
45
80
void shouldSaveAndReadEntities (@ Autowired Neo4jTemplate neo4jTemplate ) {
46
81
@@ -53,12 +88,45 @@ void shouldSaveAndReadEntities(@Autowired Neo4jTemplate neo4jTemplate) {
53
88
movie .getActorsAndRoles ().add (roles1 );
54
89
movie .getActorsAndRoles ().add (roles2 );
55
90
56
- neo4jTemplate .save (movie );
91
+ MovieEntity result = neo4jTemplate .save (movie );
92
+ // end::mapping.relationship.properties[]
93
+ assertThat (result .getActorsAndRoles ()).allSatisfy (relationship -> assertThat (relationship .getId ()).isNotNull ());
94
+ // tag::mapping.relationship.properties[]
57
95
58
96
Optional <PersonEntity > person = neo4jTemplate .findById ("Dean Jones" , PersonEntity .class );
59
97
assertThat (person ).map (PersonEntity ::getBorn ).hasValue (1931 );
60
98
61
99
assertThat (neo4jTemplate .count (PersonEntity .class )).isEqualTo (2L );
62
100
}
101
+
102
+ // end::faq.template-imperative-pt2[]
103
+ @ Configuration
104
+ @ EnableTransactionManagement
105
+ @ EnableNeo4jRepositories (considerNestedRepositories = true )
106
+ static class Config extends Neo4jImperativeTestConfiguration {
107
+
108
+ @ Bean
109
+ public Driver driver () {
110
+ return neo4jConnectionSupport .getDriver ();
111
+ }
112
+
113
+ @ Bean
114
+ public BookmarkCapture bookmarkCapture () {
115
+ return new BookmarkCapture ();
116
+ }
117
+
118
+ @ Override
119
+ public PlatformTransactionManager transactionManager (Driver driver , DatabaseSelectionProvider databaseNameProvider ) {
120
+
121
+ BookmarkCapture bookmarkCapture = bookmarkCapture ();
122
+ return new Neo4jTransactionManager (driver , databaseNameProvider , Neo4jBookmarkManager .create (bookmarkCapture ));
123
+ }
124
+
125
+ @ Override
126
+ public boolean isCypher5Compatible () {
127
+ return neo4jConnectionSupport .isCypher5SyntaxCompatible ();
128
+ }
129
+ }
130
+ // tag::faq.template-imperative-pt2[]
63
131
}
64
132
// end::faq.template-imperative-pt2[]
0 commit comments