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