Skip to content

Commit

Permalink
polish testcontainers config
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli authored Dec 12, 2024
1 parent 35bd6fb commit 38529cc
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 35 deletions.
2 changes: 1 addition & 1 deletion api-gateway/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@
<configuration>
<java>
<googleJavaFormat>
<version>1.22.0</version>
<version>1.25.0</version>
<style>AOSP</style>
</googleJavaFormat>
<licenseHeader>
Expand Down
2 changes: 1 addition & 1 deletion api-gateway/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ jwt:
management:
endpoint:
gateway:
enabled: true
access: read_only
endpoints:
web:
exposure:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,12 @@ Licensed under MIT License Copyright (c) 2023-2024 Raja Kolli.

package com.example.api.gateway;

import com.redis.testcontainers.RedisContainer;
import com.example.api.gateway.config.ContainerConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.DockerImageName;

@TestConfiguration(proxyBeanMethods = false)
public class TestAPIGatewayApplication {

@Bean
@ServiceConnection(name = "openzipkin/zipkin")
GenericContainer<?> zipkinContainer() {
return new GenericContainer<>(DockerImageName.parse("openzipkin/zipkin:latest"))
.withExposedPorts(9411)
.withReuse(true);
}

@Bean
@ServiceConnection(name = "redis")
RedisContainer redisContainer() {
return new RedisContainer(DockerImageName.parse("redis").withTag("7.4.0-alpine"))
.withReuse(true);
}

public static void main(String[] args) {
SpringApplication.from(APIGatewayApplication::main)
.with(TestAPIGatewayApplication.class)
.run(args);
SpringApplication.from(APIGatewayApplication::main).with(ContainerConfig.class).run(args);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/***
<p>
Licensed under MIT License Copyright (c) 2023 Raja Kolli.
Licensed under MIT License Copyright (c) 2023-2024 Raja Kolli.
</p>
***/

package com.example.api.gateway.config;

import com.example.api.gateway.TestAPIGatewayApplication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.client.AutoConfigureWebClient;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -15,12 +14,8 @@ Licensed under MIT License Copyright (c) 2023 Raja Kolli.

@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = {
"spring.cloud.discovery.reactive.enabled=false",
"spring.cloud.discovery.enabled=false",
"spring.cloud.config.enabled=false"
},
classes = TestAPIGatewayApplication.class)
properties = {"spring.cloud.discovery.enabled=false", "spring.cloud.config.enabled=false"},
classes = ContainerConfig.class)
@AutoConfigureWebClient
@ActiveProfiles("test")
public abstract class AbstractIntegrationTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/***
<p>
Licensed under MIT License Copyright (c) 2024 Raja Kolli.
</p>
***/

package com.example.api.gateway.config;

import com.redis.testcontainers.RedisContainer;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.DockerImageName;

@TestConfiguration(proxyBeanMethods = false)
public class ContainerConfig {

@Bean
@ServiceConnection(name = "openzipkin/zipkin")
GenericContainer<?> zipkinContainer() {
return new GenericContainer<>(DockerImageName.parse("openzipkin/zipkin:latest"))
.withExposedPorts(9411)
.withReuse(true);
}

@Bean
@ServiceConnection(name = "redis")
RedisContainer redisContainer() {
return new RedisContainer(DockerImageName.parse("redis").withTag("7.4.1-alpine"))
.withReuse(true);
}
}

0 comments on commit 38529cc

Please sign in to comment.