-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
RedisBackedCacheTest.java
61 lines (48 loc) · 1.79 KB
/
RedisBackedCacheTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import com.mycompany.cache.Cache;
import com.mycompany.cache.RedisBackedCache;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.DockerImageName;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import redis.clients.jedis.Jedis;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration test for Redis-backed cache implementation.
*/
public class RedisBackedCacheTest {
private static GenericContainer<?> redis = new GenericContainer<>(DockerImageName.parse("redis:6-alpine"))
.withExposedPorts(6379);
private Cache cache;
@BeforeClass
public static void startContainer() {
redis.start();
}
@AfterClass
public static void stopContainer() {
redis.stop();
}
@BeforeMethod
public void setUp() {
Jedis jedis = new Jedis(redis.getHost(), redis.getMappedPort(6379));
cache = new RedisBackedCache(jedis, "test");
}
@Test
public void testFindingAnInsertedValue() {
cache.put("foo", "FOO");
Optional<String> foundObject = cache.get("foo", String.class);
assertThat(foundObject).as("When an object in the cache is retrieved, it can be found").isPresent();
assertThat(foundObject)
.as("When we put a String in to the cache and retrieve it, the value is the same")
.contains("FOO");
}
@Test
public void testNotFindingAValueThatWasNotInserted() {
Optional<String> foundObject = cache.get("bar", String.class);
assertThat(foundObject)
.as("When an object that's not in the cache is retrieved, nothing is found")
.isNotPresent();
}
}