Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade Neo4j server and driver dependencies. #4789

Merged
merged 7 commits into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/modules/databases/neo4j.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ public class ExampleTest {
}
```

!!! note
The `withDatabase` method will only work with Neo4j 3.5 and throw an exception if used in combination with a newer version.

## Choose your Neo4j license

If you need the Neo4j enterprise license, you can declare your Neo4j container like this:
Expand Down
6 changes: 3 additions & 3 deletions modules/neo4j/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ sourceSets {

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

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

api project(":testcontainers")

testImplementation "org.neo4j.driver:neo4j-java-driver:1.7.5"
testImplementation 'org.neo4j.driver:neo4j-java-driver:4.4.2'
testImplementation 'org.assertj:assertj-core:3.21.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;
import org.testcontainers.containers.wait.strategy.WaitAllStrategy;
import org.testcontainers.containers.wait.strategy.WaitStrategy;
import org.testcontainers.utility.ComparableVersion;
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.utility.LicenseAcceptance;
import org.testcontainers.utility.MountableFile;
Expand All @@ -30,7 +31,7 @@ public class Neo4jContainer<S extends Neo4jContainer<S>> extends GenericContaine
/**
* The default tag (version) to use.
*/
private static final String DEFAULT_TAG = "3.5.0";
private static final String DEFAULT_TAG = "4.4";
private static final String ENTERPRISE_TAG = DEFAULT_TAG + "-enterprise";

/**
Expand Down Expand Up @@ -194,6 +195,8 @@ public S withoutAuthentication() {
* If you want to map your database into the container instead of copying them, please use {@code #withClasspathResourceMapping},
* but this will only work when your test does not run in a container itself.
* <br>
* Note: This method only works with Neo4j 3.5.
* <br>
* Mapping would work like this:
* <pre>
* &#64;Container
Expand All @@ -202,9 +205,14 @@ public S withoutAuthentication() {
* </pre>
*
* @param graphDb The graph.db folder to copy into the container
* @throws IllegalArgumentException If the database version is not 3.5.
* @return This container.
*/
public S withDatabase(MountableFile graphDb) {
if (!isNeo4jDatabaseVersionSupportingDbCopy()) {
throw new IllegalArgumentException(
"Copying database folder is not supported for Neo4j instances with version 4.0 or higher.");
}
return withCopyFileToContainer(graphDb, "/data/databases/graph.db");
}

Expand Down Expand Up @@ -250,4 +258,23 @@ private static String formatConfigurationKey(String plainConfigKey) {
.replaceAll("_", "__")
.replaceAll("\\.", "_"));
}

private boolean isNeo4jDatabaseVersionSupportingDbCopy() {
String usedImageVersion = DockerImageName.parse(getDockerImageName()).getVersionPart();
ComparableVersion usedComparableVersion = new ComparableVersion(usedImageVersion);

boolean versionSupportingDbCopy =
usedComparableVersion.isLessThan("4.0") && usedComparableVersion.isGreaterThanOrEqualTo("2");

if (versionSupportingDbCopy) {
return true;
}
if (!usedComparableVersion.isSemanticVersion()) {
logger().warn("Version {} is not a semantic version. The function \"withDatabase\" will fail.", usedImageVersion);
logger().warn("Copying databases is only supported for Neo4j versions 3.5.x");

}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import org.junit.ClassRule;
import org.junit.Test;
import org.neo4j.driver.v1.AuthTokens;
import org.neo4j.driver.v1.Driver;
import org.neo4j.driver.v1.GraphDatabase;
import org.neo4j.driver.v1.Session;
import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
import org.neo4j.driver.Session;

import java.util.Collections;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package org.testcontainers.containers;

import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.neo4j.driver.v1.AuthToken;
import org.neo4j.driver.v1.AuthTokens;
import org.neo4j.driver.v1.Driver;
import org.neo4j.driver.v1.GraphDatabase;
import org.neo4j.driver.v1.Record;
import org.neo4j.driver.v1.Session;
import org.neo4j.driver.v1.StatementResult;
import org.neo4j.driver.AuthToken;
import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
import org.neo4j.driver.Record;
import org.neo4j.driver.Result;
import org.neo4j.driver.Session;
import org.testcontainers.utility.MountableFile;

import java.util.Collections;
Expand Down Expand Up @@ -45,21 +46,45 @@ public void shouldDisableAuthentication() {
@Test
public void shouldCopyDatabase() {
try (
Neo4jContainer<?> neo4jContainer = new Neo4jContainer<>(Neo4jTestImages.NEO4J_TEST_IMAGE)
Neo4jContainer<?> neo4jContainer = new Neo4jContainer<>("neo4j:3.5.30")
.withDatabase(MountableFile.forClasspathResource("/test-graph.db"))
) {
neo4jContainer.start();
try (
Driver driver = getDriver(neo4jContainer);
Session session = driver.session()
) {
StatementResult result = session.run("MATCH (t:Thing) RETURN t");
Result result = session.run("MATCH (t:Thing) RETURN t");
assertThat(result.list().stream().map(r -> r.get("t").get("name").asString()))
.containsExactlyInAnyOrder("Thing", "Thing 2", "Thing 3", "A box");
}
}
}

@Test
public void shouldFailOnCopyDatabaseForDefaultNeo4j4Image() {
Assertions.assertThatIllegalArgumentException()
.isThrownBy(() -> new Neo4jContainer<>()
.withDatabase(MountableFile.forClasspathResource("/test-graph.db")))
.withMessage("Copying database folder is not supported for Neo4j instances with version 4.0 or higher.");
}

@Test
public void shouldFailOnCopyDatabaseForCustomNeo4j4Image() {
Assertions.assertThatIllegalArgumentException()
.isThrownBy(() -> new Neo4jContainer<>("neo4j:4.4.1")
.withDatabase(MountableFile.forClasspathResource("/test-graph.db")))
.withMessage("Copying database folder is not supported for Neo4j instances with version 4.0 or higher.");
}

@Test
public void shouldFailOnCopyDatabaseForCustomNonSemverNeo4j4Image() {
Assertions.assertThatIllegalArgumentException()
.isThrownBy(() -> new Neo4jContainer<>("neo4j:latest")
.withDatabase(MountableFile.forClasspathResource("/test-graph.db")))
.withMessage("Copying database folder is not supported for Neo4j instances with version 4.0 or higher.");
}

@Test
public void shouldCopyPlugins() {
try (
Expand Down Expand Up @@ -93,7 +118,7 @@ public void shouldCopyPlugin() {
}

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

String expectedImageName = "neo4j:3.5.0-enterprise";
String expectedImageName = "neo4j:4.4-enterprise";

assertThatExceptionOfType(IllegalStateException.class)
.isThrownBy(() -> new Neo4jContainer<>(Neo4jTestImages.NEO4J_TEST_IMAGE).withEnterpriseEdition())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import org.testcontainers.utility.DockerImageName;

public interface Neo4jTestImages {
kiview marked this conversation as resolved.
Show resolved Hide resolved
DockerImageName NEO4J_TEST_IMAGE = DockerImageName.parse("neo4j:3.5.0");
DockerImageName NEO4J_TEST_IMAGE = DockerImageName.parse("neo4j:4.4.1");
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
neo4j:3.5.0-enterprise
neo4j:4.4.1-enterprise