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