adapted version
[staff/due1/sed-hs15-srs-purple.git] / src / test / java / ch / bfh / ti / srs / controller / UserControllerTest.java
... / ...
CommitLineData
1/*\r
2 * Copyright (c) 2015 Berner Fachhochschule, Switzerland.\r
3 *\r
4 * Project Smart Reservation System.\r
5 *\r
6 * Distributable under GPL license. See terms of license at gnu.org.\r
7 */\r
8package ch.bfh.ti.srs.controller;\r
9\r
10import static org.junit.Assert.assertEquals;\r
11import static org.junit.Assert.assertFalse;\r
12import static org.junit.Assert.assertTrue;\r
13\r
14import java.util.ArrayList;\r
15import java.util.Date;\r
16import java.util.HashSet;\r
17import java.util.Iterator;\r
18import java.util.List;\r
19import java.util.Map;\r
20import java.util.Set;\r
21import java.util.UUID;\r
22\r
23import org.junit.Ignore;\r
24import org.junit.Test;\r
25\r
26import ch.bfh.ti.daterange.DateRange;\r
27import ch.bfh.ti.daterange.impl.pojo.DateRangeImpl;\r
28import ch.bfh.ti.srs.controller.UserController;\r
29import ch.bfh.ti.srs.datahandler.DataHandler;\r
30import ch.bfh.ti.srs.datahandler.DataHandlerForTests;\r
31import ch.bfh.ti.srs.dataschema.Reservation;\r
32import ch.bfh.ti.srs.dataschema.Resource;\r
33import ch.bfh.ti.srs.dataschema.User;\r
34\r
35/**\r
36 * @author Michael Grossen\r
37 */\r
38public class UserControllerTest {\r
39\r
40 @Test\r
41 public void addUserTest() throws Exception {\r
42 DataHandler dh = new DataHandlerForTests();\r
43 UserController uc = new UserController();\r
44\r
45 uc.setDataHandler(dh);\r
46 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
47 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
48 uc.addUser("TestUser3", "pass3", "grosm9@bfh.ch", "Test", null);\r
49 uc.addUser("TestUser4", "pass4", "grosm9@bfh.ch", "Test", "User");\r
50 }\r
51\r
52 @Test(expected = Exception.class)\r
53 public void addUserExceptionTest() throws Exception {\r
54 DataHandler dh = new DataHandlerForTests();\r
55 UserController uc = new UserController();\r
56\r
57 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
58 }\r
59\r
60 @Test\r
61 public void logInTest() throws Exception {\r
62 DataHandler dh = new DataHandlerForTests();\r
63 UserController uc = new UserController();\r
64\r
65 uc.setDataHandler(dh);\r
66 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
67 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
68 uc.addUser("TestUser3", "pass3", "grosm9@bfh.ch", "Test", null);\r
69 uc.addUser("TestUser4", "pass4", "grosm9@bfh.ch", "Test", "User");\r
70\r
71 uc.logIn("TestUser3", "pass3");\r
72 }\r
73\r
74 @Test(expected = Exception.class)\r
75 public void logInException1Test() throws Exception {\r
76 DataHandler dh = new DataHandlerForTests();\r
77 UserController uc = new UserController();\r
78 uc.logIn("TestUser3", "pass3");\r
79 }\r
80\r
81 @Test(expected = Exception.class)\r
82 public void logInException2Test() throws Exception {\r
83 DataHandler dh = new DataHandlerForTests();\r
84 UserController uc = new UserController();\r
85\r
86 uc.setDataHandler(dh);\r
87 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
88 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
89\r
90 uc.logIn("TestUser1", "pass1");\r
91 uc.logIn("TestUser2", "pass2");\r
92 }\r
93\r
94 @Test\r
95 public void logInInvalidCredetialsTest() throws Exception {\r
96 DataHandler dh = new DataHandlerForTests();\r
97 UserController uc = new UserController();\r
98\r
99 uc.setDataHandler(dh);\r
100 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
101 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
102\r
103 assertFalse(uc.logIn("TestUser3", "pass3"));\r
104 assertFalse(uc.logIn("TestUser1", "pass2"));\r
105 assertTrue(uc.logIn("TestUser1", "pass1"));\r
106 }\r
107\r
108 @Test\r
109 public void getLoggedUserNameTest() throws Exception {\r
110 DataHandler dh = new DataHandlerForTests();\r
111 UserController uc = new UserController();\r
112\r
113 uc.setDataHandler(dh);\r
114 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
115 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
116 uc.addUser("TestUser3", "pass3", "grosm9@bfh.ch", "Test", null);\r
117 uc.addUser("TestUser4", "pass4", "grosm9@bfh.ch", "Test", "User");\r
118\r
119 uc.logIn("TestUser1", "pass1");\r
120 assertEquals("TestUser1", uc.getLoggedUserName());\r
121 }\r
122\r
123 @Test(expected = Exception.class)\r
124 public void getLoggedUserNameExceptionTest() throws Exception {\r
125 UserController uc = new UserController();\r
126 String currentUserName = uc.getLoggedUserName();\r
127 }\r
128\r
129 @Test\r
130 public void getLoggedUserMailTest() throws Exception {\r
131 DataHandler dh = new DataHandlerForTests();\r
132 UserController uc = new UserController();\r
133\r
134 uc.setDataHandler(dh);\r
135 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
136 uc.addUser("TestUser2", "pass2", "hanfli@bfh.ch", null, null);\r
137 uc.addUser("TestUser3", "pass3", "grosm9@bfh.ch", "Test", null);\r
138 uc.addUser("TestUser4", "pass4", "grosm9@bfh.ch", "Test", "User");\r
139\r
140 uc.logIn("TestUser2", "pass2");\r
141 assertEquals("hanfli@bfh.ch", uc.getLoggedMail());\r
142 }\r
143\r
144 @Test(expected = Exception.class)\r
145 public void getLoggedMailExceptionTest() throws Exception {\r
146 UserController uc = new UserController();\r
147 String currentMail = uc.getLoggedMail();\r
148 }\r
149\r
150 @Test\r
151 public void getLoggedNameTest() throws Exception {\r
152 DataHandler dh = new DataHandlerForTests();\r
153 UserController uc = new UserController();\r
154\r
155 uc.setDataHandler(dh);\r
156 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
157 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
158 uc.addUser("TestUser3", "pass3", "grosm9@bfh.ch", "Hanfli", null);\r
159 uc.addUser("TestUser4", "pass4", "grosm9@bfh.ch", "Test", "User");\r
160\r
161 uc.logIn("TestUser3", "pass3");\r
162 assertEquals("Hanfli", uc.getLoggedName());\r
163 }\r
164\r
165 @Test(expected = Exception.class)\r
166 public void getLoggedNameExceptionTest() throws Exception {\r
167 UserController uc = new UserController();\r
168 uc.getLoggedName();\r
169 }\r
170\r
171 @Test\r
172 public void getLoggedLastnameTest() throws Exception {\r
173 DataHandler dh = new DataHandlerForTests();\r
174 UserController uc = new UserController();\r
175\r
176 uc.setDataHandler(dh);\r
177 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
178 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
179 uc.addUser("TestUser3", "pass3", "grosm9@bfh.ch", "Test", null);\r
180 uc.addUser("TestUser4", "pass4", "grosm9@bfh.ch", "Test", "User");\r
181\r
182 uc.logIn("TestUser4", "pass4");\r
183 assertEquals("User", uc.getLoggedLastname());\r
184 }\r
185\r
186 @Test(expected = Exception.class)\r
187 public void getLoggedLastnameExceptionTest() throws Exception {\r
188 UserController uc = new UserController();\r
189 String currentLastName = uc.getLoggedLastname();\r
190 }\r
191\r
192 @Test\r
193 public void getLoggedUserTest() throws Exception {\r
194 DataHandler dh = new DataHandlerForTests();\r
195 UserController uc = new UserController();\r
196\r
197 uc.setDataHandler(dh);\r
198 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
199 assertEquals(null, uc.getLoggedUser());\r
200 uc.logIn("TestUser1", "pass1");\r
201 assertEquals("TestUser1", uc.getLoggedUser().getUserName());\r
202 }\r
203\r
204 // @Test(expected=Exception.class)\r
205 // public void getLoggedUserExceptionTest() throws Exception{\r
206 // DataHandler dh = new DataHandlerForTests();\r
207 // UserController uc = new UserController();\r
208 //\r
209 // uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
210 // uc.logIn("TestUser1", "pass1");\r
211 // assertEquals("TestUser1", uc.getLoggedUser().getUserName());\r
212 // }\r
213\r
214 @Test\r
215 public void getUserTest() throws Exception {\r
216 DataHandler dh = new DataHandlerForTests();\r
217 UserController uc = new UserController();\r
218\r
219 uc.setDataHandler(dh);\r
220 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
221 uc.logIn("TestUser1", "pass1");\r
222 assertEquals("grosm9@bfh.ch", uc.getUser("TestUser1").getMail());\r
223 }\r
224\r
225 @Test(expected = Exception.class)\r
226 public void getUserExceptionTest() throws Exception {\r
227 DataHandler dh = new DataHandlerForTests();\r
228 UserController uc = new UserController();\r
229\r
230 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
231 uc.logIn("TestUser1", "pass1");\r
232 assertEquals("grosm9@bfh.ch", uc.getUser("TestUser1").getMail());\r
233 }\r
234\r
235 @Test\r
236 public void logOutTest() throws Exception {\r
237 DataHandler dh = new DataHandlerForTests();\r
238 UserController uc = new UserController();\r
239\r
240 uc.setDataHandler(dh);\r
241 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
242 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
243 uc.addUser("TestUser3", "pass3", "grosm9@bfh.ch", "Test", null);\r
244 uc.addUser("TestUser4", "pass4", "grosm9@bfh.ch", "Test", "User");\r
245\r
246 uc.logIn("TestUser1", "pass1");\r
247 assertEquals("TestUser1", uc.getLoggedUserName());\r
248 uc.logOut();\r
249 }\r
250\r
251 @Test(expected = Exception.class)\r
252 public void logOutException1Test() throws Exception {\r
253 DataHandler dh = new DataHandlerForTests();\r
254 UserController uc = new UserController();\r
255\r
256 uc.logOut();\r
257 }\r
258\r
259 @Test(expected = Exception.class)\r
260 public void logOutException2Test() throws Exception {\r
261 DataHandler dh = new DataHandlerForTests();\r
262 UserController uc = new UserController();\r
263\r
264 uc.setDataHandler(dh);\r
265 uc.logOut();\r
266 }\r
267\r
268 @Test(expected = Exception.class)\r
269 public void getUserExpectionTest() throws Exception {\r
270 UserController uc = new UserController();\r
271 uc.getUser(null);\r
272 }\r
273\r
274 @Test\r
275 public void getReservationTests() throws Exception {\r
276 DataHandler dh = new DataHandlerForTests();\r
277 UserController uc = new UserController();\r
278 uc.setDataHandler(dh);\r
279 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
280 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
281 uc.logIn("TestUser1", "pass1");\r
282\r
283 UUID groupId = UUID.randomUUID();\r
284 Date start = new Date(2015, 11, 28, 11, 00, 00);\r
285 Date finish = new Date(2015, 11, 28, 11, 30, 00);\r
286 DateRange dateRange = new DateRangeImpl(start, finish);\r
287\r
288 Resource resource1 = new Resource("Room101", "Class room");\r
289 Resource resource2 = new Resource("Room101", "Class room");\r
290 Resource resource3 = new Resource("Room101", "Class room");\r
291 dh.addResource(resource1);\r
292 dh.addResource(resource2);\r
293 dh.addResource(resource3);\r
294\r
295 User loggedUser = uc.getLoggedUser();\r
296 User testUser2 = uc.getUser("TestUser2");\r
297\r
298 Reservation reservation1 = new Reservation(groupId, uc.getLoggedUser(), resource1, dateRange,\r
299 "Test reservation1");\r
300 Reservation reservation2 = new Reservation(groupId, uc.getLoggedUser(), resource2, dateRange,\r
301 "Test reservation2");\r
302 Reservation reservation3 = new Reservation(groupId, testUser2, resource3, dateRange, "Test reservation3");\r
303 dh.addReservation(reservation1);\r
304 dh.addReservation(reservation2);\r
305 dh.addReservation(reservation3);\r
306\r
307 Map<Reservation, Boolean> loggedUserReservation = uc.getUserReservations(null);\r
308 Map<Reservation, Boolean> reservationMap = uc.getUserReservations(testUser2);\r
309 Map<Reservation, Boolean> reservationDateRangeMap = uc.getReservationsInDateRange(dateRange);\r
310 Map<Reservation, Boolean> reservationGroupMap = uc.getReservationGroup(groupId);\r
311\r
312 assertEquals(reservationDateRangeMap, reservationGroupMap);\r
313\r
314 assertTrue(loggedUserReservation.containsKey(reservation1));\r
315 assertTrue(loggedUserReservation.containsKey(reservation2));\r
316 assertFalse(loggedUserReservation.containsKey(reservation3));\r
317 assertTrue(reservationMap.containsKey(reservation3));\r
318\r
319 for (Map.Entry<Reservation, Boolean> entry : reservationDateRangeMap.entrySet()) {\r
320 if (entry.getKey().getUser().getUserName().equals("TestUser1")) {\r
321 assertTrue(entry.getValue());\r
322 assertEquals(entry.getKey().getId(), uc.getReservation(entry.getKey().getId()).getId());\r
323 } else {\r
324 assertFalse(entry.getValue());\r
325 assertEquals(entry.getKey().getId(), uc.getReservation(entry.getKey().getId()).getId());\r
326 }\r
327 }\r
328 assertEquals(null, uc.getReservation(UUID.randomUUID()));\r
329 }\r
330\r
331 @Test(expected = Exception.class)\r
332 public void getReservationGroupException1Test() throws Exception {\r
333 UserController uc = new UserController();\r
334 uc.getReservationGroup(null);\r
335 }\r
336\r
337 @Test(expected = IllegalArgumentException.class)\r
338 public void getReservationGroupException2Test() throws Exception {\r
339 DataHandler dh = new DataHandlerForTests();\r
340 UserController uc = new UserController();\r
341 uc.setDataHandler(dh);\r
342 uc.getReservationGroup(null);\r
343 }\r
344\r
345 @Test(expected = Exception.class)\r
346 public void getUserReservationException1Test() throws Exception {\r
347 UserController uc = new UserController();\r
348 uc.getUserReservations(null);\r
349 }\r
350\r
351 @Test(expected = Exception.class)\r
352 public void getUserReservationException2Test() throws Exception {\r
353 DataHandler dh = new DataHandlerForTests();\r
354 UserController uc = new UserController();\r
355 uc.setDataHandler(dh);\r
356 uc.getUserReservations(null);\r
357 }\r
358\r
359 @Test(expected = Exception.class)\r
360 public void getReservationException1Test() throws Exception {\r
361 UserController uc = new UserController();\r
362 uc.getReservation(null);\r
363 }\r
364\r
365 @Test(expected = Exception.class)\r
366 public void getReservationException2Test() throws Exception {\r
367 DataHandler dh = new DataHandlerForTests();\r
368 UserController uc = new UserController();\r
369 uc.setDataHandler(dh);\r
370 uc.getReservation(null);\r
371 }\r
372\r
373 @Test(expected = Exception.class)\r
374 public void getReservationsInDateRangeException1Test() throws Exception {\r
375 UserController uc = new UserController();\r
376 uc.getReservationsInDateRange(null);\r
377 }\r
378\r
379 @Test(expected = Exception.class)\r
380 public void getReservationsInDateRangeException2Test() throws Exception {\r
381 DataHandler dh = new DataHandlerForTests();\r
382 UserController uc = new UserController();\r
383 uc.setDataHandler(dh);\r
384 uc.getReservationsInDateRange(null);\r
385 }\r
386\r
387 @Test\r
388 public void makeReservationTest() throws Exception {\r
389 DataHandler dh = new DataHandlerForTests();\r
390 UserController uc = new UserController();\r
391 uc.setDataHandler(dh);\r
392 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
393 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
394 uc.logIn("TestUser1", "pass1");\r
395\r
396 Resource resource1 = new Resource("Room101", "Class room");\r
397 Resource resource2 = new Resource("Room101", "Class room");\r
398 Resource resource3 = new Resource("Room101", "Class room");\r
399 dh.addResource(resource1);\r
400 dh.addResource(resource2);\r
401 dh.addResource(resource3);\r
402\r
403 List<DateRange> dtList = new ArrayList<DateRange>();\r
404 for (int i = 1; i < 10; i++) {\r
405 dtList.add(new DateRangeImpl(new Date(2015, 12, 2 + i, 11, 00, 00), new Date(2015, 12, 2 + i, 11, 00, 00)));\r
406 }\r
407\r
408 assertEquals(null, uc.makeReservation(uc.getLoggedUser(), resource1, dtList, "remark"));\r
409 assertEquals(null, uc.makeReservation(null, resource2, dtList, "remark"));\r
410 }\r
411\r
412 @Test\r
413 public void makeInvalidReservationTest() throws Exception {\r
414 DataHandler dh = new DataHandlerForTests();\r
415 UserController uc = new UserController();\r
416 uc.setDataHandler(dh);\r
417 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
418 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
419 uc.logIn("TestUser1", "pass1");\r
420\r
421 Resource resource1 = new Resource("Room101", "Class room");\r
422 Resource resource2 = new Resource("Room101", "Class room");\r
423 Resource resource3 = new Resource("Room101", "Class room");\r
424 dh.addResource(resource1);\r
425 dh.addResource(resource2);\r
426 dh.addResource(resource3);\r
427\r
428 List<DateRange> dtList = new ArrayList<DateRange>();\r
429 for (int i = 1; i < 10; i++) {\r
430 dtList.add(new DateRangeImpl(new Date(2015, 12, 2 + i, 11, 00, 00), new Date(2015, 12, 2 + i, 12, 00, 00)));\r
431 }\r
432\r
433 List<DateRange> conflictingList = new ArrayList<DateRange>();\r
434 // Conflicts\r
435 conflictingList.add(new DateRangeImpl(new Date(2015, 12, 4, 11, 00, 00), new Date(2015, 12, 4, 12, 00, 00)));\r
436 conflictingList.add(new DateRangeImpl(new Date(2015, 12, 4, 11, 15, 00), new Date(2015, 12, 4, 11, 45, 00)));\r
437 conflictingList.add(new DateRangeImpl(new Date(2015, 12, 4, 10, 00, 00), new Date(2015, 12, 4, 17, 00, 00)));\r
438 conflictingList.add(new DateRangeImpl(new Date(2015, 12, 4, 11, 30, 00), new Date(2015, 12, 4, 12, 30, 00)));\r
439\r
440 // OK\r
441 conflictingList.add(new DateRangeImpl(new Date(2015, 12, 4, 10, 30, 00), new Date(2015, 12, 4, 10, 59, 59)));\r
442 conflictingList.add(new DateRangeImpl(new Date(2015, 12, 4, 12, 00, 01), new Date(2015, 12, 4, 12, 45, 00)));\r
443\r
444 assertEquals(null, uc.makeReservation(uc.getLoggedUser(), resource1, dtList, "remark"));\r
445 assertEquals(4, uc.makeReservation(uc.getLoggedUser(), resource1, conflictingList, null).size());\r
446 }\r
447\r
448 @Test(expected = IllegalArgumentException.class)\r
449 public void makeReservationException1Test() throws Exception {\r
450 DataHandler dh = new DataHandlerForTests();\r
451 UserController uc = new UserController();\r
452 uc.setDataHandler(dh);\r
453 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
454 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
455 uc.logIn("TestUser1", "pass1");\r
456\r
457 Resource resource1 = new Resource("Room101", "Class room");\r
458 Resource resource2 = new Resource("Room101", "Class room");\r
459 Resource resource3 = new Resource("Room101", "Class room");\r
460 dh.addResource(resource1);\r
461 dh.addResource(resource2);\r
462 dh.addResource(resource3);\r
463\r
464 List<DateRange> dtList = new ArrayList<DateRange>();\r
465 for (int i = 1; i < 10; i++) {\r
466 dtList.add(new DateRangeImpl(new Date(2015, 12, 2 + i, 11, 00, 00), new Date(2015, 12, 2 + i, 12, 00, 00)));\r
467 }\r
468\r
469 uc.logOut();\r
470 assertEquals(null, uc.makeReservation(null, resource1, dtList, "remark"));\r
471 }\r
472\r
473 @Test(expected = IllegalArgumentException.class)\r
474 public void makeReservationException2Test() throws Exception {\r
475 DataHandler dh = new DataHandlerForTests();\r
476 UserController uc = new UserController();\r
477 uc.setDataHandler(dh);\r
478 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
479 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
480 uc.logIn("TestUser1", "pass1");\r
481\r
482 Resource resource1 = new Resource("Room101", "Class room");\r
483 Resource resource2 = new Resource("Room101", "Class room");\r
484 Resource resource3 = new Resource("Room101", "Class room");\r
485 dh.addResource(resource1);\r
486 dh.addResource(resource2);\r
487 dh.addResource(resource3);\r
488\r
489 List<DateRange> dtList = new ArrayList<DateRange>();\r
490 for (int i = 1; i < 10; i++) {\r
491 dtList.add(new DateRangeImpl(new Date(2015, 12, 2 + i, 11, 00, 00), new Date(2015, 12, 2 + i, 12, 00, 00)));\r
492 }\r
493\r
494 assertEquals(null, uc.makeReservation(null, null, dtList, "remark"));\r
495 }\r
496\r
497 @Test(expected = IllegalArgumentException.class)\r
498 public void makeReservationException3Test() throws Exception {\r
499 DataHandler dh = new DataHandlerForTests();\r
500 UserController uc = new UserController();\r
501 uc.setDataHandler(dh);\r
502 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
503 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
504 uc.logIn("TestUser1", "pass1");\r
505\r
506 Resource resource1 = new Resource("Room101", "Class room");\r
507 Resource resource2 = new Resource("Room101", "Class room");\r
508 Resource resource3 = new Resource("Room101", "Class room");\r
509 dh.addResource(resource1);\r
510 dh.addResource(resource2);\r
511 dh.addResource(resource3);\r
512\r
513 List<DateRange> dtList = new ArrayList<DateRange>();\r
514 for (int i = 1; i < 10; i++) {\r
515 dtList.add(new DateRangeImpl(new Date(2015, 12, 2 + i, 11, 00, 00), new Date(2015, 12, 2 + i, 12, 00, 00)));\r
516 }\r
517\r
518 assertEquals(null, uc.makeReservation(null, resource1, null, "remark"));\r
519 }\r
520\r
521 @Test(expected = IllegalArgumentException.class)\r
522 public void makeReservationException4Test() throws Exception {\r
523 DataHandler dh = new DataHandlerForTests();\r
524 UserController uc = new UserController();\r
525 uc.setDataHandler(dh);\r
526 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
527 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
528 uc.logIn("TestUser1", "pass1");\r
529\r
530 Resource resource1 = new Resource("Room101", "Class room");\r
531 Resource resource2 = new Resource("Room101", "Class room");\r
532 Resource resource3 = new Resource("Room101", "Class room");\r
533 dh.addResource(resource1);\r
534 dh.addResource(resource2);\r
535 dh.addResource(resource3);\r
536\r
537 List<DateRange> dtList = new ArrayList<DateRange>();\r
538\r
539 assertEquals(null, uc.makeReservation(null, resource1, dtList, "remark"));\r
540 }\r
541\r
542 @Test\r
543 public void changeReservationTest() throws Exception {\r
544 DataHandler dh = new DataHandlerForTests();\r
545 UserController uc = new UserController();\r
546 uc.setDataHandler(dh);\r
547 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
548 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
549 uc.logIn("TestUser1", "pass1");\r
550\r
551 UUID groupId = UUID.randomUUID();\r
552 DateRange dateRange = new DateRangeImpl(new Date(2015, 12, 7, 14, 00, 00), new Date(2015, 12, 7, 15, 00, 00));\r
553\r
554 Resource resource1 = new Resource("Room101", "Class room");\r
555 dh.addResource(resource1);\r
556\r
557 User loggedUser = uc.getLoggedUser();\r
558 User testUser2 = uc.getUser("TestUser2");\r
559\r
560 Reservation reservation1 = new Reservation(groupId, uc.getLoggedUser(), resource1, dateRange,\r
561 "Test reservation1");\r
562 dh.addReservation(reservation1);\r
563\r
564 Reservation reservationClone = uc.getReservation(reservation1.getId());\r
565 DateRange newDateRange = new DateRangeImpl(new Date(2015, 12, 7, 20, 00, 00),\r
566 new Date(2015, 12, 8, 02, 00, 00));\r
567 reservationClone.setDateRange(newDateRange);\r
568 uc.changeReservation(reservationClone);\r
569\r
570 assertEquals(newDateRange, uc.getReservation(reservationClone.getId()).getDateRange());\r
571 }\r
572\r
573 @Test(expected = Exception.class)\r
574 public void changeReservationException1Test() throws Exception {\r
575 DataHandler dh = new DataHandlerForTests();\r
576 UserController uc = new UserController();\r
577 uc.setDataHandler(dh);\r
578 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
579 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
580 uc.logIn("TestUser1", "pass1");\r
581\r
582 UUID groupId = UUID.randomUUID();\r
583 Date start = new Date();\r
584 start.setTime((System.currentTimeMillis() % 1000));\r
585 Date finish = (Date) start.clone();\r
586 finish.setMinutes(start.getMinutes() + 30);\r
587 DateRange dateRange = new DateRangeImpl(start, finish);\r
588\r
589 Resource resource1 = new Resource("Room101", "Class room");\r
590 dh.addResource(resource1);\r
591\r
592 User loggedUser = uc.getLoggedUser();\r
593 User testUser2 = uc.getUser("TestUser2");\r
594\r
595 Reservation reservation1 = new Reservation(groupId, uc.getLoggedUser(), resource1, dateRange,\r
596 "Test reservation1");\r
597 dh.addReservation(reservation1);\r
598\r
599 Reservation reservationClone = uc.getReservation(reservation1.getId());\r
600 start.setDate(start.getDate() + 1);\r
601 finish.setDate(finish.getDate() + 1);\r
602 DateRange newDateRange = new DateRangeImpl(start, finish);\r
603 reservationClone.setDateRange(newDateRange);\r
604\r
605 uc.setDataHandler(null);\r
606 uc.changeReservation(reservationClone);\r
607\r
608 assertEquals(newDateRange, uc.getReservation(reservationClone.getId()).getDateRange());\r
609 }\r
610\r
611 @Test(expected = Exception.class)\r
612 public void changeReservationException2Test() throws Exception {\r
613 DataHandler dh = new DataHandlerForTests();\r
614 UserController uc = new UserController();\r
615 uc.setDataHandler(dh);\r
616 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
617 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
618 uc.logIn("TestUser1", "pass1");\r
619\r
620 UUID groupId = UUID.randomUUID();\r
621 Date start = new Date();\r
622 start.setTime((System.currentTimeMillis() % 1000));\r
623 Date finish = (Date) start.clone();\r
624 finish.setMinutes(start.getMinutes() + 30);\r
625 DateRange dateRange = new DateRangeImpl(start, finish);\r
626\r
627 Resource resource1 = new Resource("Room101", "Class room");\r
628 dh.addResource(resource1);\r
629\r
630 User loggedUser = uc.getLoggedUser();\r
631 User testUser2 = uc.getUser("TestUser2");\r
632\r
633 Reservation reservation1 = new Reservation(groupId, uc.getLoggedUser(), resource1, dateRange,\r
634 "Test reservation1");\r
635 dh.addReservation(reservation1);\r
636\r
637 Reservation reservationClone = uc.getReservation(reservation1.getId());\r
638 start.setDate(start.getDate() + 1);\r
639 finish.setDate(finish.getDate() + 1);\r
640 DateRange newDateRange = new DateRangeImpl(start, finish);\r
641 reservationClone.setDateRange(newDateRange);\r
642\r
643 uc.logOut();\r
644 uc.changeReservation(reservationClone);\r
645\r
646 assertEquals(newDateRange, uc.getReservation(reservationClone.getId()).getDateRange());\r
647 }\r
648\r
649 @Test(expected = IllegalArgumentException.class)\r
650 public void changeReservationException3Test() throws Exception {\r
651 DataHandler dh = new DataHandlerForTests();\r
652 UserController uc = new UserController();\r
653 uc.setDataHandler(dh);\r
654 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
655 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
656 uc.logIn("TestUser1", "pass1");\r
657\r
658 UUID groupId = UUID.randomUUID();\r
659 Date start = new Date();\r
660 start.setTime((System.currentTimeMillis() % 1000));\r
661 Date finish = (Date) start.clone();\r
662 finish.setMinutes(start.getMinutes() + 30);\r
663 DateRange dateRange = new DateRangeImpl(start, finish);\r
664\r
665 Resource resource1 = new Resource("Room101", "Class room");\r
666 dh.addResource(resource1);\r
667\r
668 User loggedUser = uc.getLoggedUser();\r
669 User testUser2 = uc.getUser("TestUser2");\r
670\r
671 Reservation reservation1 = new Reservation(groupId, uc.getLoggedUser(), resource1, dateRange,\r
672 "Test reservation1");\r
673 dh.addReservation(reservation1);\r
674\r
675 Reservation reservationClone = uc.getReservation(reservation1.getId());\r
676 start.setDate(start.getDate() + 1);\r
677 finish.setDate(finish.getDate() + 1);\r
678 DateRange newDateRange = new DateRangeImpl(start, finish);\r
679 reservationClone.setDateRange(newDateRange);\r
680\r
681 uc.changeReservation(null);\r
682\r
683 assertEquals(newDateRange, uc.getReservation(reservationClone.getId()).getDateRange());\r
684 }\r
685\r
686 @Test(expected = Exception.class)\r
687 public void changeReservationException4Test() throws Exception {\r
688 DataHandler dh = new DataHandlerForTests();\r
689 UserController uc = new UserController();\r
690 uc.setDataHandler(dh);\r
691 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
692 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
693 uc.logIn("TestUser1", "pass1");\r
694\r
695 UUID groupId = UUID.randomUUID();\r
696 Date start = new Date();\r
697 start.setTime((System.currentTimeMillis() % 1000));\r
698 Date finish = (Date) start.clone();\r
699 finish.setMinutes(start.getMinutes() + 30);\r
700 DateRange dateRange = new DateRangeImpl(start, finish);\r
701\r
702 Resource resource1 = new Resource("Room101", "Class room");\r
703 dh.addResource(resource1);\r
704\r
705 User loggedUser = uc.getLoggedUser();\r
706 User testUser2 = uc.getUser("TestUser2");\r
707\r
708 Reservation reservation1 = new Reservation(groupId, uc.getLoggedUser(), resource1, dateRange,\r
709 "Test reservation1");\r
710 dh.addReservation(reservation1);\r
711\r
712 Reservation reservationClone = uc.getReservation(reservation1.getId());\r
713 start.setDate(start.getDate() + 1);\r
714 finish.setDate(finish.getDate() + 1);\r
715 DateRange newDateRange = new DateRangeImpl(start, finish);\r
716 reservationClone.setDateRange(newDateRange);\r
717\r
718 uc.logOut();\r
719 uc.logIn("TestUser2", "pass2");\r
720 uc.changeReservation(reservationClone);\r
721\r
722 assertEquals(newDateRange, uc.getReservation(reservationClone.getId()).getDateRange());\r
723 }\r
724\r
725 @Test\r
726 public void deleteReservationTest() throws Exception {\r
727 DataHandler dh = new DataHandlerForTests();\r
728 UserController uc = new UserController();\r
729 uc.setDataHandler(dh);\r
730 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
731 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
732 uc.logIn("TestUser1", "pass1");\r
733\r
734 UUID groupId = UUID.randomUUID();\r
735 Date start = new Date();\r
736 start.setTime((System.currentTimeMillis() % 1000));\r
737 Date finish = (Date) start.clone();\r
738 finish.setMinutes(start.getMinutes() + 30);\r
739 DateRange dateRange = new DateRangeImpl(start, finish);\r
740\r
741 Resource resource1 = new Resource("Room101", "Class room");\r
742 dh.addResource(resource1);\r
743\r
744 User loggedUser = uc.getLoggedUser();\r
745 User testUser2 = uc.getUser("TestUser2");\r
746\r
747 Reservation reservation1 = new Reservation(groupId, uc.getLoggedUser(), resource1, dateRange,\r
748 "Test reservation1");\r
749 dh.addReservation(reservation1);\r
750\r
751 uc.deleteReservation(reservation1);\r
752\r
753 assertEquals(null, uc.getReservation(reservation1.getId()));\r
754 }\r
755\r
756 @Test(expected = Exception.class)\r
757 public void deleteReservationException1Test() throws Exception {\r
758 DataHandler dh = new DataHandlerForTests();\r
759 UserController uc = new UserController();\r
760 uc.setDataHandler(dh);\r
761 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
762 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
763 uc.logIn("TestUser1", "pass1");\r
764\r
765 UUID groupId = UUID.randomUUID();\r
766 Date start = new Date();\r
767 start.setTime((System.currentTimeMillis() % 1000));\r
768 Date finish = (Date) start.clone();\r
769 finish.setMinutes(start.getMinutes() + 30);\r
770 DateRange dateRange = new DateRangeImpl(start, finish);\r
771\r
772 Resource resource1 = new Resource("Room101", "Class room");\r
773 dh.addResource(resource1);\r
774\r
775 User loggedUser = uc.getLoggedUser();\r
776 User testUser2 = uc.getUser("TestUser2");\r
777\r
778 Reservation reservation1 = new Reservation(groupId, uc.getLoggedUser(), resource1, dateRange,\r
779 "Test reservation1");\r
780 dh.addReservation(reservation1);\r
781\r
782 uc.setDataHandler(null);\r
783 uc.deleteReservation(reservation1);\r
784\r
785 assertEquals(null, uc.getReservation(reservation1.getId()));\r
786 }\r
787\r
788 @Test(expected = Exception.class)\r
789 public void deleteReservationException2Test() throws Exception {\r
790 DataHandler dh = new DataHandlerForTests();\r
791 UserController uc = new UserController();\r
792 uc.setDataHandler(dh);\r
793 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
794 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
795 uc.logIn("TestUser1", "pass1");\r
796\r
797 UUID groupId = UUID.randomUUID();\r
798 Date start = new Date();\r
799 start.setTime((System.currentTimeMillis() % 1000));\r
800 Date finish = (Date) start.clone();\r
801 finish.setMinutes(start.getMinutes() + 30);\r
802 DateRange dateRange = new DateRangeImpl(start, finish);\r
803\r
804 Resource resource1 = new Resource("Room101", "Class room");\r
805 dh.addResource(resource1);\r
806\r
807 User loggedUser = uc.getLoggedUser();\r
808 User testUser2 = uc.getUser("TestUser2");\r
809\r
810 Reservation reservation1 = new Reservation(groupId, uc.getLoggedUser(), resource1, dateRange,\r
811 "Test reservation1");\r
812 dh.addReservation(reservation1);\r
813\r
814 uc.logOut();\r
815 uc.deleteReservation(reservation1);\r
816\r
817 assertEquals(null, uc.getReservation(reservation1.getId()));\r
818 }\r
819\r
820 @Test(expected = IllegalArgumentException.class)\r
821 public void deleteReservationException3Test() throws Exception {\r
822 DataHandler dh = new DataHandlerForTests();\r
823 UserController uc = new UserController();\r
824 uc.setDataHandler(dh);\r
825 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
826 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
827 uc.logIn("TestUser1", "pass1");\r
828\r
829 UUID groupId = UUID.randomUUID();\r
830 Date start = new Date();\r
831 start.setTime((System.currentTimeMillis() % 1000));\r
832 Date finish = (Date) start.clone();\r
833 finish.setMinutes(start.getMinutes() + 30);\r
834 DateRange dateRange = new DateRangeImpl(start, finish);\r
835\r
836 Resource resource1 = new Resource("Room101", "Class room");\r
837 dh.addResource(resource1);\r
838\r
839 User loggedUser = uc.getLoggedUser();\r
840 User testUser2 = uc.getUser("TestUser2");\r
841\r
842 Reservation reservation1 = new Reservation(groupId, uc.getLoggedUser(), resource1, dateRange,\r
843 "Test reservation1");\r
844 dh.addReservation(reservation1);\r
845\r
846 uc.deleteReservation(null);\r
847\r
848 assertEquals(null, uc.getReservation(reservation1.getId()));\r
849 }\r
850\r
851 @Test(expected = Exception.class)\r
852 public void deleteReservationException4Test() throws Exception {\r
853 DataHandler dh = new DataHandlerForTests();\r
854 UserController uc = new UserController();\r
855 uc.setDataHandler(dh);\r
856 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
857 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
858 uc.logIn("TestUser1", "pass1");\r
859\r
860 UUID groupId = UUID.randomUUID();\r
861 Date start = new Date();\r
862 start.setTime((System.currentTimeMillis() % 1000));\r
863 Date finish = (Date) start.clone();\r
864 finish.setMinutes(start.getMinutes() + 30);\r
865 DateRange dateRange = new DateRangeImpl(start, finish);\r
866\r
867 Resource resource1 = new Resource("Room101", "Class room");\r
868 dh.addResource(resource1);\r
869\r
870 User loggedUser = uc.getLoggedUser();\r
871 User testUser2 = uc.getUser("TestUser2");\r
872\r
873 Reservation reservation1 = new Reservation(groupId, uc.getLoggedUser(), resource1, dateRange,\r
874 "Test reservation1");\r
875 dh.addReservation(reservation1);\r
876\r
877 uc.logOut();\r
878 uc.logIn("TestUser2", "pass2");\r
879 uc.deleteReservation(reservation1);\r
880\r
881 assertEquals(null, uc.getReservation(reservation1.getId()));\r
882 }\r
883\r
884 @Test\r
885 public void getResourcesTest() throws Exception {\r
886 DataHandler dh = new DataHandlerForTests();\r
887 UserController uc = new UserController();\r
888 uc.setDataHandler(dh);\r
889 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
890 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
891 uc.logIn("TestUser1", "pass1");\r
892\r
893 Date start = new Date();\r
894 start.setTime((System.currentTimeMillis() % 1000));\r
895 Date finish = (Date) start.clone();\r
896 finish.setMinutes(start.getMinutes() + 30);\r
897\r
898 Resource resource1 = new Resource("Room101", "Class room");\r
899 Resource resource2 = new Resource("Room101", "Class room");\r
900 Resource resource3 = new Resource("Room101", "Class room");\r
901 dh.addResource(resource1);\r
902 dh.addResource(resource2);\r
903 dh.addResource(resource3);\r
904\r
905 List<Resource> resList = uc.getResources();\r
906\r
907 assertEquals(3, resList.size());\r
908 }\r
909\r
910 @Test(expected = Exception.class)\r
911 public void getResourcesExceptionTest() throws Exception {\r
912 DataHandler dh = new DataHandlerForTests();\r
913 UserController uc = new UserController();\r
914 uc.setDataHandler(dh);\r
915 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
916 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
917 uc.logIn("TestUser1", "pass1");\r
918\r
919 Date start = new Date();\r
920 start.setTime((System.currentTimeMillis() % 1000));\r
921 Date finish = (Date) start.clone();\r
922 finish.setMinutes(start.getMinutes() + 30);\r
923\r
924 Resource resource1 = new Resource("Room101", "Class room");\r
925 Resource resource2 = new Resource("Room101", "Class room");\r
926 Resource resource3 = new Resource("Room101", "Class room");\r
927 dh.addResource(resource1);\r
928 dh.addResource(resource2);\r
929 dh.addResource(resource3);\r
930\r
931 uc.setDataHandler(null);\r
932 List<Resource> resList = uc.getResources();\r
933\r
934 assertEquals(3, resList.size());\r
935 }\r
936}\r