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