Skip to content

Commit

Permalink
testing github actions #7
Browse files Browse the repository at this point in the history
  • Loading branch information
cookieID committed Nov 28, 2024
1 parent b8e8977 commit f37c8bd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
9 changes: 5 additions & 4 deletions src/test/java/RedisTestContainerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@
public class RedisTestContainerTest {

@Container // define the redis container (will be shared between the test methods and started only once)
private static final GenericContainer<?> redisContainer =
private static final RedisContainer REDIS_CONTAINER =
new RedisContainer(DockerImageName.parse("redis:7.4.0-alpine")).withExposedPorts(6379);


@DynamicPropertySource
static void configureRedis(DynamicPropertyRegistry registry) {
registry.add("spring.redis.host", redisContainer::getHost);
registry.add("spring.redis.port", () -> redisContainer.getMappedPort(6379).toString());
System.out.println("\\u001B[45m\\u001B[37m" + REDIS_CONTAINER.getHost() + ":" + REDIS_CONTAINER.getMappedPort(6379).toString() + "\\u001B[0m");
registry.add("spring.data.redis.host", REDIS_CONTAINER::getHost);
registry.add("spring.data.redis.port", () -> REDIS_CONTAINER.getMappedPort(6379).toString());
}

@Test
void givenRedisContainerConfiguredWithDynamicProperties_whenCheckingRunningStatus_thenStatusIsRunning() {
assertTrue(redisContainer.isRunning());
assertTrue(REDIS_CONTAINER.isRunning());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class RedisRealConnectionTest {
@Autowired
private StringRedisTemplate redisTemplate;

@Test
/*@Test
public void testRedisConnection() {
// Attempt to connect and perform a basic operation
String key = "test:connection";
Expand All @@ -33,7 +33,7 @@ public void testRedisConnection() {
// Clean up
redisTemplate.delete(key);
}
}*/

// @Test
// public void testRedisConnectionFailure() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -28,6 +29,7 @@
import io.github.ardoco.rest.api.api_response.ErrorResponse;
import io.github.ardoco.rest.api.api_response.TraceLinkType;
import io.github.ardoco.rest.api.repository.RedisAccessor;
import org.testcontainers.utility.DockerImageName;
import testUtil.TestUtils;

@Testcontainers
Expand All @@ -47,16 +49,29 @@ public abstract class AbstractTLRControllerTest {
@Autowired
protected RedisAccessor redisAccessor;

private static GenericContainer<?> redis;
private static final String REDIS_IMAGE_NAME = "redis:7.0-alpine";
private static final int REDIS_PORT = 6379;

@BeforeAll
static void beforeAll() {
redis = new GenericContainer<>(DockerImageName.parse(REDIS_IMAGE_NAME)).withExposedPorts(REDIS_PORT);
redis.start();
System.setProperty("spring.data.redis.host", redis.getHost());
System.setProperty("spring.data.redis.port", redis.getMappedPort(REDIS_PORT).toString());
System.out.println(redis.getHost() + ":" + redis.getMappedPort(REDIS_PORT));
}

// Redis container shared across all subclasses
@Container
/*@Container
@ServiceConnection
public static GenericContainer<?> redisContainer = new GenericContainer<>("redis:7-alpine").withExposedPorts(6379);
@DynamicPropertySource
static void redisProperties(DynamicPropertyRegistry registry) {
registry.add("spring.redis.host", redisContainer::getHost);
registry.add("spring.redis.port", () -> redisContainer.getMappedPort(6379));
}
registry.add("spring.data.redis.host", redisContainer::getHost);
registry.add("spring.data.redis.port", () -> redisContainer.getMappedPort(6379));
}*/

public AbstractTLRControllerTest(TraceLinkType traceLinkType) {
this.traceLinkType = traceLinkType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.DockerImageName;

Expand All @@ -26,8 +28,17 @@ static void beforeAll() {
redis.start();
System.setProperty("spring.data.redis.host", redis.getHost());
System.setProperty("spring.data.redis.port", redis.getMappedPort(REDIS_PORT).toString());
System.out.println(redis.getHost() + ":" + redis.getMappedPort(REDIS_PORT));
}

@DynamicPropertySource
static void configureRedisProperties(DynamicPropertyRegistry registry) {
// Dynamically set Redis host and port
registry.add("spring.data.redis.host", redis::getHost);
registry.add("spring.data.redis.port", () -> redis.getMappedPort(REDIS_PORT));
System.out.println(redis.getHost() + ":" + redis.getMappedPort(REDIS_PORT));
}

@AfterAll
static void afterAll() {
redis.stop();
Expand Down

0 comments on commit f37c8bd

Please sign in to comment.