Merge remote-tracking branch 'origin/development'
[staff/due1/sed-hs15-srs-purple.git] / src / test / java / ch / bfh / ti / srs / dataschema / ReservationTest.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.dataschema;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotEquals;
13 import static org.junit.Assert.assertTrue;
14
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.dataschema.Reservation;
24 import ch.bfh.ti.srs.dataschema.Resource;
25 import ch.bfh.ti.srs.dataschema.User;
26
27 public class ReservationTest {
28
29 private DateRangeFactory factory;
30
31 @Before
32 public void setUp() throws Exception {
33 String className = System.getProperty("daterangefactory.name",
34 "ch.bfh.ti.daterange.impl.pojo.DateRangeFactory");
35 Class<?> clazz = Class.forName(className);
36 factory = (DateRangeFactory) clazz.newInstance();
37 }
38
39 @Test
40 public void createReservationTest() throws Exception {
41 User testUser = new User("TestUser", "pass");
42 Resource testResource = new Resource("N321", "class room");
43 DateRange testDr = factory.createDateRange(DateFactory.createDate(2015, 11, 9),
44 DateFactory.createDate(2015, 11, 13));
45 Reservation testReservation = new Reservation(UUID.randomUUID(), testUser, testResource, testDr,
46 "TestReservation");
47
48 assertNotEquals(null, testReservation.getId());
49 assertNotEquals(null, testReservation.getGroupId());
50 assertEquals(testUser, testReservation.getUser());
51 assertEquals(testResource, testReservation.getResource());
52 assertEquals(testDr, testReservation.getDateRange());
53 assertEquals("TestReservation", testReservation.getRemark());
54 }
55
56 @Test(expected = IllegalArgumentException.class)
57 public void createReservationInvalidUserTest() throws Exception {
58 Resource testResource = new Resource("N321", "class room");
59 DateRange testDr = factory.createDateRange(DateFactory.createDate(2015, 11, 9),
60 DateFactory.createDate(2015, 11, 13));
61 Reservation testReservation = new Reservation(UUID.randomUUID(), null, testResource, testDr, "TestReservation");
62 }
63
64 @Test(expected = IllegalArgumentException.class)
65 public void createReservationInvalidResourceTest() throws Exception {
66 User testUser = new User("TestUser", "pass");
67 DateRange testDr = factory.createDateRange(DateFactory.createDate(2015, 11, 9),
68 DateFactory.createDate(2015, 11, 13));
69 Reservation testReservation = new Reservation(UUID.randomUUID(), testUser, null, testDr, "TestReservation");
70 }
71
72 @Test
73 public void setAndGetNameTest() throws Exception {
74 User testUser = new User("TestUser", "pass");
75 Resource testResource = new Resource("N321", "class room");
76 DateRange testDr = factory.createDateRange(DateFactory.createDate(2015, 11, 9),
77 DateFactory.createDate(2015, 11, 13));
78 Reservation testReservation = new Reservation(UUID.randomUUID(), testUser, testResource, testDr,
79 "TestReservation");
80
81 testReservation.setRemark("otherTestReservation");
82 assertEquals("otherTestReservation", testReservation.getRemark());
83 }
84
85 @Test
86 public void setAndGetUserTest() throws Exception {
87 User testUser = new User("TestUser", "pass");
88 Resource testResource = new Resource("N321", "class room");
89 DateRange testDr = factory.createDateRange(DateFactory.createDate(2015, 11, 9),
90 DateFactory.createDate(2015, 11, 13));
91 Reservation testReservation = new Reservation(UUID.randomUUID(), testUser, testResource, testDr,
92 "TestReservation");
93
94 User otherTestUser = new User("otherTestUser", "pass");
95 testReservation.setUser(otherTestUser);
96 assertEquals(otherTestUser, testReservation.getUser());
97 }
98
99 @Test
100 public void setAndGetResourceTest() throws Exception {
101 User testUser = new User("TestUser", "pass");
102 Resource testResource = new Resource("N321", "class room");
103 DateRange testDr = factory.createDateRange(DateFactory.createDate(2015, 11, 9),
104 DateFactory.createDate(2015, 11, 13));
105 Reservation testReservation = new Reservation(UUID.randomUUID(), testUser, testResource, testDr,
106 "TestReservation");
107
108 Resource otherTestResource = new Resource("N421", "lecture hall");
109 testReservation.setResource(otherTestResource);
110 assertEquals(otherTestResource, testReservation.getResource());
111 }
112
113 @Test
114 public void setAndGetDateRangeTest() throws Exception {
115 User testUser = new User("TestUser", "pass");
116 Resource testResource = new Resource("N321", "class room");
117 DateRange testDr = factory.createDateRange(DateFactory.createDate(2015, 11, 9),
118 DateFactory.createDate(2015, 11, 13));
119 Reservation testReservation = new Reservation(UUID.randomUUID(), testUser, testResource, testDr,
120 "TestReservation");
121
122 DateRange otherTestDr = factory.createDateRange(DateFactory.createDate(2015, 12, 9),
123 DateFactory.createDate(2015, 12, 13));
124 testReservation.setDateRange(otherTestDr);
125 assertEquals(otherTestDr, testReservation.getDateRange());
126 }
127
128 @Test
129 public void isModifiedTest() throws Exception {
130 User testUser = new User("TestUser", "pass");
131 Resource testResource = new Resource("N321", "class room");
132 DateRange testDr = factory.createDateRange(DateFactory.createDate(2015, 11, 9),
133 DateFactory.createDate(2015, 11, 13));
134 Reservation testReservation = new Reservation(UUID.randomUUID(), testUser, testResource, testDr,
135 "TestReservation");
136 Reservation clonedTestReservation = testReservation.clone();
137
138 assertTrue(testReservation.isModified(null));
139 assertFalse(testReservation.isModified(testReservation));
140 assertFalse(testReservation.isModified(clonedTestReservation));
141
142 User otherTestUser = new User("otherTestUser", "pass");
143 clonedTestReservation.setUser(otherTestUser);
144 assertTrue(testReservation.isModified(clonedTestReservation));
145
146 clonedTestReservation.setUser(testUser);
147 assertFalse(testReservation.isModified(clonedTestReservation));
148
149 Resource otherTestResource = new Resource("N421", "lecture hall");
150 clonedTestReservation.setResource(otherTestResource);
151 assertTrue(testReservation.isModified(clonedTestReservation));
152
153 clonedTestReservation.setResource(testResource);
154 assertFalse(testReservation.isModified(clonedTestReservation));
155
156 DateRange otherTestDr = factory.createDateRange(DateFactory.createDate(2015, 12, 9),
157 DateFactory.createDate(2015, 12, 13));
158 clonedTestReservation.setDateRange(otherTestDr);
159 assertTrue(testReservation.isModified(clonedTestReservation));
160
161 clonedTestReservation.setDateRange(testDr);
162 assertFalse(testReservation.isModified(clonedTestReservation));
163
164 clonedTestReservation.setRemark("some other remark");
165 assertTrue(testReservation.isModified(clonedTestReservation));
166 }
167 }