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