1

badpayloadexception + more reordering ugh

This commit is contained in:
Christoph
2020-04-06 01:06:22 +02:00
parent 3d66f58af8
commit 9e158ad6d0
15 changed files with 430 additions and 422 deletions

View File

@ -9,9 +9,7 @@ import mops.gruppen2.domain.event.Event;
import mops.gruppen2.domain.exception.EventException; import mops.gruppen2.domain.exception.EventException;
import mops.gruppen2.service.APIService; import mops.gruppen2.service.APIService;
import mops.gruppen2.service.EventStoreService; import mops.gruppen2.service.EventStoreService;
import mops.gruppen2.service.GroupService;
import mops.gruppen2.service.ProjectionService; import mops.gruppen2.service.ProjectionService;
import mops.gruppen2.service.UserService;
import org.springframework.security.access.annotation.Secured; import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
@ -31,14 +29,10 @@ import java.util.stream.Collectors;
public class APIController { public class APIController {
private final EventStoreService eventStoreService; private final EventStoreService eventStoreService;
private final GroupService groupService;
private final UserService userService;
private final ProjectionService projectionService; private final ProjectionService projectionService;
public APIController(EventStoreService eventStoreService, GroupService groupService, UserService userService, ProjectionService projectionService) { public APIController(EventStoreService eventStoreService, ProjectionService projectionService) {
this.eventStoreService = eventStoreService; this.eventStoreService = eventStoreService;
this.groupService = groupService;
this.userService = userService;
this.projectionService = projectionService; this.projectionService = projectionService;
} }
@ -55,9 +49,9 @@ public class APIController {
@Secured("ROLE_api_user") @Secured("ROLE_api_user")
@ApiOperation("Gibt alle Gruppen zurück, in denen sich ein Teilnehmer befindet") @ApiOperation("Gibt alle Gruppen zurück, in denen sich ein Teilnehmer befindet")
public List<String> getGroupIdsOfUser(@ApiParam("Teilnehmer dessen groupIds zurückgegeben werden sollen") @PathVariable String userId) { public List<String> getGroupIdsOfUser(@ApiParam("Teilnehmer dessen groupIds zurückgegeben werden sollen") @PathVariable String userId) {
return userService.getUserGroups(userId).stream() return projectionService.getUserGroups(userId).stream()
.map(group -> group.getId().toString()) .map(group -> group.getId().toString())
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@GetMapping("/getGroup/{groupId}") @GetMapping("/getGroup/{groupId}")

View File

@ -64,7 +64,7 @@ public class GroupCreationController {
@RequestParam(value = "file", required = false) MultipartFile file) { @RequestParam(value = "file", required = false) MultipartFile file) {
Account account = KeyCloakService.createAccountFromPrincipal(token); Account account = KeyCloakService.createAccountFromPrincipal(token);
UUID parentUUID = controllerService.getUUID(parent); UUID parentUUID = GroupService.getUUID(parent);
validationService.checkFields(description, title, userMaximum, maxInfiniteUsers); validationService.checkFields(description, title, userMaximum, maxInfiniteUsers);
@ -105,7 +105,7 @@ public class GroupCreationController {
@RequestParam(value = "parent", required = false) String parent) { @RequestParam(value = "parent", required = false) String parent) {
Account account = KeyCloakService.createAccountFromPrincipal(token); Account account = KeyCloakService.createAccountFromPrincipal(token);
UUID parentUUID = controllerService.getUUID(parent); UUID parentUUID = GroupService.getUUID(parent);
validationService.checkFields(description, title, userMaximum, maxInfiniteUsers); validationService.checkFields(description, title, userMaximum, maxInfiniteUsers);

View File

@ -5,10 +5,10 @@ import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.Role; import mops.gruppen2.domain.Role;
import mops.gruppen2.domain.User; import mops.gruppen2.domain.User;
import mops.gruppen2.domain.Visibility; import mops.gruppen2.domain.Visibility;
import mops.gruppen2.service.ControllerService; import mops.gruppen2.service.GroupService;
import mops.gruppen2.service.InviteService; import mops.gruppen2.service.InviteService;
import mops.gruppen2.service.KeyCloakService; import mops.gruppen2.service.KeyCloakService;
import mops.gruppen2.service.UserService; import mops.gruppen2.service.ProjectionService;
import mops.gruppen2.service.ValidationService; import mops.gruppen2.service.ValidationService;
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken; import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CacheEvict;
@ -31,16 +31,16 @@ import java.util.UUID;
@RequestMapping("/gruppen2") @RequestMapping("/gruppen2")
public class GroupDetailsController { public class GroupDetailsController {
private final ControllerService controllerService;
private final UserService userService;
private final ValidationService validationService; private final ValidationService validationService;
private final InviteService inviteService; private final InviteService inviteService;
private final GroupService groupService;
private final ProjectionService projectionService;
public GroupDetailsController(ControllerService controllerService, UserService userService, ValidationService validationService, InviteService inviteService) { public GroupDetailsController(ValidationService validationService, InviteService inviteService, GroupService groupService, ProjectionService projectionService) {
this.controllerService = controllerService;
this.userService = userService;
this.validationService = validationService; this.validationService = validationService;
this.inviteService = inviteService; this.inviteService = inviteService;
this.groupService = groupService;
this.projectionService = projectionService;
} }
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"}) @RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"})
@ -50,13 +50,13 @@ public class GroupDetailsController {
HttpServletRequest request, HttpServletRequest request,
@PathVariable("id") String groupId) { @PathVariable("id") String groupId) {
Group group = userService.getGroupById(UUID.fromString(groupId)); Group group = projectionService.getGroupById(UUID.fromString(groupId));
Account account = KeyCloakService.createAccountFromPrincipal(token); Account account = KeyCloakService.createAccountFromPrincipal(token);
User user = new User(account); User user = new User(account);
UUID parentId = group.getParent(); UUID parentId = group.getParent();
String actualURL = request.getRequestURL().toString(); String actualURL = request.getRequestURL().toString();
String serverURL = actualURL.substring(0, actualURL.indexOf("gruppen2/")); String serverURL = actualURL.substring(0, actualURL.indexOf("gruppen2/"));
Group parent = controllerService.getParent(parentId); Group parent = groupService.getParent(parentId);
validationService.throwIfGroupNotExisting(group.getTitle()); validationService.throwIfGroupNotExisting(group.getTitle());
@ -93,7 +93,7 @@ public class GroupDetailsController {
Account account = KeyCloakService.createAccountFromPrincipal(token); Account account = KeyCloakService.createAccountFromPrincipal(token);
User user = new User(account); User user = new User(account);
Group group = userService.getGroupById(UUID.fromString(groupId)); Group group = projectionService.getGroupById(UUID.fromString(groupId));
validationService.throwIfNoAdmin(group, user); validationService.throwIfNoAdmin(group, user);
@ -118,12 +118,12 @@ public class GroupDetailsController {
Account account = KeyCloakService.createAccountFromPrincipal(token); Account account = KeyCloakService.createAccountFromPrincipal(token);
User user = new User(account); User user = new User(account);
Group group = userService.getGroupById(UUID.fromString(groupId)); Group group = projectionService.getGroupById(UUID.fromString(groupId));
validationService.throwIfNoAdmin(group, user); validationService.throwIfNoAdmin(group, user);
validationService.checkFields(title, description); validationService.checkFields(title, description);
controllerService.changeMetaData(account, group, title, description); groupService.changeMetaData(account, group, title, description);
return "redirect:/gruppen2/details/" + groupId; return "redirect:/gruppen2/details/" + groupId;
} }
@ -135,7 +135,7 @@ public class GroupDetailsController {
@PathVariable("id") String groupId) { @PathVariable("id") String groupId) {
Account account = KeyCloakService.createAccountFromPrincipal(token); Account account = KeyCloakService.createAccountFromPrincipal(token);
Group group = userService.getGroupById(UUID.fromString(groupId)); Group group = projectionService.getGroupById(UUID.fromString(groupId));
User user = new User(account); User user = new User(account);
validationService.throwIfNoAdmin(group, user); validationService.throwIfNoAdmin(group, user);
@ -156,7 +156,7 @@ public class GroupDetailsController {
@RequestParam("user_id") String userId) { @RequestParam("user_id") String userId) {
Account account = KeyCloakService.createAccountFromPrincipal(token); Account account = KeyCloakService.createAccountFromPrincipal(token);
Group group = userService.getGroupById(UUID.fromString(groupId)); Group group = projectionService.getGroupById(UUID.fromString(groupId));
User principle = new User(account); User principle = new User(account);
User user = new User(userId, "", "", ""); User user = new User(userId, "", "", "");
@ -164,7 +164,7 @@ public class GroupDetailsController {
//TODO: checkIfAdmin checkt nicht, dass die rolle geändert wurde. oder die rolle wird nicht geändert //TODO: checkIfAdmin checkt nicht, dass die rolle geändert wurde. oder die rolle wird nicht geändert
controllerService.changeRole(account, user, group); groupService.changeRole(account, user, group);
if (!validationService.checkIfAdmin(group, principle)) { if (!validationService.checkIfAdmin(group, principle)) {
return "redirect:/gruppen2/details/" + groupId; return "redirect:/gruppen2/details/" + groupId;
@ -181,11 +181,11 @@ public class GroupDetailsController {
@RequestParam("group_id") String groupId) { @RequestParam("group_id") String groupId) {
Account account = KeyCloakService.createAccountFromPrincipal(token); Account account = KeyCloakService.createAccountFromPrincipal(token);
Group group = userService.getGroupById(UUID.fromString(groupId)); Group group = projectionService.getGroupById(UUID.fromString(groupId));
validationService.throwIfNewMaximumIsValid(maximum, group); validationService.throwIfNewMaximumIsValid(maximum, group);
controllerService.updateMaxUser(account, UUID.fromString(groupId), maximum); groupService.updateMaxUser(account, UUID.fromString(groupId), maximum);
return "redirect:/gruppen2/details/members/" + groupId; return "redirect:/gruppen2/details/members/" + groupId;
} }
@ -200,11 +200,11 @@ public class GroupDetailsController {
Account account = KeyCloakService.createAccountFromPrincipal(token); Account account = KeyCloakService.createAccountFromPrincipal(token);
User principle = new User(account); User principle = new User(account);
User user = new User(userId, "", "", ""); User user = new User(userId, "", "", "");
Group group = userService.getGroupById(UUID.fromString(groupId)); Group group = projectionService.getGroupById(UUID.fromString(groupId));
validationService.throwIfNoAdmin(group, principle); validationService.throwIfNoAdmin(group, principle);
controllerService.deleteUser(account, user, group); groupService.deleteUser(account, user, group);
if (!validationService.checkIfUserInGroup(group, principle)) { if (!validationService.checkIfUserInGroup(group, principle)) {
return "redirect:/gruppen2"; return "redirect:/gruppen2";
@ -222,12 +222,12 @@ public class GroupDetailsController {
Account account = KeyCloakService.createAccountFromPrincipal(token); Account account = KeyCloakService.createAccountFromPrincipal(token);
User user = new User(account); User user = new User(account);
Group group = userService.getGroupById(UUID.fromString(groupId)); Group group = projectionService.getGroupById(UUID.fromString(groupId));
validationService.throwIfUserAlreadyInGroup(group, user); validationService.throwIfUserAlreadyInGroup(group, user);
validationService.throwIfGroupFull(group); validationService.throwIfGroupFull(group);
controllerService.addUser(account, UUID.fromString(groupId)); groupService.addUser(account, UUID.fromString(groupId));
model.addAttribute("account", account); model.addAttribute("account", account);
@ -242,9 +242,9 @@ public class GroupDetailsController {
Account account = KeyCloakService.createAccountFromPrincipal(token); Account account = KeyCloakService.createAccountFromPrincipal(token);
User user = new User(account); User user = new User(account);
Group group = userService.getGroupById(UUID.fromString(groupId)); Group group = projectionService.getGroupById(UUID.fromString(groupId));
controllerService.deleteUser(account, user, group); groupService.deleteUser(account, user, group);
return "redirect:/gruppen2"; return "redirect:/gruppen2";
} }
@ -257,11 +257,11 @@ public class GroupDetailsController {
Account account = KeyCloakService.createAccountFromPrincipal(token); Account account = KeyCloakService.createAccountFromPrincipal(token);
User user = new User(account); User user = new User(account);
Group group = userService.getGroupById(UUID.fromString(groupId)); Group group = projectionService.getGroupById(UUID.fromString(groupId));
validationService.throwIfNoAdmin(group, user); validationService.throwIfNoAdmin(group, user);
controllerService.deleteGroupEvent(user.getId(), UUID.fromString(groupId)); groupService.deleteGroupEvent(user.getId(), UUID.fromString(groupId));
return "redirect:/gruppen2"; return "redirect:/gruppen2";
} }
@ -274,7 +274,7 @@ public class GroupDetailsController {
@RequestParam(value = "file", required = false) MultipartFile file) { @RequestParam(value = "file", required = false) MultipartFile file) {
Account account = KeyCloakService.createAccountFromPrincipal(token); Account account = KeyCloakService.createAccountFromPrincipal(token);
controllerService.addUsersFromCsv(account, file, groupId); groupService.addUsersFromCsv(account, file, groupId);
return "redirect:/gruppen2/details/members/" + groupId; return "redirect:/gruppen2/details/members/" + groupId;
} }

View File

@ -4,7 +4,7 @@ import mops.gruppen2.domain.Account;
import mops.gruppen2.domain.User; import mops.gruppen2.domain.User;
import mops.gruppen2.domain.exception.PageNotFoundException; import mops.gruppen2.domain.exception.PageNotFoundException;
import mops.gruppen2.service.KeyCloakService; import mops.gruppen2.service.KeyCloakService;
import mops.gruppen2.service.UserService; import mops.gruppen2.service.ProjectionService;
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken; import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
@ -17,10 +17,10 @@ import javax.servlet.http.HttpServletRequest;
@Controller @Controller
public class GruppenfindungController { public class GruppenfindungController {
private final UserService userService; private final ProjectionService projectionService;
public GruppenfindungController(UserService userService) { public GruppenfindungController(ProjectionService projectionService) {
this.userService = userService; this.projectionService = projectionService;
} }
@GetMapping("") @GetMapping("")
@ -37,7 +37,7 @@ public class GruppenfindungController {
User user = new User(account); User user = new User(account);
model.addAttribute("account", account); model.addAttribute("account", account);
model.addAttribute("gruppen", userService.getUserGroups(user)); model.addAttribute("gruppen", projectionService.getUserGroups(user));
model.addAttribute("user", user); model.addAttribute("user", user);
return "index"; return "index";

View File

@ -4,10 +4,10 @@ import mops.gruppen2.domain.Account;
import mops.gruppen2.domain.Group; import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.User; import mops.gruppen2.domain.User;
import mops.gruppen2.domain.Visibility; import mops.gruppen2.domain.Visibility;
import mops.gruppen2.service.ControllerService; import mops.gruppen2.service.GroupService;
import mops.gruppen2.service.InviteService; import mops.gruppen2.service.InviteService;
import mops.gruppen2.service.KeyCloakService; import mops.gruppen2.service.KeyCloakService;
import mops.gruppen2.service.UserService; import mops.gruppen2.service.ProjectionService;
import mops.gruppen2.service.ValidationService; import mops.gruppen2.service.ValidationService;
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken; import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CacheEvict;
@ -32,14 +32,14 @@ public class SearchAndInviteController {
private final ValidationService validationService; private final ValidationService validationService;
private final InviteService inviteService; private final InviteService inviteService;
private final UserService userService; private final GroupService groupService;
private final ControllerService controllerService; private final ProjectionService projectionService;
public SearchAndInviteController(ValidationService validationService, InviteService inviteService, UserService userService, ControllerService controllerService) { public SearchAndInviteController(ValidationService validationService, InviteService inviteService, GroupService groupService, ProjectionService projectionService) {
this.validationService = validationService; this.validationService = validationService;
this.inviteService = inviteService; this.inviteService = inviteService;
this.userService = userService; this.groupService = groupService;
this.controllerService = controllerService; this.projectionService = projectionService;
} }
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"}) @RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"})
@ -66,9 +66,9 @@ public class SearchAndInviteController {
@RequestParam("id") String groupId) { @RequestParam("id") String groupId) {
Account account = KeyCloakService.createAccountFromPrincipal(token); Account account = KeyCloakService.createAccountFromPrincipal(token);
Group group = userService.getGroupById(UUID.fromString(groupId)); Group group = projectionService.getGroupById(UUID.fromString(groupId));
UUID parentId = group.getParent(); UUID parentId = group.getParent();
Group parent = controllerService.getParent(parentId); Group parent = groupService.getParent(parentId);
User user = new User(account); User user = new User(account);
model.addAttribute("account", account); model.addAttribute("account", account);
@ -89,7 +89,7 @@ public class SearchAndInviteController {
Model model, Model model,
@PathVariable("link") String link) { @PathVariable("link") String link) {
Group group = userService.getGroupById(inviteService.getGroupIdFromLink(link)); Group group = projectionService.getGroupById(inviteService.getGroupIdFromLink(link));
validationService.throwIfGroupNotExisting(group.getTitle()); validationService.throwIfGroupNotExisting(group.getTitle());
@ -111,12 +111,12 @@ public class SearchAndInviteController {
Account account = KeyCloakService.createAccountFromPrincipal(token); Account account = KeyCloakService.createAccountFromPrincipal(token);
User user = new User(account); User user = new User(account);
Group group = userService.getGroupById(UUID.fromString(groupId)); Group group = projectionService.getGroupById(UUID.fromString(groupId));
validationService.throwIfUserAlreadyInGroup(group, user); validationService.throwIfUserAlreadyInGroup(group, user);
validationService.throwIfGroupFull(group); validationService.throwIfGroupFull(group);
controllerService.addUser(account, UUID.fromString(groupId)); groupService.addUser(account, UUID.fromString(groupId));
return "redirect:/gruppen2/details/" + groupId; return "redirect:/gruppen2/details/" + groupId;
} }

View File

@ -0,0 +1,10 @@
package mops.gruppen2.domain.exception;
import org.springframework.http.HttpStatus;
public class BadPayloadException extends EventException {
public BadPayloadException(String info) {
super(HttpStatus.INTERNAL_SERVER_ERROR, "Die Payload konnte nicht übersetzt werden!", info);
}
}

View File

@ -1,20 +1,10 @@
package mops.gruppen2.service; package mops.gruppen2.service;
import mops.gruppen2.domain.Account; import mops.gruppen2.domain.Account;
import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.GroupType; import mops.gruppen2.domain.GroupType;
import mops.gruppen2.domain.Role;
import mops.gruppen2.domain.User; import mops.gruppen2.domain.User;
import mops.gruppen2.domain.Visibility; import mops.gruppen2.domain.Visibility;
import mops.gruppen2.domain.event.AddUserEvent;
import mops.gruppen2.domain.event.CreateGroupEvent; import mops.gruppen2.domain.event.CreateGroupEvent;
import mops.gruppen2.domain.event.DeleteGroupEvent;
import mops.gruppen2.domain.event.DeleteUserEvent;
import mops.gruppen2.domain.event.UpdateGroupDescriptionEvent;
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 org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -22,26 +12,23 @@ import org.springframework.web.multipart.MultipartFile;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
import static mops.gruppen2.domain.Role.ADMIN;
@Service @Service
public class ControllerService { public class ControllerService {
private static final Logger LOG = LoggerFactory.getLogger("controllerServiceLogger"); private static final Logger LOG = LoggerFactory.getLogger("controllerServiceLogger");
private final EventStoreService eventStoreService; private final EventStoreService eventStoreService;
private final UserService userService;
private final ValidationService validationService; private final ValidationService validationService;
private final InviteService inviteService; private final InviteService inviteService;
private final GroupService groupService;
public ControllerService(EventStoreService eventStoreService, UserService userService, ValidationService validationService, InviteService inviteService) { public ControllerService(EventStoreService eventStoreService, ValidationService validationService, InviteService inviteService, GroupService groupService) {
this.eventStoreService = eventStoreService; this.eventStoreService = eventStoreService;
this.userService = userService;
this.validationService = validationService; this.validationService = validationService;
this.inviteService = inviteService; this.inviteService = inviteService;
this.groupService = groupService;
} }
/** /**
@ -90,7 +77,7 @@ public class ControllerService {
isMaximumInfinite, isMaximumInfinite,
userMaximum, parent); userMaximum, parent);
addUserList(newUsers, groupId); groupService.addUserList(newUsers, groupId);
} }
/** /**
@ -132,158 +119,12 @@ public class ControllerService {
User user = new User(account.getName(), "", "", ""); User user = new User(account.getName(), "", "", "");
addUser(account, groupId); groupService.addUser(account, groupId);
updateTitle(account, groupId, title); groupService.updateTitle(account, groupId, title);
updateDescription(account, groupId, description); groupService.updateDescription(account, groupId, description);
updateRole(user, groupId); groupService.updateRole(user, groupId);
return groupId; return groupId;
} }
//TODO: GroupService/eventbuilderservice
private void addUserList(List<User> newUsers, UUID groupId) {
for (User user : newUsers) {
Group group = userService.getGroupById(groupId);
if (group.getMembers().contains(user)) {
LOG.info("Benutzer {} ist bereits in Gruppe", user.getId());
} else {
AddUserEvent addUserEvent = new AddUserEvent(groupId, user.getId(), user.getGivenname(), user.getFamilyname(), user.getEmail());
eventStoreService.saveEvent(addUserEvent);
}
}
}
//TODO: GroupService/eventbuilderservice
public void addUser(Account account, UUID groupId) {
AddUserEvent addUserEvent = new AddUserEvent(groupId, account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
eventStoreService.saveEvent(addUserEvent);
}
//TODO: GroupService/eventbuilderservice
private void updateTitle(Account account, UUID groupId, String title) {
UpdateGroupTitleEvent updateGroupTitleEvent = new UpdateGroupTitleEvent(groupId, account.getName(), title);
eventStoreService.saveEvent(updateGroupTitleEvent);
}
//TODO: GroupService/eventbuilderservice
public void updateRole(User user, UUID groupId) throws EventException {
UpdateRoleEvent updateRoleEvent;
Group group = userService.getGroupById(groupId);
validationService.throwIfNotInGroup(group, user);
if (group.getRoles().get(user.getId()) == ADMIN) {
updateRoleEvent = new UpdateRoleEvent(group.getId(), user.getId(), Role.MEMBER);
} else {
updateRoleEvent = new UpdateRoleEvent(group.getId(), user.getId(), ADMIN);
}
eventStoreService.saveEvent(updateRoleEvent);
}
//TODO: GroupService/eventbuilderservice
private void updateDescription(Account account, UUID groupId, String description) {
UpdateGroupDescriptionEvent updateGroupDescriptionEvent = new UpdateGroupDescriptionEvent(groupId, account.getName(), description);
eventStoreService.saveEvent(updateGroupDescriptionEvent);
}
//TODO: GroupService
public void addUsersFromCsv(Account account, MultipartFile file, String groupId) {
Group group = userService.getGroupById(UUID.fromString(groupId));
List<User> newUserList = CsvService.readCsvFile(file);
GroupService.removeOldUsersFromNewUsers(group.getMembers(), newUserList);
UUID groupUUID = getUUID(groupId);
Long newUserMaximum = GroupService.adjustUserMaximum((long) newUserList.size(), (long) group.getMembers().size(), group.getUserMaximum());
if (newUserMaximum > group.getUserMaximum()) {
updateMaxUser(account, groupUUID, newUserMaximum);
}
addUserList(newUserList, groupUUID);
}
//TODO: GroupService
public UUID getUUID(String id) {
return UUID.fromString(Objects.requireNonNullElse(id, "00000000-0000-0000-0000-000000000000"));
}
//TODO: GroupService/eventbuilderservice
public void updateMaxUser(Account account, UUID groupId, Long userMaximum) {
UpdateUserMaxEvent updateUserMaxEvent = new UpdateUserMaxEvent(groupId, account.getName(), userMaximum);
eventStoreService.saveEvent(updateUserMaxEvent);
}
//TODO: GroupService
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);
}
}
//TODO: GroupService oder in Group?
public Group getParent(UUID parentId) {
Group parent = new Group();
if (!GroupService.idIsEmpty(parentId)) {
parent = userService.getGroupById(parentId);
}
return parent;
}
//TODO: GroupService
public void deleteUser(Account account, User user, Group group) throws EventException {
changeRoleIfLastAdmin(account, group);
validationService.throwIfNotInGroup(group, user);
deleteUserEvent(user, group.getId());
if (validationService.checkIfGroupEmpty(group.getId())) {
deleteGroupEvent(user.getId(), group.getId());
}
}
//TODO: GroupService/eventbuilderservice
private void deleteUserEvent(User user, UUID groupId) {
DeleteUserEvent deleteUserEvent = new DeleteUserEvent(groupId, user.getId());
eventStoreService.saveEvent(deleteUserEvent);
}
//TODO: GroupService/eventbuilderservice
public void deleteGroupEvent(String userId, UUID groupId) {
DeleteGroupEvent deleteGroupEvent = new DeleteGroupEvent(groupId, userId);
inviteService.destroyLink(groupId);
eventStoreService.saveEvent(deleteGroupEvent);
}
//TODO: GroupService
private void promoteVeteranMember(Account account, Group group) {
if (validationService.checkIfLastAdmin(account, group)) {
User newAdmin = GroupService.getVeteranMember(account, group);
updateRole(newAdmin, group.getId());
}
}
//TODO: GroupService
public void changeRoleIfLastAdmin(Account account, Group group) {
if (group.getMembers().size() <= 1) {
return;
}
promoteVeteranMember(account, group);
}
//TODO: GroupService
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);
}
updateRole(user, group.getId());
}
} }

View File

@ -1,4 +1,7 @@
package mops.gruppen2.service; package mops.gruppen2.service;
import org.springframework.stereotype.Service;
@Service
public class EventBuilderService { public class EventBuilderService {
} }

View File

@ -3,6 +3,7 @@ package mops.gruppen2.service;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import mops.gruppen2.domain.dto.EventDTO; import mops.gruppen2.domain.dto.EventDTO;
import mops.gruppen2.domain.event.Event; import mops.gruppen2.domain.event.Event;
import mops.gruppen2.domain.exception.BadPayloadException;
import mops.gruppen2.repository.EventRepository; import mops.gruppen2.repository.EventRepository;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -39,6 +40,20 @@ public class EventStoreService {
} }
} }
/**
* Speichert alle Events aus der übergebenen Liste in der DB.
*
* @param events Liste an Events die gespeichert werden soll
*/
@SafeVarargs
public final void saveAll(List<Event>... events) {
for (List<Event> eventlist : events) {
for (Event event : eventlist) {
eventStore.save(getDTOFromEvent(event));
}
}
}
/** /**
* Erzeugt aus einem Event Objekt ein EventDTO Objekt. * Erzeugt aus einem Event Objekt ein EventDTO Objekt.
* *
@ -46,7 +61,7 @@ public class EventStoreService {
* *
* @return EventDTO (Neues DTO) * @return EventDTO (Neues DTO)
*/ */
public EventDTO getDTOFromEvent(Event event) { public static EventDTO getDTOFromEvent(Event event) {
String payload = ""; String payload = "";
try { try {
payload = JsonService.serializeEvent(event); payload = JsonService.serializeEvent(event);
@ -57,6 +72,28 @@ public class EventStoreService {
return new EventDTO(null, event.getGroupId().toString(), event.getUserId(), getEventType(event), payload); return new EventDTO(null, event.getGroupId().toString(), event.getUserId(), getEventType(event), payload);
} }
/**
* Erzeugt aus einer Liste von eventDTOs eine Liste von Events.
*
* @param eventDTOS Liste von DTOs
*
* @return Liste von Events
*/
static List<Event> getEventsFromDTOs(List<EventDTO> eventDTOS) {
return eventDTOS.stream()
.map(EventStoreService::getEventFromDTO)
.collect(Collectors.toList());
}
static Event getEventFromDTO(EventDTO dto) throws BadPayloadException {
try {
return JsonService.deserializeEvent(dto.getEvent_payload());
} catch (JsonProcessingException e) {
LOG.error("Payload\n {}\n konnte nicht deserialisiert werden!", dto.getEvent_payload());
throw new BadPayloadException(EventStoreService.class.toString());
}
}
/** /**
* Gibt den Eventtyp als String wieder. * Gibt den Eventtyp als String wieder.
* *
@ -71,17 +108,20 @@ public class EventStoreService {
} }
/** /**
* Speichert alle Events aus der übergebenen Liste in der DB. * Sucht in der DB alle Zeilen raus welche eine der Gruppen_ids hat.
* Wandelt die Zeilen in Events um und gibt davon eine Liste zurück.
* *
* @param events Liste an Events die gespeichert werden soll * @param groupIds Liste an IDs
*
* @return Liste an Events
*/ */
@SafeVarargs //TODO: EventStoreService
public final void saveAll(List<Event>... events) { public List<Event> getGroupEvents(List<UUID> groupIds) {
for (List<Event> eventlist : events) { List<EventDTO> eventDTOS = new ArrayList<>();
for (Event event : eventlist) { for (UUID groupId : groupIds) {
eventStore.save(getDTOFromEvent(event)); eventDTOS.addAll(eventStore.findEventDTOByGroupId(groupId.toString()));
}
} }
return getEventsFromDTOs(eventDTOS);
} }
/** /**
@ -99,26 +139,6 @@ public class EventStoreService {
return getEventsFromDTOs(groupEventDTOS); return getEventsFromDTOs(groupEventDTOS);
} }
/**
* Erzeugt aus einer Liste von eventDTOs eine Liste von Events.
*
* @param eventDTOS Liste von DTOs
*
* @return Liste von Events
*/
List<Event> getEventsFromDTOs(Iterable<EventDTO> eventDTOS) {
List<Event> events = new ArrayList<>();
for (EventDTO eventDTO : eventDTOS) {
try {
events.add(JsonService.deserializeEvent(eventDTO.getEvent_payload()));
} catch (JsonProcessingException e) {
LOG.error("Payload\n {}\n konnte nicht deserialisiert werden!", eventDTO.getEvent_payload());
}
}
return events;
}
public long getMaxEventId() { public long getMaxEventId() {
long highestEvent = 0; long highestEvent = 0;

View File

@ -3,17 +3,29 @@ package mops.gruppen2.service;
import mops.gruppen2.domain.Account; import mops.gruppen2.domain.Account;
import mops.gruppen2.domain.Group; import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.GroupType; import mops.gruppen2.domain.GroupType;
import mops.gruppen2.domain.Role;
import mops.gruppen2.domain.User; import mops.gruppen2.domain.User;
import mops.gruppen2.domain.Visibility; import mops.gruppen2.domain.Visibility;
import mops.gruppen2.domain.dto.EventDTO; import mops.gruppen2.domain.event.AddUserEvent;
import mops.gruppen2.domain.event.Event; import mops.gruppen2.domain.event.DeleteGroupEvent;
import mops.gruppen2.domain.event.DeleteUserEvent;
import mops.gruppen2.domain.event.UpdateGroupDescriptionEvent;
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.repository.EventRepository; import mops.gruppen2.repository.EventRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
import static mops.gruppen2.domain.Role.ADMIN;
/** /**
* Behandelt Aufgaben, welche sich auf eine Gruppe beziehen * Behandelt Aufgaben, welche sich auf eine Gruppe beziehen
*/ */
@ -22,10 +34,18 @@ public class GroupService {
private final EventStoreService eventStoreService; private final EventStoreService eventStoreService;
private final EventRepository eventRepository; private final EventRepository eventRepository;
private final ValidationService validationService;
private final InviteService inviteService;
private final ProjectionService projectionService;
public GroupService(EventStoreService eventStoreService, EventRepository eventRepository) { private static final Logger LOG = LoggerFactory.getLogger(GroupService.class);
public GroupService(EventStoreService eventStoreService, EventRepository eventRepository, ValidationService validationService, InviteService inviteService, ProjectionService projectionService) {
this.eventStoreService = eventStoreService; this.eventStoreService = eventStoreService;
this.eventRepository = eventRepository; this.eventRepository = eventRepository;
this.validationService = validationService;
this.inviteService = inviteService;
this.projectionService = projectionService;
} }
static User getVeteranMember(Account account, Group group) { static User getVeteranMember(Account account, Group group) {
@ -98,21 +118,150 @@ public class GroupService {
return "00000000-0000-0000-0000-000000000000".equals(id.toString()); return "00000000-0000-0000-0000-000000000000".equals(id.toString());
} }
/** //TODO: GroupService/eventbuilderservice
* Sucht in der DB alle Zeilen raus welche eine der Gruppen_ids hat. void addUserList(List<User> newUsers, UUID groupId) {
* Wandelt die Zeilen in Events um und gibt davon eine Liste zurück. for (User user : newUsers) {
* Group group = projectionService.getGroupById(groupId);
* @param groupIds Liste an IDs if (group.getMembers().contains(user)) {
* LOG.info("Benutzer {} ist bereits in Gruppe", user.getId());
* @return Liste an Events } else {
*/ AddUserEvent addUserEvent = new AddUserEvent(groupId, user.getId(), user.getGivenname(), user.getFamilyname(), user.getEmail());
//TODO: Das vielleicht in den EventRepoService? eventStoreService.saveEvent(addUserEvent);
public List<Event> getGroupEvents(List<UUID> groupIds) { }
List<EventDTO> eventDTOS = new ArrayList<>();
for (UUID groupId : groupIds) {
eventDTOS.addAll(eventRepository.findEventDTOByGroupId(groupId.toString()));
} }
return eventStoreService.getEventsFromDTOs(eventDTOS); }
//TODO: GroupService/eventbuilderservice
public void addUser(Account account, UUID groupId) {
AddUserEvent addUserEvent = new AddUserEvent(groupId, account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
eventStoreService.saveEvent(addUserEvent);
}
//TODO: GroupService/eventbuilderservice
void updateTitle(Account account, UUID groupId, String title) {
UpdateGroupTitleEvent updateGroupTitleEvent = new UpdateGroupTitleEvent(groupId, account.getName(), title);
eventStoreService.saveEvent(updateGroupTitleEvent);
}
//TODO: GroupService/eventbuilderservice
public void updateRole(User user, UUID groupId) throws EventException {
UpdateRoleEvent updateRoleEvent;
Group group = projectionService.getGroupById(groupId);
validationService.throwIfNotInGroup(group, user);
if (group.getRoles().get(user.getId()) == ADMIN) {
updateRoleEvent = new UpdateRoleEvent(group.getId(), user.getId(), Role.MEMBER);
} else {
updateRoleEvent = new UpdateRoleEvent(group.getId(), user.getId(), ADMIN);
}
eventStoreService.saveEvent(updateRoleEvent);
}
//TODO: GroupService/eventbuilderservice
void updateDescription(Account account, UUID groupId, String description) {
UpdateGroupDescriptionEvent updateGroupDescriptionEvent = new UpdateGroupDescriptionEvent(groupId, account.getName(), description);
eventStoreService.saveEvent(updateGroupDescriptionEvent);
}
//TODO: GroupService
public void addUsersFromCsv(Account account, MultipartFile file, String groupId) {
Group group = projectionService.getGroupById(UUID.fromString(groupId));
List<User> newUserList = CsvService.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);
}
//TODO: GroupService
public static UUID getUUID(String id) {
return UUID.fromString(Objects.requireNonNullElse(id, "00000000-0000-0000-0000-000000000000"));
}
//TODO: GroupService/eventbuilderservice
public void updateMaxUser(Account account, UUID groupId, Long userMaximum) {
UpdateUserMaxEvent updateUserMaxEvent = new UpdateUserMaxEvent(groupId, account.getName(), userMaximum);
eventStoreService.saveEvent(updateUserMaxEvent);
}
//TODO: GroupService
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);
}
}
//TODO: GroupService oder in Group?
public Group getParent(UUID parentId) {
Group parent = new Group();
if (!idIsEmpty(parentId)) {
parent = projectionService.getGroupById(parentId);
}
return parent;
}
//TODO: GroupService
public void deleteUser(Account account, User user, Group group) throws EventException {
changeRoleIfLastAdmin(account, group);
validationService.throwIfNotInGroup(group, user);
deleteUserEvent(user, group.getId());
if (validationService.checkIfGroupEmpty(group.getId())) {
deleteGroupEvent(user.getId(), group.getId());
}
}
//TODO: GroupService/eventbuilderservice
private void deleteUserEvent(User user, UUID groupId) {
DeleteUserEvent deleteUserEvent = new DeleteUserEvent(groupId, user.getId());
eventStoreService.saveEvent(deleteUserEvent);
}
//TODO: GroupService/eventbuilderservice
public void deleteGroupEvent(String userId, UUID groupId) {
DeleteGroupEvent deleteGroupEvent = new DeleteGroupEvent(groupId, userId);
inviteService.destroyLink(groupId);
eventStoreService.saveEvent(deleteGroupEvent);
}
//TODO: GroupService
private void promoteVeteranMember(Account account, Group group) {
if (validationService.checkIfLastAdmin(account, group)) {
User newAdmin = getVeteranMember(account, group);
updateRole(newAdmin, group.getId());
}
}
//TODO: GroupService
public void changeRoleIfLastAdmin(Account account, Group group) {
if (group.getMembers().size() <= 1) {
return;
}
promoteVeteranMember(account, group);
}
//TODO: GroupService
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);
}
updateRole(user, group.getId());
} }
} }

View File

@ -2,9 +2,11 @@ package mops.gruppen2.service;
import mops.gruppen2.domain.Group; import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.GroupType; import mops.gruppen2.domain.GroupType;
import mops.gruppen2.domain.User;
import mops.gruppen2.domain.Visibility; import mops.gruppen2.domain.Visibility;
import mops.gruppen2.domain.event.Event; import mops.gruppen2.domain.event.Event;
import mops.gruppen2.domain.exception.EventException; import mops.gruppen2.domain.exception.EventException;
import mops.gruppen2.domain.exception.GroupNotFoundException;
import mops.gruppen2.repository.EventRepository; import mops.gruppen2.repository.EventRepository;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -60,11 +62,11 @@ public class ProjectionService {
//TODO Rename //TODO Rename
@Cacheable("groups") @Cacheable("groups")
public List<Group> getAllGroupWithVisibilityPublic(String userId) throws EventException { public List<Group> getAllGroupWithVisibilityPublic(String userId) throws EventException {
List<Event> groupEvents = eventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("CreateGroupEvent")); List<Event> groupEvents = EventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("CreateGroupEvent"));
groupEvents.addAll(eventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("UpdateGroupDescriptionEvent"))); groupEvents.addAll(EventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("UpdateGroupDescriptionEvent")));
groupEvents.addAll(eventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("UpdateGroupTitleEvent"))); groupEvents.addAll(EventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("UpdateGroupTitleEvent")));
groupEvents.addAll(eventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("DeleteGroupEvent"))); groupEvents.addAll(EventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("DeleteGroupEvent")));
groupEvents.addAll(eventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("UpdateUserMaxEvent"))); groupEvents.addAll(EventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("UpdateUserMaxEvent")));
List<Group> visibleGroups = projectEventList(groupEvents); List<Group> visibleGroups = projectEventList(groupEvents);
@ -85,10 +87,10 @@ public class ProjectionService {
@Cacheable("groups") @Cacheable("groups")
//TODO: ProjectionService //TODO: ProjectionService
public List<Group> getAllLecturesWithVisibilityPublic() { public List<Group> getAllLecturesWithVisibilityPublic() {
List<Event> createEvents = eventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("CreateGroupEvent")); List<Event> createEvents = EventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("CreateGroupEvent"));
createEvents.addAll(eventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("DeleteGroupEvent"))); createEvents.addAll(EventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("DeleteGroupEvent")));
createEvents.addAll(eventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("UpdateGroupTitleEvent"))); createEvents.addAll(EventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("UpdateGroupTitleEvent")));
createEvents.addAll(eventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("DeleteGroupEvent"))); createEvents.addAll(EventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("DeleteGroupEvent")));
List<Group> visibleGroups = projectEventList(createEvents); List<Group> visibleGroups = projectEventList(createEvents);
@ -117,4 +119,59 @@ public class ProjectionService {
return new ArrayList<>(groupMap.values()); return new ArrayList<>(groupMap.values());
} }
//TODO: ProjectionService
@Cacheable("groups")
public List<Group> getUserGroups(String userId) throws EventException {
return getUserGroups(new User(userId, "", "", ""));
}
/**
* Gibt eine Liste aus Gruppen zurück, in denen sich der übergebene User befindet.
*
* @param user Der User
*
* @return Liste aus Gruppen
*/
//TODO: ProjectionService
//TODO: Nur AddUserEvents + DeleteUserEvents betrachten
@Cacheable("groups")
public List<Group> getUserGroups(User user) {
List<UUID> groupIds = eventStoreService.findGroupIdsByUser(user.getId());
List<Event> events = eventStoreService.getGroupEvents(groupIds);
List<Group> groups = projectEventList(events);
List<Group> newGroups = new ArrayList<>();
for (Group group : groups) {
if (group.getMembers().contains(user)) {
newGroups.add(group);
}
}
SearchService.sortByGroupType(newGroups);
return newGroups;
}
/**
* Gibt die Gruppe zurück, die zu der übergebenen Id passt.
*
* @param groupId Die Id der gesuchten Gruppe
*
* @return Die gesuchte Gruppe
*
* @throws EventException Wenn die Gruppe nicht gefunden wird
*/
//TODO: ProjectionService
public Group getGroupById(UUID groupId) throws EventException {
List<UUID> groupIds = new ArrayList<>();
groupIds.add(groupId);
try {
List<Event> events = eventStoreService.getGroupEvents(groupIds);
return projectEventList(events).get(0);
} catch (IndexOutOfBoundsException e) {
throw new GroupNotFoundException("@UserService");
}
}
} }

