removed all checkstyle errors, updated sprint backlog
[staff/due1/sed-hs15-srs-purple.git] / src / test / java / ch / bfh / ti / srs / datahandler / DataHandlerForTestsTest.java
CommitLineData
5db85ce1
M
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 */
8package ch.bfh.ti.srs.datahandler;
9
10import static org.junit.Assert.assertEquals;
11import static org.junit.Assert.fail;
12
13import java.util.ArrayList;
14import java.util.List;
15import java.util.UUID;
16
17import org.junit.Before;
18import org.junit.Test;
19
20import ch.bfh.ti.daterange.DateFactory;
21import ch.bfh.ti.daterange.DateRange;
22import ch.bfh.ti.daterange.DateRangeFactory;
23import ch.bfh.ti.srs.datahandler.DataHandler;
24import ch.bfh.ti.srs.datahandler.DataHandlerForTests;
25import ch.bfh.ti.srs.dataschema.Reservation;
26import ch.bfh.ti.srs.dataschema.Resource;
27import ch.bfh.ti.srs.dataschema.User;
28
29/**
30 * @author Michael Grossen
31 */
66cc653d 32public class DataHandlerForTestsTest {
5db85ce1
M
33
34 private DateRangeFactory factory;
35 private Reservation testReservation;
66cc653d 36 private Reservation testReservation2; // same ID as testReservation
5db85ce1
M
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");
66cc653d
D
50 DateRange testDr = factory.createDateRange(DateFactory.createDate(2015, 11, 9),
51 DateFactory.createDate(2015, 11, 13));
5db85ce1
M
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");
66cc653d
D
60 DateRange testDr3 = factory.createDateRange(DateFactory.createDate(2016, 11, 9),
61 DateFactory.createDate(2016, 11, 13));
5db85ce1
M
62 testReservation3 = new Reservation(groupid, testUser3, testResource3, testDr3, "TestReservation3");
63
64 User testUser4 = new User("TestUser4", "pass4");
65 Resource testResource4 = new Resource("TestResource4", "TestType4");
66cc653d
D
66 DateRange testDr4 = factory.createDateRange(DateFactory.createDate(2017, 11, 9),
67 DateFactory.createDate(2017, 11, 13));
5db85ce1
M
68 UUID groupid4 = UUID.randomUUID();
69 testReservation4 = new Reservation(groupid4, testUser4, testResource4, testDr4, "TestReservation4");
70
66cc653d
D
71 DateRange testDr5 = factory.createDateRange(DateFactory.createDate(2015, 11, 10),
72 DateFactory.createDate(2015, 11, 14));
5db85ce1 73 testReservation5 = new Reservation(groupid, testUser, testResource, testDr5, "TestReservation5");
66cc653d 74
5db85ce1
M
75 }
76
77 @Test
66cc653d 78 public void addUserTest() throws Exception {
5db85ce1
M
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
66cc653d
D
89 @Test(expected = IllegalArgumentException.class)
90 public void addIllegalUserTest() throws Exception {
5db85ce1
M
91 DataHandler dh = new DataHandlerForTests();
92 User testUser = null;
93 dh.addUser(testUser);
94 }
95
96 @Test
66cc653d 97 public void addSameUserTest() throws Exception {
5db85ce1
M
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
66cc653d 110 public void findUserTest() throws Exception {
5db85ce1
M
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
66cc653d 119 public void findNotAddedUser() throws Exception {
5db85ce1
M
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
66cc653d 128 public void addResourceTest() throws Exception {
5db85ce1
M
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
66cc653d 140 public void addSameResourceTest() throws Exception {
5db85ce1
M
141 DataHandler dh = new DataHandlerForTests();
142 Resource testResource = null;
143 try {
66cc653d
D
144 testResource = new Resource("TestResource", "TestType");
145 ;
5db85ce1
M
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
66cc653d
D
153 @Test(expected = IllegalArgumentException.class)
154 public void addIllegalResourceTest() throws Exception {
5db85ce1
M
155 DataHandler dh = new DataHandlerForTests();
156 Resource testResource = null;
157 dh.addResource(testResource);
158 }
159
160 @Test
66cc653d 161 public void findResourceTest() throws Exception {
5db85ce1
M
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 }
66cc653d 168
5db85ce1 169 @Test
66cc653d 170 public void findNoResourceTest() throws Exception {
5db85ce1
M
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
66cc653d 180 public void addResourceSameIdTest() throws Exception {
5db85ce1
M
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
66cc653d 191 public void addReservationTest() throws Exception {
5db85ce1
M
192 DataHandler dh = new DataHandlerForTests();
193 dh.addReservation(testReservation);
194 }
195
196 @Test
66cc653d 197 public void findReservationTest() throws Exception {
5db85ce1
M
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
66cc653d 207 public void findReservationFalseIdTest() throws Exception {
5db85ce1
M
208 DataHandler dh = new DataHandlerForTests();
209 dh.addReservation(testReservation);
210
211 assertEquals(null, dh.findReservation(UUID.randomUUID()));
212 }
213
214 @Test
66cc653d 215 public void editReservationTest() throws Exception {
5db85ce1
M
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
66cc653d 225 public void editReservationFalseIdTest() throws Exception {
5db85ce1
M
226 DataHandler dh = new DataHandlerForTests();
227 dh.addReservation(testReservation);
228
229 assertEquals(false, dh.editReservation(testReservation3));
230 }
231
66cc653d
D
232 @Test(expected = IllegalArgumentException.class)
233 public void findGroupReservationsNullTest() throws Exception {
5db85ce1
M
234 DataHandler dh = new DataHandlerForTests();
235 dh.findGroupReservations(null);
236 }
237
66cc653d
D
238 @Test(expected = IllegalArgumentException.class)
239 public void userReservationsNullTest() throws Exception {
5db85ce1
M
240 DataHandler dh = new DataHandlerForTests();
241 dh.userReservations(null);
242 }
243
66cc653d
D
244 @Test(expected = IllegalArgumentException.class)
245 public void overlapsNullTest() throws Exception {
5db85ce1
M
246 DataHandler dh = new DataHandlerForTests();
247 dh.overlaps(null);
248 }
249
66cc653d
D
250 @Test(expected = IllegalArgumentException.class)
251 public void addReservationNullTest() throws Exception {
5db85ce1
M
252 DataHandler dh = new DataHandlerForTests();
253 dh.addReservation(null);
254 }
255
256 @Test
66cc653d 257 public void addSameReservationTest() throws Exception {
5db85ce1
M
258 DataHandler dh = new DataHandlerForTests();
259
260 dh.addReservation(testReservation);
261 assertEquals(false, dh.addReservation(testReservation));
262 assertEquals(false, dh.addReservation(testReservation2));
263
264 }
66cc653d 265
5db85ce1 266 @Test
66cc653d 267 public void addReservationSameDateTest() throws Exception {
5db85ce1
M
268 DataHandler dh = new DataHandlerForTests();
269
270 dh.addReservation(testReservation);
271 assertEquals(false, dh.addReservation(testReservation5));
272 }
273
66cc653d
D
274 @Test(expected = IllegalArgumentException.class)
275 public void editReservationNullTest() throws Exception {
5db85ce1
M
276 DataHandler dh = new DataHandlerForTests();
277 dh.editReservation(null);
278 }
279
66cc653d
D
280 @Test(expected = IllegalArgumentException.class)
281 public void deleteReservationsNullTest() throws Exception {
5db85ce1
M
282 DataHandler dh = new DataHandlerForTests();
283 dh.deleteReservation(null);
284 }
285
286 @Test
66cc653d 287 public void deleteReservationsFailTest() throws Exception {
5db85ce1
M
288 DataHandler dh = new DataHandlerForTests();
289 dh.addReservation(testReservation);
290 assertEquals(true, dh.deleteReservation(testReservation));
291 assertEquals(false, dh.deleteReservation(testReservation2));
292 }
293
66cc653d
D
294 @Test(expected = Exception.class)
295 public void refreshTest() throws Exception {
5db85ce1
M
296 DataHandler dh = new DataHandlerForTests();
297 dh.refresh(true);
298 }
299
300 @Test
66cc653d 301 public void findAllReservationsTest() throws Exception {
5db85ce1
M
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();
66cc653d 308
5db85ce1
M
309 assertEquals(3, allReservations.size());
310 }
311}