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