Skip to content

Commit d33a2c4

Browse files
committed
Merge pull request #1582 from eddumelendez
* pr/1582: Polish "Generate test app when spring cloud azure storage is selected" Generate test app when spring cloud azure storage is selected Closes gh-1582
2 parents ddf254a + 4400544 commit d33a2c4

File tree

7 files changed

+256
-0
lines changed

7 files changed

+256
-0
lines changed

start-site/src/main/java/io/spring/start/site/container/SimpleDockerServiceResolver.java

+8
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public SimpleDockerServiceResolver() {
3737
this.dockerServices.put("activeMQ", activeMQ());
3838
this.dockerServices.put("activeMQClassic", activeMQClassic());
3939
this.dockerServices.put("artemis", artemis());
40+
this.dockerServices.put("azurite", azurite());
4041
this.dockerServices.put("cassandra", cassandra());
4142
this.dockerServices.put("chroma", chroma());
4243
this.dockerServices.put("elasticsearch", elasticsearch());
@@ -81,6 +82,13 @@ private static DockerService artemis() {
8182
.build();
8283
}
8384

85+
private static DockerService azurite() {
86+
return DockerService.withImageAndTag("mcr.microsoft.com/azure-storage/azurite")
87+
.website("https://github.com/Azure/Azurite?tab=readme-ov-file#dockerhub")
88+
.ports(10000, 10001, 10002)
89+
.build();
90+
}
91+
8492
private static DockerService cassandra() {
8593
return DockerService.withImageAndTag("cassandra")
8694
.website("https://hub.docker.com/_/cassandra")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.spring.start.site.extension.dependency.springazure;
18+
19+
import io.spring.initializr.generator.buildsystem.Build;
20+
import io.spring.initializr.generator.buildsystem.Dependency;
21+
import io.spring.initializr.generator.buildsystem.DependencyScope;
22+
import io.spring.initializr.generator.buildsystem.gradle.GradleBuildSystem;
23+
import io.spring.initializr.generator.buildsystem.maven.MavenBuildSystem;
24+
import io.spring.initializr.generator.condition.ConditionalOnBuildSystem;
25+
import io.spring.initializr.generator.condition.ConditionalOnRequestedDependency;
26+
import io.spring.initializr.generator.project.ProjectGenerationConfiguration;
27+
import io.spring.initializr.generator.spring.build.BuildCustomizer;
28+
import io.spring.initializr.generator.spring.build.gradle.DevelopmentOnlyDependencyGradleBuildCustomizer;
29+
import io.spring.initializr.generator.spring.build.maven.OptionalDependencyMavenBuildCustomizer;
30+
import io.spring.start.site.container.ComposeFileCustomizer;
31+
import io.spring.start.site.container.DockerServiceResolver;
32+
33+
import org.springframework.context.annotation.Bean;
34+
35+
/**
36+
* Configuration for generation of projects that depend on Spring Azure Docker Compose.
37+
*
38+
* @author Eddú Meléndez
39+
* @author Moritz Halbritter
40+
*/
41+
@ProjectGenerationConfiguration
42+
@ConditionalOnRequestedDependency("azure-storage")
43+
class SpringAzureDockerComposeProjectGenerationConfiguration {
44+
45+
private static final String DEPENDENCY_ID = "spring-azure-docker-compose";
46+
47+
@Bean
48+
@ConditionalOnRequestedDependency("docker-compose")
49+
BuildCustomizer<Build> springAzureDockerComposeBuildCustomizer() {
50+
return (build) -> build.dependencies()
51+
.add(DEPENDENCY_ID, Dependency.withCoordinates("com.azure.spring", "spring-cloud-azure-docker-compose")
52+
.scope(DependencyScope.RUNTIME));
53+
}
54+
55+
@Bean
56+
@ConditionalOnBuildSystem(MavenBuildSystem.ID)
57+
OptionalDependencyMavenBuildCustomizer springAzureDockerComposeMavenBuildCustomizer() {
58+
return new OptionalDependencyMavenBuildCustomizer(DEPENDENCY_ID);
59+
}
60+
61+
@Bean
62+
@ConditionalOnBuildSystem(GradleBuildSystem.ID)
63+
DevelopmentOnlyDependencyGradleBuildCustomizer springAzureDockerComposeGradleBuildCustomizer() {
64+
return new DevelopmentOnlyDependencyGradleBuildCustomizer(DEPENDENCY_ID);
65+
}
66+
67+
@Bean
68+
@ConditionalOnRequestedDependency("docker-compose")
69+
ComposeFileCustomizer azureStorageComposeFileCustomizer(DockerServiceResolver serviceResolver) {
70+
return (composeFile) -> serviceResolver.doWith("azurite",
71+
(service) -> composeFile.services().add("azurite", service));
72+
}
73+
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.spring.start.site.extension.dependency.springazure;
18+
19+
import io.spring.initializr.generator.buildsystem.Build;
20+
import io.spring.initializr.generator.buildsystem.Dependency;
21+
import io.spring.initializr.generator.buildsystem.DependencyScope;
22+
import io.spring.initializr.generator.condition.ConditionalOnRequestedDependency;
23+
import io.spring.initializr.generator.project.ProjectGenerationConfiguration;
24+
import io.spring.initializr.generator.spring.build.BuildCustomizer;
25+
import io.spring.start.site.container.DockerServiceResolver;
26+
import io.spring.start.site.container.ServiceConnections;
27+
import io.spring.start.site.container.ServiceConnectionsCustomizer;
28+
29+
import org.springframework.context.annotation.Bean;
30+
31+
/**
32+
* Configuration for generation of projects that depend on Spring Azure Testcontainers.
33+
*
34+
* @author Eddú Meléndez
35+
*/
36+
@ProjectGenerationConfiguration
37+
@ConditionalOnRequestedDependency("azure-storage")
38+
class SpringAzureTestcontainersProjectGenerationConfiguration {
39+
40+
@Bean
41+
@ConditionalOnRequestedDependency("testcontainers")
42+
BuildCustomizer<Build> springAzureTestcontainersBuildCustomizer() {
43+
return (build) -> build.dependencies()
44+
.add("spring-azure-testcontainers",
45+
Dependency.withCoordinates("com.azure.spring", "spring-cloud-azure-testcontainers")
46+
.scope(DependencyScope.TEST_COMPILE));
47+
}
48+
49+
@Bean
50+
@ConditionalOnRequestedDependency("testcontainers")
51+
ServiceConnectionsCustomizer azureStorageServiceConnectionsCustomizer(DockerServiceResolver serviceResolver) {
52+
return (serviceConnections) -> serviceResolver.doWith("azurite",
53+
(service) -> serviceConnections.addServiceConnection(ServiceConnections.ServiceConnection
54+
.ofGenericContainer("azurite", service, "azure-storage/azurite")));
55+
}
56+
57+
}

start-site/src/main/resources/META-INF/spring.factories

+2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ io.spring.start.site.extension.dependency.springai.SpringAiQdrantProjectGenerati
3535
io.spring.start.site.extension.dependency.springai.SpringAiTestcontainersProjectGenerationConfiguration,\
3636
io.spring.start.site.extension.dependency.springai.SpringAiWeaviateProjectGenerationConfiguration,\
3737
io.spring.start.site.extension.dependency.springamqp.SpringAmqpProjectGenerationConfiguration,\
38+
io.spring.start.site.extension.dependency.springazure.SpringAzureDockerComposeProjectGenerationConfiguration,\
3839
io.spring.start.site.extension.dependency.springazure.SpringAzureProjectGenerationConfiguration,\
40+
io.spring.start.site.extension.dependency.springazure.SpringAzureTestcontainersProjectGenerationConfiguration,\
3941
io.spring.start.site.extension.dependency.springboot.SpringBootProjectGenerationConfiguration,\
4042
io.spring.start.site.extension.dependency.springcloud.SpringCloudProjectGenerationConfiguration,\
4143
io.spring.start.site.extension.dependency.springdata.SpringDataProjectGenerationConfiguration,\
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.spring.start.site.extension.dependency.springazure;
18+
19+
import io.spring.initializr.generator.test.project.ProjectStructure;
20+
import io.spring.initializr.web.project.ProjectRequest;
21+
import io.spring.start.site.extension.AbstractExtensionTests;
22+
import org.junit.jupiter.api.Test;
23+
24+
import org.springframework.core.io.ClassPathResource;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Tests for {@link SpringAzureDockerComposeProjectGenerationConfiguration}.
30+
*
31+
* @author Eddú Meléndez
32+
*/
33+
class SpringAzureDockerComposeProjectGenerationConfigurationTests extends AbstractExtensionTests {
34+
35+
@Test
36+
void doesNothingWithoutDockerCompose() {
37+
ProjectRequest request = createProjectRequest("web", "azure-storage");
38+
ProjectStructure structure = generateProject(request);
39+
assertThat(structure.getProjectDirectory().resolve("compose.yaml")).doesNotExist();
40+
}
41+
42+
@Test
43+
void createsAzuriteService() {
44+
ProjectRequest request = createProjectRequest("docker-compose", "azure-storage");
45+
assertThat(composeFile(request)).hasSameContentAs(new ClassPathResource("compose/azurite.yaml"));
46+
}
47+
48+
@Test
49+
void springAzureDockerComposeDependencyIsAdded() {
50+
ProjectRequest projectRequest = createProjectRequest("docker-compose", "azure-storage");
51+
assertThat(mavenPom(projectRequest)).hasDependency("com.azure.spring", "spring-cloud-azure-docker-compose",
52+
null, "runtime");
53+
}
54+
55+
@Test
56+
void shouldNotAddDockerComposeTestcontainersDependencyIfNoSpringAzureDependencyIsSelected() {
57+
ProjectRequest projectRequest = createProjectRequest("docker-compose", "web");
58+
assertThat(mavenPom(projectRequest)).doesNotHaveDependency("com.azure.spring",
59+
"spring-cloud-azure-docker-compose");
60+
}
61+
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.spring.start.site.extension.dependency.springazure;
18+
19+
import io.spring.initializr.web.project.ProjectRequest;
20+
import io.spring.start.site.extension.AbstractExtensionTests;
21+
import org.junit.jupiter.api.Test;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
/**
26+
* Tests for {@link SpringAzureTestcontainersProjectGenerationConfiguration}.
27+
*
28+
* @author Eddú Meléndez
29+
*/
30+
class SpringAzureTestcontainersProjectGenerationConfigurationTests extends AbstractExtensionTests {
31+
32+
@Test
33+
void springAzureTestcontainersDependencyIsAdded() {
34+
ProjectRequest projectRequest = createProjectRequest("testcontainers", "azure-storage");
35+
assertThat(mavenPom(projectRequest)).hasDependency("com.azure.spring", "spring-cloud-azure-testcontainers",
36+
null, "test");
37+
}
38+
39+
@Test
40+
void shouldNotAddSpringAzureTestcontainersDependencyIfNoSpringAzureDependencyIsSelected() {
41+
ProjectRequest projectRequest = createProjectRequest("testcontainers", "azure-keyvault");
42+
assertThat(mavenPom(projectRequest)).doesNotHaveDependency("com.azure.spring",
43+
"spring-cloud-azure-testcontainers");
44+
}
45+
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
azurite:
3+
image: 'mcr.microsoft.com/azure-storage/azurite:latest'
4+
ports:
5+
- '10000'
6+
- '10001'
7+
- '10002'

0 commit comments

Comments
 (0)