|
| 1 | +package io.micronaut.cache; |
| 2 | + |
| 3 | +import io.micronaut.test.extensions.junit5.annotation.MicronautTest; |
| 4 | +import jakarta.inject.Inject; |
| 5 | +import org.junit.jupiter.api.MethodOrderer; |
| 6 | +import org.junit.jupiter.api.Order; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.junit.jupiter.api.TestMethodOrder; |
| 9 | +import org.junit.jupiter.api.Timeout; |
| 10 | + |
| 11 | +import java.time.Month; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 16 | + |
| 17 | +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) |
| 18 | +@MicronautTest |
| 19 | +@SuppressWarnings("java:S5976") // Sonar thinks these can be parameterized |
| 20 | +class NewsServiceTest { |
| 21 | + |
| 22 | + @Inject |
| 23 | + NewsService newsService; |
| 24 | + |
| 25 | + @Timeout(4) |
| 26 | + @Test |
| 27 | + @Order(1) |
| 28 | + void firstInvocationOfNovemberDoesNotHitCache() { |
| 29 | + List<String> headlines = newsService.headlines(Month.NOVEMBER); |
| 30 | + assertEquals(2, headlines.size()); |
| 31 | + } |
| 32 | + |
| 33 | + @Timeout(1) |
| 34 | + @Test |
| 35 | + @Order(2) |
| 36 | + void secondInvocationOfNovemberHitsCache() { |
| 37 | + List<String> headlines = newsService.headlines(Month.NOVEMBER); |
| 38 | + assertEquals(2, headlines.size()); |
| 39 | + } |
| 40 | + |
| 41 | + @Timeout(4) |
| 42 | + @Test |
| 43 | + @Order(3) |
| 44 | + void firstInvocationOfOctoberDoesNotHitCache() { |
| 45 | + List<String> headlines = newsService.headlines(Month.OCTOBER); |
| 46 | + assertEquals(1, headlines.size()); |
| 47 | + } |
| 48 | + |
| 49 | + @Timeout(1) |
| 50 | + @Test |
| 51 | + @Order(4) |
| 52 | + void secondInvocationOfOctoberHitsCache() { |
| 53 | + List<String> headlines = newsService.headlines(Month.OCTOBER); |
| 54 | + assertEquals(1, headlines.size()); |
| 55 | + } |
| 56 | + |
| 57 | + @Timeout(1) |
| 58 | + @Test |
| 59 | + @Order(5) |
| 60 | + void addingAHeadlineToNovemberUpdatesCache() { |
| 61 | + List<String> headlines = newsService.addHeadline(Month.NOVEMBER, "Micronaut 1.3 Milestone 1 Released"); |
| 62 | + assertEquals(3, headlines.size()); |
| 63 | + } |
| 64 | + |
| 65 | + @Timeout(1) |
| 66 | + @Test |
| 67 | + @Order(6) |
| 68 | + void novemberCacheWasUpdatedByCachePutAndThusTheValueIsRetrievedFromTheCache() { |
| 69 | + List<String> headlines = newsService.headlines(Month.NOVEMBER); |
| 70 | + assertEquals(3, headlines.size()); |
| 71 | + } |
| 72 | + |
| 73 | + @Timeout(1) |
| 74 | + @Test |
| 75 | + @Order(7) |
| 76 | + void invalidateNovemberCacheWithCacheInvalidate() { |
| 77 | + assertDoesNotThrow(() -> { |
| 78 | + newsService.removeHeadline(Month.NOVEMBER, "Micronaut 1.3 Milestone 1 Released"); |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + @Timeout(1) |
| 83 | + @Test |
| 84 | + @Order(8) |
| 85 | + void octoberCacheIsStillValid() { |
| 86 | + List<String> headlines = newsService.headlines(Month.OCTOBER); |
| 87 | + assertEquals(1, headlines.size()); |
| 88 | + } |
| 89 | + |
| 90 | + @Timeout(4) |
| 91 | + @Test |
| 92 | + @Order(9) |
| 93 | + void novemberCacheWasInvalidated() { |
| 94 | + List<String> headlines = newsService.headlines(Month.NOVEMBER); |
| 95 | + assertEquals(2, headlines.size()); |
| 96 | + } |
| 97 | +} |
0 commit comments