Skip to content

Commit 727b644

Browse files
committed
update to use encryption
1 parent 4c19b77 commit 727b644

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

examples/src/main/java/org/neo4j/docs/driver/DriverIntroductionExample.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,16 @@
2828
import java.util.Map;
2929
import java.util.logging.Level;
3030
import java.util.logging.Logger;
31+
32+
import static org.neo4j.driver.Config.TrustStrategy.trustAllCertificates;
3133
// end::driver-introduction-example-import[]
3234

3335
// tag::driver-introduction-example[]
3436
public class DriverIntroductionExample implements AutoCloseable {
3537
private static final Logger LOGGER = Logger.getLogger(DriverIntroductionExample.class.getName());
3638
private final Driver driver;
3739

38-
public DriverIntroductionExample(String uri, String user, String password) {
39-
// Aura queries use an encrypted connection
40-
Config config = Config.builder()
41-
.withEncryption()
42-
.build();
43-
40+
public DriverIntroductionExample(String uri, String user, String password, Config config) {
4441
// The driver is a long living object and should be opened during the start of your application
4542
driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password), config);
4643
}
@@ -64,6 +61,7 @@ public void createFriendship(final String person1Name, final String person2Name)
6461
params.put("person2_name", person2Name);
6562

6663
try (Session session = driver.session()) {
64+
// Write transactions allow the driver to handle retries and transient errors
6765
Record record = session.writeTransaction(tx -> {
6866
Result result = tx.run(createFriendshipQuery, params);
6967
return result.single();
@@ -90,7 +88,7 @@ public void findPerson(final String personName) {
9088
Result result = tx.run(readPersonByNameQuery, params);
9189
return result.single();
9290
});
93-
System.out.println(String.format("Found person:: %s", record.get("name").asString()));
91+
System.out.println(String.format("Found person: %s", record.get("name").asString()));
9492
// You should capture any errors along with the query and data for traceability
9593
} catch (Neo4jException ex) {
9694
LOGGER.log(Level.SEVERE, readPersonByNameQuery + " raised an exception", ex);
@@ -99,10 +97,16 @@ public void findPerson(final String personName) {
9997
}
10098

10199
public static void main(String... args) throws Exception {
100+
// Aura uses the "neo4j" protocol
102101
String boltUrl = "%%BOLT_URL_PLACEHOLDER%%";
102+
103103
String user = "<Username for Neo4j Aura database>";
104104
String password = "<Password for Neo4j Aura database>";
105-
try (DriverIntroductionExample app = new DriverIntroductionExample(boltUrl, user, password)) {
105+
106+
// Aura queries use an encrypted connection
107+
Config config = Config.builder().withEncryption().build();
108+
109+
try (DriverIntroductionExample app = new DriverIntroductionExample(boltUrl, user, password, config)) {
106110
app.createFriendship("Alice", "David");
107111
app.findPerson("Alice");
108112
}

examples/src/test/java/org/neo4j/docs/driver/ExamplesIT.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@
3232
import java.util.Set;
3333
import java.util.UUID;
3434

35-
import org.neo4j.driver.Driver;
36-
import org.neo4j.driver.Session;
37-
import org.neo4j.driver.SessionConfig;
38-
import org.neo4j.driver.Value;
39-
import org.neo4j.driver.Values;
35+
import org.neo4j.driver.*;
4036
import org.neo4j.driver.internal.util.EnabledOnNeo4jWith;
4137
import org.neo4j.driver.summary.QueryType;
4238
import org.neo4j.driver.summary.ResultSummary;
@@ -57,14 +53,14 @@
5753
import static org.hamcrest.junit.MatcherAssert.assertThat;
5854
import static org.junit.jupiter.api.Assertions.assertEquals;
5955
import static org.junit.jupiter.api.Assertions.assertTrue;
56+
import static org.neo4j.driver.Config.TrustStrategy.trustAllCertificates;
6057
import static org.neo4j.driver.Values.parameters;
6158
import static org.neo4j.driver.internal.util.Neo4jEdition.ENTERPRISE;
6259
import static org.neo4j.driver.internal.util.Neo4jFeature.BOLT_V4;
6360
import static org.neo4j.driver.util.Neo4jRunner.PASSWORD;
6461
import static org.neo4j.driver.util.Neo4jRunner.USER;
6562
import static org.neo4j.driver.util.TestUtil.await;
6663
import static org.neo4j.driver.util.TestUtil.createDatabase;
67-
import static org.neo4j.driver.util.TestUtil.databaseExists;
6864
import static org.neo4j.driver.util.TestUtil.dropDatabase;
6965

7066
@ParallelizableIT
@@ -308,7 +304,8 @@ void testShouldRunHelloWorld() throws Exception
308304
void testShouldRunDriverIntroduction() throws Exception
309305
{
310306
// Given
311-
try ( DriverIntroductionExample intro = new DriverIntroductionExample( uri, USER, PASSWORD ) )
307+
Config config = Config.builder().withEncryption().withTrustStrategy(trustAllCertificates()).build();
308+
try (DriverIntroductionExample intro = new DriverIntroductionExample( uri, USER, PASSWORD, config) )
312309
{
313310
// When
314311
StdIOCapture stdIO = new StdIOCapture();
@@ -321,7 +318,7 @@ void testShouldRunDriverIntroduction() throws Exception
321318

322319
// Then
323320
assertThat( stdIO.stdout().size(), equalTo( 2 ) );
324-
assertThat( stdIO.stdout().get( 0 ), containsString( "Create friendship between: Alice, David" ) );
321+
assertThat( stdIO.stdout().get( 0 ), containsString( "Created friendship between: Alice, David" ) );
325322
assertThat( stdIO.stdout().get( 1 ), containsString( "Found person: Alice" ) );
326323
}
327324
}

0 commit comments

Comments
 (0)