From c63661c3bca23638c7ec3b028f44637838efc743 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Thu, 3 Oct 2024 12:52:41 -0600 Subject: [PATCH] Generate test app when Spring AI MongoDB Atlas is selected See gh-1660 --- .../SimpleDockerServiceResolver.java | 8 +++ ...DbAtlasProjectGenerationConfiguration.java | 56 +++++++++++++++++++ .../TestcontainersModuleRegistry.java | 3 +- .../main/resources/META-INF/spring.factories | 1 + ...asProjectGenerationConfigurationTests.java | 48 ++++++++++++++++ ...rsProjectGenerationConfigurationTests.java | 1 + .../test/resources/compose/mongodb-atlas.yaml | 5 ++ 7 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 start-site/src/main/java/io/spring/start/site/extension/dependency/springai/SpringAiMongoDbAtlasProjectGenerationConfiguration.java create mode 100644 start-site/src/test/java/io/spring/start/site/extension/dependency/springai/SpringAiMongoDbAtlasProjectGenerationConfigurationTests.java create mode 100644 start-site/src/test/resources/compose/mongodb-atlas.yaml diff --git a/start-site/src/main/java/io/spring/start/site/container/SimpleDockerServiceResolver.java b/start-site/src/main/java/io/spring/start/site/container/SimpleDockerServiceResolver.java index 5d158a9e0e0..bd9342b5c38 100644 --- a/start-site/src/main/java/io/spring/start/site/container/SimpleDockerServiceResolver.java +++ b/start-site/src/main/java/io/spring/start/site/container/SimpleDockerServiceResolver.java @@ -46,6 +46,7 @@ public SimpleDockerServiceResolver() { this.dockerServices.put("mariaDb", mariaDb()); this.dockerServices.put("milvus", milvus()); this.dockerServices.put("mongoDb", mongoDb()); + this.dockerServices.put("mongoDbAtlas", mongoDbAtlas()); this.dockerServices.put("mysql", mysql()); this.dockerServices.put("neo4j", neo4j()); this.dockerServices.put("ollama", ollama()); @@ -141,6 +142,13 @@ private static DockerService mongoDb() { return DockerService.withImageAndTag("mongo").website("https://hub.docker.com/_/mongo").ports(27017).build(); } + private static DockerService mongoDbAtlas() { + return DockerService.withImageAndTag("mongodb/mongodb-atlas-local") + .website("https://hub.docker.com/r/mongodb/mongodb-atlas-local") + .ports(27017) + .build(); + } + private static DockerService mysql() { return DockerService.withImageAndTag("mysql").website("https://hub.docker.com/_/mysql").ports(3306).build(); } diff --git a/start-site/src/main/java/io/spring/start/site/extension/dependency/springai/SpringAiMongoDbAtlasProjectGenerationConfiguration.java b/start-site/src/main/java/io/spring/start/site/extension/dependency/springai/SpringAiMongoDbAtlasProjectGenerationConfiguration.java new file mode 100644 index 00000000000..6f6bde95e69 --- /dev/null +++ b/start-site/src/main/java/io/spring/start/site/extension/dependency/springai/SpringAiMongoDbAtlasProjectGenerationConfiguration.java @@ -0,0 +1,56 @@ +/* + * 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 io.spring.start.site.extension.dependency.springai; + +import io.spring.initializr.generator.condition.ConditionalOnPlatformVersion; +import io.spring.initializr.generator.condition.ConditionalOnRequestedDependency; +import io.spring.initializr.generator.project.ProjectGenerationConfiguration; +import io.spring.start.site.container.ComposeFileCustomizer; +import io.spring.start.site.container.DockerServiceResolver; +import io.spring.start.site.container.ServiceConnections.ServiceConnection; +import io.spring.start.site.container.ServiceConnectionsCustomizer; + +import org.springframework.context.annotation.Bean; + +/** + * Configuration for generation of projects that depend on MongoDb Atlas. + * + * @author Eddú Meléndez + */ +@ProjectGenerationConfiguration +@ConditionalOnPlatformVersion("3.4.0") +@ConditionalOnRequestedDependency("spring-ai-vectordb-mongodb-atlas") +class SpringAiMongoDbAtlasProjectGenerationConfiguration { + + private static final String TESTCONTAINERS_CLASS_NAME = "org.testcontainers.mongodb.MongoDBAtlasLocalContainer"; + + @Bean + @ConditionalOnRequestedDependency("testcontainers") + ServiceConnectionsCustomizer mongodbAtlasServiceConnectionsCustomizer(DockerServiceResolver serviceResolver) { + return (serviceConnections) -> serviceResolver.doWith("mongoDbAtlas", + (service) -> serviceConnections.addServiceConnection( + ServiceConnection.ofContainer("mongoDbAtlas", service, TESTCONTAINERS_CLASS_NAME, false))); + } + + @Bean + @ConditionalOnRequestedDependency("docker-compose") + ComposeFileCustomizer mongodbAtlasComposeFileCustomizer(DockerServiceResolver serviceResolver) { + return (composeFile) -> serviceResolver.doWith("mongoDbAtlas", + (service) -> composeFile.services().add("mongodbatlas", service)); + } + +} diff --git a/start-site/src/main/java/io/spring/start/site/extension/dependency/testcontainers/TestcontainersModuleRegistry.java b/start-site/src/main/java/io/spring/start/site/extension/dependency/testcontainers/TestcontainersModuleRegistry.java index 858ed5206b8..dcfc7156acc 100644 --- a/start-site/src/main/java/io/spring/start/site/extension/dependency/testcontainers/TestcontainersModuleRegistry.java +++ b/start-site/src/main/java/io/spring/start/site/extension/dependency/testcontainers/TestcontainersModuleRegistry.java @@ -59,7 +59,8 @@ static Iterable create(Version platformVersion) { builders.add(onDependencies("data-elasticsearch", "spring-ai-vectordb-elasticsearch") .customizeBuild(addModule("elasticsearch")) .customizeHelpDocument(addReferenceLink("Elasticsearch Container", "elasticsearch/"))); - builders.add(onDependencies("data-mongodb", "data-mongodb-reactive").customizeBuild(addModule("mongodb")) + builders.add(onDependencies("data-mongodb", "data-mongodb-reactive", "spring-ai-vectordb-mongodb-atlas") + .customizeBuild(addModule("mongodb")) .customizeHelpDocument(addReferenceLink("MongoDB Module", "databases/mongodb/"))); builders.add(onDependencies("data-neo4j", "spring-ai-vectordb-neo4j").customizeBuild(addModule("neo4j")) .customizeHelpDocument(addReferenceLink("Neo4j Module", "databases/neo4j/"))); diff --git a/start-site/src/main/resources/META-INF/spring.factories b/start-site/src/main/resources/META-INF/spring.factories index 4d5022e73ec..d4480adf3ff 100644 --- a/start-site/src/main/resources/META-INF/spring.factories +++ b/start-site/src/main/resources/META-INF/spring.factories @@ -31,6 +31,7 @@ io.spring.start.site.extension.dependency.solace.SolaceProjectGenerationConfigur io.spring.start.site.extension.dependency.springai.SpringAiChromaProjectGenerationConfiguration,\ io.spring.start.site.extension.dependency.springai.SpringAiDockerComposeProjectGenerationConfiguration,\ io.spring.start.site.extension.dependency.springai.SpringAiMilvusProjectGenerationConfiguration,\ +io.spring.start.site.extension.dependency.springai.SpringAiMongoDbAtlasProjectGenerationConfiguration,\ io.spring.start.site.extension.dependency.springai.SpringAiOllamaProjectGenerationConfiguration,\ io.spring.start.site.extension.dependency.springai.SpringAiQdrantProjectGenerationConfiguration,\ io.spring.start.site.extension.dependency.springai.SpringAiTestcontainersProjectGenerationConfiguration,\ diff --git a/start-site/src/test/java/io/spring/start/site/extension/dependency/springai/SpringAiMongoDbAtlasProjectGenerationConfigurationTests.java b/start-site/src/test/java/io/spring/start/site/extension/dependency/springai/SpringAiMongoDbAtlasProjectGenerationConfigurationTests.java new file mode 100644 index 00000000000..76d7589f365 --- /dev/null +++ b/start-site/src/test/java/io/spring/start/site/extension/dependency/springai/SpringAiMongoDbAtlasProjectGenerationConfigurationTests.java @@ -0,0 +1,48 @@ +/* + * 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 io.spring.start.site.extension.dependency.springai; + +import io.spring.initializr.generator.test.project.ProjectStructure; +import io.spring.initializr.web.project.ProjectRequest; +import io.spring.start.site.extension.AbstractExtensionTests; +import org.junit.jupiter.api.Test; + +import org.springframework.core.io.ClassPathResource; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link SpringAiMongoDbAtlasProjectGenerationConfiguration} + * + * @author Eddú Meléndez + */ +class SpringAiMongoDbAtlasProjectGenerationConfigurationTests extends AbstractExtensionTests { + + @Test + void doesNothingWithoutDockerCompose() { + ProjectRequest request = createProjectRequest("web", "spring-ai-vectordb-mongodb-atlas"); + ProjectStructure structure = generateProject(request); + assertThat(structure.getProjectDirectory().resolve("compose.yaml")).doesNotExist(); + } + + @Test + void createsMongoDbAtlasService() { + ProjectRequest request = createProjectRequest("docker-compose", "spring-ai-vectordb-mongodb-atlas"); + assertThat(composeFile(request)).hasSameContentAs(new ClassPathResource("compose/mongodb-atlas.yaml")); + } + +} diff --git a/start-site/src/test/java/io/spring/start/site/extension/dependency/testcontainers/TestcontainersProjectGenerationConfigurationTests.java b/start-site/src/test/java/io/spring/start/site/extension/dependency/testcontainers/TestcontainersProjectGenerationConfigurationTests.java index 774c0b75956..95213535765 100644 --- a/start-site/src/test/java/io/spring/start/site/extension/dependency/testcontainers/TestcontainersProjectGenerationConfigurationTests.java +++ b/start-site/src/test/java/io/spring/start/site/extension/dependency/testcontainers/TestcontainersProjectGenerationConfigurationTests.java @@ -79,6 +79,7 @@ static Stream supportedTestcontainersActiveMQEntriesBuild() { static Stream supportedTestcontainersSpringAiEntriesBuild() { return Stream.of(Arguments.arguments("spring-ai-vectordb-chroma", "chromadb"), Arguments.arguments("spring-ai-vectordb-milvus", "milvus"), + Arguments.arguments("spring-ai-vectordb-mongodb-atlas", "mongodb"), Arguments.arguments("spring-ai-vectordb-qdrant", "qdrant"), Arguments.arguments("spring-ai-vectordb-weaviate", "weaviate")); } diff --git a/start-site/src/test/resources/compose/mongodb-atlas.yaml b/start-site/src/test/resources/compose/mongodb-atlas.yaml new file mode 100644 index 00000000000..e50a9727fa0 --- /dev/null +++ b/start-site/src/test/resources/compose/mongodb-atlas.yaml @@ -0,0 +1,5 @@ +services: + mongodbatlas: + image: 'mongodb/mongodb-atlas-local:latest' + ports: + - '27017'