From b5d688311dbb1005fef156f16812c7abb06b1164 Mon Sep 17 00:00:00 2001 From: Lukas Ettel <34522828+LukasEttel@users.noreply.github.com> Date: Thu, 26 Mar 2020 15:23:52 +0100 Subject: [PATCH 1/8] Refactor of all addFromCsv in Deteils complete Co-authored-by: AndiBuls --- .../gruppen2/controller/WebController.java | 6 +----- .../gruppen2/service/ControllerService.java | 20 +++++++++++++++++-- .../gruppen2/service/ValidationService.java | 14 ------------- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/main/java/mops/gruppen2/controller/WebController.java b/src/main/java/mops/gruppen2/controller/WebController.java index 94fc6a6..f2270a3 100644 --- a/src/main/java/mops/gruppen2/controller/WebController.java +++ b/src/main/java/mops/gruppen2/controller/WebController.java @@ -129,11 +129,7 @@ public class WebController { @RequestParam("group_id") String groupId, @RequestParam(value = "file", required = false) MultipartFile file) throws IOException { Account account = keyCloakService.createAccountFromPrincipal(token); - List userList = new ArrayList<>(); - Group group = userService.getGroupById(UUID.fromString(groupId)); - userList = validationService.checkFile(file, userList, groupId, group, account); - UUID groupUUID = controllerService.getUUID(groupId); - controllerService.addUserList(userList, groupUUID); + controllerService.addUsersFromCsv(account, file, groupId); return "redirect:/gruppen2/details/members/" + groupId; } diff --git a/src/main/java/mops/gruppen2/service/ControllerService.java b/src/main/java/mops/gruppen2/service/ControllerService.java index 0198c75..ff17b07 100644 --- a/src/main/java/mops/gruppen2/service/ControllerService.java +++ b/src/main/java/mops/gruppen2/service/ControllerService.java @@ -92,6 +92,22 @@ public class ControllerService { addUserList(newUsers, groupId); } + public void addUsersFromCsv(Account account, MultipartFile file, String groupId) throws IOException{ + Group group = userService.getGroupById(UUID.fromString(groupId)); + + List newUserList = readCsvFile(file); + removeOldUsersFromNewUsers(group.getMembers(), newUserList); + + UUID groupUUID = getUUID(groupId); + + Long newUserMaximum = adjustUserMaximum((long) newUserList.size(), (long) group.getMembers().size(), group.getUserMaximum()); + if (newUserMaximum > group.getUserMaximum()){ + updateMaxUser(account, groupUUID, newUserMaximum); + } + + addUserList(newUserList, groupUUID); + } + private void removeOldUsersFromNewUsers(List oldUsers, List newUsers) { for (User oldUser : oldUsers) { newUsers.remove(oldUser); @@ -153,8 +169,8 @@ public class ControllerService { eventService.saveEvent(addUserEvent); } - public void addUserList(List users, UUID groupId) { - for (User user : users) { + public void addUserList(List newUsers, UUID groupId) { + for (User user : newUsers) { Group group = userService.getGroupById(groupId); if (group.getMembers().contains(user)) { logger.info("Benutzer " + user.getId() + " ist bereits in Gruppe"); diff --git a/src/main/java/mops/gruppen2/service/ValidationService.java b/src/main/java/mops/gruppen2/service/ValidationService.java index 94148c7..05b6ea8 100644 --- a/src/main/java/mops/gruppen2/service/ValidationService.java +++ b/src/main/java/mops/gruppen2/service/ValidationService.java @@ -108,20 +108,6 @@ public class ValidationService { return false; } - public List checkFile(MultipartFile file, List userList, String groupId, Group group, Account account) { - if (!file.isEmpty()) { - try { - userList = CsvService.read(file.getInputStream()); - if (userList.size() + group.getMembers().size() > group.getUserMaximum()) { - controllerService.updateMaxUser(account, UUID.fromString(groupId), (long) userList.size() + group.getMembers().size()); - } - } catch (IOException ex) { - throw new WrongFileException(file.getOriginalFilename()); - } - } - return userList; - } - /** * Überprüft ob alle Felder richtig gesetzt sind. * From d58cde597b694828042aa7dd353dac09b97a03ba Mon Sep 17 00:00:00 2001 From: Lukas Ettel <34522828+LukasEttel@users.noreply.github.com> Date: Thu, 26 Mar 2020 16:31:17 +0100 Subject: [PATCH 2/8] Futher refactor Co-authored-by: AndiBuls <49692239+andibuls@users.noreply.github.com> Co-authored-by: XXNitram <17749825+xxnitram@users.noreply.github.com> --- .../gruppen2/controller/WebController.java | 66 +++++++++++-------- .../exception/BadParameterException.java | 2 +- .../domain/exception/NoValueException.java | 2 +- .../gruppen2/service/ControllerService.java | 18 +++++ .../gruppen2/service/ValidationService.java | 55 ++++++++-------- .../resources/templates/detailsMember.html | 2 +- 6 files changed, 85 insertions(+), 60 deletions(-) diff --git a/src/main/java/mops/gruppen2/controller/WebController.java b/src/main/java/mops/gruppen2/controller/WebController.java index f2270a3..77b2cd3 100644 --- a/src/main/java/mops/gruppen2/controller/WebController.java +++ b/src/main/java/mops/gruppen2/controller/WebController.java @@ -61,6 +61,7 @@ public class WebController { public String index(KeycloakAuthenticationToken token, Model model) throws EventException { Account account = keyCloakService.createAccountFromPrincipal(token); User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); + model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token)); model.addAttribute("gruppen", userService.getUserGroups(user)); model.addAttribute("user", user); @@ -71,6 +72,7 @@ public class WebController { @GetMapping("/createOrga") public String createGroupAsOrga(KeycloakAuthenticationToken token, Model model) { Account account = keyCloakService.createAccountFromPrincipal(token); + model.addAttribute("account", account); model.addAttribute("lectures", groupService.getAllLecturesWithVisibilityPublic()); return "createOrga"; @@ -92,8 +94,8 @@ public class WebController { UUID parentUUID = controllerService.getUUID(parent); validationService.checkFields(description, title, userMaximum, maxInfiniteUsers); - controllerService.createGroupAsOrga(account, title, description, visibility, lecture, maxInfiniteUsers, userMaximum, parentUUID, file); + controllerService.createGroupAsOrga(account, title, description, visibility, lecture, maxInfiniteUsers, userMaximum, parentUUID, file); return "redirect:/gruppen2/"; } @@ -101,6 +103,7 @@ public class WebController { @GetMapping("/createStudent") public String createGroupAsStudent(KeycloakAuthenticationToken token, Model model) { Account account = keyCloakService.createAccountFromPrincipal(token); + model.addAttribute("account", account); model.addAttribute("lectures", groupService.getAllLecturesWithVisibilityPublic()); return "createStudent"; @@ -118,7 +121,9 @@ public class WebController { Account account = keyCloakService.createAccountFromPrincipal(token); UUID parentUUID = controllerService.getUUID(parent); + validationService.checkFields(description, title, userMaximum, maxInfiniteUsers); + controllerService.createGroup(account, title, description, visibility, null, maxInfiniteUsers, userMaximum, parentUUID); return "redirect:/gruppen2/"; } @@ -129,6 +134,7 @@ public class WebController { @RequestParam("group_id") String groupId, @RequestParam(value = "file", required = false) MultipartFile file) throws IOException { Account account = keyCloakService.createAccountFromPrincipal(token); + controllerService.addUsersFromCsv(account, file, groupId); return "redirect:/gruppen2/details/members/" + groupId; } @@ -139,16 +145,12 @@ public class WebController { Account account = keyCloakService.createAccountFromPrincipal(token); User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); Group group = userService.getGroupById(UUID.fromString(groupId)); - validationService.checkIfAdmin(group, user); - model.addAttribute("account", account); UUID parentId = group.getParent(); Group parent = new Group(); - if (!validationService.checkIfUserInGroup(group, user)) { - model.addAttribute("group", group); - model.addAttribute("parentId", parentId); - model.addAttribute("parent", parent); - return "detailsNoMember"; - } + + validationService.throwIfNoAdmin(group, user); + + model.addAttribute("account", account); model.addAttribute("title", group.getTitle()); model.addAttribute("description", group.getDescription()); model.addAttribute("admin", Role.ADMIN); @@ -160,16 +162,19 @@ public class WebController { @RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"}) @PostMapping("/details/changeMetadata") - public String pChangeMetadata(KeycloakAuthenticationToken token, - @RequestParam("title") String title, - @RequestParam("description") String description, - @RequestParam("groupId") String groupId) throws EventException { + public String postChangeMetadata(KeycloakAuthenticationToken token, + @RequestParam("title") String title, + @RequestParam("description") String description, + @RequestParam("groupId") String groupId) throws EventException { Account account = keyCloakService.createAccountFromPrincipal(token); - User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); + User user = new User(account.getName(), "", "", ""); Group group = userService.getGroupById(UUID.fromString(groupId)); - validationService.checkIfAdmin(group, user); - validationService.checkTitleAndDescription(title, description, account, groupId); + + validationService.throwIfNoAdmin(group, user); + validationService.checkFields(title, description); + + controllerService.changeMetaData(account, group, title, description); return "redirect:/gruppen2/details/" + groupId; } @@ -181,6 +186,7 @@ public class WebController { Account account = keyCloakService.createAccountFromPrincipal(token); List groups = new ArrayList<>(); groups = validationService.checkSearch(search, groups, account); + model.addAttribute("account", account); model.addAttribute("gruppen", groups); return "search"; @@ -189,17 +195,20 @@ public class WebController { @RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"}) @GetMapping("/details/{id}") public String showGroupDetails(KeycloakAuthenticationToken token, Model model, HttpServletRequest request, @PathVariable("id") String groupId) throws EventException { - model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token)); - Group group = userService.getGroupById(UUID.fromString(groupId)); Account account = keyCloakService.createAccountFromPrincipal(token); User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); UUID parentId = group.getParent(); + String actualURL = request.getRequestURL().toString(); + String serverURL = actualURL.substring(0, actualURL.indexOf("gruppen2/")); - validationService.checkGroup(group.getTitle()); - Group parent = validationService.checkParent(parentId); + validationService.throwIfGroupNotExisting(group.getTitle()); + Group parent = controllerService.getParent(parentId); + + model.addAttribute("account", account); if (!validationService.checkIfUserInGroup(group, user)) { + validationService.throwIfNoAccessToPrivate(group, user); model.addAttribute("group", group); model.addAttribute("parentId", parentId); model.addAttribute("parent", parent); @@ -213,10 +222,9 @@ public class WebController { model.addAttribute("user", user); model.addAttribute("admin", Role.ADMIN); - String actualURL = request.getRequestURL().toString(); - String serverURL = actualURL.substring(0, actualURL.indexOf("gruppen2/")); - model.addAttribute("link", serverURL + "gruppen2/acceptinvite/" + groupId); - + if (validationService.checkIfAdmin(group, user)) { + model.addAttribute("link", serverURL + "gruppen2/acceptinvite/" + groupId); + } return "detailsMember"; } @@ -228,7 +236,7 @@ public class WebController { Account account = keyCloakService.createAccountFromPrincipal(token); User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); Group group = userService.getGroupById(UUID.fromString(groupId)); - validationService.checkIfUserInGroupJoin(group, user); + validationService.throwIfUserAlreadyInGroup(group, user); validationService.checkIfGroupFull(group); controllerService.addUser(account, UUID.fromString(groupId)); return "redirect:/gruppen2/"; @@ -244,7 +252,7 @@ public class WebController { validationService.checkIfGroupFull(group); UUID parentId = group.getParent(); - Group parent = validationService.checkParent(parentId); + Group parent = controllerService.getParent(parentId); model.addAttribute("group", group); model.addAttribute("parentId", parentId); @@ -259,7 +267,7 @@ public class WebController { Model model, @PathVariable String groupId) throws EventException { model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token)); Group group = userService.getGroupById(UUID.fromString(groupId)); - validationService.checkGroup(group.getTitle()); + validationService.throwIfGroupNotExisting(group.getTitle()); model.addAttribute("group", group); return "redirect:/gruppen2/detailsSearch?id=" + group.getId(); } @@ -283,7 +291,7 @@ public class WebController { Account account = keyCloakService.createAccountFromPrincipal(token); User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); Group group = userService.getGroupById(UUID.fromString(groupId)); - validationService.checkIfAdmin(group, user); + validationService.throwIfNoAdmin(group, user); controllerService.deleteGroupEvent(user.getId(), UUID.fromString(groupId)); return "redirect:/gruppen2/"; } @@ -296,7 +304,7 @@ public class WebController { Account account = keyCloakService.createAccountFromPrincipal(token); Group group = userService.getGroupById(UUID.fromString(groupId)); User user = new User(account.getName(), "", "", ""); - validationService.checkIfAdmin(group, user); + validationService.throwIfNoAdmin(group, user); model.addAttribute("account", account); model.addAttribute("members", group.getMembers()); model.addAttribute("group", group); diff --git a/src/main/java/mops/gruppen2/domain/exception/BadParameterException.java b/src/main/java/mops/gruppen2/domain/exception/BadParameterException.java index e933ba6..a6b8d6e 100644 --- a/src/main/java/mops/gruppen2/domain/exception/BadParameterException.java +++ b/src/main/java/mops/gruppen2/domain/exception/BadParameterException.java @@ -5,6 +5,6 @@ import org.springframework.http.HttpStatus; public class BadParameterException extends EventException { public BadParameterException(String info) { - super(HttpStatus.INTERNAL_SERVER_ERROR, "Fehlerhafter Parameter angegeben!", info); + super(HttpStatus.BAD_REQUEST, "Fehlerhafter Parameter angegeben!", info); } } diff --git a/src/main/java/mops/gruppen2/domain/exception/NoValueException.java b/src/main/java/mops/gruppen2/domain/exception/NoValueException.java index f4e24fa..3fd91dd 100644 --- a/src/main/java/mops/gruppen2/domain/exception/NoValueException.java +++ b/src/main/java/mops/gruppen2/domain/exception/NoValueException.java @@ -5,6 +5,6 @@ import org.springframework.http.HttpStatus; public class NoValueException extends EventException { public NoValueException(String info) { - super(HttpStatus.NO_CONTENT, "Eine Information fehlt.", info); + super(HttpStatus.BAD_REQUEST, "Eine Information fehlt.", info); } } diff --git a/src/main/java/mops/gruppen2/service/ControllerService.java b/src/main/java/mops/gruppen2/service/ControllerService.java index ff17b07..362e35d 100644 --- a/src/main/java/mops/gruppen2/service/ControllerService.java +++ b/src/main/java/mops/gruppen2/service/ControllerService.java @@ -108,6 +108,24 @@ public class ControllerService { addUserList(newUserList, groupUUID); } + public void changeMetaData(Account account, Group group, String title, String description) { + if (!title.equals(group.getTitle())){ + updateTitle(account, group.getId(), title); + } + + if (!description.equals(group.getDescription())) { + updateDescription(account, group.getId(), description); + } + } + + public Group getParent(UUID parentId) { + Group parent = new Group(); + if (!idIsEmpty(parentId)) { + parent = userService.getGroupById(parentId); + } + return parent; + } + private void removeOldUsersFromNewUsers(List oldUsers, List newUsers) { for (User oldUser : oldUsers) { newUsers.remove(oldUser); diff --git a/src/main/java/mops/gruppen2/service/ValidationService.java b/src/main/java/mops/gruppen2/service/ValidationService.java index 05b6ea8..6de3fa4 100644 --- a/src/main/java/mops/gruppen2/service/ValidationService.java +++ b/src/main/java/mops/gruppen2/service/ValidationService.java @@ -9,14 +9,10 @@ import mops.gruppen2.domain.exception.GroupFullException; import mops.gruppen2.domain.exception.GroupNotFoundException; import mops.gruppen2.domain.exception.NoAccessException; import mops.gruppen2.domain.exception.NoAdminAfterActionException; -import mops.gruppen2.domain.exception.NoValueException; import mops.gruppen2.domain.exception.UserAlreadyExistsException; -import mops.gruppen2.domain.exception.WrongFileException; import mops.gruppen2.security.Account; import org.springframework.stereotype.Service; -import org.springframework.web.multipart.MultipartFile; -import java.io.IOException; import java.util.List; import java.util.UUID; @@ -33,14 +29,6 @@ public class ValidationService { this.groupService = groupService; } - public void checkTitleAndDescription(String title, String description, Account account, String groupId) { - if (title == null || description == null) { - throw new NoValueException("Titel und Beschreibung müssen ausgefüllt werden"); - } - controllerService.updateTitle(account, UUID.fromString(groupId), title); - controllerService.updateDescription(account, UUID.fromString(groupId), description); - } - public List checkSearch(String search, List groups, Account account) { if (search != null) { groups = groupService.findGroupWith(search, account); @@ -48,30 +36,24 @@ public class ValidationService { return groups; } - public void checkGroup(String title) { + public void throwIfGroupNotExisting(String title) { if (title == null) { throw new GroupNotFoundException("@details"); } } - public boolean checkIfUserInGroup(Group group, User user) { - if (!group.getMembers().contains(user) && group.getVisibility() == Visibility.PRIVATE) { + public void throwIfNoAccessToPrivate(Group group, User user) { + if (!checkIfUserInGroup(group, user) && group.getVisibility() == Visibility.PRIVATE) { throw new NoAccessException(""); - } else { - return group.getMembers().contains(user); } } - public Group checkParent(UUID parentId) { - Group parent = new Group(); - if (!controllerService.idIsEmpty(parentId)) { - parent = userService.getGroupById(parentId); - } - return parent; + public boolean checkIfUserInGroup(Group group, User user) { + return group.getMembers().contains(user); } - public void checkIfUserInGroupJoin(Group group, User user) { - if (group.getMembers().contains(user)) { + public void throwIfUserAlreadyInGroup(Group group, User user) { + if (checkIfUserInGroup(group, user)) { throw new UserAlreadyExistsException("@details"); } } @@ -89,13 +71,20 @@ public class ValidationService { } } - public void checkIfAdmin(Group group, User user) { - checkIfUserInGroup(group, user); + public void throwIfNoAdmin(Group group, User user) { + throwIfNoAccessToPrivate(group, user); if (group.getRoles().get(user.getId()) != Role.ADMIN) { throw new NoAccessException(""); } } + public boolean checkIfAdmin(Group group, User user) { + if (checkIfUserInGroup(group, user)) { + return group.getRoles().get(user.getId()) == Role.ADMIN; + } + return false; + } + public boolean checkIfDemotingSelf(String userId, String groupId, Account account) { if (userId.equals(account.getName())) { if (controllerService.passIfLastAdmin(account, UUID.fromString(groupId))) { @@ -115,7 +104,7 @@ public class ValidationService { * @param title Der Titel der Gruppe * @param userMaximum Das user Limit der Gruppe */ - public void checkFields(String description, String title, Long userMaximum, Boolean maxInfiniteUsers) { + public void checkFields(String title, String description, Long userMaximum, Boolean maxInfiniteUsers) { if (description == null || description.trim().length() == 0) { throw new BadParameterException("Die Beschreibung wurde nicht korrekt angegeben"); } @@ -135,6 +124,16 @@ public class ValidationService { } } + public void checkFields(String title, String description) { + if (description == null || description.trim().length() == 0) { + throw new BadParameterException("Die Beschreibung wurde nicht korrekt angegeben"); + } + + if (title == null || title.trim().length() == 0) { + throw new BadParameterException("Der Titel wurde nicht korrekt angegeben"); + } + } + public void checkIfNewMaximumIsValid(Long newUserMaximum, String groupId) { Group group = userService.getGroupById(UUID.fromString(groupId)); if (newUserMaximum == null) { diff --git a/src/main/resources/templates/detailsMember.html b/src/main/resources/templates/detailsMember.html index 664cfc7..47856cf 100644 --- a/src/main/resources/templates/detailsMember.html +++ b/src/main/resources/templates/detailsMember.html @@ -57,7 +57,7 @@ th:text="${parent.getTitle()}">Parent
+ th:if="${roles.get(user.getId()) == admin}">
Einladungslink: From 3fc4d82dbe1e64e1196c8afb94662e5e713679c0 Mon Sep 17 00:00:00 2001 From: Lukas Ettel <34522828+LukasEttel@users.noreply.github.com> Date: Thu, 26 Mar 2020 16:57:46 +0100 Subject: [PATCH 3/8] Futher further refactor Co-authored-by: AndiBuls <49692239+andibuls@users.noreply.github.com> Co-authored-by: XXNitram <17749825+xxnitram@users.noreply.github.com> --- .../gruppen2/controller/WebController.java | 30 ++++++++++++++----- .../gruppen2/service/ValidationService.java | 2 +- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/main/java/mops/gruppen2/controller/WebController.java b/src/main/java/mops/gruppen2/controller/WebController.java index 77b2cd3..8edeec0 100644 --- a/src/main/java/mops/gruppen2/controller/WebController.java +++ b/src/main/java/mops/gruppen2/controller/WebController.java @@ -201,11 +201,10 @@ public class WebController { UUID parentId = group.getParent(); String actualURL = request.getRequestURL().toString(); String serverURL = actualURL.substring(0, actualURL.indexOf("gruppen2/")); + Group parent = controllerService.getParent(parentId); validationService.throwIfGroupNotExisting(group.getTitle()); - Group parent = controllerService.getParent(parentId); - model.addAttribute("account", account); if (!validationService.checkIfUserInGroup(group, user)) { validationService.throwIfNoAccessToPrivate(group, user); @@ -232,13 +231,16 @@ public class WebController { @PostMapping("/detailsBeitreten") public String joinGroup(KeycloakAuthenticationToken token, Model model, @RequestParam("id") String groupId) throws EventException { - model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token)); Account account = keyCloakService.createAccountFromPrincipal(token); User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); Group group = userService.getGroupById(UUID.fromString(groupId)); + validationService.throwIfUserAlreadyInGroup(group, user); - validationService.checkIfGroupFull(group); + validationService.throwIfGroupFull(group); + controllerService.addUser(account, UUID.fromString(groupId)); + + model.addAttribute("account", account); return "redirect:/gruppen2/"; } @@ -247,12 +249,16 @@ public class WebController { public String showGroupDetailsNoMember(KeycloakAuthenticationToken token, Model model, @RequestParam("id") String groupId) throws EventException { - model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token)); + Account account = keyCloakService.createAccountFromPrincipal(token); Group group = userService.getGroupById(UUID.fromString(groupId)); - validationService.checkIfGroupFull(group); - UUID parentId = group.getParent(); Group parent = controllerService.getParent(parentId); + User user = new User(account.getName(), "", "", ""); + + model.addAttribute("account", account); + if (validationService.checkIfUserInGroup(group, user)){ + return "redirect:/gruppen2/details/" + groupId; + } model.addAttribute("group", group); model.addAttribute("parentId", parentId); @@ -265,13 +271,18 @@ public class WebController { @GetMapping("/acceptinvite/{groupId}") public String acceptInvite(KeycloakAuthenticationToken token, Model model, @PathVariable String groupId) throws EventException { - model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token)); + Account account = keyCloakService.createAccountFromPrincipal(token); Group group = userService.getGroupById(UUID.fromString(groupId)); + validationService.throwIfGroupNotExisting(group.getTitle()); + + model.addAttribute("account", account); model.addAttribute("group", group); return "redirect:/gruppen2/detailsSearch?id=" + group.getId(); } + //Bis hier gekommen + @RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"}) @PostMapping("/leaveGroup") public String pLeaveGroup(KeycloakAuthenticationToken token, @@ -318,6 +329,9 @@ public class WebController { @RequestParam("group_id") String groupId, @RequestParam("user_id") String userId) throws EventException { Account account = keyCloakService.createAccountFromPrincipal(token); + Group group = userService.getGroupById(UUID.fromString(groupId)); + User user = new User(account.getName(), "", "", ""); + validationService.throwIfNoAdmin(group, user); if (validationService.checkIfDemotingSelf(userId, groupId, account)) { return "redirect:/gruppen2/details/" + groupId; } diff --git a/src/main/java/mops/gruppen2/service/ValidationService.java b/src/main/java/mops/gruppen2/service/ValidationService.java index 6de3fa4..6d67175 100644 --- a/src/main/java/mops/gruppen2/service/ValidationService.java +++ b/src/main/java/mops/gruppen2/service/ValidationService.java @@ -58,7 +58,7 @@ public class ValidationService { } } - public void checkIfGroupFull(Group group) { + public void throwIfGroupFull(Group group) { if (group.getUserMaximum() < group.getMembers().size() + 1) { throw new GroupFullException("Du kannst der Gruppe daher leider nicht beitreten."); } From 289299f6e81d8962be64ab6d43c5bebabccfc952 Mon Sep 17 00:00:00 2001 From: XXNitram Date: Fri, 27 Mar 2020 15:02:33 +0100 Subject: [PATCH 4/8] Further further further refactor Co-Authored-By: andibuls Co-Authored-By: Lukas Ettel --- .../gruppen2/controller/WebController.java | 47 +++++++--- .../gruppen2/service/ControllerService.java | 94 +++++++++---------- .../gruppen2/service/ValidationService.java | 51 +++++----- .../service/ControllerServiceTest.java | 2 +- 4 files changed, 107 insertions(+), 87 deletions(-) diff --git a/src/main/java/mops/gruppen2/controller/WebController.java b/src/main/java/mops/gruppen2/controller/WebController.java index 8edeec0..8face14 100644 --- a/src/main/java/mops/gruppen2/controller/WebController.java +++ b/src/main/java/mops/gruppen2/controller/WebController.java @@ -281,17 +281,15 @@ public class WebController { return "redirect:/gruppen2/detailsSearch?id=" + group.getId(); } - //Bis hier gekommen - @RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"}) @PostMapping("/leaveGroup") public String pLeaveGroup(KeycloakAuthenticationToken token, @RequestParam("group_id") String groupId) throws EventException { Account account = keyCloakService.createAccountFromPrincipal(token); - User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); - controllerService.passIfLastAdmin(account, UUID.fromString(groupId)); - controllerService.deleteUser(user.getId(), UUID.fromString(groupId)); - validationService.checkIfGroupEmpty(groupId, user); + User user = new User(account.getName(), "", "", ""); + Group group = userService.getGroupById(UUID.fromString(groupId)); + + controllerService.deleteUser(account, user, group); return "redirect:/gruppen2/"; } @@ -302,7 +300,9 @@ public class WebController { Account account = keyCloakService.createAccountFromPrincipal(token); User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); Group group = userService.getGroupById(UUID.fromString(groupId)); + validationService.throwIfNoAdmin(group, user); + controllerService.deleteGroupEvent(user.getId(), UUID.fromString(groupId)); return "redirect:/gruppen2/"; } @@ -315,7 +315,9 @@ public class WebController { Account account = keyCloakService.createAccountFromPrincipal(token); Group group = userService.getGroupById(UUID.fromString(groupId)); User user = new User(account.getName(), "", "", ""); + validationService.throwIfNoAdmin(group, user); + model.addAttribute("account", account); model.addAttribute("members", group.getMembers()); model.addAttribute("group", group); @@ -330,9 +332,15 @@ public class WebController { @RequestParam("user_id") String userId) throws EventException { Account account = keyCloakService.createAccountFromPrincipal(token); Group group = userService.getGroupById(UUID.fromString(groupId)); - User user = new User(account.getName(), "", "", ""); - validationService.throwIfNoAdmin(group, user); - if (validationService.checkIfDemotingSelf(userId, groupId, account)) { + User principle = new User(account.getName(), "", "", ""); + User user = new User(userId, "", "", ""); + + validationService.throwIfNoAdmin(group, principle); + + controllerService.changeRole(account, user, group); + + group = userService.getGroupById(UUID.fromString(groupId)); + if (!validationService.checkIfAdmin(group, principle)) { return "redirect:/gruppen2/details/" + groupId; } return "redirect:/gruppen2/details/members/" + groupId; @@ -344,7 +352,10 @@ public class WebController { @RequestParam("group_id") String groupId, KeycloakAuthenticationToken token) { Account account = keyCloakService.createAccountFromPrincipal(token); - validationService.checkIfNewMaximumIsValid(maximum, groupId); + Group group = userService.getGroupById(UUID.fromString(groupId)); + + validationService.throwIfNewMaximumIsValid(maximum, group); + controllerService.updateMaxUser(account, UUID.fromString(groupId), maximum); return "redirect:/gruppen2/details/members/" + groupId; } @@ -352,10 +363,20 @@ public class WebController { @RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"}) @PostMapping("/details/members/deleteUser") public String deleteUser(@RequestParam("group_id") String groupId, - @RequestParam("user_id") String userId) throws EventException { + @RequestParam("user_id") String userId, + KeycloakAuthenticationToken token) throws EventException { + Account account = keyCloakService.createAccountFromPrincipal(token); + User principle = new User(account.getName(), "", "", ""); User user = new User(userId, "", "", ""); - controllerService.deleteUser(userId, UUID.fromString(groupId)); - validationService.checkIfGroupEmpty(groupId, user); + Group group = userService.getGroupById(UUID.fromString(groupId)); + + validationService.throwIfNoAdmin(group, principle); + + controllerService.deleteUser(account, user, group); + + if (!validationService.checkIfUserInGroup(group, principle)) { + return "redirect:/gruppen2/"; + } return "redirect:/gruppen2/details/members/" + groupId; } diff --git a/src/main/java/mops/gruppen2/service/ControllerService.java b/src/main/java/mops/gruppen2/service/ControllerService.java index 362e35d..7732845 100644 --- a/src/main/java/mops/gruppen2/service/ControllerService.java +++ b/src/main/java/mops/gruppen2/service/ControllerService.java @@ -15,7 +15,6 @@ import mops.gruppen2.domain.event.UpdateGroupTitleEvent; import mops.gruppen2.domain.event.UpdateRoleEvent; import mops.gruppen2.domain.event.UpdateUserMaxEvent; import mops.gruppen2.domain.exception.EventException; -import mops.gruppen2.domain.exception.UserNotFoundException; import mops.gruppen2.domain.exception.WrongFileException; import mops.gruppen2.security.Account; import org.springframework.stereotype.Service; @@ -25,7 +24,6 @@ import java.io.CharConversionException; import java.io.IOException; import java.util.ArrayList; import java.util.List; -import java.util.Map; import java.util.UUID; import java.util.logging.Logger; import java.util.stream.Collectors; @@ -38,11 +36,13 @@ public class ControllerService { private final EventService eventService; private final UserService userService; + private final ValidationService validationService; private final Logger logger; - public ControllerService(EventService eventService, UserService userService) { + public ControllerService(EventService eventService, UserService userService, ValidationService validationService) { this.eventService = eventService; this.userService = userService; + this.validationService = validationService; this.logger = Logger.getLogger("controllerServiceLogger"); } @@ -66,10 +66,12 @@ public class ControllerService { CreateGroupEvent createGroupEvent = new CreateGroupEvent(groupId, account.getName(), parent, groupType, groupVisibility, userMaximum); eventService.saveEvent(createGroupEvent); + User user = new User(account.getName(), "", "", ""); + addUser(account, groupId); updateTitle(account, groupId, title); updateDescription(account, groupId, description); - updateRole(account.getName(), groupId); + updateRole(user, groupId); return groupId; } @@ -214,41 +216,32 @@ public class ControllerService { eventService.saveEvent(updateUserMaxEvent); } - public void updateRole(String userId, UUID groupId) throws EventException { + public void updateRole(User user, UUID groupId) throws EventException { UpdateRoleEvent updateRoleEvent; Group group = userService.getGroupById(groupId); - User user = null; - for (User member : group.getMembers()) { - if (member.getId().equals(userId)) { - user = member; - } - } - - if (user == null) { - throw new UserNotFoundException(this.getClass().toString()); - } + validationService.throwIfNotInGroup(group, user); if (group.getRoles().get(user.getId()) == ADMIN) { - updateRoleEvent = new UpdateRoleEvent(groupId, user.getId(), Role.MEMBER); + updateRoleEvent = new UpdateRoleEvent(group.getId(), user.getId(), Role.MEMBER); } else { - updateRoleEvent = new UpdateRoleEvent(groupId, user.getId(), ADMIN); + updateRoleEvent = new UpdateRoleEvent(group.getId(), user.getId(), ADMIN); } eventService.saveEvent(updateRoleEvent); } - public void deleteUser(String userId, UUID groupId) throws EventException { - Group group = userService.getGroupById(groupId); - User user = null; - for (User member : group.getMembers()) { - if (member.getId().equals(userId)) { - user = member; - } - } + public void deleteUser(Account account, User user, Group group) throws EventException { + changeRoleIfLastAdmin(account, group); - if (user == null) { - throw new UserNotFoundException(this.getClass().toString()); - } + validationService.throwIfNotInGroup(group, user); + deleteUserEvent(user, group.getId()); + + if (validationService.checkIfGroupEmpty(group.getId())) { + deleteGroupEvent(user.getId(), group.getId()); + } + } + + private void deleteUserEvent(User user, UUID groupId) { DeleteUserEvent deleteUserEvent = new DeleteUserEvent(groupId, user.getId()); eventService.saveEvent(deleteUserEvent); } @@ -258,36 +251,39 @@ public class ControllerService { eventService.saveEvent(deleteGroupEvent); } - public boolean passIfLastAdmin(Account account, UUID groupId) { - Group group = userService.getGroupById(groupId); + private void changeRoleIfLastAdmin(Account account, Group group) { if (group.getMembers().size() <= 1) { - return true; + return; } - - if (isLastAdmin(account, group)) { - String newAdminId = getVeteranMember(account, group); - updateRole(newAdminId, groupId); - } - return false; + promoteVeteranMember(account, group); } - private boolean isLastAdmin(Account account, Group group) { - for (Map.Entry entry : group.getRoles().entrySet()) { - if (entry.getValue() == ADMIN) { - if (!(entry.getKey().equals(account.getName()))) { - return false; - } + private void promoteVeteranMember(Account account, Group group) { + if (validationService.checkIfLastAdmin(account, group)) { + User newAdmin = getVeteranMember(account, group); + updateRole(newAdmin, group.getId()); + } + } + + public void changeRole(Account account, User user, Group group) { + if (user.getId().equals(account.getName())) { + if (group.getMembers().size() <= 1) { + validationService.throwIfLastAdmin(account, group); } + promoteVeteranMember(account, group); } - return true; + updateRole(user, group.getId()); } - private String getVeteranMember(Account account, Group group) { - List mitglieder = group.getMembers(); - if (mitglieder.get(0).getId().equals(account.getName())) { - return mitglieder.get(1).getId(); + private User getVeteranMember(Account account, Group group) { + List members = group.getMembers(); + String newAdminId; + if (members.get(0).getId().equals(account.getName())) { + newAdminId = members.get(1).getId(); + } else { + newAdminId = members.get(0).getId(); } - return mitglieder.get(0).getId(); + return new User(newAdminId, "", "", ""); } public UUID getUUID(String id) { diff --git a/src/main/java/mops/gruppen2/service/ValidationService.java b/src/main/java/mops/gruppen2/service/ValidationService.java index 6d67175..745da93 100644 --- a/src/main/java/mops/gruppen2/service/ValidationService.java +++ b/src/main/java/mops/gruppen2/service/ValidationService.java @@ -4,27 +4,23 @@ import mops.gruppen2.domain.Group; import mops.gruppen2.domain.Role; import mops.gruppen2.domain.User; import mops.gruppen2.domain.Visibility; -import mops.gruppen2.domain.exception.BadParameterException; -import mops.gruppen2.domain.exception.GroupFullException; -import mops.gruppen2.domain.exception.GroupNotFoundException; -import mops.gruppen2.domain.exception.NoAccessException; -import mops.gruppen2.domain.exception.NoAdminAfterActionException; -import mops.gruppen2.domain.exception.UserAlreadyExistsException; +import mops.gruppen2.domain.exception.*; import mops.gruppen2.security.Account; import org.springframework.stereotype.Service; import java.util.List; +import java.util.Map; import java.util.UUID; +import static mops.gruppen2.domain.Role.ADMIN; + @Service public class ValidationService { - private final ControllerService controllerService; private final UserService userService; private final GroupService groupService; - public ValidationService(ControllerService controllerService, UserService userService, GroupService groupService) { - this.controllerService = controllerService; + public ValidationService(UserService userService, GroupService groupService) { this.userService = userService; this.groupService = groupService; } @@ -58,17 +54,20 @@ public class ValidationService { } } + public void throwIfNotInGroup(Group group, User user) { + if (!checkIfUserInGroup(group, user)) { + throw new UserNotFoundException(this.getClass().toString()); + } + } + public void throwIfGroupFull(Group group) { if (group.getUserMaximum() < group.getMembers().size() + 1) { throw new GroupFullException("Du kannst der Gruppe daher leider nicht beitreten."); } } - - public void checkIfGroupEmpty(String groupId, User user) { - if (userService.getGroupById(UUID.fromString(groupId)).getMembers().isEmpty()) { - controllerService.deleteGroupEvent(user.getId(), UUID.fromString(groupId)); - } + public boolean checkIfGroupEmpty(UUID groupId) { + return userService.getGroupById(groupId).getMembers().isEmpty(); } public void throwIfNoAdmin(Group group, User user) { @@ -85,16 +84,21 @@ public class ValidationService { return false; } - public boolean checkIfDemotingSelf(String userId, String groupId, Account account) { - if (userId.equals(account.getName())) { - if (controllerService.passIfLastAdmin(account, UUID.fromString(groupId))) { - throw new NoAdminAfterActionException("Du Otto bist letzter Admin"); + public boolean checkIfLastAdmin(Account account, Group group) { + for (Map.Entry entry : group.getRoles().entrySet()) { + if (entry.getValue() == ADMIN) { + if (!(entry.getKey().equals(account.getName()))) { + return false; + } } - controllerService.updateRole(userId, UUID.fromString(groupId)); - return true; } - controllerService.updateRole(userId, UUID.fromString(groupId)); - return false; + return true; + } + + public void throwIfLastAdmin(Account account, Group group) { + if (checkIfLastAdmin(account, group)) { + throw new NoAdminAfterActionException("Du Otto bist letzter Admin!"); + } } /** @@ -134,8 +138,7 @@ public class ValidationService { } } - public void checkIfNewMaximumIsValid(Long newUserMaximum, String groupId) { - Group group = userService.getGroupById(UUID.fromString(groupId)); + public void throwIfNewMaximumIsValid(Long newUserMaximum, Group group) { if (newUserMaximum == null) { throw new BadParameterException("Es wurde keine neue maximale Teilnehmeranzahl angegeben!"); } diff --git a/src/test/java/mops/gruppen2/service/ControllerServiceTest.java b/src/test/java/mops/gruppen2/service/ControllerServiceTest.java index 205edd9..d115d45 100644 --- a/src/test/java/mops/gruppen2/service/ControllerServiceTest.java +++ b/src/test/java/mops/gruppen2/service/ControllerServiceTest.java @@ -30,7 +30,7 @@ class ControllerServiceTest { eventService = new EventService(jsonService, eventRepository); groupService = new GroupService(eventService, eventRepository); userService = new UserService(groupService, eventService); - controllerService = new ControllerService(eventService, userService); + controllerService = new ControllerService(eventService, userService, validationService); Set roles = new HashSet<>(); roles.add("l"); account = new Account("ich", "ich@hhu.de", "l", "ichdude", "jap", roles); From 2e3f705544b8b2020de5fac7c84b677a56df02b5 Mon Sep 17 00:00:00 2001 From: XXNitram Date: Fri, 27 Mar 2020 15:26:32 +0100 Subject: [PATCH 5/8] Fix merge confilcts Co-Authored-By: andibuls Co-Authored-By: Lukas Ettel --- src/main/java/mops/gruppen2/controller/WebController.java | 4 +++- src/main/java/mops/gruppen2/service/ControllerService.java | 2 +- .../java/mops/gruppen2/service/ControllerServiceTest.java | 4 +++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/mops/gruppen2/controller/WebController.java b/src/main/java/mops/gruppen2/controller/WebController.java index 264785a..b55d56d 100644 --- a/src/main/java/mops/gruppen2/controller/WebController.java +++ b/src/main/java/mops/gruppen2/controller/WebController.java @@ -287,9 +287,11 @@ public class WebController { public String acceptInvite(KeycloakAuthenticationToken token, Model model, @PathVariable("link") String link) throws EventException { - model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token)); Group group = userService.getGroupById(inviteService.getGroupIdFromLink(link)); + validationService.throwIfGroupNotExisting(group.getTitle()); + + model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token)); model.addAttribute("group", group); //controllerService.addUser(keyCloakService.createAccountFromPrincipal(token), group.getId()); diff --git a/src/main/java/mops/gruppen2/service/ControllerService.java b/src/main/java/mops/gruppen2/service/ControllerService.java index 5da4357..baba745 100644 --- a/src/main/java/mops/gruppen2/service/ControllerService.java +++ b/src/main/java/mops/gruppen2/service/ControllerService.java @@ -42,7 +42,7 @@ public class ControllerService { private final InviteService inviteService; private final Logger logger; - public ControllerService(EventService eventService, UserService userService, ValidationService validationService) { + public ControllerService(EventService eventService, UserService userService, ValidationService validationService, InviteService inviteService) { this.eventService = eventService; this.userService = userService; this.validationService = validationService; diff --git a/src/test/java/mops/gruppen2/service/ControllerServiceTest.java b/src/test/java/mops/gruppen2/service/ControllerServiceTest.java index 189495c..8b87d15 100644 --- a/src/test/java/mops/gruppen2/service/ControllerServiceTest.java +++ b/src/test/java/mops/gruppen2/service/ControllerServiceTest.java @@ -19,6 +19,7 @@ class ControllerServiceTest { ControllerService controllerService; EventService eventService; UserService userService; + ValidationService validationService; EventRepository eventRepository; GroupService groupService; JsonService jsonService; @@ -33,9 +34,10 @@ class ControllerServiceTest { eventService = new EventService(jsonService, eventRepository); groupService = new GroupService(eventService, eventRepository); userService = new UserService(groupService, eventService); + validationService = new ValidationService(userService, groupService); inviteRepository = mock(InviteRepository.class); inviteService = new InviteService(inviteRepository); - controllerService = new ControllerService(eventService, userService, inviteService); + controllerService = new ControllerService(eventService, userService, validationService, inviteService); Set roles = new HashSet<>(); roles.add("l"); account = new Account("ich", "ich@hhu.de", "l", "ichdude", "jap", roles); From 59e9ff4d4d7b9b5c9ef2f0ea7105c2c5540ad5f3 Mon Sep 17 00:00:00 2001 From: XXNitram Date: Fri, 27 Mar 2020 15:55:23 +0100 Subject: [PATCH 6/8] Merge with master Co-Authored-By: andibuls Co-Authored-By: Lukas Ettel --- .../gruppen2/controller/WebController.java | 2 +- .../gruppen2/service/ControllerService.java | 2 +- .../service/ControllerServiceTest.java | 33 ++++++++++++------- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/main/java/mops/gruppen2/controller/WebController.java b/src/main/java/mops/gruppen2/controller/WebController.java index f1ececa..90632e6 100644 --- a/src/main/java/mops/gruppen2/controller/WebController.java +++ b/src/main/java/mops/gruppen2/controller/WebController.java @@ -311,7 +311,7 @@ public class WebController { User user = new User(acc.getName(), acc.getGivenname(), acc.getFamilyname(), acc.getEmail()); - if (!validationService.checkIfUserInGroupWithoutNoAccessAcception(userService.getGroupById(UUID.fromString(groupId)), user)) { + if (!validationService.checkIfUserInGroup(userService.getGroupById(UUID.fromString(groupId)), user)) { controllerService.addUser(keyCloakService.createAccountFromPrincipal(token), UUID.fromString(groupId)); } diff --git a/src/main/java/mops/gruppen2/service/ControllerService.java b/src/main/java/mops/gruppen2/service/ControllerService.java index f99730e..8a57a13 100644 --- a/src/main/java/mops/gruppen2/service/ControllerService.java +++ b/src/main/java/mops/gruppen2/service/ControllerService.java @@ -259,7 +259,7 @@ public class ControllerService { eventService.saveEvent(deleteGroupEvent); } - private void changeRoleIfLastAdmin(Account account, Group group) { + public void changeRoleIfLastAdmin(Account account, Group group) { if (group.getMembers().size() <= 1) { return; } diff --git a/src/test/java/mops/gruppen2/service/ControllerServiceTest.java b/src/test/java/mops/gruppen2/service/ControllerServiceTest.java index cf15ac0..5e4545a 100644 --- a/src/test/java/mops/gruppen2/service/ControllerServiceTest.java +++ b/src/test/java/mops/gruppen2/service/ControllerServiceTest.java @@ -222,7 +222,8 @@ class ControllerServiceTest { controllerService.createGroup(account, "test", "hi", true, true, true, null, null); List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); controllerService.addUser(account2, groups.get(0).getId()); - controllerService.deleteUser(account.getName(), groups.get(0).getId()); + User user = new User(account.getName(), "", "", ""); + controllerService.deleteUser(account, user, groups.get(0)); assertTrue(userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())).isEmpty()); } @@ -231,7 +232,8 @@ class ControllerServiceTest { controllerService.createGroup(account, "test", "hi", null, null, true, null, null); List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); controllerService.addUser(account2, groups.get(0).getId()); - controllerService.updateRole(account.getName(), groups.get(0).getId()); + User user = new User(account.getName(), "", "", ""); + controllerService.updateRole(user, groups.get(0).getId()); groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); assertEquals(Role.MEMBER, groups.get(0).getRoles().get(account.getName())); } @@ -241,7 +243,8 @@ class ControllerServiceTest { controllerService.createGroup(account, "test", "hi", null, null, true, null, null); List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); controllerService.addUser(account2, groups.get(0).getId()); - controllerService.updateRole(account2.getName(), groups.get(0).getId()); + User user = new User(account.getName(), "", "", ""); + controllerService.updateRole(user, groups.get(0).getId()); groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); assertEquals(Role.ADMIN, groups.get(0).getRoles().get(account2.getName())); } @@ -250,7 +253,8 @@ class ControllerServiceTest { public void updateRoleNonUserTest() { controllerService.createGroup(account, "test", "hi", null, null, true, null, null); List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); - Throwable exception = assertThrows(UserNotFoundException.class, () -> controllerService.updateRole(account2.getName(), groups.get(0).getId())); + User user = new User(account.getName(), "", "", ""); + Throwable exception = assertThrows(UserNotFoundException.class, () -> controllerService.updateRole(user, groups.get(0).getId())); assertEquals("404 NOT_FOUND \"Der User wurde nicht gefunden. (class mops.gruppen2.service.ControllerService)\"", exception.getMessage()); } @@ -258,7 +262,8 @@ class ControllerServiceTest { public void deleteNonUserTest() { controllerService.createGroup(account, "test", "hi", true, null, true, null, null); List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); - Throwable exception = assertThrows(UserNotFoundException.class, () -> controllerService.deleteUser(account2.getName(), groups.get(0).getId())); + User user = new User(account.getName(), "", "", ""); + Throwable exception = assertThrows(UserNotFoundException.class, () -> controllerService.deleteUser(account, user, groups.get(0))); assertEquals("404 NOT_FOUND \"Der User wurde nicht gefunden. (class mops.gruppen2.service.ControllerService)\"", exception.getMessage()); } @@ -272,8 +277,9 @@ class ControllerServiceTest { controllerService.createGroup(account, "test", "hi", null, null, true, null, null); List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); controllerService.addUser(account2, groups.get(0).getId()); - controllerService.passIfLastAdmin(account, groups.get(0).getId()); - controllerService.deleteUser(account.getName(), groups.get(0).getId()); + controllerService.changeRoleIfLastAdmin(account, groups.get(0)); + User user = new User(account.getName(), "", "", ""); + controllerService.deleteUser(account, user, groups.get(0)); groups = userService.getUserGroups(new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail())); assertEquals(Role.ADMIN, groups.get(0).getRoles().get(account2.getName())); } @@ -283,10 +289,12 @@ class ControllerServiceTest { controllerService.createGroup(account, "test", "hi", null, null, true, null, null); List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); controllerService.addUser(account2, groups.get(0).getId()); - controllerService.updateRole(account2.getName(), groups.get(0).getId()); + User user2 = new User(account2.getName(), "", "", ""); + controllerService.updateRole(user2, groups.get(0).getId()); controllerService.addUser(account3, groups.get(0).getId()); - controllerService.passIfLastAdmin(account, groups.get(0).getId()); - controllerService.deleteUser(account.getName(), groups.get(0).getId()); + controllerService.changeRoleIfLastAdmin(account, groups.get(0)); + User user = new User(account.getName(), "", "", ""); + controllerService.deleteUser(account, user, groups.get(0)); groups = userService.getUserGroups(new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail())); assertEquals(Role.MEMBER, groups.get(0).getRoles().get(account3.getName())); } @@ -297,8 +305,9 @@ class ControllerServiceTest { List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); controllerService.addUser(account2, groups.get(0).getId()); controllerService.addUser(account3, groups.get(0).getId()); - controllerService.passIfLastAdmin(account, groups.get(0).getId()); - controllerService.deleteUser(account.getName(), groups.get(0).getId()); + controllerService.changeRoleIfLastAdmin(account, groups.get(0)); + User user = new User(account.getName(), "", "", ""); + controllerService.deleteUser(account, user, groups.get(0)); groups = userService.getUserGroups(new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail())); assertEquals(Role.ADMIN, groups.get(0).getRoles().get(account2.getName())); assertEquals(Role.MEMBER, groups.get(0).getRoles().get(account3.getName())); From 82ae081f95909d14037d14ba4041e5ea7c5006e7 Mon Sep 17 00:00:00 2001 From: XXNitram Date: Fri, 27 Mar 2020 16:17:45 +0100 Subject: [PATCH 7/8] Fix tests Co-Authored-By: andibuls Co-Authored-By: Lukas Ettel --- .../gruppen2/service/ControllerServiceTest.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/test/java/mops/gruppen2/service/ControllerServiceTest.java b/src/test/java/mops/gruppen2/service/ControllerServiceTest.java index 5e4545a..62495de 100644 --- a/src/test/java/mops/gruppen2/service/ControllerServiceTest.java +++ b/src/test/java/mops/gruppen2/service/ControllerServiceTest.java @@ -34,8 +34,8 @@ class ControllerServiceTest { ControllerService controllerService; EventService eventService; UserService userService; - @Autowired ValidationService validationService; + @Autowired EventRepository eventRepository; GroupService groupService; @Autowired @@ -243,7 +243,7 @@ class ControllerServiceTest { controllerService.createGroup(account, "test", "hi", null, null, true, null, null); List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); controllerService.addUser(account2, groups.get(0).getId()); - User user = new User(account.getName(), "", "", ""); + User user = new User(account2.getName(), "", "", ""); controllerService.updateRole(user, groups.get(0).getId()); groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); assertEquals(Role.ADMIN, groups.get(0).getRoles().get(account2.getName())); @@ -253,18 +253,18 @@ class ControllerServiceTest { public void updateRoleNonUserTest() { controllerService.createGroup(account, "test", "hi", null, null, true, null, null); List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); - User user = new User(account.getName(), "", "", ""); + User user = new User(account2.getName(), "", "", ""); Throwable exception = assertThrows(UserNotFoundException.class, () -> controllerService.updateRole(user, groups.get(0).getId())); - assertEquals("404 NOT_FOUND \"Der User wurde nicht gefunden. (class mops.gruppen2.service.ControllerService)\"", exception.getMessage()); + assertEquals("404 NOT_FOUND \"Der User wurde nicht gefunden. (class mops.gruppen2.service.ValidationService)\"", exception.getMessage()); } @Test public void deleteNonUserTest() { controllerService.createGroup(account, "test", "hi", true, null, true, null, null); List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); - User user = new User(account.getName(), "", "", ""); + User user = new User(account2.getName(), "", "", ""); Throwable exception = assertThrows(UserNotFoundException.class, () -> controllerService.deleteUser(account, user, groups.get(0))); - assertEquals("404 NOT_FOUND \"Der User wurde nicht gefunden. (class mops.gruppen2.service.ControllerService)\"", exception.getMessage()); + assertEquals("404 NOT_FOUND \"Der User wurde nicht gefunden. (class mops.gruppen2.service.ValidationService)\"", exception.getMessage()); } void testTitleAndDescription(String title, String description) { @@ -305,7 +305,6 @@ class ControllerServiceTest { List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); controllerService.addUser(account2, groups.get(0).getId()); controllerService.addUser(account3, groups.get(0).getId()); - controllerService.changeRoleIfLastAdmin(account, groups.get(0)); User user = new User(account.getName(), "", "", ""); controllerService.deleteUser(account, user, groups.get(0)); groups = userService.getUserGroups(new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail())); From b1069b5049fcbbfeab8f17aa25416526398b2a8d Mon Sep 17 00:00:00 2001 From: XXNitram Date: Fri, 27 Mar 2020 16:30:59 +0100 Subject: [PATCH 8/8] Fix tests and checkstyle Co-Authored-By: andibuls Co-Authored-By: Lukas Ettel Co-Authored-By: Talha Caliskan --- .../service/ControllerServiceTest.java | 12 +++++-- .../gruppen2/service/EventServiceTest.java | 32 ------------------- 2 files changed, 9 insertions(+), 35 deletions(-) diff --git a/src/test/java/mops/gruppen2/service/ControllerServiceTest.java b/src/test/java/mops/gruppen2/service/ControllerServiceTest.java index 62495de..75200e6 100644 --- a/src/test/java/mops/gruppen2/service/ControllerServiceTest.java +++ b/src/test/java/mops/gruppen2/service/ControllerServiceTest.java @@ -23,14 +23,19 @@ import java.util.HashSet; import java.util.List; import java.util.Set; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; @ExtendWith(SpringExtension.class) @SpringBootTest(classes = Gruppen2Application.class) @Transactional @Rollback class ControllerServiceTest { - Account account, account2, account3; + Account account; + Account account2; + Account account3; ControllerService controllerService; EventService eventService; UserService userService; @@ -277,8 +282,8 @@ class ControllerServiceTest { controllerService.createGroup(account, "test", "hi", null, null, true, null, null); List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); controllerService.addUser(account2, groups.get(0).getId()); - controllerService.changeRoleIfLastAdmin(account, groups.get(0)); User user = new User(account.getName(), "", "", ""); + groups = userService.getUserGroups(new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail())); controllerService.deleteUser(account, user, groups.get(0)); groups = userService.getUserGroups(new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail())); assertEquals(Role.ADMIN, groups.get(0).getRoles().get(account2.getName())); @@ -306,6 +311,7 @@ class ControllerServiceTest { controllerService.addUser(account2, groups.get(0).getId()); controllerService.addUser(account3, groups.get(0).getId()); User user = new User(account.getName(), "", "", ""); + groups = userService.getUserGroups(new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail())); controllerService.deleteUser(account, user, groups.get(0)); groups = userService.getUserGroups(new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail())); assertEquals(Role.ADMIN, groups.get(0).getRoles().get(account2.getName())); diff --git a/src/test/java/mops/gruppen2/service/EventServiceTest.java b/src/test/java/mops/gruppen2/service/EventServiceTest.java index 427a35e..609687a 100644 --- a/src/test/java/mops/gruppen2/service/EventServiceTest.java +++ b/src/test/java/mops/gruppen2/service/EventServiceTest.java @@ -77,38 +77,6 @@ class EventServiceTest { assertThat(dto.getEvent_type()).isEqualTo("CreateGroupEvent"); } - @Disabled - @Test - void getNewEvents_createGroupEvents() { - eventService.saveAll(createPrivateGroupEvents(10)); - - assertThat(eventService.getNewEvents(0L)).hasSize(10); - assertThat(eventService.getNewEvents(5L)).hasSize(5); - assertThat(eventService.getNewEvents(10L)).isEmpty(); - } - - @Disabled - @Test - void getNewEvents_mixedEvents() { - eventService.saveAll(createPrivateGroupEvent(uuidFromInt(0)), - updateGroupDescriptionEvent(uuidFromInt(0)), - createPrivateGroupEvent(uuidFromInt(1)), - updateGroupDescriptionEvent(uuidFromInt(1))); - - assertThat(eventService.getNewEvents(0L)).hasSize(4); - assertThat(eventService.getNewEvents(1L)).hasSize(4); - assertThat(eventService.getNewEvents(2L)).hasSize(2); - assertThat(eventService.getNewEvents(3L)).hasSize(2); - } - - @Disabled - @Test - void getMaxEvent_id() { - eventService.saveAll(createPrivateGroupEvents(10)); - - assertThat(eventService.getMaxEvent_id()).isEqualTo(10); - } - @Test void getEventsOfGroup() { eventService.saveAll(addUserEvents(10, uuidFromInt(0)),