DataHandlerForTestTest update
authorMatthias <balmm1@bfh.ch>
Sat, 28 Nov 2015 09:04:56 +0000 (10:04 +0100)
committerMatthias <balmm1@bfh.ch>
Sat, 28 Nov 2015 09:04:56 +0000 (10:04 +0100)
src/test/java/ch/bfh/ti/srs/datahandler/DataHandlerForTestsTest.java

index cc27131..94ff551 100644 (file)
-/*\r
- * Copyright (c) 2015 Berner Fachhochschule, Switzerland.\r
- *\r
- * Project Smart Reservation System.\r
- *\r
- * Distributable under GPL license. See terms of license at gnu.org.\r
- */\r
-package ch.bfh.ti.srs.datahandler;\r
-\r
-import static org.junit.Assert.assertEquals;\r
-import static org.junit.Assert.fail;\r
-\r
-import org.junit.Test;\r
-\r
-import ch.bfh.ti.srs.datahandler.DataHandler;\r
-import ch.bfh.ti.srs.datahandler.DataHandlerForTests;\r
-import ch.bfh.ti.srs.dataschema.User;\r
-\r
-/**\r
- * @author Michael Grossen\r
- */\r
-public class DataHandlerForTestsTest {\r
-\r
-       @Test\r
-       public void addUserTest() throws Exception{\r
-               DataHandler dh = new DataHandlerForTests();\r
-               User testUser = null;\r
-               try {\r
-                       testUser = new User("TestUser", "pass");\r
-               } catch (Exception e) {\r
-                       fail("Check UserTest (Exception was thrown by User Cunstructor)!");\r
-               }\r
-               dh.addUser(testUser);\r
-       }\r
-       \r
-       @Test(expected=IllegalArgumentException.class)\r
-       public void addIllegalUserTest() throws Exception{\r
-               DataHandler dh = new DataHandlerForTests();\r
-               User testUser = null;\r
-               dh.addUser(testUser);\r
-       }\r
-       \r
-       @Test\r
-       public void findUserTest() throws Exception{\r
-               DataHandler dh = new DataHandlerForTests();\r
-               User addedUser = new User("TestUser", "pass");\r
-               dh.addUser(addedUser);\r
-               User foundUser = dh.findUser("TestUser");\r
-               assertEquals(addedUser.getUserName(), foundUser.getUserName());\r
-       }\r
-       \r
-       @Test\r
-       public void findNotAddedUser() throws Exception{\r
-               DataHandler dh = new DataHandlerForTests();\r
-               assertEquals(null, dh.findUser("notAddedUser"));\r
-               User addedUser = new User("TestUser", "pass");\r
-               dh.addUser(addedUser);\r
-               assertEquals(null, dh.findUser("otherUser"));\r
-       }\r
-\r
-}\r
+/*
+ * Copyright (c) 2015 Berner Fachhochschule, Switzerland.
+ *
+ * Project Smart Reservation System.
+ *
+ * Distributable under GPL license. See terms of license at gnu.org.
+ */
+package ch.bfh.ti.srs.datahandler;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import ch.bfh.ti.daterange.DateFactory;
+import ch.bfh.ti.daterange.DateRange;
+import ch.bfh.ti.daterange.DateRangeFactory;
+import ch.bfh.ti.srs.datahandler.DataHandler;
+import ch.bfh.ti.srs.datahandler.DataHandlerForTests;
+import ch.bfh.ti.srs.dataschema.Reservation;
+import ch.bfh.ti.srs.dataschema.Resource;
+import ch.bfh.ti.srs.dataschema.User;
+
+/**
+ * @author Michael Grossen
+ */
+public class DataHandlerForTestsTest{
+
+       private DateRangeFactory factory;
+       private Reservation testReservation;
+       private Reservation testReservation2; //same ID as testReservation
+       private Reservation testReservation3;
+       private Reservation testReservation4;
+       private Reservation testReservation5;
+
+       @Before
+       public void setUp() throws Exception {
+               String className = System.getProperty("daterangefactory.name",
+                               "ch.bfh.ti.daterange.impl.pojo.DateRangeFactory");
+               Class<?> clazz = Class.forName(className);
+               factory = (DateRangeFactory) clazz.newInstance();
+
+               User testUser = new User("TestUser", "pass");
+               Resource testResource = new Resource("TestResource", "TestType");
+               DateRange testDr = factory.createDateRange(DateFactory.createDate(2015, 11, 9), DateFactory.createDate(2015, 11, 13));
+               UUID groupid = UUID.randomUUID();
+               testReservation = new Reservation(groupid, testUser, testResource, testDr, "TestReservation");
+
+               testReservation2 = testReservation.clone();
+               testReservation2.setRemark("TestReservation2");
+
+               User testUser3 = new User("TestUser3", "pass3");
+               Resource testResource3 = new Resource("TestResource3", "TestType3");
+               DateRange testDr3 = factory.createDateRange(DateFactory.createDate(2016, 11, 9), DateFactory.createDate(2016, 11, 13));
+               testReservation3 = new Reservation(groupid, testUser3, testResource3, testDr3, "TestReservation3");
+
+               User testUser4 = new User("TestUser4", "pass4");
+               Resource testResource4 = new Resource("TestResource4", "TestType4");
+               DateRange testDr4 = factory.createDateRange(DateFactory.createDate(2017, 11, 9), DateFactory.createDate(2017, 11, 13));
+               UUID groupid4 = UUID.randomUUID();
+               testReservation4 = new Reservation(groupid4, testUser4, testResource4, testDr4, "TestReservation4");
+
+               
+               DateRange testDr5 = factory.createDateRange(DateFactory.createDate(2015, 11, 10), DateFactory.createDate(2015, 11, 14));
+               testReservation5 = new Reservation(groupid, testUser, testResource, testDr5, "TestReservation5");
+               
+       }
+
+       @Test
+       public void addUserTest() throws Exception{
+               DataHandler dh = new DataHandlerForTests();
+               User testUser = null;
+               try {
+                       testUser = new User("TestUser", "pass");
+               } catch (Exception e) {
+                       fail("Check UserTest (Exception was thrown by User Cunstructor)!");
+               }
+               dh.addUser(testUser);
+       }
+
+       @Test(expected=IllegalArgumentException.class)
+       public void addIllegalUserTest() throws Exception{
+               DataHandler dh = new DataHandlerForTests();
+               User testUser = null;
+               dh.addUser(testUser);
+       }
+
+       @Test
+       public void addSameUserTest() throws Exception{
+               DataHandler dh = new DataHandlerForTests();
+               User testUser = null;
+               try {
+                       testUser = new User("TestUser", "pass");
+               } catch (Exception e) {
+                       fail("Check UserTest (Exception was thrown by User Cunstructor)!");
+               }
+               dh.addUser(testUser);
+               assertEquals(false, dh.addUser(testUser));
+       }
+
+       @Test
+       public void findUserTest() throws Exception{
+               DataHandler dh = new DataHandlerForTests();
+               User addedUser = new User("TestUser", "pass");
+               dh.addUser(addedUser);
+               User foundUser = dh.findUser("TestUser");
+               assertEquals(addedUser.getUserName(), foundUser.getUserName());
+       }
+
+       @Test
+       public void findNotAddedUser() throws Exception{
+               DataHandler dh = new DataHandlerForTests();
+               assertEquals(null, dh.findUser("notAddedUser"));
+               User addedUser = new User("TestUser", "pass");
+               dh.addUser(addedUser);
+               assertEquals(null, dh.findUser("otherUser"));
+       }
+
+       @Test
+       public void addResourceTest() throws Exception{
+               DataHandler dh = new DataHandlerForTests();
+               Resource testResource = null;
+               try {
+                       testResource = new Resource("TestResource", "TestType");
+               } catch (Exception e) {
+                       fail("Check ResourceTest (Exception was thrown by Resource Cunstructor)!");
+               }
+               dh.addResource(testResource);
+       }
+
+       @Test
+       public void addSameResourceTest() throws Exception{
+               DataHandler dh = new DataHandlerForTests();
+               Resource testResource = null;
+               try {
+                       testResource = new Resource("TestResource", "TestType");;
+               } catch (Exception e) {
+                       fail("Check UserTest (Exception was thrown by Resource Cunstructor)!");
+               }
+               dh.addResource(testResource);
+               assertEquals(false, dh.addResource(testResource));
+       }
+
+       @Test(expected=IllegalArgumentException.class)
+       public void addIllegalResourceTest() throws Exception{
+               DataHandler dh = new DataHandlerForTests();
+               Resource testResource = null;
+               dh.addResource(testResource);
+       }
+
+       @Test
+       public void findResourceTest() throws Exception{
+               DataHandler dh = new DataHandlerForTests();
+               Resource addedResource = new Resource("TestResource", "TestType");
+               dh.addResource(addedResource);
+               Resource foundResource = dh.findResource("TestResource");
+               assertEquals(addedResource.getName(), foundResource.getName());
+       }
+       @Test
+       public void findNoResourceTest() throws Exception{
+               DataHandler dh = new DataHandlerForTests();
+               Resource addedResource = new Resource("TestResource", "TestType");
+               Resource addedResource2 = new Resource("TestResource2", "TestType2");
+               dh.addResource(addedResource);
+               dh.addResource(addedResource2);
+               assertEquals(null, dh.findResource("TestResource3"));
+       }
+
+       @Test
+       public void addResourceSameIdTest() throws Exception{
+               DataHandler dh = new DataHandlerForTests();
+               Resource addedResource = new Resource("TestResource", "TestType");
+               Resource addedResource2 = addedResource.clone();
+               addedResource2.setName("TestResource2");
+               addedResource2.setType("TestType");
+               dh.addResource(addedResource);
+               assertEquals(false, dh.addResource(addedResource2));
+       }
+
+       @Test
+       public void addReservationTest() throws Exception{
+               DataHandler dh = new DataHandlerForTests();
+               dh.addReservation(testReservation);
+       }
+
+       @Test
+       public void findReservationTest() throws Exception{
+               DataHandler dh = new DataHandlerForTests();
+               UUID testId = testReservation.getId();
+               dh.addReservation(testReservation);
+
+               Reservation foundReservation = dh.findReservation(testId);
+               assertEquals(foundReservation, testReservation);
+       }
+
+       @Test
+       public void findReservationFalseIdTest() throws Exception{
+               DataHandler dh = new DataHandlerForTests();
+               dh.addReservation(testReservation);
+
+               assertEquals(null, dh.findReservation(UUID.randomUUID()));
+       }
+
+       @Test
+       public void editReservationTest() throws Exception{
+               DataHandler dh = new DataHandlerForTests();
+               dh.addReservation(testReservation);
+
+               assertEquals("TestReservation", dh.findReservation(testReservation.getId()).getRemark());
+               dh.editReservation(testReservation2);
+               assertEquals("TestReservation2", dh.findReservation(testReservation.getId()).getRemark());
+       }
+
+       @Test
+       public void editReservationFalseIdTest() throws Exception{
+               DataHandler dh = new DataHandlerForTests();
+               dh.addReservation(testReservation);
+
+               assertEquals(false, dh.editReservation(testReservation3));
+       }
+
+       @Test(expected=IllegalArgumentException.class)
+       public void findGroupReservationsNullTest() throws Exception{
+               DataHandler dh = new DataHandlerForTests();
+               dh.findGroupReservations(null);
+       }
+
+       @Test(expected=IllegalArgumentException.class)
+       public void userReservationsNullTest() throws Exception{
+               DataHandler dh = new DataHandlerForTests();
+               dh.userReservations(null);
+       }
+
+       @Test(expected=IllegalArgumentException.class)
+       public void overlapsNullTest() throws Exception{
+               DataHandler dh = new DataHandlerForTests();
+               dh.overlaps(null);
+       }
+
+       @Test(expected=IllegalArgumentException.class)
+       public void addReservationNullTest() throws Exception{
+               DataHandler dh = new DataHandlerForTests();
+               dh.addReservation(null);
+       }
+
+       @Test
+       public void addSameReservationTest() throws Exception{
+               DataHandler dh = new DataHandlerForTests();
+
+               dh.addReservation(testReservation);
+               assertEquals(false, dh.addReservation(testReservation));
+               assertEquals(false, dh.addReservation(testReservation2));
+
+       }
+       
+       @Test
+       public void addReservationSameDateTest() throws Exception{
+               DataHandler dh = new DataHandlerForTests();
+
+               dh.addReservation(testReservation);
+               assertEquals(false, dh.addReservation(testReservation5));
+       }
+
+       @Test(expected=IllegalArgumentException.class)
+       public void editReservationNullTest() throws Exception{
+               DataHandler dh = new DataHandlerForTests();
+               dh.editReservation(null);
+       }
+
+       @Test(expected=IllegalArgumentException.class)
+       public void deleteReservationsNullTest() throws Exception{
+               DataHandler dh = new DataHandlerForTests();
+               dh.deleteReservation(null);
+       }
+
+       @Test
+       public void deleteReservationsFailTest() throws Exception{
+               DataHandler dh = new DataHandlerForTests();
+               dh.addReservation(testReservation);
+               assertEquals(true, dh.deleteReservation(testReservation));
+               assertEquals(false, dh.deleteReservation(testReservation2));
+       }
+
+       @Test(expected=Exception.class)
+       public void refreshTest() throws Exception{
+               DataHandler dh = new DataHandlerForTests();
+               dh.refresh(true);
+       }
+
+       @Test
+       public void findAllReservationsTest() throws Exception{
+               List<Reservation> allReservations = new ArrayList<Reservation>();
+               DataHandler dh = new DataHandlerForTests();
+               dh.addReservation(testReservation);
+               dh.addReservation(testReservation3);
+               dh.addReservation(testReservation4);
+               allReservations = dh.findAllReservations();
+                       
+               assertEquals(3, allReservations.size());
+       }
+}