Move Neo4j Docker Compose support into spring-boot-neo4j

This commit is contained in:
Phillip Webb
2025-05-19 19:32:21 -07:00
parent d5809e4fe8
commit 081fd9bfe0
10 changed files with 9 additions and 7 deletions

View File

@@ -1,55 +0,0 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.docker.compose.service.connection.neo4j;
import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
import org.springframework.boot.docker.compose.service.connection.test.DockerComposeTest;
import org.springframework.boot.neo4j.autoconfigure.Neo4jConnectionDetails;
import org.springframework.boot.testsupport.container.TestImage;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNoException;
/**
* Integration tests for {@link Neo4jDockerComposeConnectionDetailsFactory}.
*
* @author Andy Wilkinson
* @author Scott Frederick
*/
class Neo4jDockerComposeConnectionDetailsFactoryIntegrationTests {
@DockerComposeTest(composeFile = "neo4j-compose.yaml", image = TestImage.NEO4J)
void runCreatesConnectionDetailsThatCanAccessNeo4j(Neo4jConnectionDetails connectionDetails) {
assertConnectionDetailsWithPassword(connectionDetails, "secret");
}
@DockerComposeTest(composeFile = "neo4j-bitnami-compose.yaml", image = TestImage.BITNAMI_NEO4J)
void runWithBitnamiImageCreatesConnectionDetailsThatCanAccessNeo4j(Neo4jConnectionDetails connectionDetails) {
assertConnectionDetailsWithPassword(connectionDetails, "bitnami2");
}
private void assertConnectionDetailsWithPassword(Neo4jConnectionDetails connectionDetails, String password) {
assertThat(connectionDetails.getAuthToken()).isEqualTo(AuthTokens.basic("neo4j", password));
try (Driver driver = GraphDatabase.driver(connectionDetails.getUri(), connectionDetails.getAuthToken())) {
assertThatNoException().isThrownBy(driver::verifyConnectivity);
}
}
}

View File

@@ -1,7 +0,0 @@
services:
neo4j:
image: 'bitnami/neo4j:5.16.0'
ports:
- '7687'
environment:
- 'NEO4J_PASSWORD=bitnami2'

View File

@@ -1,8 +0,0 @@
services:
neo4j:
image: '{imageName}'
ports:
- '7687'
environment:
- 'NEO4J_AUTH=neo4j/secret'

View File

@@ -1,79 +0,0 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.docker.compose.service.connection.neo4j;
import java.net.URI;
import org.neo4j.driver.AuthToken;
import org.springframework.boot.docker.compose.core.RunningService;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionSource;
import org.springframework.boot.neo4j.autoconfigure.Neo4jConnectionDetails;
/**
* {@link DockerComposeConnectionDetailsFactory} to create {@link Neo4jConnectionDetails}
* for a {@code Neo4j} service.
*
* @author Andy Wilkinson
* @author Scott Frederick
*/
class Neo4jDockerComposeConnectionDetailsFactory extends DockerComposeConnectionDetailsFactory<Neo4jConnectionDetails> {
private static final String[] NEO4J_CONTAINER_NAMES = { "neo4j", "bitnami/neo4j" };
Neo4jDockerComposeConnectionDetailsFactory() {
super(NEO4J_CONTAINER_NAMES);
}
@Override
protected Neo4jConnectionDetails getDockerComposeConnectionDetails(DockerComposeConnectionSource source) {
return new Neo4jDockerComposeConnectionDetails(source.getRunningService());
}
/**
* {@link Neo4jConnectionDetails} backed by a {@code Neo4j} {@link RunningService}.
*/
static class Neo4jDockerComposeConnectionDetails extends DockerComposeConnectionDetails
implements Neo4jConnectionDetails {
private static final int BOLT_PORT = 7687;
private final AuthToken authToken;
private final URI uri;
Neo4jDockerComposeConnectionDetails(RunningService service) {
super(service);
Neo4jEnvironment neo4jEnvironment = new Neo4jEnvironment(service.env());
this.authToken = neo4jEnvironment.getAuthToken();
this.uri = URI.create("neo4j://%s:%d".formatted(service.host(), service.ports().get(BOLT_PORT)));
}
@Override
public URI getUri() {
return this.uri;
}
@Override
public AuthToken getAuthToken() {
return this.authToken;
}
}
}

