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