Skip to content

Commit

Permalink
Generate test app when RabbitMQ Streams is selected
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
eddumelendez committed Oct 8, 2024
1 parent df9f120 commit c45a19a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 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.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;
Expand All @@ -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");
}

}
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 @@ -35,6 +35,7 @@
*
* @author Stephane Nicoll
* @author Moritz Halbritter
* @author Eddú Meléndez
*/
class SpringAmqpProjectGenerationConfigurationTests extends AbstractExtensionTests {

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

}

0 comments on commit c45a19a

Please sign in to comment.