From be3ac9a3826b79c09406d3d9588ac8a96eb0881e Mon Sep 17 00:00:00 2001
From: Julian Dehne <julian.dehne@uni-potsdam.de>
Date: Mon, 5 Nov 2018 11:26:37 +0100
Subject: [PATCH] UserView refaktorisiert

---
 .../gf/exceptions/MysqlDownException.java     |  4 +
 .../exceptions/RocketChatDownException.java   |  4 +
 .../UserDoesNotExistInMysqlException.java     |  4 +
 ...UserDoesNotExistInRocketChatException.java |  4 +
 .../UserExistsInMysqlException.java           |  9 ++
 .../UserExistsInRocketChatException.java      |  8 ++
 .../gf/interfaces/ICommunication.java         | 33 ++++---
 .../service/CommunicationService.java         | 60 ++++++++----
 .../communication/view/CommunicationView.java | 17 +++-
 .../gf/modules/project/ProjectView.java       | 20 ++--
 .../unipotsdam/gf/modules/user/UserView.java  | 92 +++++++++----------
 .../gf/process/ProjectCreationProcess.java    | 36 +++++++-
 .../unipotsdam/gf/session/GFContexts.java     |  9 ++
 .../unipotsdam/gf/taglibs/ChatWindow.java     | 13 ++-
 .../unipotsdam/gf/core/database/UpdateDB.java |  2 +-
 .../service/CommunicationServiceTest.java     | 37 +++++---
 .../gf/process/tasks/GroupPhaseTaskTest.java  |  4 +-
 17 files changed, 246 insertions(+), 110 deletions(-)
 create mode 100644 gemeinsamforschen/src/main/java/unipotsdam/gf/exceptions/MysqlDownException.java
 create mode 100644 gemeinsamforschen/src/main/java/unipotsdam/gf/exceptions/RocketChatDownException.java
 create mode 100644 gemeinsamforschen/src/main/java/unipotsdam/gf/exceptions/UserDoesNotExistInMysqlException.java
 create mode 100644 gemeinsamforschen/src/main/java/unipotsdam/gf/exceptions/UserDoesNotExistInRocketChatException.java
 create mode 100644 gemeinsamforschen/src/main/java/unipotsdam/gf/exceptions/UserExistsInMysqlException.java
 create mode 100644 gemeinsamforschen/src/main/java/unipotsdam/gf/exceptions/UserExistsInRocketChatException.java

diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/exceptions/MysqlDownException.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/exceptions/MysqlDownException.java
new file mode 100644
index 00000000..647f39f6
--- /dev/null
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/exceptions/MysqlDownException.java
@@ -0,0 +1,4 @@
+package unipotsdam.gf.exceptions;
+
+public class MysqlDownException {
+}
diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/exceptions/RocketChatDownException.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/exceptions/RocketChatDownException.java
new file mode 100644
index 00000000..6b9bdb42
--- /dev/null
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/exceptions/RocketChatDownException.java
@@ -0,0 +1,4 @@
+package unipotsdam.gf.exceptions;
+
+public class RocketChatDownException extends Exception {
+}
diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/exceptions/UserDoesNotExistInMysqlException.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/exceptions/UserDoesNotExistInMysqlException.java
new file mode 100644
index 00000000..5231c47a
--- /dev/null
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/exceptions/UserDoesNotExistInMysqlException.java
@@ -0,0 +1,4 @@
+package unipotsdam.gf.exceptions;
+
+public class UserDoesNotExistInMysqlException extends Exception {
+}
diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/exceptions/UserDoesNotExistInRocketChatException.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/exceptions/UserDoesNotExistInRocketChatException.java
new file mode 100644
index 00000000..a877a7f2
--- /dev/null
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/exceptions/UserDoesNotExistInRocketChatException.java
@@ -0,0 +1,4 @@
+package unipotsdam.gf.exceptions;
+
+public class UserDoesNotExistInRocketChatException extends Exception {
+}
diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/exceptions/UserExistsInMysqlException.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/exceptions/UserExistsInMysqlException.java
new file mode 100644
index 00000000..f9982f91
--- /dev/null
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/exceptions/UserExistsInMysqlException.java
@@ -0,0 +1,9 @@
+package unipotsdam.gf.exceptions;
+
+public class UserExistsInMysqlException extends Exception {
+
+    @Override
+    public String getMessage() {
+        return "Tried to create User but exists in mysql";
+    }
+}
diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/exceptions/UserExistsInRocketChatException.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/exceptions/UserExistsInRocketChatException.java
new file mode 100644
index 00000000..8160dadf
--- /dev/null
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/exceptions/UserExistsInRocketChatException.java
@@ -0,0 +1,8 @@
+package unipotsdam.gf.exceptions;
+
+public class UserExistsInRocketChatException extends Exception {
+    @Override
+    public String getMessage() {
+        return "Tried to create User but exists in RocketChat";
+    }
+}
diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/interfaces/ICommunication.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/interfaces/ICommunication.java
index b5c9b6e7..ddb8fe6d 100644
--- a/gemeinsamforschen/src/main/java/unipotsdam/gf/interfaces/ICommunication.java
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/interfaces/ICommunication.java
@@ -1,6 +1,9 @@
 package unipotsdam.gf.interfaces;
 
 
