|
22 | 22 | import org.junit.Test;
|
23 | 23 |
|
24 | 24 | import java.io.File;
|
| 25 | +import java.util.List; |
25 | 26 |
|
| 27 | +import org.neo4j.driver.internal.spi.Logger; |
| 28 | +import org.neo4j.driver.internal.spi.Logging; |
26 | 29 | import org.neo4j.driver.v1.Config;
|
27 | 30 | import org.neo4j.driver.v1.Driver;
|
28 | 31 | import org.neo4j.driver.v1.GraphDatabase;
|
| 32 | +import org.neo4j.driver.v1.Notification; |
| 33 | +import org.neo4j.driver.v1.Pair; |
| 34 | +import org.neo4j.driver.v1.Record; |
29 | 35 | import org.neo4j.driver.v1.ResultCursor;
|
| 36 | +import org.neo4j.driver.v1.ResultSummary; |
30 | 37 | import org.neo4j.driver.v1.Session;
|
31 | 38 | import org.neo4j.driver.v1.Transaction;
|
| 39 | +import org.neo4j.driver.v1.Value; |
32 | 40 | import org.neo4j.driver.v1.Values;
|
33 | 41 | import org.neo4j.driver.v1.util.StdIOCapture;
|
34 | 42 | import org.neo4j.driver.v1.util.TestNeo4j;
|
35 | 43 |
|
36 | 44 | import static java.util.Arrays.asList;
|
37 | 45 | import static org.hamcrest.Matchers.equalTo;
|
| 46 | +import static org.junit.Assert.assertNotNull; |
38 | 47 | import static org.junit.Assert.assertThat;
|
39 | 48 | import static org.neo4j.driver.v1.Config.TlsAuthenticationConfig.usingKnownCerts;
|
40 | 49 | import static org.neo4j.driver.v1.Config.TlsAuthenticationConfig.usingTrustedCert;
|
41 | 50 |
|
| 51 | +// NOTE: Be careful about auto-formatting here. The below segment should contain GraphDatabase, Driver and Session imports |
| 52 | +// tag::include-driver[] |
| 53 | +// end::include-driver[] |
| 54 | + |
42 | 55 | /**
|
43 | 56 | * The tests below are examples that get pulled into the Driver Manual using the tags inside the tests.
|
44 | 57 | *
|
@@ -77,6 +90,31 @@ public void minimumViableSnippet() throws Throwable
|
77 | 90 | assertThat( stdIO.stdout(), equalTo( asList("Neo is 23 years old.") ) );
|
78 | 91 | }
|
79 | 92 |
|
| 93 | + @Test |
| 94 | + public void constructDriver() throws Throwable |
| 95 | + { |
| 96 | + // tag::construct-driver[] |
| 97 | + Driver driver = GraphDatabase.driver( "bolt://localhost" ); |
| 98 | + // end::construct-driver[] |
| 99 | + |
| 100 | + // Then |
| 101 | + assertNotNull( driver ); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void configuration() throws Throwable |
| 106 | + { |
| 107 | + // tag::configuration[] |
| 108 | + Driver driver = GraphDatabase.driver( "bolt://localhost", |
| 109 | + Config.build() |
| 110 | + .withLogging( new MyLogging() ) |
| 111 | + .toConfig()); |
| 112 | + // end::configuration[] |
| 113 | + |
| 114 | + // Then |
| 115 | + assertNotNull( driver ); |
| 116 | + } |
| 117 | + |
80 | 118 | @Test
|
81 | 119 | public void statement() throws Throwable
|
82 | 120 | {
|
@@ -117,6 +155,99 @@ public void statementWithoutParameters() throws Throwable
|
117 | 155 | assertThat( stdIO.stdout(), equalTo( asList("There were 1 the ones created.") ) );
|
118 | 156 | }
|
119 | 157 |
|
| 158 | + @Test |
| 159 | + public void resultCursor() throws Throwable |
| 160 | + { |
| 161 | + StdIOCapture stdIO = new StdIOCapture(); |
| 162 | + try( AutoCloseable captured = stdIO.capture(); |
| 163 | + Driver driver = GraphDatabase.driver( "bolt://localhost" ); |
| 164 | + Session session = driver.session() ) |
| 165 | + { |
| 166 | + session.run( "MATCH (n) DETACH DELETE n" ); |
| 167 | + session.run( "CREATE (p:Person { name: 'The One', age:23 })" ); |
| 168 | + |
| 169 | + // tag::result-cursor[] |
| 170 | + ResultCursor result = session.run( "MATCH (p:Person { name: {name} }) RETURN p.age", |
| 171 | + Values.parameters( "name", "The One" ) ); |
| 172 | + |
| 173 | + while( result.next() ) |
| 174 | + { |
| 175 | + System.out.println("Record: " + result.position() ); |
| 176 | + for ( Pair<String,Value> fieldInRecord : result.fields() ) |
| 177 | + { |
| 178 | + System.out.println( " " + fieldInRecord.key() + " = " + fieldInRecord.value() ); |
| 179 | + } |
| 180 | + } |
| 181 | + // end::result-cursor[] |
| 182 | + } |
| 183 | + |
| 184 | + // Then |
| 185 | + assertThat( stdIO.stdout(), equalTo( asList("Record: 0"," p.age = 23 :: INTEGER") ) ); |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + public void retainResultsForLaterProcessing() throws Throwable |
| 190 | + { |
| 191 | + StdIOCapture stdIO = new StdIOCapture(); |
| 192 | + try( AutoCloseable captured = stdIO.capture(); |
| 193 | + Driver driver = GraphDatabase.driver( "bolt://localhost" ); |
| 194 | + Session session = driver.session() ) |
| 195 | + { |
| 196 | + session.run( "MATCH (n) DETACH DELETE n" ); |
| 197 | + session.run( "CREATE (p:Person { name: 'The One', age:23 })" ); |
| 198 | + |
| 199 | + // tag::retain-result-query[] |
| 200 | + ResultCursor result = session.run( "MATCH (p:Person { name: {name} }) RETURN id(p)", |
| 201 | + Values.parameters( "name", "The One" ) ); |
| 202 | + |
| 203 | + for ( Record record : result.list() ) |
| 204 | + { |
| 205 | + session.run( "MATCH (p) WHERE id(p) = {id} " + |
| 206 | + "CREATE (p)-[:HAS_TRAIT]->(:Trait {type:'Immortal'})", |
| 207 | + Values.parameters( "id", record.value( "id(p)" ) ) ); |
| 208 | + } |
| 209 | + // end::retain-result-query[] |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + @Test |
| 214 | + public void retainResultsForNestedQuerying() throws Throwable |
| 215 | + { |
| 216 | + StdIOCapture stdIO = new StdIOCapture(); |
| 217 | + try( AutoCloseable captured = stdIO.capture(); |
| 218 | + Driver driver = GraphDatabase.driver( "bolt://localhost" ); ) |
| 219 | + { |
| 220 | + try( Session setup = driver.session() ) |
| 221 | + { |
| 222 | + setup.run( "MATCH (n) DETACH DELETE n" ); |
| 223 | + setup.run( "CREATE (p:Person { name: 'The One', age:23 })" ); |
| 224 | + } |
| 225 | + |
| 226 | + // tag::retain-result-process[] |
| 227 | + Session session = driver.session(); |
| 228 | + |
| 229 | + ResultCursor result = session.run( "MATCH (p:Person { name: {name} }) RETURN p.age", |
| 230 | + Values.parameters( "name", "The One" ) ); |
| 231 | + |
| 232 | + List<Record> records = result.list(); |
| 233 | + |
| 234 | + session.close(); |
| 235 | + |
| 236 | + for ( Record record : records ) |
| 237 | + { |
| 238 | + for ( Pair<String,Value> fieldInRecord : record.fields() ) |
| 239 | + { |
| 240 | + System.out.println( fieldInRecord.key() + " = " + fieldInRecord.value() ); |
| 241 | + } |
| 242 | + } |
| 243 | + // end::retain-result-process[] |
| 244 | + } |
| 245 | + |
| 246 | + // Then |
| 247 | + assertThat( stdIO.stdout(), equalTo( asList("p.age = 23 :: INTEGER") ) ); |
| 248 | + } |
| 249 | + |
| 250 | + |
120 | 251 | @Test
|
121 | 252 | public void transactionCommit() throws Throwable
|
122 | 253 | {
|
@@ -159,6 +290,45 @@ public void transactionRollback() throws Throwable
|
159 | 290 | }
|
160 | 291 | }
|
161 | 292 |
|
| 293 | + @Test |
| 294 | + public void resultSummary() throws Throwable |
| 295 | + { |
| 296 | + StdIOCapture stdIO = new StdIOCapture(); |
| 297 | + try( AutoCloseable captured = stdIO.capture(); |
| 298 | + Driver driver = GraphDatabase.driver( "bolt://localhost" ); |
| 299 | + Session session = driver.session() ) |
| 300 | + { |
| 301 | + // tag::result-summary-query-profile[] |
| 302 | + ResultCursor result = session.run( "PROFILE MATCH (p:Person { name: {name} }) RETURN id(p)", |
| 303 | + Values.parameters( "name", "The One" ) ); |
| 304 | + |
| 305 | + ResultSummary summary = result.summarize(); |
| 306 | + |
| 307 | + System.out.println( summary.statementType() ); |
| 308 | + System.out.println( summary.profile() ); |
| 309 | + // end::result-summary-query-profile[] |
| 310 | + } |
| 311 | + } |
| 312 | + |
| 313 | + @Test |
| 314 | + public void notifications() throws Throwable |
| 315 | + { |
| 316 | + StdIOCapture stdIO = new StdIOCapture(); |
| 317 | + try( AutoCloseable captured = stdIO.capture(); |
| 318 | + Driver driver = GraphDatabase.driver( "bolt://localhost" ); |
| 319 | + Session session = driver.session() ) |
| 320 | + { |
| 321 | + // tag::result-summary-notifications[] |
| 322 | + ResultSummary summary = session.run( "EXPLAIN MATCH (a), (b) RETURN a,b" ).summarize(); |
| 323 | + |
| 324 | + for ( Notification notification : summary.notifications() ) |
| 325 | + { |
| 326 | + System.out.println(notification); |
| 327 | + } |
| 328 | + // end::result-summary-notifications[] |
| 329 | + } |
| 330 | + } |
| 331 | + |
162 | 332 | @Test
|
163 | 333 | public void requireEncryption() throws Throwable
|
164 | 334 | {
|
@@ -193,4 +363,13 @@ public void trustSignedCertificates() throws Throwable
|
193 | 363 | // end::tls-signed[]
|
194 | 364 | driver.close();
|
195 | 365 | }
|
| 366 | + |
| 367 | + private class MyLogging implements Logging |
| 368 | + { |
| 369 | + @Override |
| 370 | + public Logger getLog( String name ) |
| 371 | + { |
| 372 | + return null; |
| 373 | + } |
| 374 | + } |
196 | 375 | }
|
0 commit comments