View File

@@ -1,62 +0,0 @@
/*
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.docker.compose.service.connection.neo4j;
import java.util.Map;
import org.neo4j.driver.AuthToken;
import org.neo4j.driver.AuthTokens;
/**
* Neo4j environment details.
*
* @author Andy Wilkinson
* @author Scott Frederick
*/
class Neo4jEnvironment {
private final AuthToken authToken;
Neo4jEnvironment(Map<String, String> env) {
AuthToken authToken = parse(env.get("NEO4J_AUTH"));
if (authToken == null && env.containsKey("NEO4J_PASSWORD")) {
authToken = parse("neo4j/" + env.get("NEO4J_PASSWORD"));
}
this.authToken = authToken;
}
private AuthToken parse(String neo4jAuth) {
if (neo4jAuth == null) {
return null;
}
if ("none".equals(neo4jAuth)) {
return AuthTokens.none();
}
if (neo4jAuth.startsWith("neo4j/")) {
return AuthTokens.basic("neo4j", neo4jAuth.substring(6));
}
throw new IllegalStateException(
"Cannot extract auth token from NEO4J_AUTH environment variable with value '" + neo4jAuth + "'."
+ " Value should be 'none' to disable authentication or start with 'neo4j/' to specify"
+ " the neo4j user's password");
}
AuthToken getAuthToken() {
return this.authToken;
}
}

View File

@@ -1,20 +0,0 @@
/*
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Auto-configuration for Docker Compose Neo4j service connections.
*/
package org.springframework.boot.docker.compose.service.connection.neo4j;

View File

@@ -3,7 +3,6 @@ org.springframework.boot.autoconfigure.service.connection.ConnectionDetailsFacto
org.springframework.boot.docker.compose.service.connection.clickhouse.ClickHouseR2dbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.mariadb.MariaDbR2dbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.mysql.MySqlR2dbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.neo4j.Neo4jDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.oracle.OracleFreeR2dbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.oracle.OracleXeR2dbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.otlp.OpenTelemetryLoggingDockerComposeConnectionDetailsFactory,\

View File

@@ -1,66 +0,0 @@
/*
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.docker.compose.service.connection.neo4j;
import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.neo4j.driver.AuthTokens;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Tests for {@link Neo4jEnvironment}.
*
* @author Andy Wilkinson
* @author Scott Frederick
*/
class Neo4jEnvironmentTests {
@Test
void whenNeo4jAuthAndPasswordAreNullThenAuthTokenIsNull() {
Neo4jEnvironment environment = new Neo4jEnvironment(Collections.emptyMap());
assertThat(environment.getAuthToken()).isNull();
}
@Test
void whenNeo4jAuthIsNoneThenAuthTokenIsNone() {
Neo4jEnvironment environment = new Neo4jEnvironment(Map.of("NEO4J_AUTH", "none"));
assertThat(environment.getAuthToken()).isEqualTo(AuthTokens.none());
}
@Test
void whenNeo4jAuthIsNeo4jSlashPasswordThenAuthTokenIsBasic() {
Neo4jEnvironment environment = new Neo4jEnvironment(Map.of("NEO4J_AUTH", "neo4j/custom-password"));
assertThat(environment.getAuthToken()).isEqualTo(AuthTokens.basic("neo4j", "custom-password"));
}
@Test
void whenNeo4jAuthIsNeitherNoneNorNeo4jSlashPasswordEnvironmentCreationThrows() {
assertThatIllegalStateException()
.isThrownBy(() -> new Neo4jEnvironment(Map.of("NEO4J_AUTH", "graphdb/custom-password")));
}
@Test
void whenNeo4jPasswordIsProvidedThenAuthTokenIsBasic() {
Neo4jEnvironment environment = new Neo4jEnvironment(Map.of("NEO4J_PASSWORD", "custom-password"));
assertThat(environment.getAuthToken()).isEqualTo(AuthTokens.basic("neo4j", "custom-password"));
}
}