1 package dst.ass1.kv.tests;
3 import static org.hamcrest.Matchers.allOf;
4 import static org.hamcrest.Matchers.greaterThan;
5 import static org.hamcrest.Matchers.lessThan;
6 import static org.junit.Assert.assertEquals;
7 import static org.junit.Assert.assertNotNull;
8 import static org.junit.Assert.assertNull;
9 import static org.junit.Assert.assertThat;
10 import static org.junit.Assert.assertTrue;
12 import java.io.IOException;
14 import org.junit.After;
15 import org.junit.Before;
16 import org.junit.Rule;
17 import org.junit.Test;
18 import org.junit.rules.ExpectedException;
20 import dst.ass1.kv.ISessionManager;
21 import dst.ass1.kv.ISessionManagerFactory;
22 import dst.ass1.kv.RedisCleaner;
23 import dst.ass1.kv.SessionNotFoundException;
24 import dst.ass1.kv.impl.SessionManagerFactory;
26 public class Ass1_5_1Test {
29 public RedisCleaner redisRule = new RedisCleaner();
32 public ExpectedException expectedException = ExpectedException.none();
34 private ISessionManager sessionManager;
38 ISessionManagerFactory sessionManagerFactory = new SessionManagerFactory();
40 sessionManager = sessionManagerFactory.createSessionManager(redisRule.getProperties());
44 public void tearDown() {
45 sessionManager.close();
49 public void createSession_createsSessionId() throws Exception {
50 String sessionId = sessionManager.createSession(1337L, 15000);
51 assertNotNull(sessionId);
55 public void getAndSetSessionVariable_behavesCorrectly() throws Exception {
56 String sessionId = sessionManager.createSession(1337L, 15000);
57 sessionManager.setSessionVariable(sessionId, "f00", "bar");
58 assertEquals("bar", sessionManager.getSessionVariable(sessionId, "f00"));
61 @Test(expected = SessionNotFoundException.class)
62 public void setSessionVariable_forNonExistingSession_throwsSessionNotFoundException() throws Exception {
63 sessionManager.setSessionVariable("nonExistingSessionId", "f00", "bar");
66 @Test(expected = SessionNotFoundException.class)
67 public void getSessionVariable_forNonExistingSession_throwsSessionNotFoundException() throws Exception {
68 sessionManager.getSessionVariable("nonExistingSessionId", "f00");
72 public void getSessionVariable_onNonExistingVariable_returnsNull() throws Exception {
73 String sessionId = sessionManager.createSession(1337L, 15000);
74 String value = sessionManager.getSessionVariable(sessionId, "f00");
79 public void getUserId_returnsCorrectUserID() throws Exception {
81 String sessionId = sessionManager.createSession(userId, 15000);
82 assertEquals(userId, sessionManager.getUserId(sessionId));
85 @Test(expected = SessionNotFoundException.class)
86 public void getUserId_forNonExistingSession_throwsException() throws Exception {
87 sessionManager.getUserId("nonExistingSessionId");
91 public void getTimeToLive_returnsCorrectValue() throws Exception {
92 String sessionId = sessionManager.createSession(1337L, 60);
93 int ttl = sessionManager.getTimeToLive(sessionId);
94 assertThat(ttl, allOf(greaterThan(57), lessThan(61)));
98 public void getTimeToLive_afterExpiry_throwsSessionNotFoundException() throws Exception {
99 String sessionId = sessionManager.createSession(1337L, 2);
100 int ttl = sessionManager.getTimeToLive(sessionId);
101 assertThat(ttl, greaterThan(0));
104 expectedException.expect(SessionNotFoundException.class);
105 sessionManager.getTimeToLive(sessionId);
108 @Test(expected = SessionNotFoundException.class)
109 public void getTimeToLive_forNonExistingSession_throwsSessionNotFoundException() throws Exception {
110 sessionManager.getTimeToLive("nonExistingSessionId");