Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import static org.assertj.core.api.Assertions.assertThat;
- import static org.mockito.Mockito.times;
- import static org.mockito.Mockito.verify;
- import static org.mockito.Mockito.verifyNoMoreInteractions;
- import static org.mockito.Mockito.when;
- import java.util.List;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.boot.test.mock.mockito.MockBean;
- import org.springframework.cache.Cache;
- import org.springframework.cache.CacheManager;
- import org.springframework.cache.annotation.CacheEvict;
- import org.springframework.cache.annotation.Cacheable;
- import org.springframework.cache.annotation.EnableCaching;
- import org.springframework.cache.guava.GuavaCache;
- import org.springframework.cache.support.SimpleCacheManager;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.annotation.Import;
- import org.springframework.stereotype.Service;
- import org.springframework.test.annotation.DirtiesContext;
- import org.springframework.test.context.junit4.SpringRunner;
- import com.google.common.cache.CacheBuilder;
- @RunWith(SpringRunner.class)
- @SpringBootTest(classes = { IndexServiceTest.IndexCacheConfiguration.class, })
- @DirtiesContext
- public class IndexServiceTest {
- public static class Index {
- private final long timestamp;
- public Index(long timestamp) {
- this.timestamp = timestamp;
- }
- public long getTimestamp() {
- return timestamp;
- }
- }
- public interface IndexCalculator {
- public Index calculateIndex();
- public long getCurrentTimestamp();
- }
- @Service
- public static class IndexService {
- @Autowired
- private IndexCalculator indexCalculator;
- @Cacheable(cacheNames = "index")
- @CacheEvict(cacheNames = "index", condition = "target.isObsolete(#result)")
- public Index getIndex() {
- System.out.println("calculating index");
- return indexCalculator.calculateIndex();
- }
- public boolean isObsolete(Index index) {
- long indexTimestamp = index.getTimestamp();
- long currentTimestamp = indexCalculator.getCurrentTimestamp();
- System.out.println("indexTimestamp=" + indexTimestamp);
- System.out.println("currentTimestamp=" + indexTimestamp);
- if (index == null || indexTimestamp < currentTimestamp) {
- System.out.println("evicting");
- return true;
- } else {
- return false;
- }
- }
- }
- @Configuration
- @EnableCaching
- @Import({ IndexService.class })
- public static class IndexCacheConfiguration {
- @Bean
- public Cache indexCache() {
- return new GuavaCache("index", CacheBuilder.newBuilder().build());
- }
- @Bean
- public CacheManager indexCacheManager(List<Cache> caches) {
- SimpleCacheManager cacheManager = new SimpleCacheManager();
- cacheManager.setCaches(caches);
- return cacheManager;
- }
- }
- @MockBean
- private IndexCalculator indexCalculator;
- @Autowired
- private IndexService indexService;
- @Test
- public void test() {
- final Index index100 = new Index(100);
- final Index index200 = new Index(200);
- when(indexCalculator.calculateIndex()).thenReturn(index100);
- when(indexCalculator.getCurrentTimestamp()).thenReturn(100L);
- assertThat(indexService.getIndex()).isSameAs(index100);
- verify(indexCalculator).calculateIndex();
- verify(indexCalculator).getCurrentTimestamp();
- when(indexCalculator.getCurrentTimestamp()).thenReturn(200L);
- when(indexCalculator.calculateIndex()).thenReturn(index200);
- assertThat(indexService.getIndex()).isSameAs(index100);
- verify(indexCalculator, times(2)).getCurrentTimestamp();
- assertThat(indexService.getIndex()).isSameAs(index200);
- verify(indexCalculator, times(2)).calculateIndex();
- verify(indexCalculator, times(3)).getCurrentTimestamp();
- verifyNoMoreInteractions(indexCalculator);
- }
- }
Add Comment
Please, Sign In to add comment