SessionController added/updated
[staff/due1/sed-hs15-srs-purple.git] / src / test / java / ch / bfh / ti / srs / controller / SessionControllerTest.java
1 /*
2 * Copyright (c) 2015 Berner Fachhochschule, Switzerland.
3 *
4 * Project Smart Reservation System.
5 *
6 * Distributable under GPL license. See terms of license at gnu.org.
7 */
8 package ch.bfh.ti.srs.controller;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13
14 import java.util.Set;
15
16 import org.junit.Test;
17
18 import ch.bfh.ti.srs.datahandler.DataHandler;
19 import ch.bfh.ti.srs.datahandler.DataHandlerForTests;
20 import ch.bfh.ti.srs.dataschema.Role;
21 import ch.bfh.ti.srs.dataschema.User;
22
23 public class SessionControllerTest {
24
25
26 @Test
27 public void logInTest() throws Exception {
28 DataHandler dh = new DataHandlerForTests();
29 SessionController sc = new SessionController(dh);
30 UserController uc = new UserController(dh,sc);
31
32 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);
33 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);
34 uc.addUser("TestUser3", "pass3", "grosm9@bfh.ch", "Test", null);
35 uc.addUser("TestUser4", "pass4", "grosm9@bfh.ch", "Test", "User");
36
37 User user3 = uc.getUser("TestUser3");
38 user3.addRole(Role.User);
39 user3.addRole(Role.ResourceManager);
40
41 Set<Controller> controllers = sc.logIn("TestUser3", "pass3");
42 assertFalse(controllers.isEmpty());
43 }
44
45 @Test(expected = Exception.class)
46 public void logInException1Test() throws Exception {
47 DataHandler dh = new DataHandlerForTests();
48 SessionController sc = new SessionController(dh);
49 UserController uc = new UserController(dh,sc);
50
51 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);
52 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);
53
54 sc.logIn("TestUser1", "pass1");
55 sc.logIn("TestUser2", "pass2");
56 }
57
58 @Test(expected = Exception.class)
59 public void logInInvalidCredetialsTest() throws Exception {
60 DataHandler dh = new DataHandlerForTests();
61 SessionController sc = new SessionController(dh);
62 UserController uc = new UserController(dh,sc);
63
64 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);
65 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);
66
67 sc.logIn("TestUser3", "pass3");
68 }
69
70 @SuppressWarnings("deprecation")
71 @Test
72 public void getLoggedUserNameTest() throws Exception {
73 DataHandler dh = new DataHandlerForTests();
74 SessionController sc = new SessionController(dh);
75 UserController uc = new UserController(dh,sc);
76
77 //uc.setDataHandler(dh);
78 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);
79 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);
80 uc.addUser("TestUser3", "pass3", "grosm9@bfh.ch", "Test", null);
81 uc.addUser("TestUser4", "pass4", "grosm9@bfh.ch", "Test", "User");
82
83 sc.logIn("TestUser1", "pass1");
84 assertEquals("TestUser1", sc.getLoggedUserName());
85 }
86
87 @SuppressWarnings("deprecation")
88 @Test(expected = Exception.class)
89 public void getLoggedUserNameExceptionTest() throws Exception {
90 DataHandler dh = new DataHandlerForTests();
91 SessionController sc = new SessionController(dh);
92
93 sc.getLoggedUserName();
94 }
95
96 @SuppressWarnings("deprecation")
97 @Test
98 public void getLoggedUserMailTest() throws Exception {
99 DataHandler dh = new DataHandlerForTests();
100 SessionController sc = new SessionController(dh);
101 UserController uc = new UserController(dh,sc);
102
103 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);
104 uc.addUser("TestUser2", "pass2", "hanfli@bfh.ch", null, null);
105 uc.addUser("TestUser3", "pass3", "grosm9@bfh.ch", "Test", null);
106 uc.addUser("TestUser4", "pass4", "grosm9@bfh.ch", "Test", "User");
107
108 sc.logIn("TestUser2", "pass2");
109 assertEquals("hanfli@bfh.ch", sc.getLoggedMail());
110 }
111
112 @SuppressWarnings("deprecation")
113 @Test(expected = Exception.class)
114 public void getLoggedMailExceptionTest() throws Exception {
115 DataHandler dh = new DataHandlerForTests();
116 SessionController sc = new SessionController(dh);
117
118 sc.getLoggedMail();
119 }
120
121 @SuppressWarnings("deprecation")
122 @Test
123 public void getLoggedNameTest() throws Exception {
124 DataHandler dh = new DataHandlerForTests();
125 SessionController sc = new SessionController(dh);
126 UserController uc = new UserController(dh,sc);
127
128 //uc.setDataHandler(dh);
129 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);
130 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);
131 uc.addUser("TestUser3", "pass3", "grosm9@bfh.ch", "Hanfli", null);
132 uc.addUser("TestUser4", "pass4", "grosm9@bfh.ch", "Test", "User");
133
134 sc.logIn("TestUser3", "pass3");
135 assertEquals("Hanfli", sc.getLoggedName());
136 }
137
138 @SuppressWarnings("deprecation")
139 @Test(expected = Exception.class)
140 public void getLoggedNameExceptionTest() throws Exception {
141 DataHandler dh = new DataHandlerForTests();
142 SessionController sc = new SessionController(dh);
143
144 sc.getLoggedName();
145 }
146
147 @SuppressWarnings("deprecation")
148 @Test
149 public void getLoggedLastnameTest() throws Exception {
150 DataHandler dh = new DataHandlerForTests();
151 SessionController sc = new SessionController(dh);
152 UserController uc = new UserController(dh,sc);
153
154 //uc.setDataHandler(dh);
155 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);
156 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);
157 uc.addUser("TestUser3", "pass3", "grosm9@bfh.ch", "Test", null);
158 uc.addUser("TestUser4", "pass4", "grosm9@bfh.ch", "Test", "User");
159
160 sc.logIn("TestUser4", "pass4");
161 assertEquals("User", sc.getLoggedLastname());
162 }
163
164 @SuppressWarnings("deprecation")
165 @Test(expected = Exception.class)
166 public void getLoggedLastnameExceptionTest() throws Exception {
167 DataHandler dh = new DataHandlerForTests();
168 SessionController sc = new SessionController(dh);
169
170 sc.getLoggedLastname();
171 }
172
173 @Test
174 public void getLoggedUserTest() throws Exception {
175 DataHandler dh = new DataHandlerForTests();
176 SessionController sc = new SessionController(dh);
177 UserController uc = new UserController(dh,sc);
178
179 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);
180 assertEquals(null, sc.getLoggedUser());
181 sc.logIn("TestUser1", "pass1");
182 assertEquals("TestUser1", sc.getLoggedUser().getUserName());
183 }
184
185 @SuppressWarnings("deprecation")
186 @Test
187 public void logOutTest() throws Exception {
188 DataHandler dh = new DataHandlerForTests();
189 SessionController sc = new SessionController(dh);
190 UserController uc = new UserController(dh,sc);
191
192 //uc.setDataHandler(dh);
193 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);
194 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);
195 uc.addUser("TestUser3", "pass3", "grosm9@bfh.ch", "Test", null);
196 uc.addUser("TestUser4", "pass4", "grosm9@bfh.ch", "Test", "User");
197
198 sc.logIn("TestUser1", "pass1");
199 assertEquals("TestUser1", sc.getLoggedUserName());
200 sc.logOut();
201 }
202
203 @Test(expected = Exception.class)
204 public void logOutException1Test() throws Exception {
205 DataHandler dh = new DataHandlerForTests();
206 SessionController sc = new SessionController(dh);
207
208 sc.logOut();
209 }
210
211 @Test(expected = Exception.class)
212 public void logOutException2Test() throws Exception {
213 DataHandler dh = new DataHandlerForTests();
214 SessionController sc = new SessionController(null);
215
216 sc.logOut();
217 }
218
219
220
221 }