From 798f1c7ad5fb4fcd90b8204f3e0659166ade12df Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Tue, 24 Dec 2024 20:02:25 +0000 Subject: [PATCH] fix : issue with starting in cluster mode --- boot-ultimate-redis/pom.xml | 2 +- .../TestUltimateRedisApplication.java | 2 + .../common/AbstractIntegrationTest.java | 4 +- .../common/TestcontainersConfiguration.java | 26 +++++---- .../controller/RedisControllerTest.java | 56 +++++++++++-------- 5 files changed, 54 insertions(+), 36 deletions(-) diff --git a/boot-ultimate-redis/pom.xml b/boot-ultimate-redis/pom.xml index b340d115d..c0457014d 100644 --- a/boot-ultimate-redis/pom.xml +++ b/boot-ultimate-redis/pom.xml @@ -92,7 +92,7 @@ - 1.23.0 + 1.25.2 diff --git a/boot-ultimate-redis/src/test/java/com/example/ultimateredis/TestUltimateRedisApplication.java b/boot-ultimate-redis/src/test/java/com/example/ultimateredis/TestUltimateRedisApplication.java index 4e06b6490..254041b81 100644 --- a/boot-ultimate-redis/src/test/java/com/example/ultimateredis/TestUltimateRedisApplication.java +++ b/boot-ultimate-redis/src/test/java/com/example/ultimateredis/TestUltimateRedisApplication.java @@ -1,6 +1,7 @@ package com.example.ultimateredis; import com.example.ultimateredis.common.TestcontainersConfiguration; +import com.example.ultimateredis.utils.AppConstants; import org.springframework.boot.SpringApplication; class TestUltimateRedisApplication { @@ -8,6 +9,7 @@ class TestUltimateRedisApplication { public static void main(String[] args) { SpringApplication.from(UltimateRedisApplication::main) .with(TestcontainersConfiguration.class) + .withAdditionalProfiles(AppConstants.PROFILE_CLUSTER) .run(args); } } diff --git a/boot-ultimate-redis/src/test/java/com/example/ultimateredis/common/AbstractIntegrationTest.java b/boot-ultimate-redis/src/test/java/com/example/ultimateredis/common/AbstractIntegrationTest.java index 4829fa05d..285a221d2 100644 --- a/boot-ultimate-redis/src/test/java/com/example/ultimateredis/common/AbstractIntegrationTest.java +++ b/boot-ultimate-redis/src/test/java/com/example/ultimateredis/common/AbstractIntegrationTest.java @@ -8,7 +8,7 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.assertj.MockMvcTester; @ActiveProfiles({AppConstants.PROFILE_STANDALONE}) @SpringBootTest( @@ -17,7 +17,7 @@ @AutoConfigureMockMvc public abstract class AbstractIntegrationTest { - @Autowired protected MockMvc mockMvc; + @Autowired protected MockMvcTester mockMvcTester; @Autowired protected ObjectMapper objectMapper; } diff --git a/boot-ultimate-redis/src/test/java/com/example/ultimateredis/common/TestcontainersConfiguration.java b/boot-ultimate-redis/src/test/java/com/example/ultimateredis/common/TestcontainersConfiguration.java index 4a7f069d8..eb378522b 100644 --- a/boot-ultimate-redis/src/test/java/com/example/ultimateredis/common/TestcontainersConfiguration.java +++ b/boot-ultimate-redis/src/test/java/com/example/ultimateredis/common/TestcontainersConfiguration.java @@ -10,7 +10,7 @@ import org.springframework.boot.testcontainers.service.connection.ServiceConnection; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Profile; -import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertyRegistrar; import org.testcontainers.utility.DockerImageName; @TestConfiguration(proxyBeanMethods = false) @@ -18,25 +18,29 @@ public class TestcontainersConfiguration { @Bean @Profile(AppConstants.PROFILE_CLUSTER) - RedisClusterContainer redisClusterContainer(DynamicPropertyRegistry dynamicPropertyRegistry) { - RedisClusterContainer redisClusterContainer = - new RedisClusterContainer(RedisClusterContainer.DEFAULT_IMAGE_NAME) - .withKeyspaceNotifications() - .withStartupAttempts(5) - .withStartupTimeout(Duration.ofMinutes(4)); - redisClusterContainer.start(); + RedisClusterContainer redisClusterContainer() { + return new RedisClusterContainer(RedisClusterContainer.DEFAULT_IMAGE_NAME) + .withKeyspaceNotifications() + .withStartupAttempts(5) + .withStartupTimeout(Duration.ofMinutes(4)); + } + + @Bean + @Profile(AppConstants.PROFILE_CLUSTER) + DynamicPropertyRegistrar dynamicPropertyRegistrar(RedisClusterContainer redisClusterContainer) { List list = Arrays.stream(redisClusterContainer.getRedisURIs()) .map(s -> s.substring(8)) .toList(); - dynamicPropertyRegistry.add("spring.data.redis.cluster.nodes", () -> list); - return redisClusterContainer; + return registry -> { + registry.add("spring.data.redis.cluster.nodes", () -> list); + }; } @Bean @ServiceConnection("redis") @Profile(AppConstants.PROFILE_NOT_CLUSTER) RedisContainer redisContainer() { - return new RedisContainer(DockerImageName.parse("redis").withTag("7.4.0-alpine")); + return new RedisContainer(DockerImageName.parse("redis").withTag("7.4.1-alpine")); } } diff --git a/boot-ultimate-redis/src/test/java/com/example/ultimateredis/controller/RedisControllerTest.java b/boot-ultimate-redis/src/test/java/com/example/ultimateredis/controller/RedisControllerTest.java index 29d2f63d9..da5d7bebe 100644 --- a/boot-ultimate-redis/src/test/java/com/example/ultimateredis/controller/RedisControllerTest.java +++ b/boot-ultimate-redis/src/test/java/com/example/ultimateredis/controller/RedisControllerTest.java @@ -1,20 +1,17 @@ package com.example.ultimateredis.controller; +import static org.assertj.core.api.Assertions.assertThat; import static org.awaitility.Awaitility.await; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.nullValue; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import com.example.ultimateredis.common.AbstractIntegrationTest; import com.example.ultimateredis.model.AddRedisRequest; +import com.example.ultimateredis.model.GenericResponse; import java.time.Duration; import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; +import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @TestMethodOrder(MethodOrderer.OrderAnnotation.class) @@ -24,22 +21,30 @@ class RedisControllerTest extends AbstractIntegrationTest { @Order(1) void addRedisKeyValue() throws Exception { AddRedisRequest addRedisRequest = new AddRedisRequest("junit", "JunitValue", 1); - this.mockMvc - .perform( - post("/v1/redis/add") - .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(addRedisRequest))) - .andExpect(status().isCreated()) - .andExpect(jsonPath("$.response", is(Boolean.TRUE))); + this.mockMvcTester + .post() + .uri("/v1/redis/add") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(addRedisRequest)) + .assertThat() + .hasStatus(HttpStatus.CREATED) + .bodyJson() + .convertTo(GenericResponse.class) + .satisfies(response -> assertThat(response.response()).isEqualTo(true)); } @Test @Order(2) - void getFromCache() throws Exception { - this.mockMvc - .perform(get("/v1/redis").param("key", "junit")) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.response", is("JunitValue"))); + void getFromCache() { + this.mockMvcTester + .get() + .uri("/v1/redis") + .param("key", "junit") + .assertThat() + .hasStatusOk() + .bodyJson() + .convertTo(GenericResponse.class) + .satisfies(response -> assertThat(response.response()).isEqualTo("JunitValue")); } @Test @@ -50,9 +55,16 @@ void expireFromCache() { .atMost(Duration.ofSeconds(70)) .untilAsserted( () -> - this.mockMvc - .perform(get("/v1/redis").param("key", "junit")) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.response", nullValue()))); + this.mockMvcTester + .get() + .uri("/v1/redis") + .param("key", "junit") + .assertThat() + .hasStatusOk() + .bodyJson() + .convertTo(GenericResponse.class) + .satisfies( + response -> + assertThat(response.response()).isNull())); } }