+import unipotsdam.gf.exceptions.RocketChatDownException;
+import unipotsdam.gf.exceptions.UserDoesNotExistInRocketChatException;
+import unipotsdam.gf.exceptions.UserExistsInRocketChatException;
 import unipotsdam.gf.modules.assessment.controller.model.StudentIdentifier;
 import unipotsdam.gf.modules.communication.model.EMailMessage;
 import unipotsdam.gf.modules.communication.model.chat.ChatMessage;
@@ -37,7 +40,8 @@ public interface ICommunication {
      * @param name chat room name
      * @return chat room id
      */
-    String createChatRoom(String name, boolean readOnly, List<User> users);
+    String createChatRoom(String name, boolean readOnly, List<User> users)
+            throws RocketChatDownException, UserDoesNotExistInRocketChatException;
 
     /**
      * creates chatRoom with name "group.projectId - group.id" and set chatRoomId for group
@@ -45,11 +49,13 @@ public interface ICommunication {
      * @param group Object for information
      * @return true if chatRoom was created, otherwise false
      */
-    boolean createChatRoom(Group group, boolean readOnly);
+    boolean createChatRoom(Group group, boolean readOnly)
+            throws RocketChatDownException, UserDoesNotExistInRocketChatException;
 
-    String createEmptyChatRoom(String name, boolean readOnly);
+    String createEmptyChatRoom(String name, boolean readOnly)
+            throws RocketChatDownException, UserDoesNotExistInRocketChatException;
 
-    boolean deleteChatRoom(String roomId);
+    boolean deleteChatRoom(String roomId) throws RocketChatDownException, UserDoesNotExistInRocketChatException;
 
     /**
      * endpoint: https://rocket.chat/docs/developer-guides/rest-api/groups/invite/
@@ -58,9 +64,11 @@ public interface ICommunication {
      * @param user   information about user
      * @return if user was added successfully
      */
-    boolean addUserToChatRoom(User user, String roomId);
+    boolean addUserToChatRoom(User user, String roomId)
+            throws RocketChatDownException, UserDoesNotExistInRocketChatException;
 
-    boolean removeUserFromChatRoom(User user, String roomId);
+    boolean removeUserFromChatRoom(User user, String roomId)
+            throws RocketChatDownException, UserDoesNotExistInRocketChatException;
 
     /**
      * endpoint: https://rocket.chat/docs/developer-guides/rest-api/groups/settopic/
@@ -80,9 +88,9 @@ public interface ICommunication {
      * @param roomId chat room id
      * @return chat room information
      */
-    String getChatRoomName(String roomId);
+    String getChatRoomName(String roomId) throws RocketChatDownException, UserDoesNotExistInRocketChatException;
 
-    boolean exists(String roomId);
+    boolean exists(String roomId) throws RocketChatDownException, UserDoesNotExistInRocketChatException;
 
     /**
      * api: https://rocket.chat/docs/developer-guides/rest-api/authentication/login/
@@ -90,7 +98,8 @@ public interface ICommunication {
      * @param user username and password
      * @return information about user, especially authtoken for later use of endpoints
      */
-    User loginUser(User user);
+    User loginUser(User user)
+            throws RocketChatDownException, UserDoesNotExistInRocketChatException;
 
     /**
      * api 1: https://rocket.chat/docs/developer-guides/rest-api/users/register/
@@ -102,9 +111,11 @@ public interface ICommunication {
      * @param user registers user to rocket.chat
      * @return user id
      */
-    boolean registerUser(User user);
+    boolean registerUser(User user)
+            throws RocketChatDownException, UserExistsInRocketChatException;
 
-    String getChatRoomLink(String userToken, String projectId);
+    String getChatRoomLink(String userToken, String projectId)
+            throws RocketChatDownException, UserDoesNotExistInRocketChatException;
 
     String getProjectChatRoomLink(String projectName);
 
diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/communication/service/CommunicationService.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/communication/service/CommunicationService.java
index a887d0e2..6adb98d1 100644
--- a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/communication/service/CommunicationService.java
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/communication/service/CommunicationService.java
@@ -7,6 +7,9 @@ import org.apache.logging.log4j.util.Strings;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import unipotsdam.gf.config.GFRocketChatConfig;
+import unipotsdam.gf.exceptions.RocketChatDownException;
+import unipotsdam.gf.exceptions.UserDoesNotExistInRocketChatException;
+import unipotsdam.gf.exceptions.UserExistsInRocketChatException;
 import unipotsdam.gf.interfaces.ICommunication;
 import unipotsdam.gf.modules.assessment.controller.model.StudentIdentifier;
 import unipotsdam.gf.modules.communication.model.EMailMessage;
@@ -102,12 +105,14 @@ public class CommunicationService implements ICommunication {
     }
 
     @Override
-    public String createEmptyChatRoom(String name, boolean readOnly) {
+    public String createEmptyChatRoom(String name, boolean readOnly)
+            throws RocketChatDownException, UserDoesNotExistInRocketChatException {
         return createChatRoom(name, readOnly, new ArrayList<>());
     }
 
     @Override
-    public String createChatRoom(String name, boolean readOnly, List<User> member) {
+    public String createChatRoom(String name, boolean readOnly, List<User> member)
+            throws RocketChatDownException, UserDoesNotExistInRocketChatException {
         loginUser(ADMIN_USER);
 
         Map<String, String> headerMap = new RocketChatHeaderMapBuilder().withRocketChatAdminAuth().build();
@@ -141,7 +146,8 @@ public class CommunicationService implements ICommunication {
     }
 
     @Override
-    public boolean createChatRoom(Group group, boolean readOnly) {
+    public boolean createChatRoom(Group group, boolean readOnly)
+            throws RocketChatDownException, UserDoesNotExistInRocketChatException {
         loginUser(ADMIN_USER);
 
         // chatRoom name: projectId - GroupId
@@ -159,7 +165,7 @@ public class CommunicationService implements ICommunication {
     }
 
     @Override
-    public boolean deleteChatRoom(String roomId) {
+    public boolean deleteChatRoom(String roomId) throws RocketChatDownException, UserDoesNotExistInRocketChatException {
         // TODO: maybe add lock for getChatRoomName, so synchronized access doesn't create errors while deleting
         loginUser(ADMIN_USER);
 
@@ -184,16 +190,19 @@ public class CommunicationService implements ICommunication {
     }
 
     @Override
-    public boolean addUserToChatRoom(User user, String roomId) {
+    public boolean addUserToChatRoom(User user, String roomId)
+            throws RocketChatDownException, UserDoesNotExistInRocketChatException {
         return modifyChatRoom(user, roomId, true);
     }
 
     @Override
-    public boolean removeUserFromChatRoom(User user, String roomId) {
+    public boolean removeUserFromChatRoom(User user, String roomId)
+            throws RocketChatDownException, UserDoesNotExistInRocketChatException {
         return modifyChatRoom(user, roomId, false);
     }
 
-    private boolean modifyChatRoom(User user, String roomId, boolean addUser) {
+    private boolean modifyChatRoom(User user, String roomId, boolean addUser)
+            throws RocketChatDownException, UserDoesNotExistInRocketChatException {
         loginUser(ADMIN_USER);
 
         if (hasEmptyParameter(user.getRocketChatUserId(), roomId)) {
@@ -225,7 +234,7 @@ public class CommunicationService implements ICommunication {
     }
 
     @Override
-    public String getChatRoomName(String roomId) {
+    public String getChatRoomName(String roomId) throws RocketChatDownException, UserDoesNotExistInRocketChatException {
 
         loginUser(ADMIN_USER);
 
@@ -248,7 +257,7 @@ public class CommunicationService implements ICommunication {
     }
 
     @Override
-    public User loginUser(User user) {
+    public User loginUser(User user) throws RocketChatDownException, UserDoesNotExistInRocketChatException {
 
         if (hasEmptyParameter(user.getEmail(), user.getPassword())) {
             return null;
@@ -264,7 +273,7 @@ public class CommunicationService implements ICommunication {
                         .asObject(RocketChatLoginResponse.class);
 
         if (isBadRequest(response)) {
-            return null;
+            throw new UserDoesNotExistInRocketChatException();
         } else {
             if (ADMIN_USER.equals(user)) {
                 setAdminToken();
@@ -279,7 +288,8 @@ public class CommunicationService implements ICommunication {
     }
 
     @Override
-    public boolean registerUser(User user) {
+    public boolean registerUser(User user)
+            throws RocketChatDownException, UserExistsInRocketChatException {
 
         if (hasEmptyParameter(user.getEmail(), user.getName(), user.getPassword())) {
             return false;
@@ -296,15 +306,19 @@ public class CommunicationService implements ICommunication {
                 unirestService.post(ROCKET_CHAT_API_LINK + "users.register").body(rocketChatRegister)
                         .asObject(RocketChatRegisterResponse.class);
 
-        if (isBadRequest(response)) {
-            return false;
+        Boolean badRequest = isBadRequest(response);
+        if (badRequest) {
+            throw new UserExistsInRocketChatException();
         }
 
         RocketChatRegisterResponse registerResponse = response.getBody();
+
+        // not sure we need this test
         if (!registerResponse.isSuccessful()) {
             return false;
         }
 
+        // update user with rocket chat data
         user.setRocketChatUsername(rocketChatUsername);
         user.setRocketChatUserId(registerResponse.getUserId());
 
@@ -316,7 +330,8 @@ public class CommunicationService implements ICommunication {
         return true;
     }
 
-    public String getChatRoomLink(String userEmail, String projectName) {
+    public String getChatRoomLink(String userEmail, String projectName)
+            throws RocketChatDownException, UserDoesNotExistInRocketChatException {
 
         loginUser(ADMIN_USER);
 
@@ -445,18 +460,27 @@ public class CommunicationService implements ICommunication {
         user.setRocketChatPersonalAccessToken(responseBody.get("token").toString());
         return true;
     }*/
-    private boolean isBadRequest(HttpResponse response) {
+    private Boolean isBadRequest(HttpResponse response)
+            throws RocketChatDownException{
         int status = response.getStatus();
+        if (Response.Status.OK.getStatusCode() == status) {
+            return false;
+        }
         if (Response.Status.UNAUTHORIZED.getStatusCode() == status) {
             unsetAdminToken();
+            return true;
+        }
+        if (Response.Status.NOT_FOUND.getStatusCode() == status) {
+            unsetAdminToken();
+            throw new RocketChatDownException();
+        } else {
+            return true;
         }
-        return status == Response.Status.BAD_REQUEST.getStatusCode() || status == Response.Status.UNAUTHORIZED
-                .getStatusCode();
     }
 
 
     @Override
-    public boolean exists(String roomId) {
+    public boolean exists(String roomId) throws RocketChatDownException, UserDoesNotExistInRocketChatException {
         return !getChatRoomName(roomId).isEmpty();
     }
 }
diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/communication/view/CommunicationView.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/communication/view/CommunicationView.java
index 167405da..5792187b 100644
--- a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/communication/view/CommunicationView.java
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/communication/view/CommunicationView.java
@@ -3,6 +3,8 @@ package unipotsdam.gf.modules.communication.view;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import unipotsdam.gf.config.GFRocketChatConfig;
+import unipotsdam.gf.exceptions.RocketChatDownException;
+import unipotsdam.gf.exceptions.UserDoesNotExistInRocketChatException;
 import unipotsdam.gf.modules.communication.model.LoginToken;
 import unipotsdam.gf.modules.user.User;
 import unipotsdam.gf.interfaces.ICommunication;
@@ -37,7 +39,8 @@ public class CommunicationView {
     @GET
     @Produces(MediaType.APPLICATION_JSON)
     @Path("/info/{roomId}")
-    public Response getChatRoomInformation(@PathParam("roomId") String roomId) {
+    public Response getChatRoomInformation(@PathParam("roomId") String roomId)
+            throws RocketChatDownException, UserDoesNotExistInRocketChatException {
         String chatRoomName = communicationService.getChatRoomName(roomId);
         if (chatRoomName.isEmpty()) {
             log.error("chatRoom not found for roomId: {}", roomId);
@@ -51,7 +54,8 @@ public class CommunicationView {
     @Consumes(MediaType.APPLICATION_JSON)
     @Produces(MediaType.APPLICATION_JSON)
     @Path("/addUser/{roomId}")
-    public Response addUserToChatRoom(@PathParam("roomId") String roomId, User user) {
+    public Response addUserToChatRoom(@PathParam("roomId") String roomId, User user)
+            throws RocketChatDownException, UserDoesNotExistInRocketChatException {
         if (isNull(user)) {
             log.trace("addUser user object was null");
             return Response.status(Response.Status.BAD_REQUEST).entity("must provide user").build();
@@ -79,7 +83,8 @@ public class CommunicationView {
     @Consumes(MediaType.APPLICATION_JSON)
     @Produces(MediaType.APPLICATION_JSON)
     @Path("/removeUser/{roomId}")
-    public Response removeUserFromChatRoom(User user, @PathParam("roomId") String roomId) {
+    public Response removeUserFromChatRoom(User user, @PathParam("roomId") String roomId)
+            throws RocketChatDownException, UserDoesNotExistInRocketChatException {
         if (isNull(user)) {
             log.trace("removeUser user object was null");
             return Response.status(Response.Status.BAD_REQUEST).entity("must provide user").build();
@@ -107,7 +112,8 @@ public class CommunicationView {
     @Produces(MediaType.APPLICATION_JSON)
     @Path("/room/create/{name}")
     public Response createChatRoom(
-            @PathParam("name") String name, List<User> users, @QueryParam("readOnly") boolean readOnly) {
+            @PathParam("name") String name, List<User> users, @QueryParam("readOnly") boolean readOnly)
+            throws RocketChatDownException, UserDoesNotExistInRocketChatException {
         if (isNull(name)) {
             return Response.status(Response.Status.BAD_REQUEST).entity("must provide name as queryParam").build();
         }
@@ -123,7 +129,8 @@ public class CommunicationView {
     @POST
     @Produces(MediaType.APPLICATION_JSON)
     @Path("/sso")
-    public LoginToken provideLoginToken(@Context HttpServletRequest req, Object payload) {
+    public LoginToken provideLoginToken(@Context HttpServletRequest req, Object payload)
+            throws RocketChatDownException, UserDoesNotExistInRocketChatException {
         if (req.getSession().getAttribute(GFContexts.ROCKETCHATAUTHTOKEN) != null) {
             String token = getAuthToken(req);
             return new LoginToken(token);
diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/project/ProjectView.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/project/ProjectView.java
index ab8525d3..645934de 100644
--- a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/project/ProjectView.java
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/project/ProjectView.java
@@ -1,8 +1,9 @@
 package unipotsdam.gf.modules.project;
 
-import unipotsdam.gf.process.ProjectCreationProcess;
-import unipotsdam.gf.process.tasks.TaskDAO;
+import unipotsdam.gf.exceptions.RocketChatDownException;
+import unipotsdam.gf.exceptions.UserDoesNotExistInRocketChatException;
 import unipotsdam.gf.modules.user.User;
+import unipotsdam.gf.process.ProjectCreationProcess;
 import unipotsdam.gf.session.GFContexts;
 
 import javax.annotation.ManagedBean;
@@ -36,7 +37,8 @@ public class ProjectView {
     @Consumes(MediaType.APPLICATION_JSON)
     @Produces(MediaType.TEXT_PLAIN)
     @Path("/create")
-    public void createProject(@Context HttpServletRequest req, Project project) throws URISyntaxException, IOException {
+    public void createProject(@Context HttpServletRequest req, Project project)
+            throws IOException, RocketChatDownException, UserDoesNotExistInRocketChatException {
         String userEmail = gfContexts.getUserEmail(req);
         User user = iManagement.getUserByEmail(userEmail);
         assert user != null;
@@ -68,15 +70,16 @@ public class ProjectView {
     @Consumes(MediaType.TEXT_PLAIN)
     @Produces(MediaType.TEXT_PLAIN)
     @Path("/login/{projectName}")
-    public String register(@Context HttpServletRequest req, @PathParam("projectName") String projectName,
-                           @QueryParam("password") String
-            password) throws IOException {
+    public String register(
+            @Context HttpServletRequest req, @PathParam("projectName") String projectName,
+            @QueryParam("password") String password)
+            throws IOException, RocketChatDownException, UserDoesNotExistInRocketChatException {
         User user = gfContexts.getUserFromSession(req);
         Project project = projectDAO.getProjectByName(projectName);
-        if (project == null){
+        if (project == null) {
             return "project missing";
         }
-        if (!project.getPassword().equals(password) ) {
+        if (!project.getPassword().equals(password)) {
             return "wrong password";
         }
 
@@ -97,5 +100,4 @@ public class ProjectView {
     }
 
 
-
 }
diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/user/UserView.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/user/UserView.java
index 97a2a52a..f04516c5 100644
--- a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/user/UserView.java
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/user/UserView.java
@@ -1,6 +1,8 @@
 package unipotsdam.gf.modules.user;
 
+import unipotsdam.gf.exceptions.*;
 import unipotsdam.gf.modules.project.Management;
+import unipotsdam.gf.process.ProjectCreationProcess;
 import unipotsdam.gf.session.GFContexts;
 import unipotsdam.gf.interfaces.ICommunication;
 
@@ -33,6 +35,9 @@ public class UserView {
     @Inject
     private Management management;
 
+    @Inject
+    private ProjectCreationProcess projectCreationProcess;
+
     @Inject
     public UserView(ICommunication communicationService, UserDAO userDAO, Management management) {
         this.communicationService = communicationService;
@@ -59,10 +64,35 @@ public class UserView {
             @FormParam("email") String email, @FormParam("isStudent") String isStudent) throws URISyntaxException {
 
         User user = new User(name, password, email, isStudent == null);
-        return register(req, true, user);
+        try {
+            projectCreationProcess.createUser(user);
+        } catch (UserExistsInRocketChatException e) {
+            return registrationError();
+        } catch (UserExistsInMysqlException e) {
+            String existsUrl = "../register.jsp?userExists=true";
+            return forwardToLocation(existsUrl);
+        } catch (RocketChatDownException e) {
+            e.printStackTrace();
+            return registrationError();
+        }
+
+        try {
+            projectCreationProcess.authenticateUser(user, req);
+        } catch (UserDoesNotExistInRocketChatException e) {
+            loginError();
+        } catch (RocketChatDownException e) {
+            e.printStackTrace();
+            return registrationError();
+        }
+        return redirectToProjectPage(req, user);
 
     }
 
+    private Response redirectToUserExists() throws URISyntaxException {
+        String existsUrl = "../index.jsp?userExists=false";
+        return forwardToLocation(existsUrl);
+    }
+
     /**
      * checks if a user exists in order to log him in
      *
@@ -76,20 +106,25 @@ public class UserView {
     @POST
     @Produces(MediaType.TEXT_HTML)
     @Path("/exists")
-    public Response loginUser(
+    public Response authenticate(
             @Context HttpServletRequest req, @FormParam("name") String name, @FormParam("password") String password,
             @FormParam("email") String email) throws URISyntaxException {
 
         User user = new User(name, password, email, null);
-        // TODO fix this
-        User isLoggedIn = communicationService.loginUser(user);
-        if (isLoggedIn != null) {
-            req.getSession().setAttribute(GFContexts.ROCKETCHATAUTHTOKEN, isLoggedIn.getRocketChatAuthToken());
-            req.getSession().setAttribute(GFContexts.ROCKETCHATID, isLoggedIn.getRocketChatUserId());
-            return register(req, false, user);
-        } else {
+        try {
+            Boolean exists = projectCreationProcess.authenticateUser(user, req);
+            if (exists) {
+                redirectToUserExists();
+            } else {
+                return loginError();
+            }
+        } catch (UserDoesNotExistInRocketChatException e) {
+            loginError();
+        } catch (RocketChatDownException e) {
             return loginError();
         }
+        user = fillUserFields(user);
+        return redirectToProjectPage(req, user);
     }
 
     @POST
@@ -106,44 +141,6 @@ public class UserView {
 
     }
 
-    /**
-     * if create User is true, the user is created and logged in if he does not exist
-     *
-     * @param createUser
-     * @param user
-     * @return
-     * @throws URISyntaxException
-     */
-    public Response register(HttpServletRequest req, boolean createUser, User user) throws URISyntaxException {
-
-        if (management.exists(user)) {
-            if (!createUser) {
-                user = fillUserFields(user);
-                return redirectToProjectPage(req, user);
-            }
-            String existsUrl = "../register.jsp?userExists=true";
-            return forwardToLocation(existsUrl);
-        } else {
-            if (createUser) {
-                boolean isRegisteredAndLoggedIn = communicationService.registerUser(user);
-                if (!isRegisteredAndLoggedIn) {
-                    return registrationError();
-                }
-                management.create(user, null);
-                user = fillUserFields(user);
-                // TODO den quatsch hier schön aufschreiben
-                User isLoggedIn = communicationService.loginUser(user);
-                req.getSession().setAttribute(GFContexts.ROCKETCHATAUTHTOKEN, isLoggedIn.getRocketChatAuthToken());
-                req.getSession().setAttribute(GFContexts.ROCKETCHATID, isLoggedIn.getRocketChatUserId());
-                return redirectToProjectPage(req, isLoggedIn);
-            } else {
-                String existsUrl = "../index.jsp?userExists=false";
-                return forwardToLocation(existsUrl);
-            }
-
-        }
-    }
-
     private User fillUserFields(User user) {
         user = userDAO.getUserByEmail(user.getEmail());
         return user;
@@ -174,7 +171,6 @@ public class UserView {
         } else {
             successUrl = "../project/overview-docent.jsp";
         }
-        req.getSession().setAttribute(GFContexts.USEREMAIL, user.getEmail());
         Response result = forwardToLocation(successUrl);
         return result;
     }
diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/process/ProjectCreationProcess.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/process/ProjectCreationProcess.java
index addead67..58e257b0 100644
--- a/gemeinsamforschen/src/main/java/unipotsdam/gf/process/ProjectCreationProcess.java
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/process/ProjectCreationProcess.java
@@ -1,5 +1,6 @@
 package unipotsdam.gf.process;
 
+import unipotsdam.gf.exceptions.*;
 import unipotsdam.gf.interfaces.ICommunication;
 import unipotsdam.gf.interfaces.IPhases;
 import unipotsdam.gf.modules.group.GroupDAO;
@@ -13,9 +14,11 @@ import unipotsdam.gf.process.tasks.Progress;
 import unipotsdam.gf.process.tasks.Task;
 import unipotsdam.gf.process.tasks.TaskDAO;
 import unipotsdam.gf.process.tasks.TaskName;
+import unipotsdam.gf.session.GFContexts;
 
 import javax.inject.Inject;
 import javax.inject.Singleton;
+import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.WebApplicationException;
 import java.io.IOException;
 
@@ -39,6 +42,9 @@ public class ProjectCreationProcess {
     @Inject
     private ICommunication iCommunication;
 
+    @Inject
+    private GFContexts gfContexts;
+
     /**
      * STEP 1
      *
@@ -46,7 +52,8 @@ public class ProjectCreationProcess {
      * @param author
      * @throws IOException
      */
-    public void createProject(Project project, User author) throws IOException {
+    public void createProject(Project project, User author)
+            throws IOException, RocketChatDownException, UserDoesNotExistInRocketChatException {
         project.setAuthorEmail(author.getEmail());
         try {
             iManagement.create(project);
@@ -66,7 +73,8 @@ public class ProjectCreationProcess {
      * @param project
      * @param user
      */
-    public void studentEntersProject(Project project, User user) {
+    public void studentEntersProject(Project project, User user)
+            throws RocketChatDownException, UserDoesNotExistInRocketChatException {
         // student enters project
         iManagement.register(user, project, null);
 
@@ -90,4 +98,28 @@ public class ProjectCreationProcess {
         }
         iCommunication.addUserToChatRoom(user, project.getName());
     }
+
+    public void createUser(User user)
+            throws UserExistsInMysqlException, RocketChatDownException, UserExistsInRocketChatException {
+        // todo implement
+        if(iManagement.exists(user)) {
+            throw new UserExistsInMysqlException();
+        }
+        // create user in rocket chat
+        iCommunication.registerUser(user);
+        // create user in mysql
+        iManagement.create(user, null);
+
+    }
+
+    public Boolean authenticateUser(User user, HttpServletRequest req)
+            throws UserDoesNotExistInRocketChatException, RocketChatDownException {
+        // todo implement
+
+        User isLoggedIn = iCommunication.loginUser(user);
+        gfContexts.updateUserSessionWithRocketChat(req, isLoggedIn);
+        gfContexts.updateUserWithEmail(req, isLoggedIn);
+        return iManagement.exists(user);
+    }
+
 }
diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/session/GFContexts.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/session/GFContexts.java
index aba9535d..df5ccd96 100644
--- a/gemeinsamforschen/src/main/java/unipotsdam/gf/session/GFContexts.java
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/session/GFContexts.java
@@ -31,4 +31,13 @@ public class GFContexts {
         String userEmail = getUserEmail(req);
         return userDAO.getUserByEmail(userEmail);
     }
+
+    public void updateUserSessionWithRocketChat(HttpServletRequest req, User user) {
+        req.getSession().setAttribute(GFContexts.ROCKETCHATAUTHTOKEN, user.getRocketChatAuthToken());
+        req.getSession().setAttribute(GFContexts.ROCKETCHATID, user.getRocketChatUserId());
+    }
+
+    public void updateUserWithEmail(HttpServletRequest req, User user) {
+        req.getSession().setAttribute(GFContexts.USEREMAIL, user.getEmail());
+    }
 }
diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/taglibs/ChatWindow.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/taglibs/ChatWindow.java
index c9e9c888..3abb7d26 100644
--- a/gemeinsamforschen/src/main/java/unipotsdam/gf/taglibs/ChatWindow.java
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/taglibs/ChatWindow.java
@@ -6,6 +6,8 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import unipotsdam.gf.config.GFApplicationBinder;
 import unipotsdam.gf.config.GFRocketChatConfig;
+import unipotsdam.gf.exceptions.RocketChatDownException;
+import unipotsdam.gf.exceptions.UserDoesNotExistInRocketChatException;
 import unipotsdam.gf.interfaces.ICommunication;
 import unipotsdam.gf.modules.communication.service.CommunicationService;
 import unipotsdam.gf.session.GFContext;
@@ -44,8 +46,15 @@ public class ChatWindow extends SimpleTagSupport {
             writeIframe(request, chatRoomLink);
         } else {
             // scope is group
-            String chatRoomLink = communicationService
-                    .getChatRoomLink(request.getSession().getAttribute(GFContexts.USEREMAIL).toString(), projectName);
+            String chatRoomLink = null;
+            try {
+                chatRoomLink = communicationService
+                        .getChatRoomLink(request.getSession().getAttribute(GFContexts.USEREMAIL).toString(), projectName);
+            } catch (RocketChatDownException e) {
+                e.printStackTrace();
+            } catch (UserDoesNotExistInRocketChatException e) {
+                e.printStackTrace();
+            }
             writeIframe(request, chatRoomLink);
         }
 
diff --git a/gemeinsamforschen/src/test/java/unipotsdam/gf/core/database/UpdateDB.java b/gemeinsamforschen/src/test/java/unipotsdam/gf/core/database/UpdateDB.java
index 17c9b042..2485150f 100644
--- a/gemeinsamforschen/src/test/java/unipotsdam/gf/core/database/UpdateDB.java
+++ b/gemeinsamforschen/src/test/java/unipotsdam/gf/core/database/UpdateDB.java
@@ -32,7 +32,7 @@ public class UpdateDB {
         System.out.println(new java.io.File( "." ).getCanonicalPath());
         updateDB.runScript(new FileReader("src/test/resources/database/db.sql"));
         updateDB.runScript(new FileReader("src/test/resources/database/fltrail.sql"));
-        updateDB.runScript(new FileReader("src/test/resources/database/testuser.sql"));
+        //updateDB.runScript(new FileReader("src/test/resources/database/testuser.sql"));
     }
 
     public static void updateTestDB() throws SQLException, ManagedProcessException, IOException {
diff --git a/gemeinsamforschen/src/test/java/unipotsdam/gf/modules/communication/service/CommunicationServiceTest.java b/gemeinsamforschen/src/test/java/unipotsdam/gf/modules/communication/service/CommunicationServiceTest.java
index a0e22662..b22d4ece 100644
--- a/gemeinsamforschen/src/test/java/unipotsdam/gf/modules/communication/service/CommunicationServiceTest.java
+++ b/gemeinsamforschen/src/test/java/unipotsdam/gf/modules/communication/service/CommunicationServiceTest.java
@@ -9,6 +9,9 @@ import uk.co.jemos.podam.api.PodamFactoryImpl;
 import unipotsdam.gf.config.GFApplicationBinder;
 import unipotsdam.gf.core.database.TestGFApplicationBinder;
 import unipotsdam.gf.core.database.UpdateDB;
+import unipotsdam.gf.exceptions.RocketChatDownException;
+import unipotsdam.gf.exceptions.UserDoesNotExistInRocketChatException;
+import unipotsdam.gf.exceptions.UserExistsInRocketChatException;
 import unipotsdam.gf.interfaces.ICommunication;
 import unipotsdam.gf.modules.assessment.controller.model.StudentIdentifier;
 import unipotsdam.gf.modules.communication.model.EMailMessage;
@@ -83,14 +86,22 @@ public class CommunicationServiceTest {
 
     @After
     public void tearDown() {
-        createdChatRooms.forEach(createdChatRoom -> iCommunication.deleteChatRoom(createdChatRoom));
+        createdChatRooms.forEach(createdChatRoom -> {
+            try {
+                iCommunication.deleteChatRoom(createdChatRoom);
+            } catch (RocketChatDownException e) {
+                e.printStackTrace();
+            } catch (UserDoesNotExistInRocketChatException e) {
+                e.printStackTrace();
+            }
+        });
         createdChatRooms.clear();
 
 
     }
 
     @Test
-    public void loginUser() {
+    public void loginUser() throws RocketChatDownException, UserDoesNotExistInRocketChatException {
         assertNotNull(iCommunication.loginUser(TEST_USER));
         assertTrue(!TEST_USER.getRocketChatAuthToken().isEmpty());
         assertTrue(!TEST_USER.getRocketChatUserId().isEmpty());
@@ -102,7 +113,7 @@ public class CommunicationServiceTest {
 
     @Ignore
     @Test
-    public void registerUser() {
+    public void registerUser() throws RocketChatDownException, UserExistsInRocketChatException {
         // TODO Side effect is not optimal because you need to know that before persisting the user
         boolean userCreated = iCommunication.registerUser(user);
         //userDAO.persist(user, null);
@@ -114,7 +125,7 @@ public class CommunicationServiceTest {
     }
 
     @Test
-    public void createEmptyChatRoom() {
+    public void createEmptyChatRoom() throws RocketChatDownException, UserDoesNotExistInRocketChatException {
         String chatRoom = iCommunication.createEmptyChatRoom("Test", false);
         assertNotNull(chatRoom);
         assertFalse(chatRoom.isEmpty());
@@ -130,7 +141,7 @@ public class CommunicationServiceTest {
     }
 
     @Test
-    public void createChatRoomWithUser() {
+    public void createChatRoomWithUser() throws RocketChatDownException, UserDoesNotExistInRocketChatException {
         List<User> userList = Arrays.asList(ADMIN_USER, TEST_USER);
         String chatRoom = iCommunication.createChatRoom("ChatWithUser", false, userList);
 
@@ -142,7 +153,7 @@ public class CommunicationServiceTest {
     }
 
     @Test
-    public void createChatRoomWithGroup() {
+    public void createChatRoomWithGroup() throws RocketChatDownException, UserDoesNotExistInRocketChatException {
         Group group = new Group();
         group.setMembers(Collections.singletonList(ADMIN_USER));
         group.setProjectName("chatWithGroup");
@@ -152,7 +163,7 @@ public class CommunicationServiceTest {
     }
 
     @Test
-    public void getChatRoomName() {
+    public void getChatRoomName() throws RocketChatDownException, UserDoesNotExistInRocketChatException {
         String expectedChatRoomName = "ChatRoomName";
         String chatRoomId = iCommunication.createEmptyChatRoom(expectedChatRoomName, false);
         assertNotNull(chatRoomId);
@@ -168,7 +179,7 @@ public class CommunicationServiceTest {
     }
 
     @Test
-    public void getChatRoomLink() {
+    public void getChatRoomLink() throws RocketChatDownException, UserDoesNotExistInRocketChatException {
         String projectId = "Projekt";
         Project project = new Project(projectId, user.getEmail());
         projectDAO.persist(project);
@@ -192,7 +203,7 @@ public class CommunicationServiceTest {
     }
 
     @Test
-    public void exists() {
+    public void exists() throws RocketChatDownException, UserDoesNotExistInRocketChatException {
         String expectedChatRoomName = "ChatRoomName";
         String chatRoomId = iCommunication.createEmptyChatRoom(expectedChatRoomName, false);
         assertNotNull(chatRoomId);
@@ -205,7 +216,7 @@ public class CommunicationServiceTest {
     }
 
     @Test
-    public void addUserToChatRoom() {
+    public void addUserToChatRoom() throws RocketChatDownException, UserDoesNotExistInRocketChatException {
         String chatRoomId = iCommunication.createEmptyChatRoom("addUser", false);
         assertNotNull(chatRoomId);
         assertFalse(chatRoomId.isEmpty());
@@ -216,7 +227,7 @@ public class CommunicationServiceTest {
     }
 
     @Test
-    public void removeUserFromChatRoom() {
+    public void removeUserFromChatRoom() throws RocketChatDownException, UserDoesNotExistInRocketChatException {
         String chatRoomId = iCommunication.createEmptyChatRoom("removeUser", false);
         assertNotNull(chatRoomId);
         assertFalse(chatRoomId.isEmpty());
@@ -229,7 +240,7 @@ public class CommunicationServiceTest {
     }
 
     @Test
-    public void deleteChatRoom() {
+    public void deleteChatRoom() throws RocketChatDownException, UserDoesNotExistInRocketChatException {
         String chatRoomId = iCommunication.createEmptyChatRoom("deleteChatRoom", false);
         assertNotNull(chatRoomId);
         assertFalse(chatRoomId.isEmpty());
@@ -271,7 +282,7 @@ public class CommunicationServiceTest {
 
     @Test
     @Ignore
-    public void createTestData() {
+    public void createTestData() throws RocketChatDownException, UserDoesNotExistInRocketChatException {
         User user = new User();
         user.setName("Martin Nachname");
         user.setPassword("test1234");
diff --git a/gemeinsamforschen/src/test/java/unipotsdam/gf/process/tasks/GroupPhaseTaskTest.java b/gemeinsamforschen/src/test/java/unipotsdam/gf/process/tasks/GroupPhaseTaskTest.java
index 27ee1232..88b5befd 100644
--- a/gemeinsamforschen/src/test/java/unipotsdam/gf/process/tasks/GroupPhaseTaskTest.java
+++ b/gemeinsamforschen/src/test/java/unipotsdam/gf/process/tasks/GroupPhaseTaskTest.java
@@ -7,6 +7,8 @@ import org.junit.Test;
 import uk.co.jemos.podam.api.PodamFactory;
 import uk.co.jemos.podam.api.PodamFactoryImpl;
 import unipotsdam.gf.core.database.TestGFApplicationBinder;
+import unipotsdam.gf.exceptions.RocketChatDownException;
+import unipotsdam.gf.exceptions.UserDoesNotExistInRocketChatException;
 import unipotsdam.gf.interfaces.IGroupFinding;
 import unipotsdam.gf.modules.group.Group;
 import unipotsdam.gf.modules.group.GroupFormationMechanism;
@@ -54,7 +56,7 @@ public class GroupPhaseTaskTest {
     }
 
     @Test
-    public void createCourse() {
+    public void createCourse() throws RocketChatDownException, UserDoesNotExistInRocketChatException {
 
         this.teacher = factory.manufacturePojo(User.class);
         teacher.setStudent(false);
-- 
GitLab