From 188e7b67d7abad5b8f8a55b7ccbc9b5edc2ad88d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Wed, 4 Sep 2024 20:35:52 -0600 Subject: [PATCH] Generate test application when spring ai elasticsearch is selected --- ...csearchProjectGenerationConfiguration.java | 38 +++++++++++++------ .../TestcontainersModuleRegistry.java | 3 +- ...chProjectGenerationConfigurationTests.java | 9 ++++- ...rsProjectGenerationConfigurationTests.java | 1 + 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/start-site/src/main/java/io/spring/start/site/extension/dependency/elasticsearch/ElasticsearchProjectGenerationConfiguration.java b/start-site/src/main/java/io/spring/start/site/extension/dependency/elasticsearch/ElasticsearchProjectGenerationConfiguration.java index 1e74daf8820..818d3614cb2 100644 --- a/start-site/src/main/java/io/spring/start/site/extension/dependency/elasticsearch/ElasticsearchProjectGenerationConfiguration.java +++ b/start-site/src/main/java/io/spring/start/site/extension/dependency/elasticsearch/ElasticsearchProjectGenerationConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * 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. @@ -16,6 +16,7 @@ package io.spring.start.site.extension.dependency.elasticsearch; +import io.spring.initializr.generator.buildsystem.Build; import io.spring.initializr.generator.condition.ConditionalOnRequestedDependency; import io.spring.start.site.container.ComposeFileCustomizer; import io.spring.start.site.container.DockerServiceResolver; @@ -30,30 +31,43 @@ * * @author Moritz Halbritter * @author Stephane Nicoll + * @author Eddú Meléndez */ @Configuration(proxyBeanMethods = false) -@ConditionalOnRequestedDependency("data-elasticsearch") class ElasticsearchProjectGenerationConfiguration { private static final String TESTCONTAINERS_CLASS_NAME = "org.testcontainers.elasticsearch.ElasticsearchContainer"; @Bean @ConditionalOnRequestedDependency("testcontainers") - ServiceConnectionsCustomizer elasticsearchServiceConnectionsCustomizer(DockerServiceResolver serviceResolver) { - return (serviceConnections) -> serviceResolver.doWith("elasticsearch", - (service) -> serviceConnections.addServiceConnection( + ServiceConnectionsCustomizer elasticsearchServiceConnectionsCustomizer(Build build, + DockerServiceResolver serviceResolver) { + return (serviceConnections) -> { + if (isElasticsearchEnabled(build)) { + serviceResolver.doWith("elasticsearch", (service) -> serviceConnections.addServiceConnection( ServiceConnection.ofContainer("elasticsearch", service, TESTCONTAINERS_CLASS_NAME, false))); + } + }; } @Bean @ConditionalOnRequestedDependency("docker-compose") - ComposeFileCustomizer elasticsearchComposeFileCustomizer(DockerServiceResolver serviceResolver) { - return (composeFile) -> serviceResolver.doWith("elasticsearch", - (service) -> composeFile.services() - .add("elasticsearch", - service.andThen((builder) -> builder.environment("ELASTIC_PASSWORD", "secret") - .environment("xpack.security.enabled", "false") - .environment("discovery.type", "single-node")))); + ComposeFileCustomizer elasticsearchComposeFileCustomizer(Build build, DockerServiceResolver serviceResolver) { + return (composeFile) -> { + if (isElasticsearchEnabled(build)) { + serviceResolver.doWith("elasticsearch", + (service) -> composeFile.services() + .add("elasticsearch", + service.andThen((builder) -> builder.environment("ELASTIC_PASSWORD", "secret") + .environment("xpack.security.enabled", "false") + .environment("discovery.type", "single-node")))); + } + }; + } + + private boolean isElasticsearchEnabled(Build build) { + return build.dependencies().has("data-elasticsearch") + || build.dependencies().has("spring-ai-vectordb-elasticsearch"); } } 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 7917ecd839e..2d536a74a15 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 @@ -62,7 +62,8 @@ static Iterable create(Version platformVersion) { .customizeHelpDocument(addReferenceLink("Cassandra Module", "databases/cassandra/"))); builders.add(onDependencies("data-couchbase", "data-couchbase-reactive").customizeBuild(addModule("couchbase")) .customizeHelpDocument(addReferenceLink("Couchbase Module", "databases/couchbase/"))); - builders.add(onDependencies("data-elasticsearch").customizeBuild(addModule("elasticsearch")) + 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")) .customizeHelpDocument(addReferenceLink("MongoDB Module", "databases/mongodb/"))); diff --git a/start-site/src/test/java/io/spring/start/site/extension/dependency/elasticsearch/ElasticsearchProjectGenerationConfigurationTests.java b/start-site/src/test/java/io/spring/start/site/extension/dependency/elasticsearch/ElasticsearchProjectGenerationConfigurationTests.java index db81ddf1c66..7bd55b1a604 100644 --- a/start-site/src/test/java/io/spring/start/site/extension/dependency/elasticsearch/ElasticsearchProjectGenerationConfigurationTests.java +++ b/start-site/src/test/java/io/spring/start/site/extension/dependency/elasticsearch/ElasticsearchProjectGenerationConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * 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. @@ -29,6 +29,7 @@ * Tests for {@link ElasticsearchProjectGenerationConfiguration}. * * @author Moritz Halbritter + * @author Eddú Meléndez */ class ElasticsearchProjectGenerationConfigurationTests extends AbstractExtensionTests { @@ -45,4 +46,10 @@ void createsElasticsearchService() { assertThat(composeFile(request)).hasSameContentAs(new ClassPathResource("compose/elasticsearch.yaml")); } + @Test + void createsElasticsearchServiceWhenSpringAiModuleIsSelected() { + ProjectRequest request = createProjectRequest("docker-compose", "spring-ai-vectordb-elasticsearch"); + assertThat(composeFile(request)).hasSameContentAs(new ClassPathResource("compose/elasticsearch.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 a18c5e3bbca..72a7f1f949a 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 @@ -86,6 +86,7 @@ static Stream supportedEntriesBuild320() { Arguments.arguments("mysql", "mysql"), Arguments.arguments("postgresql", "postgresql"), Arguments.arguments("oracle", "oracle-free"), Arguments.arguments("pulsar", "pulsar"), Arguments.arguments("pulsar-reactive", "pulsar"), Arguments.arguments("solace", "solace"), + Arguments.arguments("spring-ai-vectordb-elasticsearch", "elasticsearch"), Arguments.arguments("spring-ai-vectordb-neo4j", "neo4j"), Arguments.arguments("spring-ai-vectordb-pgvector", "postgresql"), Arguments.arguments("sqlserver", "mssqlserver"));