Skip to content

Commit

Permalink
Generate help section for Testcontainers
Browse files Browse the repository at this point in the history
This help section contains the docker images and tags used for the
Testcontainers.
  • Loading branch information
mhalbritter committed Nov 10, 2023
1 parent 28e77d5 commit 55814fd
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2012-2023 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.testcontainers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.spring.initializr.generator.project.ProjectDescription;
import io.spring.initializr.generator.spring.documentation.HelpDocument;
import io.spring.initializr.generator.spring.documentation.HelpDocumentCustomizer;
import io.spring.start.site.container.DockerService;
import io.spring.start.site.container.ServiceConnections;
import io.spring.start.site.container.ServiceConnections.ServiceConnection;

/**
* A {@link HelpDocumentCustomizer} that provide additional information about the
* Testcontainers that are defined for the project.
*
* @author Moritz Halbritter
*/
class TestContainersHelpDocumentCustomizer implements HelpDocumentCustomizer {

private final ProjectDescription description;

private final ServiceConnections serviceConnections;

TestContainersHelpDocumentCustomizer(ProjectDescription description, ServiceConnections serviceConnections) {
this.description = description;
this.serviceConnections = serviceConnections;
}

@Override
public void customize(HelpDocument document) {
String referenceDocUrl = "https://docs.spring.io/spring-boot/docs/%s/reference/html/features.html#features.testing.testcontainers"
.formatted(this.description.getPlatformVersion());
document.gettingStarted().addReferenceDocLink(referenceDocUrl, "Spring Boot Testcontainers support");

Map<String, Object> model = new HashMap<>();
List<DockerService> dockerServices = this.serviceConnections.values()
.map(ServiceConnection::dockerService)
.toList();
model.put("services", dockerServices);
model.put("testcontainersAtDevelopmentTimeLink",
"https://docs.spring.io/spring-boot/docs/%s/reference/html/features.html#features.testing.testcontainers.at-development-time"
.formatted(this.description.getPlatformVersion()));
document.addSection("testcontainers", model);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import io.spring.initializr.generator.project.ProjectDescription;
import io.spring.initializr.generator.project.ProjectGenerationConfiguration;
import io.spring.initializr.generator.spring.build.BuildCustomizer;
import io.spring.initializr.generator.spring.documentation.HelpDocumentCustomizer;
import io.spring.start.site.container.ServiceConnections;
import io.spring.start.site.container.ServiceConnectionsCustomizer;
import io.spring.start.site.support.implicit.ImplicitDependency;
Expand All @@ -39,12 +38,14 @@
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
* Configuration for generation of projects that depend on Testcontainers.
*
* @author Maciej Walkowiak
* @author Stephane Nicoll
* @author Moritz Halbritter
*/
@ProjectGenerationConfiguration
@ConditionalOnRequestedDependency("testcontainers")
Expand All @@ -68,6 +69,20 @@ public ImplicitDependencyHelpDocumentCustomizer testcontainersHelpCustomizer(Bui

@Configuration(proxyBeanMethods = false)
@ConditionalOnPlatformVersion("3.1.0-RC1")
static class ServiceConnectionsConfiguration {

@Bean
ServiceConnections serviceConnections(ObjectProvider<ServiceConnectionsCustomizer> customizers) {
ServiceConnections serviceConnections = new ServiceConnections();
customizers.orderedStream().forEach((customizer) -> customizer.customize(serviceConnections));
return serviceConnections;
}

}

@Configuration(proxyBeanMethods = false)
@ConditionalOnPlatformVersion("3.1.0-RC1")
@Import(ServiceConnectionsConfiguration.class)
static class SpringBootSupportConfiguration {

@Bean
Expand All @@ -80,20 +95,16 @@ BuildCustomizer<Build> springBootTestcontainersBuildCustomizer() {
}

@Bean
HelpDocumentCustomizer springBootTestcontainersHelpDocumentCustomizer(ProjectDescription description) {
return (helpDocument) -> {
String referenceDocUrl = String.format(
"https://docs.spring.io/spring-boot/docs/%s/reference/html/features.html#features.testing.testcontainers",
description.getPlatformVersion());
helpDocument.gettingStarted()
.addReferenceDocLink(referenceDocUrl, "Spring Boot Testcontainers support");
};
TestContainersHelpDocumentCustomizer springBootTestcontainersHelpDocumentCustomizer(
ProjectDescription description, ServiceConnections serviceConnections) {
return new TestContainersHelpDocumentCustomizer(description, serviceConnections);
}

}

@Configuration(proxyBeanMethods = false)
@ConditionalOnPlatformVersion("3.1.0-RC1")
@Import(ServiceConnectionsConfiguration.class)
static class TestApplicationConfiguration {

private final ProjectDescription description;
Expand All @@ -105,13 +116,6 @@ static class TestApplicationConfiguration {
this.indentingWriterFactory = indentingWriterFactory;
}

@Bean
ServiceConnections serviceConnections(ObjectProvider<ServiceConnectionsCustomizer> customizers) {
ServiceConnections serviceConnections = new ServiceConnections();
customizers.orderedStream().forEach((customizer) -> customizer.customize(serviceConnections));
return serviceConnections;
}

@Bean
@ConditionalOnLanguage(GroovyLanguage.ID)
GroovyTestContainersApplicationCodeProjectContributor groovyTestContainersApplicationCodeProjectContributor(
Expand Down
13 changes: 13 additions & 0 deletions start-site/src/main/resources/templates/testcontainers.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
### Testcontainers support

This project uses [Testcontainers at development time]({{testcontainersAtDevelopmentTimeLink}}).

{{#services.size}}
Testcontainers has been configured to use the following Docker images:

{{#services}}
* [`{{image}}:{{imageTag}}`]({{website}})
{{/services}}

Please review the tags of the used images and set them to the same as you're running in production.
{{/services.size}}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* @author Stephane Nicoll
* @author Eddú Meléndez
* @author Chris Bono
* @author Moritz Halbritter
*/
class TestcontainersProjectGenerationConfigurationTests extends AbstractExtensionTests {

Expand Down Expand Up @@ -344,6 +345,16 @@ fun main(args: Array<String>) {
""");
}

@Test
void shouldAddHelpSection() {
assertHelpDocument("3.1.5", "testcontainers", "data-mongodb", "postgresql").contains(
"https://docs.spring.io/spring-boot/docs/3.1.5/reference/html/features.html#features.testing.testcontainers")
.contains(
"https://docs.spring.io/spring-boot/docs/3.1.5/reference/html/features.html#features.testing.testcontainers.at-development-time")
.contains("mongo:latest")
.contains("postgres:latest");
}

private ProjectStructure generateProject(String platformVersion, String... dependencies) {
ProjectRequest request = createProjectRequest(dependencies);
request.setBootVersion(platformVersion);
Expand Down

0 comments on commit 55814fd

Please sign in to comment.