DataHandlerForTestsTest added,
authorMichael <Michael@Michael-LT.bfh.ch>
Wed, 28 Oct 2015 13:21:54 +0000 (14:21 +0100)
committerMichael <Michael@Michael-LT.bfh.ch>
Wed, 28 Oct 2015 13:21:54 +0000 (14:21 +0100)
DataHandlerForTests added,
User updated,
UserController updated,
DataHandlerForExcel moved to 'noTest'-package,
ch.bfh.ti.srs.noTest-package added for code not possible to Test yet,
DataHandler (Interface) java-doc added

src/main/java/ch/bfh/ti/srs/core/DataHandler.java
src/main/java/ch/bfh/ti/srs/core/DataHandlerForTests.java [new file with mode: 0644]
src/main/java/ch/bfh/ti/srs/core/User.java
src/main/java/ch/bfh/ti/srs/core/UserController.java
src/main/java/ch/bfh/ti/srs/noTest/DataHandlerForExcel.java [moved from src/main/java/ch/bfh/ti/srs/core/DataHandlerForExcel.java with 92% similarity]
src/test/java/ch/bfh/ti/srs/core/DataHandlerForTestsTest.java [new file with mode: 0644]
src/test/java/ch/bfh/ti/srs/core/UserControllerTest.java
src/test/java/ch/bfh/ti/srs/core/UserTest.java

index f91e363..fd7a077 100644 (file)
@@ -11,6 +11,21 @@ package ch.bfh.ti.srs.core;
  * @author Michael Grossen\r
  */\r
 public interface DataHandler {\r
+       \r
+       /**\r
+        * Finds the user by userName param in the Datacontext and returns the User object or null if not found\r
+        * \r
+        * @param userName      userName of the user you are looking for\r
+        * @return                      <code>User</code>user object\r
+        *                                      <code>null</code>if no user found with this userName\r
+        */\r
        public User findUser(String userName);\r
+       \r
+       /**\r
+        * Adds a user to the Datacontext from a given user object\r
+        * \r
+        * @param user                  User object\r
+        * @throws Exception\r
+        */\r
        public void addUser(User user) throws Exception;\r
 }\r
