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