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