BAEL-5696: Refactors spring caching TTL tutorial (#12825)
Co-authored-by: Abdul <abdul.wahab@monese.com>
This commit is contained in:
+84
@@ -0,0 +1,84 @@
|
||||
package com.baeldung.caching.redis;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import redis.embedded.RedisServer;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@Import({ CacheConfig.class, ItemService.class })
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ImportAutoConfiguration(classes = { CacheAutoConfiguration.class, RedisAutoConfiguration.class })
|
||||
@EnableCaching
|
||||
class ItemServiceCachingIntegrationTest {
|
||||
|
||||
private static final String AN_ID = "id-1";
|
||||
private static final String A_DESCRIPTION = "an item";
|
||||
|
||||
@MockBean
|
||||
private ItemRepository mockItemRepository;
|
||||
|
||||
@Autowired
|
||||
private ItemService itemService;
|
||||
|
||||
@Autowired
|
||||
private CacheManager cacheManager;
|
||||
|
||||
@Test
|
||||
void givenRedisCaching_whenFindItemById_thenItemReturnedFromCache() {
|
||||
Item anItem = new Item(AN_ID, A_DESCRIPTION);
|
||||
given(mockItemRepository.findById(AN_ID))
|
||||
.willReturn(Optional.of(anItem));
|
||||
|
||||
Item itemCacheMiss = itemService.getItemForId(AN_ID);
|
||||
Item itemCacheHit = itemService.getItemForId(AN_ID);
|
||||
|
||||
assertThat(itemCacheMiss).isEqualTo(anItem);
|
||||
assertThat(itemCacheHit).isEqualTo(anItem);
|
||||
|
||||
verify(mockItemRepository, times(1)).findById(AN_ID);
|
||||
assertThat(itemFromCache()).isEqualTo(anItem);
|
||||
}
|
||||
|
||||
private Object itemFromCache() {
|
||||
return cacheManager.getCache("itemCache").get(AN_ID).get();
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
static class EmbeddedRedisConfiguration {
|
||||
|
||||
private final RedisServer redisServer;
|
||||
|
||||
public EmbeddedRedisConfiguration() {
|
||||
this.redisServer = new RedisServer();
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void startRedis() {
|
||||
redisServer.start();
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void stopRedis() {
|
||||
this.redisServer.stop();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.caching.ttl;
|
||||
|
||||
import com.baeldung.caching.ttl.repository.HotelRepository;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.jdbc.Sql;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
|
||||
@AutoConfigureMockMvc
|
||||
@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:data.sql")
|
||||
@SlowTest
|
||||
class HotelControllerIntegrationTest {
|
||||
@Autowired private MockMvc mockMvc;
|
||||
@Autowired private HotelRepository repository;
|
||||
|
||||
@Test
|
||||
@DisplayName("When all hotels requested then request is successful")
|
||||
void whenAllHotelsRequested_thenRequestIsSuccessful() throws Exception {
|
||||
mockMvc
|
||||
.perform(get("/hotel"))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("When all hotels are requested then they correct number of hotels is returned")
|
||||
void whenAllHotelsRequested_thenReturnAllHotels() throws Exception {
|
||||
mockMvc
|
||||
.perform(get("/hotel"))
|
||||
.andExpect(jsonPath("$", hasSize((int) repository.findAll().stream().count())));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.baeldung.caching.ttl;
|
||||
|
||||
public @interface SlowTest {
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_ON_EXIT=FALSE
|
||||
spring.datasource.driverClassName=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
|
||||
# Enabling H2 Console
|
||||
spring.h2.console.enabled=true
|
||||
spring.h2.console.path=/h2
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
spring.jpa.show-sql=false
|
||||
caching.spring.hotelListTTL=43200
|
||||
# Connection details
|
||||
#spring.redis.host=localhost
|
||||
#spring.redis.port=6379
|
||||
server.port=8000
|
||||
Reference in New Issue
Block a user