|
| 1 | +package org.testcontainers.containers; |
| 2 | + |
| 3 | +import static java.net.HttpURLConnection.*; |
| 4 | +import static java.util.stream.Collectors.*; |
| 5 | + |
| 6 | +import java.time.Duration; |
| 7 | +import java.util.Set; |
| 8 | +import java.util.stream.Stream; |
| 9 | + |
| 10 | +import org.testcontainers.containers.wait.strategy.HttpWaitStrategy; |
| 11 | +import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy; |
| 12 | +import org.testcontainers.containers.wait.strategy.WaitAllStrategy; |
| 13 | +import org.testcontainers.containers.wait.strategy.WaitStrategy; |
| 14 | +import org.testcontainers.utility.LicenseAcceptance; |
| 15 | + |
| 16 | +/** |
| 17 | + * Testcontainer for Neo4j. |
| 18 | + * |
| 19 | + * @param <S> "SELF" to be used in the <code>withXXX</code> methods. |
| 20 | + * @author Michael J. Simons |
| 21 | + */ |
| 22 | +public final class Neo4jContainer<S extends Neo4jContainer<S>> extends GenericContainer<S> { |
| 23 | + |
| 24 | + /** |
| 25 | + * The image defaults to the official Neo4j image: <a href="https://hub.docker.com/_/neo4j/">Neo4j</a>. |
| 26 | + */ |
| 27 | + private static final String DEFAULT_IMAGE_NAME = "neo4j"; |
| 28 | + |
| 29 | + /** |
| 30 | + * The default tag (version) to use. |
| 31 | + */ |
| 32 | + private static final String DEFAULT_TAG = "3.5.0"; |
| 33 | + |
| 34 | + private static final String DOCKER_IMAGE_NAME = DEFAULT_IMAGE_NAME + ":" + DEFAULT_TAG; |
| 35 | + |
| 36 | + /** |
| 37 | + * Default port for the binary Bolt protocol. |
| 38 | + */ |
| 39 | + private static final int DEFAULT_BOLT_PORT = 7687; |
| 40 | + |
| 41 | + /** |
| 42 | + * The port of the transactional HTTPS endpoint: <a href="https://neo4j.com/docs/rest-docs/current/">Neo4j REST API</a>. |
| 43 | + */ |
| 44 | + private static final int DEFAULT_HTTPS_PORT = 7473; |
| 45 | + |
| 46 | + /** |
| 47 | + * The port of the transactional HTTP endpoint: <a href="https://neo4j.com/docs/rest-docs/current/">Neo4j REST API</a>. |
| 48 | + */ |
| 49 | + private static final int DEFAULT_HTTP_PORT = 7474; |
| 50 | + |
| 51 | + /** |
| 52 | + * The official image requires a change of password by default from "neo4j" to something else. This defaults to "password". |
| 53 | + */ |
| 54 | + private static final String DEFAULT_ADMIN_PASSWORD = "password"; |
| 55 | + |
| 56 | + private static final String AUTH_FORMAT = "neo4j/%s"; |
| 57 | + |
| 58 | + private String adminPassword = DEFAULT_ADMIN_PASSWORD; |
| 59 | + |
| 60 | + private boolean defaultImage = false; |
| 61 | + |
| 62 | + /** |
| 63 | + * Creates a Testcontainer using the official Neo4j docker image. |
| 64 | + */ |
| 65 | + public Neo4jContainer() { |
| 66 | + this(DOCKER_IMAGE_NAME); |
| 67 | + |
| 68 | + this.defaultImage = true; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Creates a Testcontainer using a specific docker image. |
| 73 | + * |
| 74 | + * @param dockerImageName The docker image to use. |
| 75 | + */ |
| 76 | + public Neo4jContainer(String dockerImageName) { |
| 77 | + super(dockerImageName); |
| 78 | + |
| 79 | + WaitStrategy waitForBolt = new LogMessageWaitStrategy() |
| 80 | + .withRegEx(String.format(".*Bolt enabled on 0\\.0\\.0\\.0:%d\\.\n", DEFAULT_BOLT_PORT)); |
| 81 | + WaitStrategy waitForHttp = new HttpWaitStrategy() |
| 82 | + .forPort(DEFAULT_HTTP_PORT) |
| 83 | + .forStatusCodeMatching(response -> response == HTTP_OK); |
| 84 | + |
| 85 | + this.waitStrategy = new WaitAllStrategy() |
| 86 | + .withStrategy(waitForBolt) |
| 87 | + .withStrategy(waitForHttp) |
| 88 | + .withStartupTimeout(Duration.ofMinutes(2)); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public Set<Integer> getLivenessCheckPortNumbers() { |
| 93 | + |
| 94 | + return Stream.of(DEFAULT_BOLT_PORT, DEFAULT_HTTP_PORT, DEFAULT_HTTPS_PORT) |
| 95 | + .map(this::getMappedPort) |
| 96 | + .collect(toSet()); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + protected void configure() { |
| 101 | + |
| 102 | + addExposedPorts(DEFAULT_BOLT_PORT, DEFAULT_HTTP_PORT, DEFAULT_HTTPS_PORT); |
| 103 | + |
| 104 | + boolean emptyAdminPassword = this.adminPassword == null || this.adminPassword.isEmpty(); |
| 105 | + String neo4jAuth = emptyAdminPassword ? "none" : String.format(AUTH_FORMAT, this.adminPassword); |
| 106 | + addEnv("NEO4J_AUTH", neo4jAuth); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @return Bolt URL for use with Neo4j's Java-Driver. |
| 111 | + */ |
| 112 | + public String getBoltUrl() { |
| 113 | + return String.format("bolt://" + getContainerIpAddress() + ":" + getMappedPort(DEFAULT_BOLT_PORT)); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @return URL of the transactional HTTP endpoint. |
| 118 | + */ |
| 119 | + public String getHttpUrl() { |
| 120 | + return String.format("http://" + getContainerIpAddress() + ":" + getMappedPort(DEFAULT_HTTP_PORT)); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @return URL of the transactional HTTPS endpoint. |
| 125 | + */ |
| 126 | + public String getHttpsUrl() { |
| 127 | + return String.format("https://" + getContainerIpAddress() + ":" + getMappedPort(DEFAULT_HTTPS_PORT)); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Configures the container to use the enterprise edition of the default docker image. |
| 132 | + * <br><br> |
| 133 | + * Please have a look at the <a href="https://neo4j.com/licensing/">Neo4j Licensing page</a>. While the Neo4j |
| 134 | + * Community Edition can be used for free in your projects under the GPL v3 license, Neo4j Enterprise edition |
| 135 | + * needs either a commercial, education or evaluation license. |
| 136 | + * |
| 137 | + * @return This container. |
| 138 | + */ |
| 139 | + public S withEnterpriseEdition() { |
| 140 | + |
| 141 | + if (!defaultImage) { |
| 142 | + throw new IllegalStateException( |
| 143 | + String.format("Cannot use enterprise version with alternative image %s.", getDockerImageName())); |
| 144 | + } |
| 145 | + |
| 146 | + setDockerImageName(DOCKER_IMAGE_NAME + "-enterprise"); |
| 147 | + LicenseAcceptance.assertLicenseAccepted(getDockerImageName()); |
| 148 | + |
| 149 | + addEnv("NEO4J_ACCEPT_LICENSE_AGREEMENT", "yes"); |
| 150 | + |
| 151 | + return self(); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Sets the admin password for the default account (which is <pre>neo4j</pre>). A null value or an empty string |
| 156 | + * disables authentication. |
| 157 | + * |
| 158 | + * @param adminPassword The admin password for the default database account. |
| 159 | + * @return This container. |
| 160 | + */ |
| 161 | + public S withAdminPassword(final String adminPassword) { |
| 162 | + |
| 163 | + this.adminPassword = adminPassword; |
| 164 | + return self(); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * @return The admin password for the <code>neo4j</code> account or literal <code>null</code> if auth is disabled. |
| 169 | + */ |
| 170 | + public String getAdminPassword() { |
| 171 | + return adminPassword; |
| 172 | + } |
| 173 | +} |
0 commit comments