Added isModified method to Resource, Reservation.
authorMoritz Trittibach <tritm3@bfh.ch>
Sun, 29 Nov 2015 22:00:06 +0000 (23:00 +0100)
committerMoritz Trittibach <tritm3@bfh.ch>
Sun, 29 Nov 2015 22:00:06 +0000 (23:00 +0100)
src/main/java/ch/bfh/ti/srs/dataschema/Reservation.java
src/main/java/ch/bfh/ti/srs/dataschema/Resource.java
src/test/java/ch/bfh/ti/srs/dataschema/ReservationTest.java
src/test/java/ch/bfh/ti/srs/dataschema/ResourceTest.java

index fd7da2b..1588900 100644 (file)
@@ -21,14 +21,10 @@ public class Reservation implements Cloneable {
        private String remark;\r
 \r
        public Reservation(UUID groupId, User user, Resource resource, DateRange DateRange, String remark) {\r
-               // // if (groupId == null)\r
-               // // throw new IllegalArgumentException("GroupID must not be null");\r
-               // if (user.getUserName() == null)\r
-               // throw new IllegalArgumentException("User must not be null");\r
-               // if (resource.getId() == null)\r
-               // throw new IllegalArgumentException("Resource must not be null");\r
-               // if (dateRange.isEmpty())\r
-               // throw new IllegalArgumentException("DateRange must not be null");\r
+               if (user == null)\r
+                       throw new IllegalArgumentException("User must not be null");\r
+               if (resource == null)\r
+                       throw new IllegalArgumentException("Resource must not be null");\r
 \r
                this.groupId = groupId;\r
                this.user = user;\r
@@ -79,6 +75,18 @@ public class Reservation implements Cloneable {
                this.remark = remark;\r
        }\r
 \r
+       public boolean isModified(Reservation otherReservation) {\r
+               if (otherReservation == this) {\r
+                       return false;\r
+               }\r
+               if (otherReservation == null) {\r
+                       return true;\r
+               }\r
+               return this.user != otherReservation.getUser() || this.resource != otherReservation.getResource()\r
+                               || this.dateRange.compareTo(otherReservation.getDateRange()) != 0\r
+                               || this.remark != otherReservation.getRemark();\r
+       }\r
+\r
        @Override\r
        public Reservation clone() throws CloneNotSupportedException {\r
                return (Reservation) super.clone();\r
index e5b1ebe..ab601e6 100644 (file)
@@ -55,6 +55,16 @@ public class Resource implements Cloneable {
                this.type = type;\r
        }\r
 \r
+       public boolean isModified(Resource otherResource) {\r
+               if (otherResource == this) {\r
+                       return false;\r
+               }\r
+               if (otherResource == null) {\r
+                       return true;\r
+               }\r
+               return this.name != otherResource.getName() || this.type != otherResource.getType();\r
+       }\r
+\r
        @Override\r
        public Resource clone() throws CloneNotSupportedException {\r
                return (Resource) super.clone();\r
index c4c7a39..bfd3093 100644 (file)
@@ -8,7 +8,9 @@
 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
@@ -51,6 +53,22 @@ public class ReservationTest {
                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
@@ -87,9 +105,9 @@ public class ReservationTest {
                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
+               Resource otherTestResource = new Resource("N421", "lecture hall");\r
+               testReservation.setResource(otherTestResource);\r
+               assertEquals(otherTestResource, testReservation.getResource());\r
        }\r
 \r
        @Test\r
@@ -106,4 +124,44 @@ public class ReservationTest {
                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
index 6e5b1f6..6114dbe 100644 (file)
@@ -8,7 +8,9 @@
 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 org.junit.Test;\r
 \r
@@ -46,4 +48,21 @@ public class ResourceTest {
                testRessource.setType(null);\r
                assertEquals(null, testRessource.getType());\r
        }\r
+       \r
+       @Test\r
+       public void isModifiedTest() throws Exception {\r
+               Resource testRessource = new Resource("N321", "class room");\r
+               Resource clonedTestRessource = testRessource.clone();\r
+               \r
+               assertFalse(testRessource.isModified(clonedTestRessource));\r
+               assertTrue(testRessource.isModified(null));\r
+               assertFalse(testRessource.isModified(testRessource));\r
+               clonedTestRessource.setName("N421");\r
+               assertTrue(testRessource.isModified(clonedTestRessource));\r
+               clonedTestRessource.setName("N321");\r
+               assertFalse(testRessource.isModified(clonedTestRessource));\r
+               clonedTestRessource.setType("lecture hall");\r
+               assertTrue(testRessource.isModified(clonedTestRessource));\r
+       }\r
 }\r
+\r