Skip to content

Commit 20cf347

Browse files
committed
add /cache-evict tests
1 parent d1c1026 commit 20cf347

3 files changed

Lines changed: 110 additions & 1 deletion

File tree

src/main/resources/application.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ info.repo-url=https://github.com/big-building-data/bbdata-api
1010
dynamic.info.clone-url=git.remote.origin.url
1111
dynamic.info.cache-type=spring.cache.type
1212
dynamic.info.active-profiles=spring.profiles.active
13+
dynamic.info.async-enabled=async.enabled
1314

1415
## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
1516
spring.datasource.url = jdbc:mysql://localhost:3306/bbdata2?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&allowPublicKeyRetrieval=true&serverTimezone=UTC
@@ -26,7 +27,7 @@ spring.jpa.properties.jadira.usertype.autoRegisterUserTypes = true
2627
# Debug: show sql statements
2728
spring.jpa.show-sql = false
2829

29-
## Cassandra properties
30+
## Cassandra properties
3031
spring.data.cassandra.contact-points=127.0.0.1
3132
spring.data.cassandra.keyspace-name=bbdata2
3233
spring.data.cassandra.jmx-enabled=false
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package ch.derlin.bbdata.caching
2+
3+
import ch.derlin.bbdata.*
4+
import org.junit.jupiter.api.Assertions
5+
import org.junit.jupiter.api.MethodOrderer
6+
import org.junit.jupiter.api.Test
7+
import org.junit.jupiter.api.TestMethodOrder
8+
import org.junit.jupiter.api.extension.ExtendWith
9+
import org.springframework.beans.factory.annotation.Autowired
10+
import org.springframework.boot.test.context.SpringBootTest
11+
import org.springframework.boot.test.web.client.TestRestTemplate
12+
import org.springframework.http.HttpStatus
13+
import org.springframework.test.context.ActiveProfiles
14+
import org.springframework.test.context.junit.jupiter.SpringExtension
15+
16+
/**
17+
* date: 09.09.20
18+
* @author Lucy Linder <lucy.derlin@gmail.com>
19+
*/
20+
@ExtendWith(SpringExtension::class)
21+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
22+
properties = [UNSECURED_REGULAR, "spring.cache.type=simple", "cache.evict.secret-key=111"])
23+
@ActiveProfiles(Profiles.UNSECURED, Profiles.NO_CASSANDRA, Profiles.CACHING)
24+
@TestMethodOrder(MethodOrderer.Alphanumeric::class)
25+
class ManualCacheEvictProtectedTest {
26+
@Autowired
27+
private lateinit var restTemplate: TestRestTemplate
28+
29+
@Autowired
30+
private lateinit var cacheProperties: CacheProperties
31+
32+
@Test
33+
fun `1-1 test cache evict protected fail`() {
34+
// add something to the cache
35+
36+
listOf("wrong", "", "lala").forEach { key ->
37+
// call manual cache evict with wrong keys
38+
val resp = restTemplate.getQueryString("/cache-evict?key=$key")
39+
Assertions.assertEquals(HttpStatus.FORBIDDEN, resp.statusCode)
40+
}
41+
}
42+
43+
@Test
44+
fun `1-1 test cache evict protected ok`() {
45+
// call manual cache evict with the right key
46+
val resp = restTemplate.getQueryString("/cache-evict?key=${cacheProperties.secretKey}")
47+
Assertions.assertEquals(HttpStatus.OK, resp.statusCode)
48+
}
49+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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

Comments
 (0)