Skip to content

Commit 244c77c

Browse files
authored
[Neo4j] Include code from tests in docs (#5120)
1 parent 14eff07 commit 244c77c

File tree

7 files changed

+143
-134
lines changed

7 files changed

+143
-134
lines changed

docs/modules/databases/neo4j.md

Lines changed: 30 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -6,139 +6,59 @@ Note that it's based on the [official Docker image](https://hub.docker.com/_/neo
66

77
## Usage example
88

9-
Declare your Testcontainer as a `@ClassRule` or `@Rule` in a JUnit 4 test or as static or member attribute of a JUnit 5 test annotated with `@Container` as you would with other Testcontainers.
10-
You can either use call `getHttpUrl()` or `getBoltUrl()` on the Neo4j container.
11-
`getHttpUrl()` gives you the HTTP-address of the transactional HTTP endpoint while `getBoltUrl()` is meant to be used with one of the [official Bolt drivers](https://neo4j.com/developer/language-guides/).
9+
Declare your Testcontainers as a `@ClassRule` or `@Rule` in a JUnit 4 test or as static or member attribute of a JUnit 5 test annotated with `@Container` as you would with other Testcontainers.
10+
You can either use call `getBoltUrl()` or `getHttpUrl()` on the Neo4j container.
11+
`getBoltUrl()` is meant to be used with one of the [official Bolt drivers](https://neo4j.com/developer/language-guides/) while `getHttpUrl()` gives you the HTTP-address of the transactional HTTP endpoint.
1212
On the JVM you would most likely use the [Java driver](https://github.com/neo4j/neo4j-java-driver).
1313

1414
The following example uses the JUnit 5 extension `@Testcontainers` and demonstrates both the usage of the Java Driver and the REST endpoint:
1515

16-
=== "JUnit 5 example"
17-
```java
18-
@Testcontainers
19-
public class ExampleTest {
20-
21-
@Container
22-
private static Neo4jContainer neo4jContainer = new Neo4jContainer()
23-
.withAdminPassword(null); // Disable password
24-
25-
@Test
26-
void testSomethingUsingBolt() {
27-
28-
// Retrieve the Bolt URL from the container
29-
String boltUrl = neo4jContainer.getBoltUrl();
30-
try (
31-
Driver driver = GraphDatabase.driver(boltUrl, AuthTokens.none());
32-
Session session = driver.session()
33-
) {
34-
long one = session.run("RETURN 1", Collections.emptyMap()).next().get(0).asLong();
35-
assertThat(one, is(1L));
36-
} catch (Exception e) {
37-
fail(e.getMessage());
38-
}
39-
}
40-
41-
@Test
42-
void testSomethingUsingHttp() throws IOException {
43-
44-
// Retrieve the HTTP URL from the container
45-
String httpUrl = neo4jContainer.getHttpUrl();
46-
47-
URL url = new URL(httpUrl + "/db/data/transaction/commit");
48-
HttpURLConnection con = (HttpURLConnection) url.openConnection();
49-
50-
con.setRequestMethod("POST");
51-
con.setRequestProperty("Content-Type", "application/json");
52-
con.setDoOutput(true);
53-
54-
try (Writer out = new OutputStreamWriter(con.getOutputStream())) {
55-
out.write("{\"statements\":[{\"statement\":\"RETURN 1\"}]}");
56-
out.flush();
57-
}
58-
59-
assertThat(con.getResponseCode(), is(HttpURLConnection.HTTP_OK));
60-
try (BufferedReader buffer = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
61-
String expectedResponse =
62-
"{\"results\":[{\"columns\":[\"1\"],\"data\":[{\"row\":[1],\"meta\":[null]}]}],\"errors\":[]}";
63-
String response = buffer.lines().collect(Collectors.joining("\n"));
64-
assertThat(response, is(expectedResponse));
65-
}
66-
}
67-
}
68-
```
16+
<!--codeinclude-->
17+
[JUnit 5 example](../../../examples/neo4j-container/src/test/java/org/testcontainers/containers/Neo4jExampleTest.java) inside_block:junitExample
18+
<!--/codeinclude-->
6919

70-
You are not limited to Unit tests and can of course use an instance of the Neo4j Testcontainer in vanilla Java code as well.
20+
You are not limited to Unit tests and can of course use an instance of the Neo4j Testcontainers in vanilla Java code as well.
7121

7222
## Additional features
7323

7424
### Disable authentication
7525

7626
Authentication can be disabled:
7727

78-
```java
79-
@Testcontainers
80-
public class ExampleTest {
81-
82-
@Container
83-
Neo4jContainer neo4jContainer = new Neo4jContainer()
84-
.withoutAuthentication();
85-
}
86-
```
28+
<!--codeinclude-->
29+
[Disable authentication](../../../modules/neo4j/src/test/java/org/testcontainers/containers/Neo4jContainerTest.java) inside_block:withoutAuthentication
30+
<!--/codeinclude-->
8731

8832
### Neo4j-Configuration
8933

9034
Neo4j's Docker image needs Neo4j configuration options in a dedicated format.
91-
The container takes care of that and you can configure the database with standard options like the following:
92-
93-
```java
94-
@Testcontainers
95-
public class ExampleTest {
35+
The container takes care of that, and you can configure the database with standard options like the following:
9636

97-
@Container
98-
Neo4jContainer neo4jContainer = new Neo4jContainer()
99-
.withNeo4jConfig("dbms.security.procedures.unrestricted", "apoc.*,algo.*");
100-
}
101-
```
37+
<!--codeinclude-->
38+
[Neo4j configuration](../../../modules/neo4j/src/test/java/org/testcontainers/containers/Neo4jContainerTest.java) inside_block:neo4jConfiguration
39+
<!--/codeinclude-->
10240

10341
### Add custom plugins
10442

10543
Custom plugins, like APOC, can be copied over to the container from any classpath or host resource like this:
10644

107-
```java
108-
@Testcontainers
109-
public class ExampleTest {
110-
111-
@Container
112-
Neo4jContainer neo4jContainer = new Neo4jContainer()
113-
.withPlugins(MountableFile.forClasspathResource("/apoc-3.5.0.1-all.jar"));
114-
}
115-
```
45+
<!--codeinclude-->
46+
[Plugin jar](../../../modules/neo4j/src/test/java/org/testcontainers/containers/Neo4jContainerTest.java) inside_block:registerPluginsJar
47+
<!--/codeinclude-->
11648

11749
Whole directories work as well:
11850

119-
```java
120-
@Testcontainers
121-
public class ExampleTest {
122-
123-
@Container
124-
Neo4jContainer neo4jContainer = new Neo4jContainer()
125-
.withPlugins(MountableFile.forClasspathResource("/my-plugins"));
126-
}
127-
```
51+
<!--codeinclude-->
52+
[Plugin folder](../../../modules/neo4j/src/test/java/org/testcontainers/containers/Neo4jContainerTest.java) inside_block:registerPluginsPath
53+
<!--/codeinclude-->
12854

12955
### Start the container with a predefined database
13056

13157
If you have an existing database (`graph.db`) you want to work with, copy it over to the container like this:
13258

133-
```java
134-
@Testcontainers
135-
public class ExampleTest {
136-
137-
@Container
138-
Neo4jContainer neo4jContainer = new Neo4jContainer()
139-
.withDatabase(MountableFile.forClasspathResource("/test-graph.db"));
140-
}
141-
```
59+
<!--codeinclude-->
60+
[Copy database](../../../modules/neo4j/src/test/java/org/testcontainers/containers/Neo4jContainerTest.java) inside_block:copyDatabase
61+
<!--/codeinclude-->
14262

14363
!!! note
14464
The `withDatabase` method will only work with Neo4j 3.5 and throw an exception if used in combination with a newer version.
@@ -147,16 +67,11 @@ The `withDatabase` method will only work with Neo4j 3.5 and throw an exception i
14767

14868
If you need the Neo4j enterprise license, you can declare your Neo4j container like this:
14969

150-
```java
151-
@Testcontainers
152-
public class ExampleTest {
153-
@ClassRule
154-
public static Neo4jContainer neo4jContainer = new Neo4jContainer()
155-
.withEnterpriseEdition();
156-
}
157-
```
70+
<!--codeinclude-->
71+
[Enterprise edition](../../../modules/neo4j/src/test/java/org/testcontainers/containers/Neo4jContainerTest.java) inside_block:enterpriseEdition
72+
<!--/codeinclude-->
15873

159-
This creates a Testcontainer based on the Docker image build with the Enterprise version of Neo4j.
74+
This creates a Testcontainers based on the Docker image build with the Enterprise version of Neo4j.
16075
The call to `withEnterpriseEdition` adds the required environment variable that you accepted the terms and condition of the enterprise version.
16176
You accept those by adding a file named `container-license-acceptance.txt` to the root of your classpath containing the text `neo4j:3.5.0-enterprise` in one line.
16277
You'll find more information about licensing Neo4j here: [About Neo4j Licenses](https://neo4j.com/licensing/).
@@ -181,22 +96,18 @@ Add the following dependency to your `pom.xml`/`build.gradle` file:
18196
```
18297

18398
!!! hint
184-
Add the Neo4j Java driver if you plan to access the Testcontainer via Bolt:
99+
Add the Neo4j Java driver if you plan to access the Testcontainers via Bolt:
185100

186101
=== "Gradle"
187102
```groovy
188-
compile "org.neo4j.driver:neo4j-java-driver:1.7.1"
103+
compile "org.neo4j.driver:neo4j-java-driver:4.4.3"
189104
```
190105

191106
=== "Maven"
192107
```xml
193108
<dependency>
194109
<groupId>org.neo4j.driver</groupId>
195110
<artifactId>neo4j-java-driver</artifactId>
196-
<version>1.7.1</version>
111+
<version>4.4.3</version>
197112
</dependency>
198113
```
199-
200-
201-
202-
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
plugins {
2+
id 'java'
3+
}
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testImplementation 'org.assertj:assertj-core:3.22.0'
11+
testImplementation 'org.neo4j.driver:neo4j-java-driver:4.4.3'
12+
testImplementation 'org.testcontainers:neo4j'
13+
testImplementation 'org.testcontainers:junit-jupiter'
14+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.testcontainers.containers;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.io.OutputStreamWriter;
7+
import java.io.Writer;
8+
import java.net.HttpURLConnection;
9+
import java.net.URL;
10+
import java.util.Collections;
11+
import java.util.stream.Collectors;
12+
13+
import org.junit.jupiter.api.Test;
14+
import org.neo4j.driver.AuthTokens;
15+
import org.neo4j.driver.Driver;
16+
import org.neo4j.driver.GraphDatabase;
17+
import org.neo4j.driver.Session;
18+
import org.testcontainers.junit.jupiter.Container;
19+
import org.testcontainers.junit.jupiter.Testcontainers;
20+
import org.testcontainers.utility.DockerImageName;
21+
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
import static org.junit.Assert.fail;
24+
25+
// junitExample {
26+
@Testcontainers
27+
public class Neo4jExampleTest {
28+
29+
@Container
30+
private static Neo4jContainer<?> neo4jContainer =
31+
new Neo4jContainer<>(DockerImageName.parse("neo4j:4.4"))
32+
.withAdminPassword(null); // Disable password
33+
34+
@Test
35+
void testSomethingUsingBolt() {
36+
37+
// Retrieve the Bolt URL from the container
38+
String boltUrl = neo4jContainer.getBoltUrl();
39+
try (
40+
Driver driver = GraphDatabase.driver(boltUrl, AuthTokens.none());
41+
Session session = driver.session()
42+
) {
43+
long one = session.run("RETURN 1", Collections.emptyMap()).next().get(0).asLong();
44+
assertThat(one).isEqualTo(1L);
45+
} catch (Exception e) {
46+
fail(e.getMessage());
47+
}
48+
}
49+
50+
@Test
51+
void testSomethingUsingHttp() throws IOException {
52+
53+
// Retrieve the HTTP URL from the container
54+
String httpUrl = neo4jContainer.getHttpUrl();
55+
56+
URL url = new URL(httpUrl + "/db/data/transaction/commit");
57+
HttpURLConnection con = (HttpURLConnection) url.openConnection();
58+
59+
con.setRequestMethod("POST");
60+
con.setRequestProperty("Content-Type", "application/json");
61+
con.setDoOutput(true);
62+
63+
try (Writer out = new OutputStreamWriter(con.getOutputStream())) {
64+
out.write("{\"statements\":[{\"statement\":\"RETURN 1\"}]}");
65+
out.flush();
66+
}
67+
68+
assertThat(con.getResponseCode()).isEqualTo(HttpURLConnection.HTTP_OK);
69+
try (BufferedReader buffer = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
70+
String expectedResponse =
71+
"{\"results\":[{\"columns\":[\"1\"],\"data\":[{\"row\":[1],\"meta\":[null]}]}],\"errors\":[]}";
72+
String response = buffer.lines().collect(Collectors.joining("\n"));
73+
assertThat(response).isEqualTo(expectedResponse);
74+
}
75+
}
76+
}
77+
// }

examples/settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ include 'disque-job-queue'
2323
include 'kafka-cluster'
2424
include 'linked-container'
2525
include 'mongodb-container'
26+
include 'neo4j-container'
2627
include 'redis-backed-cache'
2728
include 'redis-backed-cache-testng'
2829
include 'selenium-container'

modules/neo4j/src/test/java/org/testcontainers/containers/Neo4jContainerJUnitIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
public class Neo4jContainerJUnitIntegrationTest {
2121

2222
@ClassRule
23-
public static Neo4jContainer<?> neo4jContainer = new Neo4jContainer<>(Neo4jTestImages.NEO4J_TEST_IMAGE);
23+
public static Neo4jContainer<?> neo4jContainer = new Neo4jContainer<>("neo4j:4.4");
2424

2525
@Test
2626
public void shouldStart() {

0 commit comments

Comments
 (0)