diff --git a/src/main/java/ch/bfh/ti/srs/core/DataHandlerForTests.java b/src/main/java/ch/bfh/ti/srs/core/DataHandlerForTests.java
new file mode 100644 (file)
index 0000000..75ae9cc
--- /dev/null
@@ -0,0 +1,39 @@
+/*\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.core;\r
+\r
+import java.util.HashSet;\r
+import java.util.Set;\r
+\r
+public class DataHandlerForTests implements DataHandler{\r
+       Set<User> userList = new HashSet<User>();\r
+       \r
+       public DataHandlerForTests(){\r
+               \r
+       }\r
+\r
+       /* (non-Javadoc)\r
+        * @see ch.bfh.ti.srs.core.DataHandler#findUser(java.lang.String)\r
+        */\r
+       @Override\r
+       public User findUser(String userName) {\r
+               for (User user : userList) {\r
+                       if(user.getUserName().equals(userName)) return user;\r
+               }\r
+               return null;\r
+       }\r
+\r
+       /* (non-Javadoc)\r
+        * @see ch.bfh.ti.srs.core.DataHandler#addUser(ch.bfh.ti.srs.core.User)\r
+        */\r
+       @Override\r
+       public void addUser(User user) throws Exception {\r
+               if(user == null) throw new IllegalArgumentException("User object must not be null!");\r
+               userList.add(user);\r
+       }\r
+}\r
index 24b7339..e964628 100644 (file)
@@ -18,14 +18,17 @@ public class User {
        private String lastname;
 
        /**
-        * Contructor of already existing user in the system
-        * @param userName
-        * @param password
-        * @param mail
-        * @param name
-        * @param lastname
+        * Constructor for already existing user in the Datacontext
+        * Only to be used by DataHandler implementation!
+        * 
+        * @param userName              Username
+        * @param password              Encrypted password
+        * @param mail                  Mail
+        * @param name                  Name
+        * @param lastname              Lastname
         */
-       User(String userName, String password, String mail, String name, String lastname){
+       //TODO: Change constructor to package when DataHandlerForExcel is moved back to this package
+       public User(String userName, String password, String mail, String name, String lastname){
                this.userName = userName;
                this.password = password;
                this.mail = mail;
@@ -33,11 +36,18 @@ public class User {
                this.lastname = lastname;
        }
 
+       /**
+        * Normal constructor for new users created in the UserController
+        * 
+        * @param userName
+        * @param password
+        * @throws Exception
+        */
        public User(String userName, String password) throws Exception{
                if(userName == null) throw new IllegalArgumentException("Username must not be null");
                if(userName.length() < 4) throw new IllegalArgumentException("Username must have at least 4 characters");
                if(password == null) throw new IllegalArgumentException("Password must not be null");
-               
+
                this.userName = userName;
                this.password = Password.getSaltedHash(password);
        }
index c26f704..a3121e6 100644 (file)
@@ -16,10 +16,19 @@ import java.util.concurrent.ExecutionException;
 public class UserController {\r
        final String filePath = "test.xls";\r
        User currentUser;\r
-       DataHandler dc;\r
+       DataHandler dh;\r
        \r
-       public UserController() throws Exception {\r
-               dc = new DataHandlerForExcel(filePath);\r
+       public UserController(){\r
+       }\r
+       \r
+       \r
+       /**\r
+        * Sets the DataHandler implementation for this Controller, must be set before using all other methods!\r
+        * \r
+        * @param dh    DataHandler implementation (i.e. DataHandlerForExcel)\r
+        */\r
+       public void setDataHandler(DataHandler dh){\r
+               this.dh = dh;\r
        }\r
        \r
        /**\r
@@ -32,8 +41,9 @@ public class UserController {
         * @throws                      Exception\r
         */\r
        public boolean logIn(String userName, String password) throws Exception{\r
+               if(dh == null) throw new Exception("No DataHandler set");\r
                if(currentUser != null) throw new AlreadyBoundException("A user is already logged in");\r
-               User tempUser = dc.findUser(userName);\r
+               User tempUser = dh.findUser(userName);\r
                if(tempUser == null) return false;\r
                if(tempUser.checkPassword(password)){\r
                        currentUser = tempUser;\r
@@ -48,15 +58,73 @@ public class UserController {
         * @throws Exception \r
         */\r
        public void logOut() throws Exception{\r
+               if(dh == null) throw new Exception("No DataHandler set");\r
                if(currentUser == null) throw new Exception("No user is logged in");\r
                currentUser = null;\r
        }\r
        \r
+       /**\r
+        * Registers a new user to the current DataHandler\r
+        * \r
+        * @param userName              Username\r
+        * @param password              Plaintext password\r
+        * @param email                 E-Mail address\r
+        * @param name                  Name\r
+        * @param lastname              Lastname\r
+        * @throws Exception\r
+        */\r
        public void addUser(String userName, String password, String email, String name, String lastname) throws Exception{\r
+               if(dh == null) throw new Exception("No DataHandler set");\r
                User newUser = new User(userName,password);\r
                newUser.setMail(email);\r
                newUser.setName(name);\r
                newUser.setLastname(lastname);\r
-               dc.addUser(newUser);\r
+               dh.addUser(newUser);\r
+       }\r
+\r
+\r
+       /**\r
+        * Gets the username of the current user\r
+        * \r
+        * @return      Username\r
+        * @throws      Exception\r
+        */\r
+       public String getLoggedUserName() throws Exception{\r
+               if(currentUser == null) throw new Exception("No user is logged in");\r
+               return currentUser.getUserName();\r
+       }\r
+\r
+\r
+       /**\r
+        * Gets the mail address of the current user\r
+        * \r
+        * @return      Mail address\r
+        * @throws      Exception\r
+        */\r
+       public String getLoggedMail() throws Exception{\r
+               if(currentUser == null) throw new Exception("No user is logged in");\r
+               return currentUser.getMail();\r
+       }\r
+\r
+       /**\r
+        * Gets the first name of the current user\r
+        * \r
+        * @return      First name\r
+        * @throws      Exception\r
+        */\r
+       public String getLoggedName() throws Exception {\r
+               if(currentUser == null) throw new Exception("No user is logged in");\r
+               return currentUser.getName();\r
+       }\r
+\r
+       /**\r
+        * Gets the last name of the current user\r
+        * \r
+        * @return      Last name\r
+        * @throws      Exception\r
+        */\r
+       public String getLoggedLastname() throws Exception {\r
+               if(currentUser == null) throw new Exception("No user is logged in");\r
+               return currentUser.getLastname();\r
        }\r
 }\r
@@ -1,8 +1,12 @@
-package ch.bfh.ti.srs.core;\r
+package ch.bfh.ti.srs.noTest;\r
 \r
 import java.sql.*;\r
+import java.util.ArrayList;\r
 import java.util.HashSet;\r
 import java.util.Set;\r
+\r
+import ch.bfh.ti.srs.core.DataHandler;\r
+import ch.bfh.ti.srs.core.User;\r
 /**\r
  * @author Michael Grossen\r
  */\r
diff --git a/src/test/java/ch/bfh/ti/srs/core/DataHandlerForTestsTest.java b/src/test/java/ch/bfh/ti/srs/core/DataHandlerForTestsTest.java
new file mode 100644 (file)
index 0000000..0f61cf1
--- /dev/null
@@ -0,0 +1,57 @@
+/*\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.core;\r
+\r
+import static org.junit.Assert.*;\r
+\r
+import org.junit.BeforeClass;\r
+import org.junit.Test;\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
index 3cbade2..9e17624 100644 (file)
@@ -18,8 +18,188 @@ import org.junit.Test;
 public class UserControllerTest {\r
 \r
        @Test\r
-       public void test() {\r
+       public void addUserTest() throws Exception{\r
+               DataHandler dh = new DataHandlerForTests();\r
+               UserController uc = new UserController();\r
+               \r
+               uc.setDataHandler(dh);\r
+               uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
+               uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
+               uc.addUser("TestUser3", "pass3", "grosm9@bfh.ch", "Test", null);\r
+               uc.addUser("TestUser4", "pass4", "grosm9@bfh.ch", "Test", "User");\r
+       }\r
+       \r
+       @Test(expected=Exception.class)\r
+       public void addUserExceptionTest() throws Exception{\r
+               DataHandler dh = new DataHandlerForTests();\r
+               UserController uc = new UserController();\r
+               \r
+               uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
+       }\r
+\r
+       @Test\r
+       public void logInTest() throws Exception{\r
+               DataHandler dh = new DataHandlerForTests();\r
+               UserController uc = new UserController();\r
+               \r
+               uc.setDataHandler(dh);\r
+               uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
+               uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
+               uc.addUser("TestUser3", "pass3", "grosm9@bfh.ch", "Test", null);\r
+               uc.addUser("TestUser4", "pass4", "grosm9@bfh.ch", "Test", "User");\r
+               \r
+               uc.logIn("TestUser3", "pass3");\r
+       }\r
+       \r
+       @Test(expected=Exception.class)\r
+       public void logInException1Test() throws Exception{\r
+               DataHandler dh = new DataHandlerForTests();\r
+               UserController uc = new UserController();\r
+               uc.logIn("TestUser3", "pass3");\r
+       }\r
 \r
+       @Test(expected=Exception.class)\r
+       public void logInException2Test() throws Exception{\r
+               DataHandler dh = new DataHandlerForTests();\r
+               UserController uc = new UserController();\r
+               \r
+               uc.setDataHandler(dh);\r
+               uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
+               uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
+               \r
+               uc.logIn("TestUser1", "pass1");\r
+               uc.logIn("TestUser2", "pass2");\r
        }\r
+       \r
+       @Test\r
+       public void logInInvalidCredetialsTest() throws Exception{\r
+               DataHandler dh = new DataHandlerForTests();\r
+               UserController uc = new UserController();\r
+               \r
+               uc.setDataHandler(dh);\r
+               uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
+               uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
+               \r
+               assertFalse(uc.logIn("TestUser3", "pass3"));\r
+               assertFalse(uc.logIn("TestUser1", "pass2"));\r
+               assertTrue(uc.logIn("TestUser1", "pass1"));\r
+       }\r
+       \r
+       @Test\r
+       public void getLoggedUserNameTest() throws Exception{\r
+               DataHandler dh = new DataHandlerForTests();\r
+               UserController uc = new UserController();\r
+               \r
+               uc.setDataHandler(dh);\r
+               uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
+               uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
+               uc.addUser("TestUser3", "pass3", "grosm9@bfh.ch", "Test", null);\r
+               uc.addUser("TestUser4", "pass4", "grosm9@bfh.ch", "Test", "User");\r
+               \r
+               uc.logIn("TestUser1", "pass1");\r
+               assertEquals("TestUser1", uc.getLoggedUserName());\r
+       }\r
+       \r
 \r
+       @Test(expected=Exception.class)\r
+       public void getLoggedUserNameExceptionTest() throws Exception{\r
+               UserController uc = new UserController();\r
+               String currentUserName = uc.getLoggedUserName();\r
+       }\r
+       \r
+       @Test\r
+       public void getLoggedUserMailTest() throws Exception{\r
+               DataHandler dh = new DataHandlerForTests();\r
+               UserController uc = new UserController();\r
+               \r
+               uc.setDataHandler(dh);\r
+               uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
+               uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
+               uc.addUser("TestUser3", "pass3", "grosm9@bfh.ch", "Test", null);\r
+               uc.addUser("TestUser4", "pass4", "grosm9@bfh.ch", "Test", "User");\r
+               \r
+               uc.logIn("TestUser2", "pass2");\r
+               assertEquals("grosm9@bfh.ch", uc.getLoggedMail());\r
+       }\r
+       \r
+       @Test(expected=Exception.class)\r
+       public void getLoggedMailExceptionTest() throws Exception{\r
+               UserController uc = new UserController();\r
+               String currentMail = uc.getLoggedMail();\r
+       }\r
+       \r
+       @Test\r
+       public void getLoggedNameTest() throws Exception{\r
+               DataHandler dh = new DataHandlerForTests();\r
+               UserController uc = new UserController();\r
+               \r
+               uc.setDataHandler(dh);\r
+               uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
+               uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
+               uc.addUser("TestUser3", "pass3", "grosm9@bfh.ch", "Test", null);\r
+               uc.addUser("TestUser4", "pass4", "grosm9@bfh.ch", "Test", "User");\r
+               \r
+               uc.logIn("TestUser3", "pass3");\r
+               assertEquals("Test", uc.getLoggedName());\r
+       }\r
+       \r
+       @Test(expected=Exception.class)\r
+       public void getLoggedNameExceptionTest() throws Exception{\r
+               UserController uc = new UserController();\r
+               String currentName = uc.getLoggedName();\r
+       }\r
+       \r
+       @Test\r
+       public void getLoggedLastnameTest() throws Exception{\r
+               DataHandler dh = new DataHandlerForTests();\r
+               UserController uc = new UserController();\r
+               \r
+               uc.setDataHandler(dh);\r
+               uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
+               uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
+               uc.addUser("TestUser3", "pass3", "grosm9@bfh.ch", "Test", null);\r
+               uc.addUser("TestUser4", "pass4", "grosm9@bfh.ch", "Test", "User");\r
+               \r
+               uc.logIn("TestUser4", "pass4");\r
+               assertEquals("User", uc.getLoggedLastname());\r
+       }\r
+       \r
+       @Test(expected=Exception.class)\r
+       public void getLoggedLastnameExceptionTest() throws Exception{\r
+               UserController uc = new UserController();\r
+               String currentLastName = uc.getLoggedLastname();\r
+       }\r
+       \r
+       @Test\r
+       public void logOutTest() throws Exception{\r
+               DataHandler dh = new DataHandlerForTests();\r
+               UserController uc = new UserController();\r
+               \r
+               uc.setDataHandler(dh);\r
+               uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
+               uc.addUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
+               uc.addUser("TestUser3", "pass3", "grosm9@bfh.ch", "Test", null);\r
+               uc.addUser("TestUser4", "pass4", "grosm9@bfh.ch", "Test", "User");\r
+               \r
+               uc.logIn("TestUser1", "pass1");\r
+               assertEquals("TestUser1", uc.getLoggedUserName());\r
+               uc.logOut();\r
+       }\r
+       \r
+       @Test(expected=Exception.class)\r
+       public void logOutException1Test() throws Exception{\r
+               DataHandler dh = new DataHandlerForTests();\r
+               UserController uc = new UserController();\r
+               \r
+               uc.logOut();\r
+       }\r
+       \r
+       @Test(expected=Exception.class)\r
+       public void logOutException2Test() throws Exception{\r
+               DataHandler dh = new DataHandlerForTests();\r
+               UserController uc = new UserController();\r
+               \r
+               uc.setDataHandler(dh);\r
+               uc.logOut();\r
+       }\r
 }\r
index 6c5f414..02b8cbb 100644 (file)
@@ -15,6 +15,8 @@ import org.junit.Rule;
 import org.junit.Test;\r
 import org.junit.rules.ExpectedException;\r
 \r
+import ch.bfh.ti.srs.extern.Password;\r
+\r
 /**\r
  * @author Michael Grossen\r
  */\r
@@ -37,6 +39,23 @@ public class UserTest {
                User wrongArgumentUser2 = new User("TestUser2", null);*/\r
        }\r
        \r
+       @Test\r
+       public void createExistingTest() throws Exception{\r
+               User testUser = new User("TestUser", "pass", "grosm9@bfh.ch", "Test", "Dummy");\r
+               assertEquals("TestUser", testUser.getUserName());\r
+               assertEquals("pass", testUser.getEncryptedPassword());\r
+               assertEquals("grosm9@bfh.ch", testUser.getMail());\r
+               assertEquals("Test", testUser.getName());\r
+               assertEquals("Dummy", testUser.getLastname());\r
+       }\r
+       \r
+       @Test\r
+       public void getEncryptedPasswordTest() throws Exception{\r
+               String encryptedPass = Password.getSaltedHash("testPassword");\r
+               User testUser = new User("TestUser", encryptedPass, "grosm9@bfh.ch", "Test", "Dummy");\r
+               assertEquals(encryptedPass, testUser.getEncryptedPassword());\r
+       }\r
+       \r
        @Test(expected=IllegalArgumentException.class)\r
        public void createUserInvalidUsernameTest() throws Exception{\r
                User testUser = new User(null,"pass");\r