View File

@ -1,78 +0,0 @@
package mops.gruppen2.service;
import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.User;
import mops.gruppen2.domain.event.Event;
import mops.gruppen2.domain.exception.EventException;
import mops.gruppen2.domain.exception.GroupNotFoundException;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@Service
public class UserService {
private final GroupService groupService;
private final EventStoreService eventStoreService;
private final ProjectionService projectionService;
public UserService(GroupService groupService, EventStoreService eventStoreService, ProjectionService projectionService) {
this.groupService = groupService;
this.eventStoreService = eventStoreService;
this.projectionService = projectionService;
}
@Cacheable("groups")
public List<Group> getUserGroups(String userId) throws EventException {
return getUserGroups(new User(userId, "", "", ""));
}
/**
* Gibt eine Liste aus Gruppen zurück, in denen sich der übergebene User befindet.
*
* @param user Der User
*
* @return Liste aus Gruppen
*/
//TODO: Nur AddUserEvents + DeleteUserEvents betrachten
@Cacheable("groups")
public List<Group> getUserGroups(User user) {
List<UUID> groupIds = eventStoreService.findGroupIdsByUser(user.getId());
List<Event> events = groupService.getGroupEvents(groupIds);
List<Group> groups = projectionService.projectEventList(events);
List<Group> newGroups = new ArrayList<>();
for (Group group : groups) {
if (group.getMembers().contains(user)) {
newGroups.add(group);
}
}
SearchService.sortByGroupType(newGroups);
return newGroups;
}
/**
* Gibt die Gruppe zurück, die zu der übergebenen Id passt.
*
* @param groupId Die Id der gesuchten Gruppe
*
* @return Die gesuchte Gruppe
*
* @throws EventException Wenn die Gruppe nicht gefunden wird
*/
public Group getGroupById(UUID groupId) throws EventException {
List<UUID> groupIds = new ArrayList<>();
groupIds.add(groupId);
try {
List<Event> events = groupService.getGroupEvents(groupIds);
return projectionService.projectEventList(events).get(0);
} catch (IndexOutOfBoundsException e) {
throw new GroupNotFoundException("@UserService");
}
}
}

