@@ -66,6 +66,16 @@ public class Neo4jContainer<S extends Neo4jContainer<S>> extends GenericContaine
6666
6767 private final Set <String > labsPlugins = new HashSet <>();
6868
69+ /**
70+ * Default wait strategies
71+ */
72+ public static final WaitStrategy WAIT_FOR_BOLT = new LogMessageWaitStrategy ()
73+ .withRegEx (String .format (".*Bolt enabled on .*:%d\\ .\n " , DEFAULT_BOLT_PORT ));
74+
75+ private static final WaitStrategy WAIT_FOR_HTTP = new HttpWaitStrategy ()
76+ .forPort (DEFAULT_HTTP_PORT )
77+ .forStatusCodeMatching (response -> response == HttpURLConnection .HTTP_OK );
78+
6979 /**
7080 * Creates a Neo4jContainer using the official Neo4j docker image.
7181 * @deprecated use {@link Neo4jContainer(DockerImageName)} instead
@@ -95,16 +105,10 @@ public Neo4jContainer(final DockerImageName dockerImageName) {
95105
96106 dockerImageName .assertCompatibleWith (DEFAULT_IMAGE_NAME );
97107
98- WaitStrategy waitForBolt = new LogMessageWaitStrategy ()
99- .withRegEx (String .format (".*Bolt enabled on .*:%d\\ .\n " , DEFAULT_BOLT_PORT ));
100- WaitStrategy waitForHttp = new HttpWaitStrategy ()
101- .forPort (DEFAULT_HTTP_PORT )
102- .forStatusCodeMatching (response -> response == HttpURLConnection .HTTP_OK );
103-
104108 this .waitStrategy =
105109 new WaitAllStrategy ()
106- .withStrategy (waitForBolt )
107- .withStrategy (waitForHttp )
110+ .withStrategy (WAIT_FOR_BOLT )
111+ .withStrategy (WAIT_FOR_HTTP )
108112 .withStartupTimeout (Duration .ofMinutes (2 ));
109113
110114 addExposedPorts (DEFAULT_BOLT_PORT , DEFAULT_HTTP_PORT , DEFAULT_HTTPS_PORT );
@@ -120,15 +124,56 @@ public Set<Integer> getLivenessCheckPortNumbers() {
120124
121125 @ Override
122126 protected void configure () {
123- boolean emptyAdminPassword = this .adminPassword == null || this .adminPassword .isEmpty ();
124- String neo4jAuth = emptyAdminPassword ? "none" : String .format (AUTH_FORMAT , this .adminPassword );
125- addEnv ("NEO4J_AUTH" , neo4jAuth );
127+ configureAuth ();
128+ configureLabsPlugins ();
129+ configureWaitStrategy ();
130+ }
131+
132+ /**
133+ * Configured via {@link Neo4jContainer#withAdminPassword(String)} or {@link Neo4jContainer#withoutAuthentication()}
134+ * It is only possible to set the correct auth in the configuration call.
135+ * Also, the custom methods overrule the set env parameter.
136+ */
137+ private void configureAuth () {
138+ String neo4jAuthEnvKey = "NEO4J_AUTH" ;
139+ if (!getEnvMap ().containsKey (neo4jAuthEnvKey ) || !DEFAULT_ADMIN_PASSWORD .equals (this .adminPassword )) {
140+ boolean emptyAdminPassword = this .adminPassword == null || this .adminPassword .isEmpty ();
141+ String neo4jAuth = emptyAdminPassword ? "none" : String .format (AUTH_FORMAT , this .adminPassword );
142+ addEnv (neo4jAuthEnvKey , neo4jAuth );
143+ }
144+ }
126145
127- if (!this .labsPlugins .isEmpty ()) {
146+ /**
147+ * Configured via {@link Neo4jContainer#withLabsPlugins}.
148+ * Configuration can only happen in the configuration call because there is no default.
149+ */
150+ private void configureLabsPlugins () {
151+ String neo4jLabsPluginsEnvKey = "NEO4JLABS_PLUGINS" ;
152+ if (!getEnv ().contains (neo4jLabsPluginsEnvKey ) && !this .labsPlugins .isEmpty ()) {
128153 String enabledPlugins =
129154 this .labsPlugins .stream ().map (pluginName -> "\" " + pluginName + "\" " ).collect (Collectors .joining ("," ));
130155
131- addEnv ("NEO4JLABS_PLUGINS" , "[" + enabledPlugins + "]" );
156+ addEnv (neo4jLabsPluginsEnvKey , "[" + enabledPlugins + "]" );
157+ }
158+ }
159+
160+ /**
161+ * Update the default Neo4jContainer wait strategy based on the exposed ports.
162+ * Still possible to override the startup timeout before starting the container via {@link WaitStrategy#withStartupTimeout(Duration)}.
163+ */
164+ private void configureWaitStrategy () {
165+ List <Integer > exposedPorts = getExposedPorts ();
166+ boolean boltExposed = exposedPorts .contains (DEFAULT_BOLT_PORT );
167+ boolean httpExposed = exposedPorts .contains (DEFAULT_HTTP_PORT );
168+ boolean onlyBoltExposed = boltExposed && !httpExposed ;
169+ boolean onlyHttpExposed = !boltExposed && httpExposed ;
170+
171+ if (onlyBoltExposed ) {
172+ this .waitStrategy =
173+ new WaitAllStrategy ().withStrategy (WAIT_FOR_BOLT ).withStartupTimeout (Duration .ofMinutes (2 ));
174+ } else if (onlyHttpExposed ) {
175+ this .waitStrategy =
176+ new WaitAllStrategy ().withStrategy (WAIT_FOR_HTTP ).withStartupTimeout (Duration .ofMinutes (2 ));
132177 }
133178 }
134179
0 commit comments