Usage of Date fixed
[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 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(2015,11,28,11,00,00);
286 Date finish = new Date(2015,11,28,11,30,00);
287 DateRange dateRange = new DateRangeImpl(start, finish);
288
289 Resource resource1 = new Resource("Room101", "Class room");
290 Resource resource2 = new Resource("Room101", "Class room");
291 Resource resource3 = new Resource("Room101", "Class room");
292 dh.addResource(resource1);
293 dh.addResource(resource2);
294 dh.addResource(resource3);
295
296 User loggedUser = uc.getLoggedUser();
297 User testUser2 = uc.getUser("TestUser2");
298
299 Reservation reservation1 = new Reservation(groupId, uc.getLoggedUser(), resource1, dateRange, "Test reservation1");
300 Reservation reservation2 = new Reservation(groupId, uc.getLoggedUser(), resource2, dateRange, "Test reservation2");
301 Reservation reservation3 = new Reservation(groupId, testUser2, resource3, dateRange, "Test reservation3");
302 dh.addReservation(reservation1);
303 dh.addReservation(reservation2);
304 dh.addReservation(reservation3);
305
306 Map<Reservation, Boolean> loggedUserReservation = uc.getUserReservations(null);
307 Map<Reservation, Boolean> reservationMap = uc.getUserReservations(testUser2);
308 Map<Reservation, Boolean> reservationDateRangeMap = uc.getReservationsInDateRange(dateRange);
309 Map<Reservation, Boolean> reservationGroupMap = uc.getReservationGroup(groupId);
310
311 assertEquals(reservationDateRangeMap, reservationGroupMap);
312
313 assertTrue(loggedUserReservation.containsKey(reservation1));
314 assertTrue(loggedUserReservation.containsKey(reservation2));
315 assertFalse(loggedUserReservation.containsKey(reservation3));
316 assertTrue(reservationMap.containsKey(reservation3));
317
318 for (Map.Entry<Reservation, Boolean> entry : reservationDateRangeMap.entrySet()) {
319 if(entry.getKey().getUser().getUserName().equals("TestUser1")){
320 assertTrue(entry.getValue());
321 assertEquals(entry.getKey().getId(), uc.getReservation(entry.getKey().getId()).getId());
322 }
323 else{
324 assertFalse(entry.getValue());
325 assertEquals(entry.getKey().getId(), uc.getReservation(entry.getKey().getId()).getId());
326 }
327 }
328 assertEquals(null, uc.getReservation(UUID.randomUUID()));
329 }
330
331 @Test(expected=Exception.class)
332 public void getReservationGroupException1Test() throws Exception{
333 UserController uc = new UserController();
334 uc.getReservationGroup(null);
335 }
336
337 @Test(expected=IllegalArgumentException.class)
338 public void getReservationGroupException2Test() throws Exception{
339 DataHandler dh = new DataHandlerForTests();
340 UserController uc = new UserController();
341 uc.setDataHandler(dh);
342 uc.getReservationGroup(null);
343 }
344
345 @Test(expected=Exception.class)
346 public void getUserReservationException1Test() throws Exception{
347 UserController uc = new UserController();
348 uc.getUserReservations(null);
349 }
350
351 @Test(expected=Exception.class)
352 public void getUserReservationException2Test() throws Exception{
353 DataHandler dh = new DataHandlerForTests();
354 UserController uc = new UserController();
355 uc.setDataHandler(dh);
356 uc.getUserReservations(null);
357 }
358
359 @Test(expected=Exception.class)
360 public void getReservationException1Test() throws Exception{
361 UserController uc = new UserController();
362 uc.getReservation(null);
363 }
364
365 @Test(expected=Exception.class)
366 public void getReservationException2Test() throws Exception{
367 DataHandler dh = new DataHandlerForTests();
368 UserController uc = new UserController();
369 uc.setDataHandler(dh);
370 uc.getReservation(null);
371 }
372
373 @Test(expected=Exception.class)
374 public void getReservationsInDateRangeException1Test() throws Exception{
375 UserController uc = new UserController();
376 uc.getReservationsInDateRange(null);
377 }
378
379 @Test(expected=Exception.class)
380 public void getReservationsInDateRangeException2Test() throws Exception{
381 DataHandler dh = new DataHandlerForTests();
382 UserController uc = new UserController();
383 uc.setDataHandler(dh);
384 uc.getReservationsInDateRange(null);
385 }
386
387 @Test
388 public void makeReservationTest() throws Exception{
389 DataHandler dh = new DataHandlerForTests();
390 UserController uc = new UserController();
391 uc.setDataHandler(dh);
392 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);
393 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);
394 uc.logIn("TestUser1", "pass1");
395
396 Resource resource1 = new Resource("Room101", "Class room");
397 Resource resource2 = new Resource("Room101", "Class room");
398 Resource resource3 = new Resource("Room101", "Class room");
399 dh.addResource(resource1);
400 dh.addResource(resource2);
401 dh.addResource(resource3);
402
403 List<DateRange> dtList = new ArrayList<DateRange>();
404 for(int i = 1;i<10;i++){
405 dtList.add(new DateRangeImpl(new Date(2015,12,2+i,11,00,00), new Date(2015,12,2+i,11,00,00)));
406 }
407
408 assertEquals(null, uc.makeReservation(uc.getLoggedUser(), resource1, dtList, "remark"));
409 assertEquals(null, uc.makeReservation(null, resource2, dtList, "remark"));
410 }
411
412 @Test
413 public void makeInvalidReservationTest() throws Exception{
414 DataHandler dh = new DataHandlerForTests();
415 UserController uc = new UserController();
416 uc.setDataHandler(dh);
417 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);
418 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);
419 uc.logIn("TestUser1", "pass1");
420
421 Resource resource1 = new Resource("Room101", "Class room");
422 Resource resource2 = new Resource("Room101", "Class room");
423 Resource resource3 = new Resource("Room101", "Class room");
424 dh.addResource(resource1);
425 dh.addResource(resource2);
426 dh.addResource(resource3);
427
428 List<DateRange> dtList = new ArrayList<DateRange>();
429 for(int i = 1;i<10;i++){
430 dtList.add(new DateRangeImpl(new Date(2015,12,2+i,11,00,00), new Date(2015,12,2+i,12,00,00)));
431 }
432
433 List<DateRange> conflictingList = new ArrayList<DateRange>();
434 // Conflicts
435 conflictingList.add(new DateRangeImpl(new Date(2015,12,4,11,00,00),new Date(2015,12,4,12,00,00)));
436 conflictingList.add(new DateRangeImpl(new Date(2015,12,4,11,15,00),new Date(2015,12,4,11,45,00)));
437 conflictingList.add(new DateRangeImpl(new Date(2015,12,4,10,00,00),new Date(2015,12,4,17,00,00)));
438 conflictingList.add(new DateRangeImpl(new Date(2015,12,4,11,30,00),new Date(2015,12,4,12,30,00)));
439
440 // OK
441 conflictingList.add(new DateRangeImpl(new Date(2015,12,4,10,30,00),new Date(2015,12,4,10,59,59)));
442 conflictingList.add(new DateRangeImpl(new Date(2015,12,4,12,00,01),new Date(2015,12,4,12,45,00)));
443
444 assertEquals(null, uc.makeReservation(uc.getLoggedUser(), resource1, dtList, "remark"));
445 assertEquals(4, uc.makeReservation(uc.getLoggedUser(), resource1, conflictingList, null).size());
446 }
447
448 @Test(expected=IllegalArgumentException.class)
449 public void makeReservationException1Test() throws Exception{
450 DataHandler dh = new DataHandlerForTests();
451 UserController uc = new UserController();
452 uc.setDataHandler(dh);
453 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);
454 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);
455 uc.logIn("TestUser1", "pass1");
456
457 Resource resource1 = new Resource("Room101", "Class room");
458 Resource resource2 = new Resource("Room101", "Class room");
459 Resource resource3 = new Resource("Room101", "Class room");
460 dh.addResource(resource1);
461 dh.addResource(resource2);
462 dh.addResource(resource3);
463
464 List<DateRange> dtList = new ArrayList<DateRange>();
465 for(int i = 1;i<10;i++){
466 dtList.add(new DateRangeImpl(new Date(2015,12,2+i,11,00,00), new Date(2015,12,2+i,12,00,00)));
467 }
468
469 uc.logOut();
470 assertEquals(null, uc.makeReservation(null, resource1, dtList, "remark"));
471 }
472
473 @Test(expected=IllegalArgumentException.class)
474 public void makeReservationException2Test() throws Exception{
475 DataHandler dh = new DataHandlerForTests();
476 UserController uc = new UserController();
477 uc.setDataHandler(dh);
478 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);
479 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);
480 uc.logIn("TestUser1", "pass1");
481
482 Resource resource1 = new Resource("Room101", "Class room");
483 Resource resource2 = new Resource("Room101", "Class room");
484 Resource resource3 = new Resource("Room101", "Class room");
485 dh.addResource(resource1);
486 dh.addResource(resource2);
487 dh.addResource(resource3);
488
489 List<DateRange> dtList = new ArrayList<DateRange>();
490 for(int i = 1;i<10;i++){
491 dtList.add(new DateRangeImpl(new Date(2015,12,2+i,11,00,00), new Date(2015,12,2+i,12,00,00)));
492 }
493
494 assertEquals(null, uc.makeReservation(null, null, dtList, "remark"));
495 }
496
497 @Test(expected=IllegalArgumentException.class)
498 public void makeReservationException3Test() throws Exception{
499 DataHandler dh = new DataHandlerForTests();
500 UserController uc = new UserController();
501 uc.setDataHandler(dh);
502 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);
503 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);
504 uc.logIn("TestUser1", "pass1");
505
506 Resource resource1 = new Resource("Room101", "Class room");
507 Resource resource2 = new Resource("Room101", "Class room");
508 Resource resource3 = new Resource("Room101", "Class room");
509 dh.addResource(resource1);
510 dh.addResource(resource2);
511 dh.addResource(resource3);
512
513 List<DateRange> dtList = new ArrayList<DateRange>();
514 for(int i = 1;i<10;i++){
515 dtList.add(new DateRangeImpl(new Date(2015,12,2+i,11,00,00), new Date(2015,12,2+i,12,00,00)));
516 }
517
518 assertEquals(null, uc.makeReservation(null, resource1, null, "remark"));
519 }
520
521 @Test(expected=IllegalArgumentException.class)
522 public void makeReservationException4Test() throws Exception{
523 DataHandler dh = new DataHandlerForTests();
524 UserController uc = new UserController();
525 uc.setDataHandler(dh);
526 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);
527 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);
528 uc.logIn("TestUser1", "pass1");
529
530 Resource resource1 = new Resource("Room101", "Class room");
531 Resource resource2 = new Resource("Room101", "Class room");
532 Resource resource3 = new Resource("Room101", "Class room");
533 dh.addResource(resource1);
534 dh.addResource(resource2);
535 dh.addResource(resource3);
536
537 List<DateRange> dtList = new ArrayList<DateRange>();
538
539 assertEquals(null, uc.makeReservation(null, resource1, dtList, "remark"));
540 }
541
542 @Test
543 public void changeReservationTest() throws Exception{
544 DataHandler dh = new DataHandlerForTests();
545 UserController uc = new UserController();
546 uc.setDataHandler(dh);
547 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);
548 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);
549 uc.logIn("TestUser1", "pass1");
550
551 UUID groupId = UUID.randomUUID();
552 DateRange dateRange = new DateRangeImpl(new Date(2015,12,7,14,00,00), new Date(2015,12,7,15,00,00));
553
554 Resource resource1 = new Resource("Room101", "Class room");
555 dh.addResource(resource1);
556
557 User loggedUser = uc.getLoggedUser();
558 User testUser2 = uc.getUser("TestUser2");
559
560 Reservation reservation1 = new Reservation(groupId, uc.getLoggedUser(), resource1, dateRange, "Test reservation1");
561 dh.addReservation(reservation1);
562
563 Reservation reservationClone = uc.getReservation(reservation1.getId());
564 DateRange newDateRange = new DateRangeImpl(new Date(2015,12,7,20,00,00), new Date(2015,12,8,02,00,00));
565 reservationClone.setDateRange(newDateRange);
566 uc.changeReservation(reservationClone);
567
568 assertEquals(newDateRange, uc.getReservation(reservationClone.getId()).getDateRange());
569 }
570
571 @Test(expected=Exception.class)
572 public void changeReservationException1Test() throws Exception{
573 DataHandler dh = new DataHandlerForTests();
574 UserController uc = new UserController();
575 uc.setDataHandler(dh);
576 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);
577 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);
578 uc.logIn("TestUser1", "pass1");
579
580 UUID groupId = UUID.randomUUID();
581 Date start = new Date();
582 start.setTime((System.currentTimeMillis() % 1000));
583 Date finish = (Date)start.clone();
584 finish.setMinutes(start.getMinutes() + 30);
585 DateRange dateRange = new DateRangeImpl(start, finish);
586
587 Resource resource1 = new Resource("Room101", "Class room");
588 dh.addResource(resource1);
589
590 User loggedUser = uc.getLoggedUser();
591 User testUser2 = uc.getUser("TestUser2");
592
593 Reservation reservation1 = new Reservation(groupId, uc.getLoggedUser(), resource1, dateRange, "Test reservation1");
594 dh.addReservation(reservation1);
595
596 Reservation reservationClone = uc.getReservation(reservation1.getId());
597 start.setDate(start.getDate() + 1);
598 finish.setDate(finish.getDate() + 1);
599 DateRange newDateRange = new DateRangeImpl(start, finish);
600 reservationClone.setDateRange(newDateRange);
601
602 uc.setDataHandler(null);
603 uc.changeReservation(reservationClone);
604
605 assertEquals(newDateRange, uc.getReservation(reservationClone.getId()).getDateRange());
606 }
607
608 @Test(expected=Exception.class)
609 public void changeReservationException2Test() throws Exception{
610 DataHandler dh = new DataHandlerForTests();
611 UserController uc = new UserController();
612 uc.setDataHandler(dh);
613 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);
614 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);
615 uc.logIn("TestUser1", "pass1");
616
617 UUID groupId = UUID.randomUUID();
618 Date start = new Date();
619 start.setTime((System.currentTimeMillis() % 1000));
620 Date finish = (Date)start.clone();
621 finish.setMinutes(start.getMinutes() + 30);
622 DateRange dateRange = new DateRangeImpl(start, finish);
623
624 Resource resource1 = new Resource("Room101", "Class room");
625 dh.addResource(resource1);
626
627 User loggedUser = uc.getLoggedUser();
628 User testUser2 = uc.getUser("TestUser2");
629
630 Reservation reservation1 = new Reservation(groupId, uc.getLoggedUser(), resource1, dateRange, "Test reservation1");
631 dh.addReservation(reservation1);
632
633 Reservation reservationClone = uc.getReservation(reservation1.getId());
634 start.setDate(start.getDate() + 1);
635 finish.setDate(finish.getDate() + 1);
636 DateRange newDateRange = new DateRangeImpl(start, finish);
637 reservationClone.setDateRange(newDateRange);
638
639 uc.logOut();
640 uc.changeReservation(reservationClone);
641
642 assertEquals(newDateRange, uc.getReservation(reservationClone.getId()).getDateRange());
643 }
644
645 @Test(expected=IllegalArgumentException.class)
646 public void changeReservationException3Test() throws Exception{
647 DataHandler dh = new DataHandlerForTests();
648 UserController uc = new UserController();
649 uc.setDataHandler(dh);
650 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);
651 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);
652 uc.logIn("TestUser1", "pass1");
653
654 UUID groupId = UUID.randomUUID();
655 Date start = new Date();
656 start.setTime((System.currentTimeMillis() % 1000));
657 Date finish = (Date)start.clone();
658 finish.setMinutes(start.getMinutes() + 30);
659 DateRange dateRange = new DateRangeImpl(start, finish);
660
661 Resource resource1 = new Resource("Room101", "Class room");
662 dh.addResource(resource1);
663
664 User loggedUser = uc.getLoggedUser();
665 User testUser2 = uc.getUser("TestUser2");
666
667 Reservation reservation1 = new Reservation(groupId, uc.getLoggedUser(), resource1, dateRange, "Test reservation1");
668 dh.addReservation(reservation1);
669
670 Reservation reservationClone = uc.getReservation(reservation1.getId());
671 start.setDate(start.getDate() + 1);
672 finish.setDate(finish.getDate() + 1);
673 DateRange newDateRange = new DateRangeImpl(start, finish);
674 reservationClone.setDateRange(newDateRange);
675
676 uc.changeReservation(null);
677
678 assertEquals(newDateRange, uc.getReservation(reservationClone.getId()).getDateRange());
679 }
680
681 @Test(expected=Exception.class)
682 public void changeReservationException4Test() throws Exception{
683 DataHandler dh = new DataHandlerForTests();
684 UserController uc = new UserController();
685 uc.setDataHandler(dh);
686 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);
687 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);
688 uc.logIn("TestUser1", "pass1");
689
690 UUID groupId = UUID.randomUUID();
691 Date start = new Date();
692 start.setTime((System.currentTimeMillis() % 1000));
693 Date finish = (Date)start.clone();
694 finish.setMinutes(start.getMinutes() + 30);
695 DateRange dateRange = new DateRangeImpl(start, finish);
696
697 Resource resource1 = new Resource("Room101", "Class room");
698 dh.addResource(resource1);
699
700 User loggedUser = uc.getLoggedUser();
701 User testUser2 = uc.getUser("TestUser2");
702
703 Reservation reservation1 = new Reservation(groupId, uc.getLoggedUser(), resource1, dateRange, "Test reservation1");
704 dh.addReservation(reservation1);
705
706 Reservation reservationClone = uc.getReservation(reservation1.getId());
707 start.setDate(start.getDate() + 1);
708 finish.setDate(finish.getDate() + 1);
709 DateRange newDateRange = new DateRangeImpl(start, finish);
710 reservationClone.setDateRange(newDateRange);
711
712 uc.logOut();
713 uc.logIn("TestUser2", "pass2");
714 uc.changeReservation(reservationClone);
715
716 assertEquals(newDateRange, uc.getReservation(reservationClone.getId()).getDateRange());
717 }
718
719 @Test
720 public void deleteReservationTest() throws Exception{
721 DataHandler dh = new DataHandlerForTests();
722 UserController uc = new UserController();
723 uc.setDataHandler(dh);
724 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);
725 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);
726 uc.logIn("TestUser1", "pass1");
727
728 UUID groupId = UUID.randomUUID();
729 Date start = new Date();
730 start.setTime((System.currentTimeMillis() % 1000));
731 Date finish = (Date)start.clone();
732 finish.setMinutes(start.getMinutes() + 30);
733 DateRange dateRange = new DateRangeImpl(start, finish);
734
735 Resource resource1 = new Resource("Room101", "Class room");
736 dh.addResource(resource1);
737
738 User loggedUser = uc.getLoggedUser();
739 User testUser2 = uc.getUser("TestUser2");
740
741 Reservation reservation1 = new Reservation(groupId, uc.getLoggedUser(), resource1, dateRange, "Test reservation1");
742 dh.addReservation(reservation1);
743
744 uc.deleteReservation(reservation1);
745
746 assertEquals(null, uc.getReservation(reservation1.getId()));
747 }
748
749 @Test(expected=Exception.class)
750 public void deleteReservationException1Test() throws Exception{
751 DataHandler dh = new DataHandlerForTests();
752 UserController uc = new UserController();
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 User loggedUser = uc.getLoggedUser();
769 User testUser2 = uc.getUser("TestUser2");
770
771 Reservation reservation1 = new Reservation(groupId, uc.getLoggedUser(), resource1, dateRange, "Test reservation1");
772 dh.addReservation(reservation1);
773
774 uc.setDataHandler(null);
775 uc.deleteReservation(reservation1);
776
777 assertEquals(null, uc.getReservation(reservation1.getId()));
778 }
779
780 @Test(expected=Exception.class)
781 public void deleteReservationException2Test() throws Exception{
782 DataHandler dh = new DataHandlerForTests();
783 UserController uc = new UserController();
784 uc.setDataHandler(dh);
785 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);
786 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);
787 uc.logIn("TestUser1", "pass1");
788
789 UUID groupId = UUID.randomUUID();
790 Date start = new Date();
791 start.setTime((System.currentTimeMillis() % 1000));
792 Date finish = (Date)start.clone();
793 finish.setMinutes(start.getMinutes() + 30);
794 DateRange dateRange = new DateRangeImpl(start, finish);
795
796 Resource resource1 = new Resource("Room101", "Class room");
797 dh.addResource(resource1);
798
799 User loggedUser = uc.getLoggedUser();
800 User testUser2 = uc.getUser("TestUser2");
801
802 Reservation reservation1 = new Reservation(groupId, uc.getLoggedUser(), resource1, dateRange, "Test reservation1");
803 dh.addReservation(reservation1);
804
805 uc.logOut();
806 uc.deleteReservation(reservation1);
807
808 assertEquals(null, uc.getReservation(reservation1.getId()));
809 }
810
811 @Test(expected=IllegalArgumentException.class)
812 public void deleteReservationException3Test() throws Exception{
813 DataHandler dh = new DataHandlerForTests();
814 UserController uc = new UserController();
815 uc.setDataHandler(dh);
816 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);
817 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);
818 uc.logIn("TestUser1", "pass1");
819
820 UUID groupId = UUID.randomUUID();
821 Date start = new Date();
822 start.setTime((System.currentTimeMillis() % 1000));
823 Date finish = (Date)start.clone();
824 finish.setMinutes(start.getMinutes() + 30);
825 DateRange dateRange = new DateRangeImpl(start, finish);
826
827 Resource resource1 = new Resource("Room101", "Class room");
828 dh.addResource(resource1);
829
830 User loggedUser = uc.getLoggedUser();
831 User testUser2 = uc.getUser("TestUser2");
832
833 Reservation reservation1 = new Reservation(groupId, uc.getLoggedUser(), resource1, dateRange, "Test reservation1");
834 dh.addReservation(reservation1);
835
836 uc.deleteReservation(null);
837
838 assertEquals(null, uc.getReservation(reservation1.getId()));
839 }
840
841 @Test(expected=Exception.class)
842 public void deleteReservationException4Test() throws Exception{
843 DataHandler dh = new DataHandlerForTests();
844 UserController uc = new UserController();
845 uc.setDataHandler(dh);
846 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);
847 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);
848 uc.logIn("TestUser1", "pass1");
849
850 UUID groupId = UUID.randomUUID();
851 Date start = new Date();
852 start.setTime((System.currentTimeMillis() % 1000));
853 Date finish = (Date)start.clone();
854 finish.setMinutes(start.getMinutes() + 30);
855 DateRange dateRange = new DateRangeImpl(start, finish);
856
857 Resource resource1 = new Resource("Room101", "Class room");
858 dh.addResource(resource1);
859
860 User loggedUser = uc.getLoggedUser();
861 User testUser2 = uc.getUser("TestUser2");
862
863 Reservation reservation1 = new Reservation(groupId, uc.getLoggedUser(), resource1, dateRange, "Test reservation1");
864 dh.addReservation(reservation1);
865
866 uc.logOut();
867 uc.logIn("TestUser2", "pass2");
868 uc.deleteReservation(reservation1);
869
870 assertEquals(null, uc.getReservation(reservation1.getId()));
871 }
872
873 @Test
874 public void getResourcesTest() throws Exception{
875 DataHandler dh = new DataHandlerForTests();
876 UserController uc = new UserController();
877 uc.setDataHandler(dh);
878 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);
879 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);
880 uc.logIn("TestUser1", "pass1");
881
882 Date start = new Date();
883 start.setTime((System.currentTimeMillis() % 1000));
884 Date finish = (Date)start.clone();
885 finish.setMinutes(start.getMinutes() + 30);
886
887 Resource resource1 = new Resource("Room101", "Class room");
888 Resource resource2 = new Resource("Room101", "Class room");
889 Resource resource3 = new Resource("Room101", "Class room");
890 dh.addResource(resource1);
891 dh.addResource(resource2);
892 dh.addResource(resource3);
893
894 List<Resource> resList = uc.getResources();
895
896 assertEquals(3,resList.size());
897 }
898
899 @Test(expected=Exception.class)
900 public void getResourcesExceptionTest() throws Exception{
901 DataHandler dh = new DataHandlerForTests();
902 UserController uc = new UserController();
903 uc.setDataHandler(dh);
904 uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);
905 uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);
906 uc.logIn("TestUser1", "pass1");
907
908 Date start = new Date();
909 start.setTime((System.currentTimeMillis() % 1000));
910 Date finish = (Date)start.clone();
911 finish.setMinutes(start.getMinutes() + 30);
912
913 Resource resource1 = new Resource("Room101", "Class room");
914 Resource resource2 = new Resource("Room101", "Class room");
915 Resource resource3 = new Resource("Room101", "Class room");
916 dh.addResource(resource1);
917 dh.addResource(resource2);
918 dh.addResource(resource3);
919
920 uc.setDataHandler(null);
921 List<Resource> resList = uc.getResources();
922
923 assertEquals(3,resList.size());
924 }
925 }