View File

@ -23,12 +23,12 @@ import static mops.gruppen2.domain.Role.ADMIN;
@Service @Service
public class ValidationService { public class ValidationService {
private final UserService userService;
private final SearchService searchService; private final SearchService searchService;
private final ProjectionService projectionService;
public ValidationService(UserService userService, SearchService searchService) { public ValidationService(SearchService searchService, ProjectionService projectionService) {
this.userService = userService;
this.searchService = searchService; this.searchService = searchService;
this.projectionService = projectionService;
} }
//TODO: make static or change return + assignment //TODO: make static or change return + assignment
@ -68,7 +68,7 @@ public class ValidationService {
} }
boolean checkIfGroupEmpty(UUID groupId) { boolean checkIfGroupEmpty(UUID groupId) {
return userService.getGroupById(groupId).getMembers().isEmpty(); return projectionService.getGroupById(groupId).getMembers().isEmpty();
} }
public void throwIfNoAdmin(Group group, User user) { public void throwIfNoAdmin(Group group, User user) {

View File

@ -28,6 +28,7 @@ import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
//TODO: Alles in die entsprechenden Klassen sortieren :((((
@ExtendWith(SpringExtension.class) @ExtendWith(SpringExtension.class)
@SpringBootTest(classes = Gruppen2Application.class) @SpringBootTest(classes = Gruppen2Application.class)
@Transactional @Transactional
@ -37,12 +38,15 @@ class ControllerServiceTest {
Account account; Account account;
Account account2; Account account2;
Account account3; Account account3;
@Autowired
ControllerService controllerService; ControllerService controllerService;
@Autowired
EventStoreService eventStoreService; EventStoreService eventStoreService;
UserService userService; @Autowired
ValidationService validationService; ValidationService validationService;
@Autowired @Autowired
EventRepository eventRepository; EventRepository eventRepository;
@Autowired
GroupService groupService; GroupService groupService;
@Autowired @Autowired
InviteService inviteService; InviteService inviteService;
@ -53,11 +57,6 @@ class ControllerServiceTest {
@BeforeEach @BeforeEach
void setUp() { void setUp() {
eventStoreService = new EventStoreService(eventRepository);
groupService = new GroupService(eventStoreService, eventRepository);
userService = new UserService(groupService, eventStoreService, projectionService);
validationService = new ValidationService(userService, searchService);
controllerService = new ControllerService(eventStoreService, userService, validationService, inviteService);
Set<String> roles = new HashSet<>(); Set<String> roles = new HashSet<>();
roles.add("l"); roles.add("l");
account = new Account("ich", "ich@hhu.de", "l", "ichdude", "jap", roles); account = new Account("ich", "ich@hhu.de", "l", "ichdude", "jap", roles);
@ -69,7 +68,7 @@ class ControllerServiceTest {
@Test @Test
void createPublicGroupWithNoParentAndLimitedNumberTest() { void createPublicGroupWithNoParentAndLimitedNumberTest() {
controllerService.createGroup(account, "test", "hi", null, null, null, 20L, null); controllerService.createGroup(account, "test", "hi", null, null, null, 20L, null);
List<Group> groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); List<Group> groups = projectionService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility()); assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility());
assertEquals(20L, groups.get(0).getUserMaximum()); assertEquals(20L, groups.get(0).getUserMaximum());
@ -80,7 +79,7 @@ class ControllerServiceTest {
void createPublicGroupWithNoParentAndUnlimitedNumberTest() { void createPublicGroupWithNoParentAndUnlimitedNumberTest() {
controllerService.createGroup(account, "test", "hi", null, null, true, null, null); controllerService.createGroup(account, "test", "hi", null, null, true, null, null);
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
List<Group> groups = userService.getUserGroups(user); List<Group> groups = projectionService.getUserGroups(user);
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility()); assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility());
assertEquals(100000L, groups.get(0).getUserMaximum()); assertEquals(100000L, groups.get(0).getUserMaximum());
@ -91,7 +90,7 @@ class ControllerServiceTest {
void createPrivateGroupWithNoParentAndUnlimitedNumberTest() { void createPrivateGroupWithNoParentAndUnlimitedNumberTest() {
controllerService.createGroup(account, "test", "hi", true, null, true, null, null); controllerService.createGroup(account, "test", "hi", true, null, true, null, null);
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
List<Group> groups = userService.getUserGroups(user); List<Group> groups = projectionService.getUserGroups(user);
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
assertEquals(Visibility.PRIVATE, groups.get(0).getVisibility()); assertEquals(Visibility.PRIVATE, groups.get(0).getVisibility());
assertEquals(100000L, groups.get(0).getUserMaximum()); assertEquals(100000L, groups.get(0).getUserMaximum());
@ -102,7 +101,7 @@ class ControllerServiceTest {
void createPrivateGroupWithNoParentAndLimitedNumberTest() { void createPrivateGroupWithNoParentAndLimitedNumberTest() {
controllerService.createGroup(account, "test", "hi", true, null, null, 20L, null); controllerService.createGroup(account, "test", "hi", true, null, null, 20L, null);
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
List<Group> groups = userService.getUserGroups(user); List<Group> groups = projectionService.getUserGroups(user);
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
assertEquals(Visibility.PRIVATE, groups.get(0).getVisibility()); assertEquals(Visibility.PRIVATE, groups.get(0).getVisibility());
assertEquals(20L, groups.get(0).getUserMaximum()); assertEquals(20L, groups.get(0).getUserMaximum());
@ -113,10 +112,10 @@ class ControllerServiceTest {
void createPrivateGroupWithParentAndLimitedNumberTest() throws IOException { void createPrivateGroupWithParentAndLimitedNumberTest() throws IOException {
controllerService.createGroupAsOrga(account2, "test", "hi", null, true, true, null, null, null); controllerService.createGroupAsOrga(account2, "test", "hi", null, true, true, null, null, null);
User user = new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail()); User user = new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail());
List<Group> groups1 = userService.getUserGroups(user); List<Group> groups1 = projectionService.getUserGroups(user);
controllerService.createGroup(account, "test", "hi", true, null, null, 20L, groups1.get(0).getId()); controllerService.createGroup(account, "test", "hi", true, null, null, 20L, groups1.get(0).getId());
User user2 = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); User user2 = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
List<Group> groups = userService.getUserGroups(user2); List<Group> groups = projectionService.getUserGroups(user2);
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
assertEquals(Visibility.PRIVATE, groups.get(0).getVisibility()); assertEquals(Visibility.PRIVATE, groups.get(0).getVisibility());
assertEquals(20L, groups.get(0).getUserMaximum()); assertEquals(20L, groups.get(0).getUserMaximum());
@ -126,9 +125,9 @@ class ControllerServiceTest {
@Test @Test
void createPublicGroupWithParentAndLimitedNumberTest() throws IOException { void createPublicGroupWithParentAndLimitedNumberTest() throws IOException {
controllerService.createGroupAsOrga(account2, "test", "hi", null, null, true, null, null, null); controllerService.createGroupAsOrga(account2, "test", "hi", null, null, true, null, null, null);
List<Group> groups1 = userService.getUserGroups(new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail())); List<Group> groups1 = projectionService.getUserGroups(new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail()));
controllerService.createGroup(account, "test", "hi", null, null, null, 20L, groups1.get(0).getId()); controllerService.createGroup(account, "test", "hi", null, null, null, 20L, groups1.get(0).getId());
List<Group> groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); List<Group> groups = projectionService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility()); assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility());
assertEquals(20L, groups.get(0).getUserMaximum()); assertEquals(20L, groups.get(0).getUserMaximum());
@ -138,9 +137,9 @@ class ControllerServiceTest {
@Test @Test
void createPublicGroupWithParentAndUnlimitedNumberTest() throws IOException { void createPublicGroupWithParentAndUnlimitedNumberTest() throws IOException {
controllerService.createGroupAsOrga(account2, "test", "hi", null, null, true, null, null, null); controllerService.createGroupAsOrga(account2, "test", "hi", null, null, true, null, null, null);
List<Group> groups1 = userService.getUserGroups(new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail())); List<Group> groups1 = projectionService.getUserGroups(new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail()));
controllerService.createGroup(account, "test", "hi", null, true, true, null, groups1.get(0).getId()); controllerService.createGroup(account, "test", "hi", null, true, true, null, groups1.get(0).getId());
List<Group> groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); List<Group> groups = projectionService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility()); assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility());
assertEquals(100000L, groups.get(0).getUserMaximum()); assertEquals(100000L, groups.get(0).getUserMaximum());
@ -150,9 +149,9 @@ class ControllerServiceTest {
@Test @Test
void createPrivateGroupWithParentAndUnlimitedNumberTest() throws IOException { void createPrivateGroupWithParentAndUnlimitedNumberTest() throws IOException {
controllerService.createGroupAsOrga(account2, "test", "hi", null, null, true, null, null, null); controllerService.createGroupAsOrga(account2, "test", "hi", null, null, true, null, null, null);
List<Group> groups1 = userService.getUserGroups(new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail())); List<Group> groups1 = projectionService.getUserGroups(new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail()));
controllerService.createGroup(account, "test", "hi", true, true, true, null, groups1.get(0).getId()); controllerService.createGroup(account, "test", "hi", true, true, true, null, groups1.get(0).getId());
List<Group> groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); List<Group> groups = projectionService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
assertEquals(Visibility.PRIVATE, groups.get(0).getVisibility()); assertEquals(Visibility.PRIVATE, groups.get(0).getVisibility());
assertEquals(100000L, groups.get(0).getUserMaximum()); assertEquals(100000L, groups.get(0).getUserMaximum());
@ -162,7 +161,7 @@ class ControllerServiceTest {
@Test @Test
void createPublicOrgaGroupWithNoParentAndLimitedNumberTest() throws IOException { void createPublicOrgaGroupWithNoParentAndLimitedNumberTest() throws IOException {
controllerService.createGroupAsOrga(account, "test", "hi", null, null, null, 20L, null, null); controllerService.createGroupAsOrga(account, "test", "hi", null, null, null, 20L, null, null);
List<Group> groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); List<Group> groups = projectionService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
assertEquals(GroupType.SIMPLE, groups.get(0).getType()); assertEquals(GroupType.SIMPLE, groups.get(0).getType());
assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility()); assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility());
@ -173,7 +172,7 @@ class ControllerServiceTest {
@Test @Test
void createPublicOrgaGroupWithNoParentAndUnlimitedNumberTest() throws IOException { void createPublicOrgaGroupWithNoParentAndUnlimitedNumberTest() throws IOException {
controllerService.createGroupAsOrga(account, "test", "hi", null, null, true, null, null, null); controllerService.createGroupAsOrga(account, "test", "hi", null, null, true, null, null, null);
List<Group> groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); List<Group> groups = projectionService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
assertEquals(GroupType.SIMPLE, groups.get(0).getType()); assertEquals(GroupType.SIMPLE, groups.get(0).getType());
assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility()); assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility());
@ -184,7 +183,7 @@ class ControllerServiceTest {
@Test @Test
void createPrivateOrgaGroupWithNoParentAndLimitedNumberTest() throws IOException { void createPrivateOrgaGroupWithNoParentAndLimitedNumberTest() throws IOException {
controllerService.createGroupAsOrga(account, "test", "hi", true, null, null, 20L, null, null); controllerService.createGroupAsOrga(account, "test", "hi", true, null, null, 20L, null, null);
List<Group> groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); List<Group> groups = projectionService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
assertEquals(GroupType.SIMPLE, groups.get(0).getType()); assertEquals(GroupType.SIMPLE, groups.get(0).getType());
assertEquals(Visibility.PRIVATE, groups.get(0).getVisibility()); assertEquals(Visibility.PRIVATE, groups.get(0).getVisibility());
@ -195,7 +194,7 @@ class ControllerServiceTest {
@Test @Test
void createPrivateOrgaGroupWithNoParentAndUnlimitedNumberTest() throws IOException { void createPrivateOrgaGroupWithNoParentAndUnlimitedNumberTest() throws IOException {
controllerService.createGroupAsOrga(account, "test", "hi", true, null, true, null, null, null); controllerService.createGroupAsOrga(account, "test", "hi", true, null, true, null, null, null);
List<Group> groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); List<Group> groups = projectionService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
assertEquals(GroupType.SIMPLE, groups.get(0).getType()); assertEquals(GroupType.SIMPLE, groups.get(0).getType());
assertEquals(Visibility.PRIVATE, groups.get(0).getVisibility()); assertEquals(Visibility.PRIVATE, groups.get(0).getVisibility());
@ -206,7 +205,7 @@ class ControllerServiceTest {
@Test @Test
void createOrgaLectureGroupAndLimitedNumberTest() throws IOException { void createOrgaLectureGroupAndLimitedNumberTest() throws IOException {
controllerService.createGroupAsOrga(account, "test", "hi", null, true, null, 20L, null, null); controllerService.createGroupAsOrga(account, "test", "hi", null, true, null, 20L, null, null);
List<Group> groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); List<Group> groups = projectionService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
assertEquals(GroupType.LECTURE, groups.get(0).getType()); assertEquals(GroupType.LECTURE, groups.get(0).getType());
assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility()); assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility());
@ -217,7 +216,7 @@ class ControllerServiceTest {
@Test @Test
void createOrgaLectureGroupAndUnlimitedNumberTest() throws IOException { void createOrgaLectureGroupAndUnlimitedNumberTest() throws IOException {
controllerService.createGroupAsOrga(account, "test", "hi", null, true, true, null, null, null); controllerService.createGroupAsOrga(account, "test", "hi", null, true, true, null, null, null);
List<Group> groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); List<Group> groups = projectionService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
assertEquals(GroupType.LECTURE, groups.get(0).getType()); assertEquals(GroupType.LECTURE, groups.get(0).getType());
assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility()); assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility());
@ -225,53 +224,58 @@ class ControllerServiceTest {
assertNull(groups.get(0).getParent()); assertNull(groups.get(0).getParent());
} }
//TODO: GroupServiceTest
@Test @Test
public void deleteUserTest() { public void deleteUserTest() {
controllerService.createGroup(account, "test", "hi", true, true, true, null, null); controllerService.createGroup(account, "test", "hi", true, true, true, null, null);
List<Group> groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); List<Group> groups = projectionService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
controllerService.addUser(account2, groups.get(0).getId()); groupService.addUser(account2, groups.get(0).getId());
User user = new User(account.getName(), "", "", ""); User user = new User(account.getName(), "", "", "");
controllerService.deleteUser(account, user, groups.get(0)); groupService.deleteUser(account, user, groups.get(0));
assertTrue(userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())).isEmpty()); assertTrue(projectionService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())).isEmpty());
} }
//TODO: GroupServiceTest
@Test @Test
public void updateRoleAdminTest() { public void updateRoleAdminTest() {
controllerService.createGroup(account, "test", "hi", null, null, true, null, null); controllerService.createGroup(account, "test", "hi", null, null, true, null, null);
List<Group> groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); List<Group> groups = projectionService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
controllerService.addUser(account2, groups.get(0).getId()); groupService.addUser(account2, groups.get(0).getId());
User user = new User(account.getName(), "", "", ""); User user = new User(account.getName(), "", "", "");
controllerService.updateRole(user, groups.get(0).getId()); groupService.updateRole(user, groups.get(0).getId());
groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); groups = projectionService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
assertEquals(Role.MEMBER, groups.get(0).getRoles().get(account.getName())); assertEquals(Role.MEMBER, groups.get(0).getRoles().get(account.getName()));
} }
//TODO: GroupServiceTest
@Test @Test
public void updateRoleMemberTest() { public void updateRoleMemberTest() {
controllerService.createGroup(account, "test", "hi", null, null, true, null, null); controllerService.createGroup(account, "test", "hi", null, null, true, null, null);
List<Group> groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); List<Group> groups = projectionService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
controllerService.addUser(account2, groups.get(0).getId()); groupService.addUser(account2, groups.get(0).getId());
User user = new User(account2.getName(), "", "", ""); User user = new User(account2.getName(), "", "", "");
controllerService.updateRole(user, groups.get(0).getId()); groupService.updateRole(user, groups.get(0).getId());
groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); groups = projectionService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
assertEquals(Role.ADMIN, groups.get(0).getRoles().get(account2.getName())); assertEquals(Role.ADMIN, groups.get(0).getRoles().get(account2.getName()));
} }
//TODO: GroupServiceTest
@Test @Test
public void updateRoleNonUserTest() { public void updateRoleNonUserTest() {
controllerService.createGroup(account, "test", "hi", null, null, true, null, null); controllerService.createGroup(account, "test", "hi", null, null, true, null, null);
List<Group> groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); List<Group> groups = projectionService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
User user = new User(account2.getName(), "", "", ""); User user = new User(account2.getName(), "", "", "");
Throwable exception = assertThrows(UserNotFoundException.class, () -> controllerService.updateRole(user, groups.get(0).getId())); Throwable exception = assertThrows(UserNotFoundException.class, () -> groupService.updateRole(user, groups.get(0).getId()));
assertEquals("404 NOT_FOUND \"Der User wurde nicht gefunden. (class mops.gruppen2.service.ValidationService)\"", exception.getMessage()); assertEquals("404 NOT_FOUND \"Der User wurde nicht gefunden. (class mops.gruppen2.service.ValidationService)\"", exception.getMessage());
} }
//TODO: GroupServiceTest
@Test @Test
public void deleteNonUserTest() { public void deleteNonUserTest() {
controllerService.createGroup(account, "test", "hi", true, null, true, null, null); controllerService.createGroup(account, "test", "hi", true, null, true, null, null);
List<Group> groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); List<Group> groups = projectionService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
User user = new User(account2.getName(), "", "", ""); User user = new User(account2.getName(), "", "", "");
Throwable exception = assertThrows(UserNotFoundException.class, () -> controllerService.deleteUser(account, user, groups.get(0))); Throwable exception = assertThrows(UserNotFoundException.class, () -> groupService.deleteUser(account, user, groups.get(0)));
assertEquals("404 NOT_FOUND \"Der User wurde nicht gefunden. (class mops.gruppen2.service.ValidationService)\"", exception.getMessage()); assertEquals("404 NOT_FOUND \"Der User wurde nicht gefunden. (class mops.gruppen2.service.ValidationService)\"", exception.getMessage());
} }
@ -280,43 +284,46 @@ class ControllerServiceTest {
assertEquals("hi", description); assertEquals("hi", description);
} }
//TODO: GroupServiceTest
@Test @Test
void passIfLastAdminTest() { void passIfLastAdminTest() {
controllerService.createGroup(account, "test", "hi", null, null, true, null, null); controllerService.createGroup(account, "test", "hi", null, null, true, null, null);
List<Group> groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); List<Group> groups = projectionService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
controllerService.addUser(account2, groups.get(0).getId()); groupService.addUser(account2, groups.get(0).getId());
User user = new User(account.getName(), "", "", ""); User user = new User(account.getName(), "", "", "");
groups = userService.getUserGroups(new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail())); groups = projectionService.getUserGroups(new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail()));
controllerService.deleteUser(account, user, groups.get(0)); groupService.deleteUser(account, user, groups.get(0));
groups = userService.getUserGroups(new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail())); groups = projectionService.getUserGroups(new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail()));
assertEquals(Role.ADMIN, groups.get(0).getRoles().get(account2.getName())); assertEquals(Role.ADMIN, groups.get(0).getRoles().get(account2.getName()));
} }
//TODO: GroupServiceTest
@Test @Test
void dontPassIfNotLastAdminTest() { void dontPassIfNotLastAdminTest() {
controllerService.createGroup(account, "test", "hi", null, null, true, null, null); controllerService.createGroup(account, "test", "hi", null, null, true, null, null);
List<Group> groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); List<Group> groups = projectionService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
controllerService.addUser(account2, groups.get(0).getId()); groupService.addUser(account2, groups.get(0).getId());
User user2 = new User(account2.getName(), "", "", ""); User user2 = new User(account2.getName(), "", "", "");
controllerService.updateRole(user2, groups.get(0).getId()); groupService.updateRole(user2, groups.get(0).getId());
controllerService.addUser(account3, groups.get(0).getId()); groupService.addUser(account3, groups.get(0).getId());
controllerService.changeRoleIfLastAdmin(account, groups.get(0)); groupService.changeRoleIfLastAdmin(account, groups.get(0));
User user = new User(account.getName(), "", "", ""); User user = new User(account.getName(), "", "", "");
controllerService.deleteUser(account, user, groups.get(0)); groupService.deleteUser(account, user, groups.get(0));
groups = userService.getUserGroups(new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail())); groups = projectionService.getUserGroups(new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail()));
assertEquals(Role.MEMBER, groups.get(0).getRoles().get(account3.getName())); assertEquals(Role.MEMBER, groups.get(0).getRoles().get(account3.getName()));
} }
//TODO: GroupServiceTest
@Test @Test
void getVeteranMemberTest() { void getVeteranMemberTest() {
controllerService.createGroup(account, "test", "hi", null, null, true, null, null); controllerService.createGroup(account, "test", "hi", null, null, true, null, null);
List<Group> groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); List<Group> groups = projectionService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
controllerService.addUser(account2, groups.get(0).getId()); groupService.addUser(account2, groups.get(0).getId());
controllerService.addUser(account3, groups.get(0).getId()); groupService.addUser(account3, groups.get(0).getId());
User user = new User(account.getName(), "", "", ""); User user = new User(account.getName(), "", "", "");
groups = userService.getUserGroups(new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail())); groups = projectionService.getUserGroups(new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail()));
controllerService.deleteUser(account, user, groups.get(0)); groupService.deleteUser(account, user, groups.get(0));
groups = userService.getUserGroups(new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail())); groups = projectionService.getUserGroups(new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail()));
assertEquals(Role.ADMIN, groups.get(0).getRoles().get(account2.getName())); assertEquals(Role.ADMIN, groups.get(0).getRoles().get(account2.getName()));
assertEquals(Role.MEMBER, groups.get(0).getRoles().get(account3.getName())); assertEquals(Role.MEMBER, groups.get(0).getRoles().get(account3.getName()));
} }

