1+ package ch.derlin.bbdata.caching
2+
3+ import ch.derlin.bbdata.*
4+ import ch.derlin.bbdata.common.CacheConstants
5+ import ch.derlin.bbdata.input.InputApiTest
6+ import org.junit.jupiter.api.Assertions.*
7+ import org.junit.jupiter.api.MethodOrderer
8+ import org.junit.jupiter.api.Test
9+ import org.junit.jupiter.api.TestMethodOrder
10+ import org.junit.jupiter.api.extension.ExtendWith
11+ import org.springframework.beans.factory.annotation.Autowired
12+ import org.springframework.boot.test.context.SpringBootTest
13+ import org.springframework.boot.test.web.client.TestRestTemplate
14+ import org.springframework.cache.CacheManager
15+ import org.springframework.http.HttpStatus
16+ import org.springframework.test.context.ActiveProfiles
17+ import org.springframework.test.context.junit.jupiter.SpringExtension
18+
19+ /* *
20+ * date: 09.09.20
21+ * @author Lucy Linder <lucy.derlin@gmail.com>
22+ */
23+ @ExtendWith(SpringExtension ::class )
24+ @SpringBootTest(webEnvironment = SpringBootTest .WebEnvironment .RANDOM_PORT ,
25+ properties = [UNSECURED_REGULAR , NO_KAFKA , " spring.cache.type=simple" , " cache.evict.secret-key=" ])
26+ @ActiveProfiles(Profiles .UNSECURED , Profiles .CACHING )
27+ @TestMethodOrder(MethodOrderer .Alphanumeric ::class )
28+ class ManualCacheEvictTest {
29+ @Autowired
30+ private lateinit var restTemplate: TestRestTemplate
31+
32+ @Autowired
33+ private lateinit var cacheManager: CacheManager
34+
35+ companion object {
36+ const val objectId = 1
37+ }
38+
39+ @Test
40+ fun `1-1 test cache evict` () {
41+ val token = TOKEN (objectId)
42+ val cache = cacheManager.getCache(CacheConstants .CACHE_NAME )!!
43+ val cacheKey = " $objectId :$token "
44+
45+ // check nothing in the cache
46+ assertNull(cache.get(cacheKey))
47+
48+ // add something to the cache
49+ var resp = restTemplate.postWithBody(InputApiTest .URL ,
50+ InputApiTest .getMeasureBody(objectId = objectId, token = token))
51+ assertEquals(HttpStatus .OK , resp.statusCode)
52+ assertNotNull(cache.get(cacheKey))
53+
54+ // call manual cache evict
55+ resp = restTemplate.getQueryString(" /cache-evict" )
56+ assertEquals(HttpStatus .OK , resp.statusCode)
57+ assertNull(cache.get(cacheKey))
58+ }
59+ }
0 commit comments