Skip to content

Commit f89dece

Browse files
authored
Upgrade Neo4j server and driver dependencies. (#4789)
* Upgrade Neo4j Java Driver for testing to 4.4.2. * Upgrade Neo4j to 4.4.1. * Default to 4.4 instead concrete 4.4.1. * Throw exception if using database copy with Neo4j version !=3. * More specific javadoc and version check. * Adjust documentation for copy database functionality. * More robust check for defined image version.
1 parent 7d44f68 commit f89dece

File tree

7 files changed

+76
-21
lines changed

7 files changed

+76
-21
lines changed

docs/modules/databases/neo4j.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ public class ExampleTest {
139139
}
140140
```
141141

142+
!!! note
143+
The `withDatabase` method will only work with Neo4j 3.5 and throw an exception if used in combination with a newer version.
144+
142145
## Choose your Neo4j license
143146

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

modules/neo4j/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ sourceSets {
1818

1919
task customNeo4jPluginJar(type: Jar) {
2020
from sourceSets.customNeo4jPlugin.output
21-
archiveName = "hello-world.jar"
22-
destinationDir = customNeo4jPluginDestinationDir
21+
archiveFileName = "hello-world.jar"
22+
destinationDirectory = customNeo4jPluginDestinationDir
2323

2424
inputs.files(sourceSets.customNeo4jPlugin.java.srcDirs)
2525
outputs.cacheIf { true }
@@ -33,6 +33,6 @@ dependencies {
3333

3434
api project(":testcontainers")
3535

36-
testImplementation "org.neo4j.driver:neo4j-java-driver:1.7.5"
36+
testImplementation 'org.neo4j.driver:neo4j-java-driver:4.4.2'
3737
testImplementation 'org.assertj:assertj-core:3.21.0'
3838
}

modules/neo4j/src/main/java/org/testcontainers/containers/Neo4jContainer.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;
1111
import org.testcontainers.containers.wait.strategy.WaitAllStrategy;
1212
import org.testcontainers.containers.wait.strategy.WaitStrategy;
13+
import org.testcontainers.utility.ComparableVersion;
1314
import org.testcontainers.utility.DockerImageName;
1415
import org.testcontainers.utility.LicenseAcceptance;
1516
import org.testcontainers.utility.MountableFile;
@@ -30,7 +31,7 @@ public class Neo4jContainer<S extends Neo4jContainer<S>> extends GenericContaine
3031
/**
3132
* The default tag (version) to use.
3233
*/
33-
private static final String DEFAULT_TAG = "3.5.0";
34+
private static final String DEFAULT_TAG = "4.4";
3435
private static final String ENTERPRISE_TAG = DEFAULT_TAG + "-enterprise";
3536

3637
/**
@@ -194,6 +195,8 @@ public S withoutAuthentication() {
194195
* If you want to map your database into the container instead of copying them, please use {@code #withClasspathResourceMapping},
195196
* but this will only work when your test does not run in a container itself.
196197
* <br>
198+
* Note: This method only works with Neo4j 3.5.
199+
* <br>
197200
* Mapping would work like this:
198201
* <pre>
199202
* &#64;Container
@@ -202,9 +205,14 @@ public S withoutAuthentication() {
202205
* </pre>
203206
*
204207
* @param graphDb The graph.db folder to copy into the container
208+
* @throws IllegalArgumentException If the database version is not 3.5.
205209
* @return This container.
206210
*/
207211
public S withDatabase(MountableFile graphDb) {
212+
if (!isNeo4jDatabaseVersionSupportingDbCopy()) {
213+
throw new IllegalArgumentException(
214+
"Copying database folder is not supported for Neo4j instances with version 4.0 or higher.");
215+
}
208216
return withCopyFileToContainer(graphDb, "/data/databases/graph.db");
209217
}
210218

@@ -250,4 +258,23 @@ private static String formatConfigurationKey(String plainConfigKey) {
250258
.replaceAll("_", "__")
251259
.replaceAll("\\.", "_"));
252260
}
261+
262+
private boolean isNeo4jDatabaseVersionSupportingDbCopy() {
263+
String usedImageVersion = DockerImageName.parse(getDockerImageName()).getVersionPart();
264+
ComparableVersion usedComparableVersion = new ComparableVersion(usedImageVersion);
265+
266+
boolean versionSupportingDbCopy =
267+
usedComparableVersion.isLessThan("4.0") && usedComparableVersion.isGreaterThanOrEqualTo("2");
268+
269+
if (versionSupportingDbCopy) {
270+
return true;
271+
}
272+
if (!usedComparableVersion.isSemanticVersion()) {
273+
logger().warn("Version {} is not a semantic version. The function \"withDatabase\" will fail.", usedImageVersion);
274+
logger().warn("Copying databases is only supported for Neo4j versions 3.5.x");
275+
276+
}
277+
278+
return false;
279+
}
253280
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import org.junit.ClassRule;
44
import org.junit.Test;
5-
import org.neo4j.driver.v1.AuthTokens;
6-
import org.neo4j.driver.v1.Driver;
7-
import org.neo4j.driver.v1.GraphDatabase;
8-
import org.neo4j.driver.v1.Session;
5+
import org.neo4j.driver.AuthTokens;
6+
import org.neo4j.driver.Driver;
7+
import org.neo4j.driver.GraphDatabase;
8+
import org.neo4j.driver.Session;
99

1010
import java.util.Collections;
1111

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

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package org.testcontainers.containers;
22

3+
import org.assertj.core.api.Assertions;
34
import org.junit.Test;
4-
import org.neo4j.driver.v1.AuthToken;
5-
import org.neo4j.driver.v1.AuthTokens;
6-
import org.neo4j.driver.v1.Driver;
7-
import org.neo4j.driver.v1.GraphDatabase;
8-
import org.neo4j.driver.v1.Record;
9-
import org.neo4j.driver.v1.Session;
10-
import org.neo4j.driver.v1.StatementResult;
5+
import org.neo4j.driver.AuthToken;
6+
import org.neo4j.driver.AuthTokens;
7+
import org.neo4j.driver.Driver;
8+
import org.neo4j.driver.GraphDatabase;
9+
import org.neo4j.driver.Record;
10+
import org.neo4j.driver.Result;
11+
import org.neo4j.driver.Session;
1112
import org.testcontainers.utility.MountableFile;
1213

1314
import java.util.Collections;
@@ -45,21 +46,45 @@ public void shouldDisableAuthentication() {
4546
@Test
4647
public void shouldCopyDatabase() {
4748
try (
48-
Neo4jContainer<?> neo4jContainer = new Neo4jContainer<>(Neo4jTestImages.NEO4J_TEST_IMAGE)
49+
Neo4jContainer<?> neo4jContainer = new Neo4jContainer<>("neo4j:3.5.30")
4950
.withDatabase(MountableFile.forClasspathResource("/test-graph.db"))
5051
) {
5152
neo4jContainer.start();
5253
try (
5354
Driver driver = getDriver(neo4jContainer);
5455
Session session = driver.session()
5556
) {
56-
StatementResult result = session.run("MATCH (t:Thing) RETURN t");
57+
Result result = session.run("MATCH (t:Thing) RETURN t");
5758
assertThat(result.list().stream().map(r -> r.get("t").get("name").asString()))
5859
.containsExactlyInAnyOrder("Thing", "Thing 2", "Thing 3", "A box");
5960
}
6061
}
6162
}
6263

64+
@Test
65+
public void shouldFailOnCopyDatabaseForDefaultNeo4j4Image() {
66+
Assertions.assertThatIllegalArgumentException()
67+
.isThrownBy(() -> new Neo4jContainer<>()
68+
.withDatabase(MountableFile.forClasspathResource("/test-graph.db")))
69+
.withMessage("Copying database folder is not supported for Neo4j instances with version 4.0 or higher.");
70+
}
71+
72+
@Test
73+
public void shouldFailOnCopyDatabaseForCustomNeo4j4Image() {
74+
Assertions.assertThatIllegalArgumentException()
75+
.isThrownBy(() -> new Neo4jContainer<>("neo4j:4.4.1")
76+
.withDatabase(MountableFile.forClasspathResource("/test-graph.db")))
77+
.withMessage("Copying database folder is not supported for Neo4j instances with version 4.0 or higher.");
78+
}
79+
80+
@Test
81+
public void shouldFailOnCopyDatabaseForCustomNonSemverNeo4j4Image() {
82+
Assertions.assertThatIllegalArgumentException()
83+
.isThrownBy(() -> new Neo4jContainer<>("neo4j:latest")
84+
.withDatabase(MountableFile.forClasspathResource("/test-graph.db")))
85+
.withMessage("Copying database folder is not supported for Neo4j instances with version 4.0 or higher.");
86+
}
87+
6388
@Test
6489
public void shouldCopyPlugins() {
6590
try (
@@ -93,7 +118,7 @@ public void shouldCopyPlugin() {
93118
}
94119

95120
private static void assertThatCustomPluginWasCopied(Session session) {
96-
StatementResult result = session.run("RETURN ac.simons.helloWorld('Testcontainers') AS greeting");
121+
Result result = session.run("RETURN ac.simons.helloWorld('Testcontainers') AS greeting");
97122
Record singleRecord = result.single();
98123
assertThat(singleRecord).isNotNull();
99124
assertThat(singleRecord.get("greeting").asString()).isEqualTo("Hello, Testcontainers");
@@ -103,7 +128,7 @@ private static void assertThatCustomPluginWasCopied(Session session) {
103128
public void shouldCheckEnterpriseLicense() {
104129
assumeThat(Neo4jContainerTest.class.getResource(ACCEPTANCE_FILE_LOCATION)).isNull();
105130

106-
String expectedImageName = "neo4j:3.5.0-enterprise";
131+
String expectedImageName = "neo4j:4.4-enterprise";
107132

108133
assertThatExceptionOfType(IllegalStateException.class)
109134
.isThrownBy(() -> new Neo4jContainer<>(Neo4jTestImages.NEO4J_TEST_IMAGE).withEnterpriseEdition())

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
import org.testcontainers.utility.DockerImageName;
44

55
public interface Neo4jTestImages {
6-
DockerImageName NEO4J_TEST_IMAGE = DockerImageName.parse("neo4j:3.5.0");
6+
DockerImageName NEO4J_TEST_IMAGE = DockerImageName.parse("neo4j:4.4.1");
77
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
neo4j:3.5.0-enterprise
1+
neo4j:4.4.1-enterprise

0 commit comments

Comments
 (0)