DataHandlerForTestsTest updated
[staff/due1/sed-hs15-srs-purple.git] / src / test / java / ch / bfh / ti / srs / datahandler / DataHandlerForTestsTest.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.datahandler;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.UUID;
18
19 import org.junit.Before;
20 import org.junit.Test;
21
22 import ch.bfh.ti.daterange.DateFactory;
23 import ch.bfh.ti.daterange.DateRange;
24 import ch.bfh.ti.daterange.DateRangeFactory;
25 import ch.bfh.ti.srs.datahandler.DataHandler;
26 import ch.bfh.ti.srs.datahandler.DataHandlerForTests;
27 import ch.bfh.ti.srs.dataschema.Reservation;
28 import ch.bfh.ti.srs.dataschema.Resource;
29 import ch.bfh.ti.srs.dataschema.User;
30
31 /**
32 * @author Michael Grossen
33 */
34 public class DataHandlerForTestsTest {
35
36 private DateRangeFactory factory;
37 private Reservation testReservation;
38 private Reservation testReservation2; // same ID as testReservation
39 private Reservation testReservation3;
40 private Reservation testReservation4;
41 private Reservation testReservation5;
42
43 @Before
44 public void setUp() throws Exception {
45 String className = System.getProperty("daterangefactory.name",
46 "ch.bfh.ti.daterange.impl.pojo.DateRangeFactory");
47 Class<?> clazz = Class.forName(className);
48 factory = (DateRangeFactory) clazz.newInstance();
49
50 User testUser = new User("TestUser", "pass");
51 Resource testResource = new Resource("TestResource", "TestType");
52 DateRange testDr = factory.createDateRange(DateFactory.createDate(2015, 11, 9),
53 DateFactory.createDate(2015, 11, 13));
54 UUID groupid = UUID.randomUUID();
55 testReservation = new Reservation(groupid, testUser, testResource, testDr, "TestReservation");
56
57 testReservation2 = testReservation.clone();
58 testReservation2.setRemark("TestReservation2");
59
60 User testUser3 = new User("TestUser3", "pass3");
61 Resource testResource3 = new Resource("TestResource3", "TestType3");
62 DateRange testDr3 = factory.createDateRange(DateFactory.createDate(2016, 11, 9),
63 DateFactory.createDate(2016, 11, 13));
64 testReservation3 = new Reservation(groupid, testUser3, testResource3, testDr3, "TestReservation3");
65
66 User testUser4 = new User("TestUser4", "pass4");
67 Resource testResource4 = new Resource("TestResource4", "TestType4");
68 DateRange testDr4 = factory.createDateRange(DateFactory.createDate(2017, 11, 9),
69 DateFactory.createDate(2017, 11, 13));
70 UUID groupid4 = UUID.randomUUID();
71 testReservation4 = new Reservation(groupid4, testUser4, testResource4, testDr4, "TestReservation4");
72
73 DateRange testDr5 = factory.createDateRange(DateFactory.createDate(2015, 11, 10),
74 DateFactory.createDate(2015, 11, 14));
75 testReservation5 = new Reservation(groupid, testUser, testResource, testDr5, "TestReservation5");
76
77 }
78
79 @Test
80 public void addUserTest() throws Exception {
81 DataHandler dh = new DataHandlerForTests();
82 User testUser = null;
83 try {
84 testUser = new User("TestUser", "pass");
85 } catch (Exception e) {
86 fail("Check UserTest (Exception was thrown by User Cunstructor)!");
87 }
88 dh.addUser(testUser);
89 }
90
91 @Test(expected = IllegalArgumentException.class)
92 public void addIllegalUserTest() throws Exception {
93 DataHandler dh = new DataHandlerForTests();
94 User testUser = null;
95 dh.addUser(testUser);
96 }
97
98 @Test
99 public void addSameUserTest() throws Exception {
100 DataHandler dh = new DataHandlerForTests();
101 User testUser = null;
102 try {
103 testUser = new User("TestUser", "pass");
104 } catch (Exception e) {
105 fail("Check UserTest (Exception was thrown by User Cunstructor)!");
106 }
107 dh.addUser(testUser);
108 assertEquals(false, dh.addUser(testUser));
109 }
110
111 @Test
112 public void findUserTest() throws Exception {
113 DataHandler dh = new DataHandlerForTests();
114 User addedUser = new User("TestUser", "pass");
115 dh.addUser(addedUser);
116 User foundUser = dh.findUser("TestUser");
117 assertEquals(addedUser.getUserName(), foundUser.getUserName());
118 }
119
120 @Test
121 public void findNotAddedUser() throws Exception {
122 DataHandler dh = new DataHandlerForTests();
123 assertEquals(null, dh.findUser("notAddedUser"));
124 User addedUser = new User("TestUser", "pass");
125 dh.addUser(addedUser);
126 assertEquals(null, dh.findUser("otherUser"));
127 }
128
129 @Test
130 public void addResourceTest() throws Exception {
131 DataHandler dh = new DataHandlerForTests();
132 Resource testResource = null;
133 try {
134 testResource = new Resource("TestResource", "TestType");
135 } catch (Exception e) {
136 fail("Check ResourceTest (Exception was thrown by Resource Cunstructor)!");
137 }
138 dh.addResource(testResource);
139 }
140
141 @Test
142 public void addSameResourceTest() throws Exception {
143 DataHandler dh = new DataHandlerForTests();
144 Resource testResource = null;
145 try {
146 testResource = new Resource("TestResource", "TestType");
147 ;
148 } catch (Exception e) {
149 fail("Check UserTest (Exception was thrown by Resource Cunstructor)!");
150 }
151 dh.addResource(testResource);
152 assertEquals(false, dh.addResource(testResource));
153 }
154
155 @Test(expected = IllegalArgumentException.class)
156 public void addIllegalResourceTest() throws Exception {
157 DataHandler dh = new DataHandlerForTests();
158 Resource testResource = null;
159 dh.addResource(testResource);
160 }
161
162 @Test
163 public void findResourceTest() throws Exception {
164 DataHandler dh = new DataHandlerForTests();
165 Resource addedResource = new Resource("TestResource", "TestType");
166 dh.addResource(addedResource);
167 Resource foundResource = dh.findResource("TestResource");
168 assertEquals(addedResource.getName(), foundResource.getName());
169 }
170
171 @Test
172 public void findNoResourceTest() throws Exception {
173 DataHandler dh = new DataHandlerForTests();
174 Resource addedResource = new Resource("TestResource", "TestType");
175 Resource addedResource2 = new Resource("TestResource2", "TestType2");
176 dh.addResource(addedResource);
177 dh.addResource(addedResource2);
178 assertEquals(null, dh.findResource("TestResource3"));
179 }
180
181 @Test
182 public void addResourceSameIdTest() throws Exception {
183 DataHandler dh = new DataHandlerForTests();
184 Resource addedResource = new Resource("TestResource", "TestType");
185 Resource addedResource2 = addedResource.clone();
186 addedResource2.setName("TestResource2");
187 addedResource2.setType("TestType");
188 dh.addResource(addedResource);
189 assertEquals(false, dh.addResource(addedResource2));
190 }
191
192 @Test
193 public void addReservationTest() throws Exception {
194 DataHandler dh = new DataHandlerForTests();
195 dh.addReservation(testReservation);
196 }
197
198 @Test
199 public void findReservationTest() throws Exception {
200 DataHandler dh = new DataHandlerForTests();
201 UUID testId = testReservation.getId();
202 dh.addReservation(testReservation);
203
204 Reservation foundReservation = dh.findReservation(testId);
205 assertEquals(foundReservation, testReservation);
206 }
207
208 @Test
209 public void findReservationFalseIdTest() throws Exception {
210 DataHandler dh = new DataHandlerForTests();
211 dh.addReservation(testReservation);
212
213 assertEquals(null, dh.findReservation(UUID.randomUUID()));
214 }
215
216 @Test
217 public void editReservationTest() throws Exception {
218 DataHandler dh = new DataHandlerForTests();
219 dh.addReservation(testReservation);
220
221 assertEquals("TestReservation", dh.findReservation(testReservation.getId()).getRemark());
222 dh.editReservation(testReservation2);
223 assertEquals("TestReservation2", dh.findReservation(testReservation.getId()).getRemark());
224 }
225
226 @Test
227 public void editReservationFalseIdTest() throws Exception {
228 DataHandler dh = new DataHandlerForTests();
229 dh.addReservation(testReservation);
230
231 assertEquals(false, dh.editReservation(testReservation3));
232 }
233
234 @Test(expected = IllegalArgumentException.class)
235 public void findGroupReservationsNullTest() throws Exception {
236 DataHandler dh = new DataHandlerForTests();
237 dh.findGroupReservations(null);
238 }
239
240 @Test(expected = IllegalArgumentException.class)
241 public void userReservationsNullTest() throws Exception {
242 DataHandler dh = new DataHandlerForTests();
243 dh.userReservations(null);
244 }
245
246 @Test(expected = IllegalArgumentException.class)
247 public void overlapsNullTest() throws Exception {
248 DataHandler dh = new DataHandlerForTests();
249 dh.overlaps(null);
250 }
251
252 @Test(expected = IllegalArgumentException.class)
253 public void addReservationNullTest() throws Exception {
254 DataHandler dh = new DataHandlerForTests();
255 dh.addReservation(null);
256 }
257
258 @Test
259 public void addSameReservationTest() throws Exception {
260 DataHandler dh = new DataHandlerForTests();
261
262 dh.addReservation(testReservation);
263 assertEquals(false, dh.addReservation(testReservation));
264 assertEquals(false, dh.addReservation(testReservation2));
265
266 }
267
268 @Test
269 public void addReservationSameDateTest() throws Exception {
270 DataHandler dh = new DataHandlerForTests();
271
272 dh.addReservation(testReservation);
273 assertEquals(false, dh.addReservation(testReservation5));
274 }
275
276 @Test(expected = IllegalArgumentException.class)
277 public void editReservationNullTest() throws Exception {
278 DataHandler dh = new DataHandlerForTests();
279 dh.editReservation(null);
280 }
281
282 @Test(expected = IllegalArgumentException.class)
283 public void deleteReservationsNullTest() throws Exception {
284 DataHandler dh = new DataHandlerForTests();
285 dh.deleteReservation(null);
286 }
287
288 @Test
289 public void deleteReservationsFailTest() throws Exception {
290 DataHandler dh = new DataHandlerForTests();
291 dh.addReservation(testReservation);
292 assertEquals(true, dh.deleteReservation(testReservation));
293 assertEquals(false, dh.deleteReservation(testReservation2));
294 }
295
296 @Test(expected = Exception.class)
297 public void refreshTest() throws Exception {
298 DataHandler dh = new DataHandlerForTests();
299 dh.refresh(true);
300 }
301
302 @Test
303 public void findAllReservationsTest() throws Exception {
304 List<Reservation> allReservations = new ArrayList<Reservation>();
305 DataHandler dh = new DataHandlerForTests();
306 dh.addReservation(testReservation);
307 dh.addReservation(testReservation3);
308 dh.addReservation(testReservation4);
309 allReservations = dh.findAllReservations();
310
311 assertEquals(3, allReservations.size());
312 }
313
314 @Test
315 public void editUserTest() throws Exception {
316 DataHandlerForTests dh = new DataHandlerForTests();
317 dh.addUser(new User("TestUser1","pass1"));
318 assertTrue(dh.editUser(new User("TestUser1","pass1")));
319 }
320
321 @Test(expected = IllegalArgumentException.class)
322 public void editUserException1Test() throws Exception {
323 DataHandlerForTests dh = new DataHandlerForTests();
324
325 dh.editUser(null);
326 }
327
328 @Test
329 public void editUserException2Test() throws Exception {
330 DataHandlerForTests dh = new DataHandlerForTests();
331 dh.addUser(new User("TestUser1","pass1"));
332 assertFalse(dh.editUser(new User("TestUser2","pass1")));
333 }
334
335 @Test
336 public void deleteResourceTest() throws Exception {
337 DataHandlerForTests dh = new DataHandlerForTests();
338
339 Resource res = new Resource("Room1", "Testroom");
340 dh.addResource(res);
341 dh.deleteResource(res);
342 }
343
344 @Test(expected = IllegalArgumentException.class)
345 public void deleteResourceExceptionTest() throws Exception {
346 DataHandlerForTests dh = new DataHandlerForTests();
347
348 dh.deleteResource(null);
349 }
350
351 @Test
352 public void deleteResourceException1Test() throws Exception {
353 DataHandlerForTests dh = new DataHandlerForTests();
354
355 dh.deleteResource(new Resource("Room1", "Testroom"));
356 }
357
358 @Test
359 public void editResourceTest() throws Exception {
360 DataHandlerForTests dh = new DataHandlerForTests();
361
362 Resource res = new Resource("Room1", "Testroom");
363 dh.addResource(res);
364 dh.editResource(res);
365 }
366
367 @Test(expected = IllegalArgumentException.class)
368 public void editResourceExceptionTest() throws Exception {
369 DataHandlerForTests dh = new DataHandlerForTests();
370
371 dh.editResource(null);
372 }
373
374 @Test
375 public void editResourceException1Test() throws Exception {
376 DataHandlerForTests dh = new DataHandlerForTests();
377
378 dh.editResource(new Resource("Room1", "Testroom"));
379 }
380 }