Skip to content

Commit 3761415

Browse files
committed
Generate test application when spring ai redis is selected
Add support for Testcontainers and Docker Compose.
1 parent d2b676d commit 3761415

File tree

5 files changed

+115
-0
lines changed

5 files changed

+115
-0
lines changed

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

+8
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public SimpleDockerServiceResolver() {
5050
this.dockerServices.put("pulsar", pulsar());
5151
this.dockerServices.put("rabbit", rabbit());
5252
this.dockerServices.put("redis", redis());
53+
this.dockerServices.put("redis-stack", redisStack());
5354
this.dockerServices.put("sqlServer", sqlServer());
5455
this.dockerServices.put("zipkin", zipkin());
5556
}
@@ -153,6 +154,13 @@ private static DockerService redis() {
153154
return DockerService.withImageAndTag("redis").website("https://hub.docker.com/_/redis").ports(6379).build();
154155
}
155156

157+
private static DockerService redisStack() {
158+
return DockerService.withImageAndTag("redis/redis-stack")
159+
.website("https://hub.docker.com/r/redis/redis-stack")
160+
.ports(6379)
161+
.build();
162+
}
163+
156164
private static DockerService sqlServer() {
157165
return DockerService.withImageAndTag("mcr.microsoft.com/mssql/server")
158166
.website("https://mcr.microsoft.com/en-us/product/mssql/server/about/")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.redis;
18+
19+
import io.spring.initializr.generator.condition.ConditionalOnRequestedDependency;
20+
import io.spring.start.site.container.ComposeFileCustomizer;
21+
import io.spring.start.site.container.DockerServiceResolver;
22+
import io.spring.start.site.container.ServiceConnections.ServiceConnection;
23+
import io.spring.start.site.container.ServiceConnectionsCustomizer;
24+
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.context.annotation.Configuration;
27+
28+
/**
29+
* Configuration for generation of projects that depend on Redis Stack.
30+
*
31+
* @author Eddú Meléndez
32+
*/
33+
@Configuration(proxyBeanMethods = false)
34+
@ConditionalOnRequestedDependency("spring-ai-vectordb-redis")
35+
class RedisStackProjectGenerationConfiguration {
36+
37+
@Bean
38+
@ConditionalOnRequestedDependency("testcontainers")
39+
ServiceConnectionsCustomizer redisStackServiceConnectionsCustomizer(DockerServiceResolver serviceResolver) {
40+
return (serviceConnections) -> serviceResolver.doWith("redis-stack", (service) -> serviceConnections
41+
.addServiceConnection(ServiceConnection.ofGenericContainer("redis-stack", service, "redis")));
42+
}
43+
44+
@Bean
45+
@ConditionalOnRequestedDependency("docker-compose")
46+
ComposeFileCustomizer redisStackComposeFileCustomizer(DockerServiceResolver serviceResolver) {
47+
return (composeFile) -> serviceResolver.doWith("redis-stack", (service) -> composeFile.services()
48+
.add("redis",
49+
service.andThen(builder -> builder.label("org.springframework.boot.service-connection", "redis"))));
50+
}
51+
52+
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ io.spring.start.site.extension.dependency.oracle.OracleProjectGenerationConfigur
2424
io.spring.start.site.extension.dependency.postgresql.PgVectorProjectGenerationConfiguration,\
2525
io.spring.start.site.extension.dependency.postgresql.PostgresqlProjectGenerationConfiguration,\
2626
io.spring.start.site.extension.dependency.redis.RedisProjectGenerationConfiguration,\
27+
io.spring.start.site.extension.dependency.redis.RedisStackProjectGenerationConfiguration,\
2728
io.spring.start.site.extension.dependency.sbom.SbomProjectGenerationConfiguration,\
2829
io.spring.start.site.extension.dependency.solace.SolaceProjectGenerationConfiguration,\
2930
io.spring.start.site.extension.dependency.springamqp.SpringAmqpProjectGenerationConfiguration,\
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.redis;
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+
import org.springframework.core.io.ClassPathResource;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
/**
28+
* Tests for {@link RedisStackProjectGenerationConfiguration}.
29+
*
30+
* @author Eddú Meléndez
31+
*/
32+
class RedisStackProjectGenerationConfigurationTests extends AbstractExtensionTests {
33+
34+
@Test
35+
void doesNothingWithoutDockerCompose() {
36+
ProjectRequest request = createProjectRequest("spring-ai-vectordb-redis");
37+
ProjectStructure structure = generateProject(request);
38+
assertThat(structure.getProjectDirectory().resolve("compose.yaml")).doesNotExist();
39+
}
40+
41+
@Test
42+
void createsRedisStackService() {
43+
ProjectRequest request = createProjectRequest("docker-compose", "spring-ai-vectordb-redis");
44+
assertThat(composeFile(request)).hasSameContentAs(new ClassPathResource("compose/redis-stack.yaml"));
45+
}
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
redis:
3+
image: 'redis/redis-stack:latest'
4+
labels:
5+
- "org.springframework.boot.service-connection=redis"
6+
ports:
7+
- '6379'

0 commit comments

Comments
 (0)