Skip to content

Commit e1d9bfc

Browse files
eddumelendezmarkpollack
authored andcommitted
Add MongoDB Atlas support for TestContainers and Docker Compose
Implement service connection support for MongoDB Atlas using both TestContainers and Docker Compose. This change enables easier integration testing and local development with MongoDB Atlas. - Leverage TestContainers 1.20.2 which introduces MongoDBAtlasLocalContainer - Add TestContainers support using MongoDBAtlasLocalContainer - Implement Docker Compose configuration for MongoDB Atlas - Create connection details factories for both TestContainers and Docker Compose - Update dependency management for MongoDB Atlas integration - Add integration tests for both TestContainers and Docker Compose setups - Update documentation to include MongoDB Atlas support
1 parent e58341a commit e1d9bfc

File tree

11 files changed

+311
-0
lines changed

11 files changed

+311
-0
lines changed

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/docker-compose.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ The following service connection factories are provided in the `spring-ai-spring
3434
| `ChromaConnectionDetails`
3535
| Containers named `chromadb/chroma`, `ghcr.io/chroma-core/chroma`
3636

37+
| `MongoConnectionDetails`
38+
| Containers named `mongodb/mongodb-atlas-local`
39+
3740
| `OllamaConnectionDetails`
3841
| Containers named `ollama/ollama`
3942

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/testcontainers.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ The following service connection factories are provided in the `spring-ai-spring
3737
| `MilvusServiceClientConnectionDetails`
3838
| Containers of type `MilvusContainer`
3939

40+
| `MongoConnectionDetails`
41+
| Containers of type `MongoDBAtlasLocalContainer`
42+
4043
| `OllamaConnectionDetails`
4144
| Containers of type `OllamaContainer`
4245

spring-ai-spring-boot-docker-compose/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@
123123
<optional>true</optional>
124124
</dependency>
125125

126+
<dependency>
127+
<groupId>org.springframework.ai</groupId>
128+
<artifactId>spring-ai-mongodb-atlas-store</artifactId>
129+
<version>${project.parent.version}</version>
130+
<optional>true</optional>
131+
</dependency>
132+
126133
<!-- test dependencies -->
127134

128135
<dependency>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2023 - 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.ai.docker.compose.service.connection.mongo;
17+
18+
import com.mongodb.ConnectionString;
19+
import org.springframework.boot.autoconfigure.mongo.MongoConnectionDetails;
20+
import org.springframework.boot.docker.compose.core.RunningService;
21+
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory;
22+
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionSource;
23+
24+
/**
25+
* A {@link DockerComposeConnectionDetailsFactory} implementation that creates
26+
* {@link MongoConnectionDetails} for a MongoDB Atlas Local instance running in a Docker
27+
* container.
28+
*
29+
* <p>
30+
* This factory is designed to work with Docker Compose configurations that include a
31+
* MongoDB Atlas Local container. It provides the necessary connection details for Spring
32+
* Boot applications to connect to the MongoDB instance.
33+
*
34+
* <p>
35+
* The factory matches containers with the image name "mongodb/mongodb-atlas-local".
36+
*
37+
* <p>
38+
* Usage of this factory requires the presence of the Spring Boot Docker Compose support
39+
* and the MongoDB driver on the classpath.
40+
*
41+
* @author Eddú Meléndez
42+
* @see DockerComposeConnectionDetailsFactory
43+
* @see MongoConnectionDetails
44+
* @see org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionSource
45+
* @since 1.0.0
46+
*/
47+
class MongoDbAtlasLocalDockerComposeConnectionDetailsFactory
48+
extends DockerComposeConnectionDetailsFactory<MongoConnectionDetails> {
49+
50+
private static final int MONGODB_PORT = 27017;
51+
52+
protected MongoDbAtlasLocalDockerComposeConnectionDetailsFactory() {
53+
super("mongodb/mongodb-atlas-local");
54+
}
55+
56+
@Override
57+
protected MongoConnectionDetails getDockerComposeConnectionDetails(DockerComposeConnectionSource source) {
58+
return new MongoDbAtlasLocalContainerConnectionDetails(source.getRunningService());
59+
}
60+
61+
/**
62+
* {@link MongoConnectionDetails} backed by a {@code MongoDB Atlas}
63+
* {@link RunningService}.
64+
*/
65+
static class MongoDbAtlasLocalContainerConnectionDetails extends DockerComposeConnectionDetails
66+
implements MongoConnectionDetails {
67+
68+
private final String connectionString;
69+
70+
MongoDbAtlasLocalContainerConnectionDetails(RunningService service) {
71+
super(service);
72+
this.connectionString = String.format("mongodb://%s:%d/?directConnection=true", service.host(),
73+
service.ports().get(MONGODB_PORT));
74+
}
75+
76+
@Override
77+
public ConnectionString getConnectionString() {
78+
return new ConnectionString(this.connectionString);
79+
}
80+
81+
}
82+
83+
}

