Skip to content

Commit

Permalink
Generate test application when spring ai elasticsearch is selected
Browse files Browse the repository at this point in the history
  • Loading branch information
eddumelendez committed Sep 5, 2024
1 parent d2b676d commit 188e7b6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -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");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ static Iterable<ImplicitDependency> 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/")));
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -29,6 +29,7 @@
* Tests for {@link ElasticsearchProjectGenerationConfiguration}.
*
* @author Moritz Halbritter
* @author Eddú Meléndez
*/
class ElasticsearchProjectGenerationConfigurationTests extends AbstractExtensionTests {

Expand All @@ -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"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ static Stream<Arguments> 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"));
Expand Down

0 comments on commit 188e7b6

Please sign in to comment.