Added isModified method to Resource, Reservation.
[staff/due1/sed-hs15-srs-purple.git] / src / test / java / ch / bfh / ti / srs / dataschema / ReservationTest.java
index a860f60..bfd3093 100644 (file)
@@ -8,6 +8,11 @@
 package ch.bfh.ti.srs.dataschema;\r
 \r
 import static org.junit.Assert.assertEquals;\r
+import static org.junit.Assert.assertFalse;\r
+import static org.junit.Assert.assertNotEquals;\r
+import static org.junit.Assert.assertTrue;\r
+\r
+import java.util.UUID;\r
 \r
 import org.junit.Before;\r
 import org.junit.Test;\r
@@ -22,7 +27,7 @@ import ch.bfh.ti.srs.dataschema.User;
 public class ReservationTest {\r
 \r
        private DateRangeFactory factory;\r
-       \r
+\r
        @Before\r
        public void setUp() throws Exception {\r
                String className = System.getProperty("daterangefactory.name",\r
@@ -30,64 +35,133 @@ public class ReservationTest {
                Class<?> clazz = Class.forName(className);\r
                factory = (DateRangeFactory) clazz.newInstance();\r
        }\r
-       \r
+\r
        @Test\r
        public void createReservationTest() throws Exception {\r
                User testUser = new User("TestUser", "pass");\r
                Resource testResource = new Resource("N321", "class room");\r
-               DateRange testDr = factory.createDateRange(DateFactory.createDate(2015, 11, 9), DateFactory.createDate(2015, 11, 13));\r
-               Reservation testReservation = new Reservation("TestReservation", testUser, testResource, testDr);\r
-               \r
-               assertEquals("TestReservation", testReservation.getName());\r
+               DateRange testDr = factory.createDateRange(DateFactory.createDate(2015, 11, 9),\r
+                               DateFactory.createDate(2015, 11, 13));\r
+               Reservation testReservation = new Reservation(UUID.randomUUID(), testUser, testResource, testDr,\r
+                               "TestReservation");\r
+\r
+               assertNotEquals(null, testReservation.getId());\r
+               assertNotEquals(null, testReservation.getGroupId());\r
                assertEquals(testUser, testReservation.getUser());\r
                assertEquals(testResource, testReservation.getResource());\r
                assertEquals(testDr, testReservation.getDateRange());\r
-       }       \r
-       \r
+               assertEquals("TestReservation", testReservation.getRemark());\r
+       }\r
+\r
+       @Test(expected = IllegalArgumentException.class)\r
+       public void createReservationInvalidUserTest() throws Exception {\r
+               Resource testResource = new Resource("N321", "class room");\r
+               DateRange testDr = factory.createDateRange(DateFactory.createDate(2015, 11, 9),\r
+                               DateFactory.createDate(2015, 11, 13));\r
+               Reservation testReservation = new Reservation(UUID.randomUUID(), null, testResource, testDr, "TestReservation");\r
+       }\r
+\r
+       @Test(expected = IllegalArgumentException.class)\r
+       public void createReservationInvalidResourceTest() throws Exception {\r
+               User testUser = new User("TestUser", "pass");\r
+               DateRange testDr = factory.createDateRange(DateFactory.createDate(2015, 11, 9),\r
+                               DateFactory.createDate(2015, 11, 13));\r
+               Reservation testReservation = new Reservation(UUID.randomUUID(), testUser, null, testDr, "TestReservation");\r
+       }\r
+\r
        @Test\r
        public void setAndGetNameTest() throws Exception {\r
                User testUser = new User("TestUser", "pass");\r
                Resource testResource = new Resource("N321", "class room");\r
-               DateRange testDr = factory.createDateRange(DateFactory.createDate(2015, 11, 9), DateFactory.createDate(2015, 11, 13));\r
-               Reservation testReservation = new Reservation("TestReservation", testUser, testResource, testDr);\r
-               \r
-               testReservation.setName("otherTestReservation");\r
-               assertEquals("otherTestReservation", testReservation.getName());\r
+               DateRange testDr = factory.createDateRange(DateFactory.createDate(2015, 11, 9),\r
+                               DateFactory.createDate(2015, 11, 13));\r
+               Reservation testReservation = new Reservation(UUID.randomUUID(), testUser, testResource, testDr,\r
+                               "TestReservation");\r
+\r
+               testReservation.setRemark("otherTestReservation");\r
+               assertEquals("otherTestReservation", testReservation.getRemark());\r
        }\r
-       \r
+\r
        @Test\r
        public void setAndGetUserTest() throws Exception {\r
                User testUser = new User("TestUser", "pass");\r
                Resource testResource = new Resource("N321", "class room");\r
-               DateRange testDr = factory.createDateRange(DateFactory.createDate(2015, 11, 9), DateFactory.createDate(2015, 11, 13));\r
-               Reservation testReservation = new Reservation("TestReservation", testUser, testResource, testDr);\r
-               \r
+               DateRange testDr = factory.createDateRange(DateFactory.createDate(2015, 11, 9),\r
+                               DateFactory.createDate(2015, 11, 13));\r
+               Reservation testReservation = new Reservation(UUID.randomUUID(), testUser, testResource, testDr,\r
+                               "TestReservation");\r
+\r
                User otherTestUser = new User("otherTestUser", "pass");\r
                testReservation.setUser(otherTestUser);\r
                assertEquals(otherTestUser, testReservation.getUser());\r
        }\r
-       \r
+\r
        @Test\r
        public void setAndGetResourceTest() throws Exception {\r
                User testUser = new User("TestUser", "pass");\r
                Resource testResource = new Resource("N321", "class room");\r
-               DateRange testDr = factory.createDateRange(DateFactory.createDate(2015, 11, 9), DateFactory.createDate(2015, 11, 13));\r
-               Reservation testReservation = new Reservation("TestReservation", testUser, testResource, testDr);\r
-               \r
-               Resource OtherTestResource = new Resource("N421", "lecture hall");\r
-               testReservation.setResource(OtherTestResource);\r
-               assertEquals(OtherTestResource, testReservation.getResource());\r
+               DateRange testDr = factory.createDateRange(DateFactory.createDate(2015, 11, 9),\r
+                               DateFactory.createDate(2015, 11, 13));\r
+               Reservation testReservation = new Reservation(UUID.randomUUID(), testUser, testResource, testDr,\r
+                               "TestReservation");\r
+\r
+               Resource otherTestResource = new Resource("N421", "lecture hall");\r
+               testReservation.setResource(otherTestResource);\r
+               assertEquals(otherTestResource, testReservation.getResource());\r
        }\r
-       \r
+\r
        @Test\r
        public void setAndGetDateRangeTest() throws Exception {\r
                User testUser = new User("TestUser", "pass");\r
                Resource testResource = new Resource("N321", "class room");\r
-               DateRange testDr = factory.createDateRange(DateFactory.createDate(2015, 11, 9), DateFactory.createDate(2015, 11, 13));\r
-               Reservation testReservation = new Reservation("TestReservation", testUser, testResource, testDr);\r
-               \r
-               DateRange otherTestDr = factory.createDateRange(DateFactory.createDate(2015, 12, 9), DateFactory.createDate(2015, 12, 13));\r
+               DateRange testDr = factory.createDateRange(DateFactory.createDate(2015, 11, 9),\r
+                               DateFactory.createDate(2015, 11, 13));\r
+               Reservation testReservation = new Reservation(UUID.randomUUID(), testUser, testResource, testDr,\r
+                               "TestReservation");\r
+\r
+               DateRange otherTestDr = factory.createDateRange(DateFactory.createDate(2015, 12, 9),\r
+                               DateFactory.createDate(2015, 12, 13));\r
                testReservation.setDateRange(otherTestDr);\r
                assertEquals(otherTestDr, testReservation.getDateRange());\r
        }\r
+\r
+       @Test\r
+       public void isModifiedTest() throws Exception {\r
+               User testUser = new User("TestUser", "pass");\r
+               Resource testResource = new Resource("N321", "class room");\r
+               DateRange testDr = factory.createDateRange(DateFactory.createDate(2015, 11, 9),\r
+                               DateFactory.createDate(2015, 11, 13));\r
+               Reservation testReservation = new Reservation(UUID.randomUUID(), testUser, testResource, testDr,\r
+                               "TestReservation");\r
+               Reservation clonedTestReservation = testReservation.clone();\r
+               \r
+               assertTrue(testReservation.isModified(null));\r
+               assertFalse(testReservation.isModified(testReservation));\r
+               assertFalse(testReservation.isModified(clonedTestReservation));\r
+               \r
+               User otherTestUser = new User("otherTestUser", "pass");\r
+               clonedTestReservation.setUser(otherTestUser);\r
+               assertTrue(testReservation.isModified(clonedTestReservation));\r
+               \r
+               clonedTestReservation.setUser(testUser);\r
+               assertFalse(testReservation.isModified(clonedTestReservation));\r
+\r
+               Resource otherTestResource = new Resource("N421", "lecture hall");\r
+               clonedTestReservation.setResource(otherTestResource);\r
+               assertTrue(testReservation.isModified(clonedTestReservation));\r
+               \r
+               clonedTestReservation.setResource(testResource);\r
+               assertFalse(testReservation.isModified(clonedTestReservation));\r
+               \r
+               DateRange otherTestDr = factory.createDateRange(DateFactory.createDate(2015, 12, 9),\r
+                               DateFactory.createDate(2015, 12, 13));\r
+               clonedTestReservation.setDateRange(otherTestDr);\r
+               assertTrue(testReservation.isModified(clonedTestReservation));\r
+               \r
+               clonedTestReservation.setDateRange(testDr);\r
+               assertFalse(testReservation.isModified(clonedTestReservation));\r
+               \r
+               clonedTestReservation.setRemark("some other remark");\r
+               assertTrue(testReservation.isModified(clonedTestReservation));\r
+       }\r
 }\r