From c45a19acabcca185cf3f62445c1e24779d7ebff3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Tue, 8 Oct 2024 15:46:19 -0600 Subject: [PATCH] Generate test app when RabbitMQ Streams is selected Currently, when rabbitmq streams and docker compose/testcontainers are selected, no container is provided. For that reason, the test and test app can not be executed sucessfully. RabbitMQ Streams requires an additional configuration to enable it using Spring Boot and also in the container itself but providing RabbitMQ service would be enough to execute the test and test app. --- ...ingAmqpProjectGenerationConfiguration.java | 33 +++++++++++++------ ...qpProjectGenerationConfigurationTests.java | 16 ++++++++- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/start-site/src/main/java/io/spring/start/site/extension/dependency/springamqp/SpringAmqpProjectGenerationConfiguration.java b/start-site/src/main/java/io/spring/start/site/extension/dependency/springamqp/SpringAmqpProjectGenerationConfiguration.java index 29ccafce5c7..b667403d55a 100644 --- a/start-site/src/main/java/io/spring/start/site/extension/dependency/springamqp/SpringAmqpProjectGenerationConfiguration.java +++ b/start-site/src/main/java/io/spring/start/site/extension/dependency/springamqp/SpringAmqpProjectGenerationConfiguration.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.springamqp; +import io.spring.initializr.generator.buildsystem.Build; import io.spring.initializr.generator.condition.ConditionalOnRequestedDependency; import io.spring.initializr.generator.project.ProjectGenerationConfiguration; import io.spring.start.site.container.ComposeFileCustomizer; @@ -29,35 +30,47 @@ * Configuration for generation of projects that depend on Spring AMQP. * * @author Stephane Nicoll - * @author Stephane Nicoll + * @author Eddú Meléndez */ @ProjectGenerationConfiguration -@ConditionalOnRequestedDependency("amqp") class SpringAmqpProjectGenerationConfiguration { private static final String TESTCONTAINERS_CLASS_NAME = "org.testcontainers.containers.RabbitMQContainer"; @Bean + @ConditionalOnRequestedDependency("amqp") SpringRabbitTestBuildCustomizer springAmqpTestBuildCustomizer() { return new SpringRabbitTestBuildCustomizer(); } @Bean @ConditionalOnRequestedDependency("testcontainers") - ServiceConnectionsCustomizer rabbitServiceConnectionsCustomizer(DockerServiceResolver serviceResolver) { - return (serviceConnections) -> serviceResolver.doWith("rabbit", (service) -> serviceConnections - .addServiceConnection(ServiceConnection.ofContainer("rabbit", service, TESTCONTAINERS_CLASS_NAME, false))); + ServiceConnectionsCustomizer rabbitServiceConnectionsCustomizer(Build build, + DockerServiceResolver serviceResolver) { + return (serviceConnections) -> serviceResolver.doWith("rabbit", (service) -> { + if (isAmqpEnabled(build)) { + serviceConnections.addServiceConnection( + ServiceConnection.ofContainer("rabbit", service, TESTCONTAINERS_CLASS_NAME, false)); + } + }); } @Bean @ConditionalOnRequestedDependency("docker-compose") - ComposeFileCustomizer rabbitComposeFileCustomizer(DockerServiceResolver serviceResolver) { - return (composeFile) -> serviceResolver.doWith("rabbit", - (service) -> composeFile.services() + ComposeFileCustomizer rabbitComposeFileCustomizer(Build build, DockerServiceResolver serviceResolver) { + return (composeFile) -> serviceResolver.doWith("rabbit", (service) -> { + if (isAmqpEnabled(build)) { + composeFile.services() .add("rabbitmq", service.andThen((builder) -> builder.environment("RABBITMQ_DEFAULT_USER", "myuser") .environment("RABBITMQ_DEFAULT_PASS", "secret") - .ports(5672)))); + .ports(5672))); + } + }); + } + + private boolean isAmqpEnabled(Build build) { + return build.dependencies().has("amqp") || build.dependencies().has("amqp-streams"); } } diff --git a/start-site/src/test/java/io/spring/start/site/extension/dependency/springamqp/SpringAmqpProjectGenerationConfigurationTests.java b/start-site/src/test/java/io/spring/start/site/extension/dependency/springamqp/SpringAmqpProjectGenerationConfigurationTests.java index 5c9fe57e321..b571317603c 100644 --- a/start-site/src/test/java/io/spring/start/site/extension/dependency/springamqp/SpringAmqpProjectGenerationConfigurationTests.java +++ b/start-site/src/test/java/io/spring/start/site/extension/dependency/springamqp/SpringAmqpProjectGenerationConfigurationTests.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. @@ -35,6 +35,7 @@ * * @author Stephane Nicoll * @author Moritz Halbritter + * @author Eddú Meléndez */ class SpringAmqpProjectGenerationConfigurationTests extends AbstractExtensionTests { @@ -70,4 +71,17 @@ void springAmqpWithDockerCompose() { assertThat(composeFile(request)).hasSameContentAs(new ClassPathResource("compose/rabbitmq.yaml")); } + @Test + void springAmqpStreamsWithoutDockerCompose() { + ProjectRequest request = createProjectRequest("web", "amqp-streams"); + ProjectStructure structure = generateProject(request); + assertThat(structure.getProjectDirectory().resolve("compose.yaml")).doesNotExist(); + } + + @Test + void springAmqpStreamsWithDockerCompose() { + ProjectRequest request = createProjectRequest("docker-compose", "amqp-streams"); + assertThat(composeFile(request)).hasSameContentAs(new ClassPathResource("compose/rabbitmq.yaml")); + } + }