Move MongoDB Docker Compose support into spring-boot-mongodb

This commit is contained in:
Phillip Webb
2025-05-19 19:31:40 -07:00
parent 2b44908fe8
commit d5809e4fe8
10 changed files with 31 additions and 7 deletions

View File

@@ -1,59 +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.mongo;
import com.mongodb.ConnectionString;
import org.junit.jupiter.api.condition.OS;
import org.springframework.boot.docker.compose.service.connection.test.DockerComposeTest;
import org.springframework.boot.mongodb.autoconfigure.MongoConnectionDetails;
import org.springframework.boot.testsupport.container.TestImage;
import org.springframework.boot.testsupport.junit.DisabledOnOs;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for {@link MongoDockerComposeConnectionDetailsFactory}.
*
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
*/
class MongoDockerComposeConnectionDetailsFactoryIntegrationTests {
@DockerComposeTest(composeFile = "mongo-compose.yaml", image = TestImage.MONGODB)
void runCreatesConnectionDetails(MongoConnectionDetails connectionDetails) {
assertConnectionDetailsWithDatabase(connectionDetails, "mydatabase");
}
@DisabledOnOs(os = { OS.LINUX, OS.MAC }, architecture = "aarch64", disabledReason = "The image has no ARM support")
@DockerComposeTest(composeFile = "mongo-bitnami-compose.yaml", image = TestImage.BITNAMI_MONGODB)
void runWithBitnamiImageCreatesConnectionDetails(MongoConnectionDetails connectionDetails) {
assertConnectionDetailsWithDatabase(connectionDetails, "testdb");
}
private void assertConnectionDetailsWithDatabase(MongoConnectionDetails connectionDetails, String database) {
ConnectionString connectionString = connectionDetails.getConnectionString();
assertThat(connectionString.getCredential().getUserName()).isEqualTo("root");
assertThat(connectionString.getCredential().getPassword()).isEqualTo("secret".toCharArray());
assertThat(connectionString.getCredential().getSource()).isEqualTo("admin");
assertThat(connectionString.getDatabase()).isEqualTo(database);
assertThat(connectionDetails.getGridFs()).isNull();
}
}

View File

@@ -1,92 +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.mongo;
import com.mongodb.ConnectionString;
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.mongodb.autoconfigure.MongoConnectionDetails;
/**
* {@link DockerComposeConnectionDetailsFactory} to create {@link MongoConnectionDetails}
* for a {@code mongo} service.
*
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
*/
class MongoDockerComposeConnectionDetailsFactory extends DockerComposeConnectionDetailsFactory<MongoConnectionDetails> {
private static final String[] MONGODB_CONTAINER_NAMES = { "mongo", "bitnami/mongodb" };
private static final int MONGODB_PORT = 27017;
protected MongoDockerComposeConnectionDetailsFactory() {
super(MONGODB_CONTAINER_NAMES, "com.mongodb.ConnectionString");
}
@Override
protected MongoDockerComposeConnectionDetails getDockerComposeConnectionDetails(
DockerComposeConnectionSource source) {
return new MongoDockerComposeConnectionDetails(source.getRunningService());
}
/**
* {@link MongoConnectionDetails} backed by a {@code mongo} {@link RunningService}.
*/
static class MongoDockerComposeConnectionDetails extends DockerComposeConnectionDetails
implements MongoConnectionDetails {
private final ConnectionString connectionString;
MongoDockerComposeConnectionDetails(RunningService service) {
super(service);
this.connectionString = buildConnectionString(service);
}
private ConnectionString buildConnectionString(RunningService service) {
MongoEnvironment environment = new MongoEnvironment(service.env());
StringBuilder builder = new StringBuilder("mongodb://");
if (environment.getUsername() != null) {
builder.append(environment.getUsername());
builder.append(":");
builder.append((environment.getPassword() != null) ? environment.getPassword() : "");
builder.append("@");
}
builder.append(service.host());
builder.append(":");
builder.append(service.ports().get(MONGODB_PORT));
builder.append("/");
builder.append((environment.getDatabase() != null) ? environment.getDatabase() : "test");
if (environment.getUsername() != null) {
builder.append("?authSource=admin");
}
return new ConnectionString(builder.toString());
}
@Override
public ConnectionString getConnectionString() {
return this.connectionString;
}
}
}

