]> git.somenet.org - pub/jan/dst18.git/blob - ass1-kv/src/test/java/dst/ass1/kv/tests/Ass1_5_1Test.java
Add template for assignment 1
[pub/jan/dst18.git] / ass1-kv / src / test / java / dst / ass1 / kv / tests / Ass1_5_1Test.java
1 package dst.ass1.kv.tests;
2
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;
11
12 import java.io.IOException;
13
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;
19
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;
25
26 public class Ass1_5_1Test {
27
28     @Rule
29     public RedisCleaner redisRule = new RedisCleaner();
30
31     @Rule
32     public ExpectedException expectedException = ExpectedException.none();
33
34     private ISessionManager sessionManager;
35
36     @Before
37     public void setUp() {
38         ISessionManagerFactory sessionManagerFactory = new SessionManagerFactory();
39
40         sessionManager = sessionManagerFactory.createSessionManager(redisRule.getProperties());
41     }
42
43     @After
44     public void tearDown() {
45         sessionManager.close();
46     }
47
48     @Test
49     public void createSession_createsSessionId() throws Exception {
50         String sessionId = sessionManager.createSession(1337L, 15000);
51         assertNotNull(sessionId);
52     }
53
54     @Test
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"));
59     }
60
61     @Test(expected = SessionNotFoundException.class)
62     public void setSessionVariable_forNonExistingSession_throwsSessionNotFoundException() throws Exception {
63         sessionManager.setSessionVariable("nonExistingSessionId", "f00", "bar");
64     }
65
66     @Test(expected = SessionNotFoundException.class)
67     public void getSessionVariable_forNonExistingSession_throwsSessionNotFoundException() throws Exception {
68         sessionManager.getSessionVariable("nonExistingSessionId", "f00");
69     }
70
71     @Test
72     public void getSessionVariable_onNonExistingVariable_returnsNull() throws Exception {
73         String sessionId = sessionManager.createSession(1337L, 15000);
74         String value = sessionManager.getSessionVariable(sessionId, "f00");
75         assertNull(value);
76     }
77
78     @Test
79     public void getUserId_returnsCorrectUserID() throws Exception {
80         Long userId = 1337L;
81         String sessionId = sessionManager.createSession(userId, 15000);
82         assertEquals(userId, sessionManager.getUserId(sessionId));
83     }
84
85     @Test(expected = SessionNotFoundException.class)
86     public void getUserId_forNonExistingSession_throwsException() throws Exception {
87         sessionManager.getUserId("nonExistingSessionId");
88     }
89
90     @Test
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)));
95     }
96
97     @Test
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));
102         Thread.sleep(3000);
103
104         expectedException.expect(SessionNotFoundException.class);
105         sessionManager.getTimeToLive(sessionId);
106     }
107
108     @Test(expected = SessionNotFoundException.class)
109     public void getTimeToLive_forNonExistingSession_throwsSessionNotFoundException() throws Exception {
110         sessionManager.getTimeToLive("nonExistingSessionId");
111     }
112
113 }