View File

@ -51,10 +51,14 @@ class GroupServiceTest {
ProjectionService projectionService; ProjectionService projectionService;
@Autowired @Autowired
private EventStoreService eventStoreService; private EventStoreService eventStoreService;
@Autowired
private ValidationService validationService;
@Autowired
private InviteService inviteService;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
groupService = new GroupService(eventStoreService, eventRepository); groupService = new GroupService(eventStoreService, eventRepository, validationService, inviteService, projectionService);
eventRepository.deleteAll(); eventRepository.deleteAll();
//noinspection SqlResolve //noinspection SqlResolve
template.execute("ALTER TABLE event ALTER COLUMN event_id RESTART WITH 1"); template.execute("ALTER TABLE event ALTER COLUMN event_id RESTART WITH 1");
@ -94,6 +98,7 @@ class GroupServiceTest {
assertThat(groups.stream().map(group -> group.getMembers().size()).reduce(Integer::sum).get()).isEqualTo(70); assertThat(groups.stream().map(group -> group.getMembers().size()).reduce(Integer::sum).get()).isEqualTo(70);
} }
//TODO: EventStoreServiceTest
@Test @Test
void getGroupEvents() { void getGroupEvents() {
eventStoreService.saveAll(createPublicGroupEvent(uuidMock(0)), eventStoreService.saveAll(createPublicGroupEvent(uuidMock(0)),
@ -102,9 +107,9 @@ class GroupServiceTest {
List<UUID> groupIds = Arrays.asList(uuidMock(0), uuidMock(1)); List<UUID> groupIds = Arrays.asList(uuidMock(0), uuidMock(1));
assertThat(groupService.getGroupEvents(groupIds)).hasSize(2); assertThat(eventStoreService.getGroupEvents(groupIds)).hasSize(2);
assertThat(groupService.getGroupEvents(groupIds).get(0).getGroupId()).isEqualTo(uuidMock(0)); assertThat(eventStoreService.getGroupEvents(groupIds).get(0).getGroupId()).isEqualTo(uuidMock(0));
assertThat(groupService.getGroupEvents(groupIds).get(1).getGroupId()).isEqualTo(uuidMock(1)); assertThat(eventStoreService.getGroupEvents(groupIds).get(1).getGroupId()).isEqualTo(uuidMock(1));
} }
//TODO: ProjectionServiceTest //TODO: ProjectionServiceTest