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