View File

@@ -1,61 +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.mongo;
import java.util.Map;
import org.springframework.util.Assert;
/**
* MongoDB environment details.
*
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
*/
class MongoEnvironment {
private final String username;
private final String password;
private final String database;
MongoEnvironment(Map<String, String> env) {
Assert.state(!env.containsKey("MONGO_INITDB_ROOT_USERNAME_FILE"),
"MONGO_INITDB_ROOT_USERNAME_FILE is not supported");
Assert.state(!env.containsKey("MONGO_INITDB_ROOT_PASSWORD_FILE"),
"MONGO_INITDB_ROOT_PASSWORD_FILE is not supported");
this.username = env.getOrDefault("MONGO_INITDB_ROOT_USERNAME", env.get("MONGODB_ROOT_USERNAME"));
this.password = env.getOrDefault("MONGO_INITDB_ROOT_PASSWORD", env.get("MONGODB_ROOT_PASSWORD"));
this.database = env.getOrDefault("MONGO_INITDB_DATABASE", env.get("MONGODB_DATABASE"));
}
String getUsername() {
return this.username;
}
String getPassword() {
return this.password;
}
String getDatabase() {
return this.database;
}
}

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 MongoDB service connections.
*/
package org.springframework.boot.docker.compose.service.connection.mongo;

View File

@@ -2,7 +2,6 @@
org.springframework.boot.autoconfigure.service.connection.ConnectionDetailsFactory=\
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.mongo.MongoDockerComposeConnectionDetailsFactory,\
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,\

View File

@@ -1,86 +0,0 @@
/*
* Copyright 2012-2023 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.mongo;
import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Tests for {@link MongoEnvironment}.
*
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
*/
class MongoEnvironmentTests {
@Test
void createWhenMonoInitdbRootUsernameFileSetThrowsException() {
assertThatIllegalStateException()
.isThrownBy(() -> new MongoEnvironment(Map.of("MONGO_INITDB_ROOT_USERNAME_FILE", "file")))
.withMessage("MONGO_INITDB_ROOT_USERNAME_FILE is not supported");
}
@Test
void createWhenMonoInitdbRootPasswordFileSetThrowsException() {
assertThatIllegalStateException()
.isThrownBy(() -> new MongoEnvironment(Map.of("MONGO_INITDB_ROOT_PASSWORD_FILE", "file")))
.withMessage("MONGO_INITDB_ROOT_PASSWORD_FILE is not supported");
}
@Test
void getUsernameWhenHasNoMongoInitdbRootUsernameSet() {
MongoEnvironment environment = new MongoEnvironment(Collections.emptyMap());
assertThat(environment.getUsername()).isNull();
}
@Test
void getUsernameWhenHasMongoInitdbRootUsernameSet() {
MongoEnvironment environment = new MongoEnvironment(Map.of("MONGO_INITDB_ROOT_USERNAME", "user"));
assertThat(environment.getUsername()).isEqualTo("user");
}
@Test
void getPasswordWhenHasNoMongoInitdbRootPasswordSet() {
MongoEnvironment environment = new MongoEnvironment(Collections.emptyMap());
assertThat(environment.getPassword()).isNull();
}
@Test
void getPasswordWhenHasMongoInitdbRootPasswordSet() {
MongoEnvironment environment = new MongoEnvironment(Map.of("MONGO_INITDB_ROOT_PASSWORD", "secret"));
assertThat(environment.getPassword()).isEqualTo("secret");
}
@Test
void getDatabaseWhenHasNoMongoInitdbDatabaseSet() {
MongoEnvironment environment = new MongoEnvironment(Collections.emptyMap());
assertThat(environment.getDatabase()).isNull();
}
@Test
void getDatabaseWhenHasMongoInitdbDatabaseSet() {
MongoEnvironment environment = new MongoEnvironment(Map.of("MONGO_INITDB_DATABASE", "db"));
assertThat(environment.getDatabase()).isEqualTo("db");
}
}