spring-ai-spring-boot-docker-compose/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
org.springframework.boot.autoconfigure.service.connection.ConnectionDetailsFactory=\
22
org.springframework.ai.docker.compose.service.connection.chroma.ChromaDockerComposeConnectionDetailsFactory,\
3+
org.springframework.ai.docker.compose.service.connection.mongo.MongoDbAtlasLocalDockerComposeConnectionDetailsFactory,\
34
org.springframework.ai.docker.compose.service.connection.ollama.OllamaDockerComposeConnectionDetailsFactory,\
45
org.springframework.ai.docker.compose.service.connection.opensearch.OpenSearchDockerComposeConnectionDetailsFactory,\
56
org.springframework.ai.docker.compose.service.connection.qdrant.QdrantDockerComposeConnectionDetailsFactory,\
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.springframework.ai.docker.compose.service.connection.mongo;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.boot.autoconfigure.mongo.MongoConnectionDetails;
5+
import org.springframework.boot.docker.compose.service.connection.test.AbstractDockerComposeIntegrationTests;
6+
import org.testcontainers.utility.DockerImageName;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
class MongoDbAtlasLocalDockerComposeConnectionDetailsFactoryTests extends AbstractDockerComposeIntegrationTests {
11+
12+
protected MongoDbAtlasLocalDockerComposeConnectionDetailsFactoryTests() {
13+
super("mongo-compose.yaml", DockerImageName.parse("mongodb/mongodb-atlas-local"));
14+
}
15+
16+
@Test
17+
void runCreatesConnectionDetails() {
18+
MongoConnectionDetails connectionDetails = run(MongoConnectionDetails.class);
19+
assertThat(connectionDetails.getConnectionString()).isNotNull();
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
mongo:
3+
image: '{imageName}'
4+
ports:
5+
- '27017'

spring-ai-spring-boot-testcontainers/pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@
131131
<optional>true</optional>
132132
</dependency>
133133

134+
<dependency>
135+
<groupId>org.springframework.ai</groupId>
136+
<artifactId>spring-ai-mongodb-atlas-store</artifactId>
137+
<version>${project.parent.version}</version>
138+
<optional>true</optional>
139+
</dependency>
140+
134141
<!-- test dependencies -->
135142

136143
<dependency>
@@ -218,6 +225,13 @@
218225
<optional>true</optional>
219226
</dependency>
220227

228+
<dependency>
229+
<groupId>org.testcontainers</groupId>
230+
<artifactId>mongodb</artifactId>
231+
<version>1.20.2</version>
232+
<optional>true</optional>
233+
</dependency>
234+
221235
<dependency>
222236
<groupId>org.testcontainers</groupId>
223237
<artifactId>ollama</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2023 - 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.ai.testcontainers.service.connection.mongo;
17+
18+
import com.mongodb.ConnectionString;
19+
import org.springframework.boot.autoconfigure.mongo.MongoConnectionDetails;
20+
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionDetailsFactory;
21+
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionSource;
22+
import org.testcontainers.mongodb.MongoDBAtlasLocalContainer;
23+
24+
/**
25+
* A {@link ContainerConnectionDetailsFactory} implementation that provides
26+
* {@link MongoConnectionDetails} for a {@link MongoDBAtlasLocalContainer}.
27+
* <p>
28+
* This factory is used in conjunction with Spring Boot's auto-configuration for
29+
* Testcontainers to automatically create and configure a connection to a MongoDB instance
30+
* running in a testcontainer.
31+
* <p>
32+
* It generates {@link MongoConnectionDetails} based on the connection information
33+
* provided by the {@link MongoDBAtlasLocalContainer}, allowing integration of MongoDB
34+
* testcontainers in Spring Boot applications.
35+
*
36+
* @author Eddú Meléndez
37+
* @since 1.0.0
38+
* @see ContainerConnectionDetailsFactory
39+
* @see MongoConnectionDetails
40+
* @see MongoDBAtlasLocalContainer
41+
*/
42+
class MongoDbAtlasLocalContainerConnectionDetailsFactory
43+
extends ContainerConnectionDetailsFactory<MongoDBAtlasLocalContainer, MongoConnectionDetails> {
44+
45+
@Override
46+
protected MongoConnectionDetails getContainerConnectionDetails(
47+
ContainerConnectionSource<MongoDBAtlasLocalContainer> source) {
48+
return new MongoDbAtlasLocalContainerConnectionDetails(source);
49+
}
50+
51+
/**
52+
* {@link MongoConnectionDetails} backed by a {@link ContainerConnectionSource}.
53+
*/
54+
private static final class MongoDbAtlasLocalContainerConnectionDetails
55+
extends ContainerConnectionDetails<MongoDBAtlasLocalContainer> implements MongoConnectionDetails {
56+
57+
private MongoDbAtlasLocalContainerConnectionDetails(
58+
ContainerConnectionSource<MongoDBAtlasLocalContainer> source) {
59+
super(source);
60+
}
61+
62+
@Override
63+
public ConnectionString getConnectionString() {
64+
return new ConnectionString(getContainer().getConnectionString());
65+
}
66+
67+
}
68+
69+
}

spring-ai-spring-boot-testcontainers/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
org.springframework.boot.autoconfigure.service.connection.ConnectionDetailsFactory=\
22
org.springframework.ai.testcontainers.service.connection.chroma.ChromaContainerConnectionDetailsFactory,\
33
org.springframework.ai.testcontainers.service.connection.milvus.MilvusContainerConnectionDetailsFactory,\
4+
org.springframework.ai.testcontainers.service.connection.mongo.MongoDbAtlasLocalContainerConnectionDetailsFactory,\
45
org.springframework.ai.testcontainers.service.connection.ollama.OllamaContainerConnectionDetailsFactory,\
56
org.springframework.ai.testcontainers.service.connection.opensearch.OpenSearchContainerConnectionDetailsFactory,\
67
org.springframework.ai.testcontainers.service.connection.qdrant.QdrantContainerConnectionDetailsFactory,\

0 commit comments

Comments
 (0)