1

new eventbuilderservice + projectionservice + searchservice, some renamings etc

This commit is contained in:
Christoph
2020-04-06 00:07:46 +02:00
parent 5a5d1586ed
commit 3d66f58af8
17 changed files with 503 additions and 389 deletions

View File

@ -7,9 +7,10 @@ import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.api.GroupRequestWrapper; import mops.gruppen2.domain.api.GroupRequestWrapper;
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.service.APIFormatterService; import mops.gruppen2.service.APIService;
import mops.gruppen2.service.EventService; import mops.gruppen2.service.EventStoreService;
import mops.gruppen2.service.GroupService; import mops.gruppen2.service.GroupService;
import mops.gruppen2.service.ProjectionService;
import mops.gruppen2.service.UserService; 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;
@ -29,23 +30,25 @@ import java.util.stream.Collectors;
@RequestMapping("/gruppen2/api") @RequestMapping("/gruppen2/api")
public class APIController { public class APIController {
private final EventService eventService; private final EventStoreService eventStoreService;
private final GroupService groupService; private final GroupService groupService;
private final UserService userService; private final UserService userService;
private final ProjectionService projectionService;
public APIController(EventService eventService, GroupService groupService, UserService userService) { public APIController(EventStoreService eventStoreService, GroupService groupService, UserService userService, ProjectionService projectionService) {
this.eventService = eventService; this.eventStoreService = eventStoreService;
this.groupService = groupService; this.groupService = groupService;
this.userService = userService; this.userService = userService;
this.projectionService = projectionService;
} }
@GetMapping("/updateGroups/{lastEventId}") @GetMapping("/updateGroups/{lastEventId}")
@Secured("ROLE_api_user") @Secured("ROLE_api_user")
@ApiOperation("Gibt alle Gruppen zurück, in denen sich etwas geändert hat") @ApiOperation("Gibt alle Gruppen zurück, in denen sich etwas geändert hat")
public GroupRequestWrapper updateGroups(@ApiParam("Letzter Status des Anfragestellers") @PathVariable Long lastEventId) throws EventException { public GroupRequestWrapper updateGroups(@ApiParam("Letzter Status des Anfragestellers") @PathVariable Long lastEventId) throws EventException {
List<Event> events = eventService.getNewEvents(lastEventId); List<Event> events = eventStoreService.getNewEvents(lastEventId);
return APIFormatterService.wrap(eventService.getMaxEventId(), groupService.projectEventList(events)); return APIService.wrap(eventStoreService.getMaxEventId(), projectionService.projectEventList(events));
} }
@GetMapping("/getGroupIdsOfUser/{userId}") @GetMapping("/getGroupIdsOfUser/{userId}")
@ -61,8 +64,8 @@ public class APIController {
@Secured("ROLE_api_user") @Secured("ROLE_api_user")
@ApiOperation("Gibt die Gruppe mit der als Parameter mitgegebenden groupId zurück") @ApiOperation("Gibt die Gruppe mit der als Parameter mitgegebenden groupId zurück")
public Group getGroupById(@ApiParam("GruppenId der gefordeten Gruppe") @PathVariable String groupId) throws EventException { public Group getGroupById(@ApiParam("GruppenId der gefordeten Gruppe") @PathVariable String groupId) throws EventException {
List<Event> eventList = eventService.getEventsOfGroup(UUID.fromString(groupId)); List<Event> eventList = eventStoreService.getEventsOfGroup(UUID.fromString(groupId));
List<Group> groups = groupService.projectEventList(eventList); List<Group> groups = projectionService.projectEventList(eventList);
if (groups.isEmpty()) { if (groups.isEmpty()) {
return null; return null;

View File

@ -4,6 +4,7 @@ import mops.gruppen2.domain.Account;
import mops.gruppen2.service.ControllerService; import mops.gruppen2.service.ControllerService;
import mops.gruppen2.service.GroupService; import mops.gruppen2.service.GroupService;
import mops.gruppen2.service.KeyCloakService; import mops.gruppen2.service.KeyCloakService;
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;
@ -27,11 +28,13 @@ public class GroupCreationController {
private final GroupService groupService; private final GroupService groupService;
private final ControllerService controllerService; private final ControllerService controllerService;
private final ValidationService validationService; private final ValidationService validationService;
private final ProjectionService projectionService;
public GroupCreationController(GroupService groupService, ControllerService controllerService, ValidationService validationService) { public GroupCreationController(GroupService groupService, ControllerService controllerService, ValidationService validationService, ProjectionService projectionService) {
this.groupService = groupService; this.groupService = groupService;
this.controllerService = controllerService; this.controllerService = controllerService;
this.validationService = validationService; this.validationService = validationService;
this.projectionService = projectionService;
} }
@RolesAllowed({"ROLE_orga", "ROLE_actuator"}) @RolesAllowed({"ROLE_orga", "ROLE_actuator"})
@ -42,7 +45,7 @@ public class GroupCreationController {
Account account = KeyCloakService.createAccountFromPrincipal(token); Account account = KeyCloakService.createAccountFromPrincipal(token);
model.addAttribute("account", account); model.addAttribute("account", account);
model.addAttribute("lectures", groupService.getAllLecturesWithVisibilityPublic()); model.addAttribute("lectures", projectionService.getAllLecturesWithVisibilityPublic());
return "createOrga"; return "createOrga";
} }
@ -85,7 +88,7 @@ public class GroupCreationController {
Account account = KeyCloakService.createAccountFromPrincipal(token); Account account = KeyCloakService.createAccountFromPrincipal(token);
model.addAttribute("account", account); model.addAttribute("account", account);
model.addAttribute("lectures", groupService.getAllLecturesWithVisibilityPublic()); model.addAttribute("lectures", projectionService.getAllLecturesWithVisibilityPublic());
return "createStudent"; return "createStudent";
} }

View File

@ -7,11 +7,19 @@ import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
@Service @Service
public final class APIFormatterService { public class APIService {
private APIFormatterService() {} // private APIService() {}
public static GroupRequestWrapper wrap(long status, List<Group> groupList) { public static GroupRequestWrapper wrap(long status, List<Group> groupList) {
return new GroupRequestWrapper(status, groupList); return new GroupRequestWrapper(status, groupList);
} }
// public static void updateGroups()
// public static void getGroupIdsOfUser()
// public static void getGroupById()
// public static void updateNecessary()
} }

View File

@ -15,18 +15,15 @@ import mops.gruppen2.domain.event.UpdateGroupTitleEvent;
import mops.gruppen2.domain.event.UpdateRoleEvent; import mops.gruppen2.domain.event.UpdateRoleEvent;
import mops.gruppen2.domain.event.UpdateUserMaxEvent; import mops.gruppen2.domain.event.UpdateUserMaxEvent;
import mops.gruppen2.domain.exception.EventException; import mops.gruppen2.domain.exception.EventException;
import mops.gruppen2.domain.exception.WrongFileException;
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;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors;
import static mops.gruppen2.domain.Role.ADMIN; import static mops.gruppen2.domain.Role.ADMIN;
@ -35,29 +32,18 @@ import static mops.gruppen2.domain.Role.ADMIN;
public class ControllerService { public class ControllerService {
private static final Logger LOG = LoggerFactory.getLogger("controllerServiceLogger"); private static final Logger LOG = LoggerFactory.getLogger("controllerServiceLogger");
private final EventService eventService; private final EventStoreService eventStoreService;
private final UserService userService; private final UserService userService;
private final ValidationService validationService; private final ValidationService validationService;
private final InviteService inviteService; private final InviteService inviteService;
public ControllerService(EventService eventService, UserService userService, ValidationService validationService, InviteService inviteService) { public ControllerService(EventStoreService eventStoreService, UserService userService, ValidationService validationService, InviteService inviteService) {
this.eventService = eventService; this.eventStoreService = eventStoreService;
this.userService = userService; this.userService = userService;
this.validationService = validationService; this.validationService = validationService;
this.inviteService = inviteService; this.inviteService = inviteService;
} }
private static User getVeteranMember(Account account, Group group) {
List<User> 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 new User(newAdminId, "", "", "");
}
/** /**
* Wie createGroup, nur das hier die Gruppe auch als Veranstaltung gesetzt werden kann und CSV Dateien mit Nutzern * Wie createGroup, nur das hier die Gruppe auch als Veranstaltung gesetzt werden kann und CSV Dateien mit Nutzern
* eingelesen werden können. * eingelesen werden können.
@ -72,6 +58,8 @@ public class ControllerService {
* @param parent Parameter für die neue Gruppe * @param parent Parameter für die neue Gruppe
* @param file Parameter für die neue Gruppe * @param file Parameter für die neue Gruppe
*/ */
//TODO: remove booleans + add wrapper?
//TODO: auslagern teilweise -> EventBuilderService
public void createGroupAsOrga(Account account, public void createGroupAsOrga(Account account,
String title, String title,
String description, String description,
@ -82,17 +70,17 @@ public class ControllerService {
UUID parent, UUID parent,
MultipartFile file) { MultipartFile file) {
userMaximum = checkInfiniteUsers(isMaximumInfinite, userMaximum); userMaximum = GroupService.checkInfiniteUsers(isMaximumInfinite, userMaximum);
List<User> newUsers = readCsvFile(file); List<User> newUsers = CsvService.readCsvFile(file);
List<User> oldUsers = new ArrayList<>(); List<User> oldUsers = new ArrayList<>();
User user = new User(account); User user = new User(account);
oldUsers.add(user); oldUsers.add(user);
removeOldUsersFromNewUsers(oldUsers, newUsers); GroupService.removeOldUsersFromNewUsers(oldUsers, newUsers);
userMaximum = adjustUserMaximum((long) newUsers.size(), 1L, userMaximum); userMaximum = GroupService.adjustUserMaximum((long) newUsers.size(), 1L, userMaximum);
UUID groupId = createGroup(account, UUID groupId = createGroup(account,
title, title,
@ -105,25 +93,6 @@ public class ControllerService {
addUserList(newUsers, groupId); addUserList(newUsers, groupId);
} }
/**
* Wenn die maximale Useranzahl unendlich ist, wird das Maximum auf 100000 gesetzt. Praktisch gibt es also Maximla 100000
* Nutzer pro Gruppe.
*
* @param isMaximumInfinite Gibt an ob es unendlich viele User geben soll
* @param userMaximum Das Maximum an Usern, falls es eins gibt
*
* @return Maximum an Usern
*/
private static Long checkInfiniteUsers(Boolean isMaximumInfinite, Long userMaximum) {
isMaximumInfinite = isMaximumInfinite != null;
if (isMaximumInfinite) {
userMaximum = 100_000L;
}
return userMaximum;
}
/** /**
* Erzeugt eine neue Gruppe, fügt den User, der die Gruppe erstellt hat, hinzu und setzt seine Rolle als Admin fest. * Erzeugt eine neue Gruppe, fügt den User, der die Gruppe erstellt hat, hinzu und setzt seine Rolle als Admin fest.
* Zudem wird der Gruppentitel und die Gruppenbeschreibung erzeugt, welche vorher der Methode übergeben wurden. * Zudem wird der Gruppentitel und die Gruppenbeschreibung erzeugt, welche vorher der Methode übergeben wurden.
@ -133,7 +102,8 @@ public class ControllerService {
* @param title Gruppentitel * @param title Gruppentitel
* @param description Gruppenbeschreibung * @param description Gruppenbeschreibung
*/ */
//TODO: remove booleans //TODO: remove booleans + add wrapper?
//TODO: auslagern teilweise -> EventBuilderService
public UUID createGroup(Account account, public UUID createGroup(Account account,
String title, String title,
String description, String description,
@ -143,12 +113,12 @@ public class ControllerService {
Long userMaximum, Long userMaximum,
UUID parent) { UUID parent) {
userMaximum = checkInfiniteUsers(isMaximumInfinite, userMaximum); userMaximum = GroupService.checkInfiniteUsers(isMaximumInfinite, userMaximum);
Visibility groupVisibility = setGroupVisibility(isVisibilityPrivate); Visibility groupVisibility = GroupService.setGroupVisibility(isVisibilityPrivate);
UUID groupId = UUID.randomUUID(); UUID groupId = UUID.randomUUID();
GroupType groupType = setGroupType(isLecture); GroupType groupType = GroupService.setGroupType(isLecture);
CreateGroupEvent createGroupEvent = new CreateGroupEvent(groupId, CreateGroupEvent createGroupEvent = new CreateGroupEvent(groupId,
account.getName(), account.getName(),
@ -156,7 +126,7 @@ public class ControllerService {
groupType, groupType,
groupVisibility, groupVisibility,
userMaximum); userMaximum);
eventService.saveEvent(createGroupEvent); eventStoreService.saveEvent(createGroupEvent);
inviteService.createLink(groupId); inviteService.createLink(groupId);
@ -170,35 +140,7 @@ public class ControllerService {
return groupId; return groupId;
} }
private static List<User> readCsvFile(MultipartFile file) throws EventException { //TODO: GroupService/eventbuilderservice
if (file == null) {
return new ArrayList<>();
}
if (!file.isEmpty()) {
try {
List<User> userList = CsvService.read(file.getInputStream());
return userList.stream().distinct().collect(Collectors.toList()); //filters duplicates from list
} catch (IOException ex) {
LOG.warn("File konnte nicht gelesen werden");
throw new WrongFileException(file.getOriginalFilename());
}
}
return new ArrayList<>();
}
private static void removeOldUsersFromNewUsers(List<User> oldUsers, List<User> newUsers) {
for (User oldUser : oldUsers) {
newUsers.remove(oldUser);
}
}
private static Long adjustUserMaximum(Long newUsers, Long oldUsers, Long maxUsers) {
if (oldUsers + newUsers > maxUsers) {
maxUsers = oldUsers + newUsers;
}
return maxUsers;
}
private void addUserList(List<User> newUsers, UUID groupId) { private void addUserList(List<User> newUsers, UUID groupId) {
for (User user : newUsers) { for (User user : newUsers) {
Group group = userService.getGroupById(groupId); Group group = userService.getGroupById(groupId);
@ -206,40 +148,24 @@ public class ControllerService {
LOG.info("Benutzer {} ist bereits in Gruppe", user.getId()); LOG.info("Benutzer {} ist bereits in Gruppe", user.getId());
} else { } else {
AddUserEvent addUserEvent = new AddUserEvent(groupId, user.getId(), user.getGivenname(), user.getFamilyname(), user.getEmail()); AddUserEvent addUserEvent = new AddUserEvent(groupId, user.getId(), user.getGivenname(), user.getFamilyname(), user.getEmail());
eventService.saveEvent(addUserEvent); eventStoreService.saveEvent(addUserEvent);
} }
} }
} }
private static Visibility setGroupVisibility(Boolean isVisibilityPrivate) { //TODO: GroupService/eventbuilderservice
isVisibilityPrivate = isVisibilityPrivate != null;
if (isVisibilityPrivate) {
return Visibility.PRIVATE;
} else {
return Visibility.PUBLIC;
}
}
private static GroupType setGroupType(Boolean isLecture) {
isLecture = isLecture != null;
if (isLecture) {
return GroupType.LECTURE;
} else {
return GroupType.SIMPLE;
}
}
public void addUser(Account account, UUID groupId) { public void addUser(Account account, UUID groupId) {
AddUserEvent addUserEvent = new AddUserEvent(groupId, account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); AddUserEvent addUserEvent = new AddUserEvent(groupId, account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
eventService.saveEvent(addUserEvent); eventStoreService.saveEvent(addUserEvent);
} }
//TODO: GroupService/eventbuilderservice
private void updateTitle(Account account, UUID groupId, String title) { private void updateTitle(Account account, UUID groupId, String title) {
UpdateGroupTitleEvent updateGroupTitleEvent = new UpdateGroupTitleEvent(groupId, account.getName(), title); UpdateGroupTitleEvent updateGroupTitleEvent = new UpdateGroupTitleEvent(groupId, account.getName(), title);
eventService.saveEvent(updateGroupTitleEvent); eventStoreService.saveEvent(updateGroupTitleEvent);
} }
//TODO: GroupService/eventbuilderservice
public void updateRole(User user, UUID groupId) throws EventException { public void updateRole(User user, UUID groupId) throws EventException {
UpdateRoleEvent updateRoleEvent; UpdateRoleEvent updateRoleEvent;
Group group = userService.getGroupById(groupId); Group group = userService.getGroupById(groupId);
@ -250,23 +176,25 @@ public class ControllerService {
} else { } else {
updateRoleEvent = new UpdateRoleEvent(group.getId(), user.getId(), ADMIN); updateRoleEvent = new UpdateRoleEvent(group.getId(), user.getId(), ADMIN);
} }
eventService.saveEvent(updateRoleEvent); eventStoreService.saveEvent(updateRoleEvent);
} }
//TODO: GroupService/eventbuilderservice
private void updateDescription(Account account, UUID groupId, String description) { private void updateDescription(Account account, UUID groupId, String description) {
UpdateGroupDescriptionEvent updateGroupDescriptionEvent = new UpdateGroupDescriptionEvent(groupId, account.getName(), description); UpdateGroupDescriptionEvent updateGroupDescriptionEvent = new UpdateGroupDescriptionEvent(groupId, account.getName(), description);
eventService.saveEvent(updateGroupDescriptionEvent); eventStoreService.saveEvent(updateGroupDescriptionEvent);
} }
//TODO: GroupService
public void addUsersFromCsv(Account account, MultipartFile file, String groupId) { public void addUsersFromCsv(Account account, MultipartFile file, String groupId) {
Group group = userService.getGroupById(UUID.fromString(groupId)); Group group = userService.getGroupById(UUID.fromString(groupId));
List<User> newUserList = readCsvFile(file); List<User> newUserList = CsvService.readCsvFile(file);
removeOldUsersFromNewUsers(group.getMembers(), newUserList); GroupService.removeOldUsersFromNewUsers(group.getMembers(), newUserList);
UUID groupUUID = getUUID(groupId); UUID groupUUID = getUUID(groupId);
Long newUserMaximum = adjustUserMaximum((long) newUserList.size(), (long) group.getMembers().size(), group.getUserMaximum()); Long newUserMaximum = GroupService.adjustUserMaximum((long) newUserList.size(), (long) group.getMembers().size(), group.getUserMaximum());
if (newUserMaximum > group.getUserMaximum()) { if (newUserMaximum > group.getUserMaximum()) {
updateMaxUser(account, groupUUID, newUserMaximum); updateMaxUser(account, groupUUID, newUserMaximum);
} }
@ -274,15 +202,18 @@ public class ControllerService {
addUserList(newUserList, groupUUID); addUserList(newUserList, groupUUID);
} }
//TODO: GroupService
public UUID getUUID(String id) { public UUID getUUID(String id) {
return UUID.fromString(Objects.requireNonNullElse(id, "00000000-0000-0000-0000-000000000000")); return UUID.fromString(Objects.requireNonNullElse(id, "00000000-0000-0000-0000-000000000000"));
} }
//TODO: GroupService/eventbuilderservice
public void updateMaxUser(Account account, UUID groupId, Long userMaximum) { public void updateMaxUser(Account account, UUID groupId, Long userMaximum) {
UpdateUserMaxEvent updateUserMaxEvent = new UpdateUserMaxEvent(groupId, account.getName(), userMaximum); UpdateUserMaxEvent updateUserMaxEvent = new UpdateUserMaxEvent(groupId, account.getName(), userMaximum);
eventService.saveEvent(updateUserMaxEvent); eventStoreService.saveEvent(updateUserMaxEvent);
} }
//TODO: GroupService
public void changeMetaData(Account account, Group group, String title, String description) { public void changeMetaData(Account account, Group group, String title, String description) {
if (!title.equals(group.getTitle())) { if (!title.equals(group.getTitle())) {
updateTitle(account, group.getId(), title); updateTitle(account, group.getId(), title);
@ -293,14 +224,16 @@ public class ControllerService {
} }
} }
//TODO: GroupService oder in Group?
public Group getParent(UUID parentId) { public Group getParent(UUID parentId) {
Group parent = new Group(); Group parent = new Group();
if (!idIsEmpty(parentId)) { if (!GroupService.idIsEmpty(parentId)) {
parent = userService.getGroupById(parentId); parent = userService.getGroupById(parentId);
} }
return parent; return parent;
} }
//TODO: GroupService
public void deleteUser(Account account, User user, Group group) throws EventException { public void deleteUser(Account account, User user, Group group) throws EventException {
changeRoleIfLastAdmin(account, group); changeRoleIfLastAdmin(account, group);
@ -313,32 +246,28 @@ public class ControllerService {
} }
} }
private static boolean idIsEmpty(UUID id) { //TODO: GroupService/eventbuilderservice
if (id == null) {
return true;
}
return "00000000-0000-0000-0000-000000000000".equals(id.toString());
}
private void deleteUserEvent(User user, UUID groupId) { private void deleteUserEvent(User user, UUID groupId) {
DeleteUserEvent deleteUserEvent = new DeleteUserEvent(groupId, user.getId()); DeleteUserEvent deleteUserEvent = new DeleteUserEvent(groupId, user.getId());
eventService.saveEvent(deleteUserEvent); eventStoreService.saveEvent(deleteUserEvent);
} }
//TODO: GroupService/eventbuilderservice
public void deleteGroupEvent(String userId, UUID groupId) { public void deleteGroupEvent(String userId, UUID groupId) {
DeleteGroupEvent deleteGroupEvent = new DeleteGroupEvent(groupId, userId); DeleteGroupEvent deleteGroupEvent = new DeleteGroupEvent(groupId, userId);
inviteService.destroyLink(groupId); inviteService.destroyLink(groupId);
eventService.saveEvent(deleteGroupEvent); eventStoreService.saveEvent(deleteGroupEvent);
} }
//TODO: GroupService
private void promoteVeteranMember(Account account, Group group) { private void promoteVeteranMember(Account account, Group group) {
if (validationService.checkIfLastAdmin(account, group)) { if (validationService.checkIfLastAdmin(account, group)) {
User newAdmin = getVeteranMember(account, group); User newAdmin = GroupService.getVeteranMember(account, group);
updateRole(newAdmin, group.getId()); updateRole(newAdmin, group.getId());
} }
} }
//TODO: GroupService
public void changeRoleIfLastAdmin(Account account, Group group) { public void changeRoleIfLastAdmin(Account account, Group group) {
if (group.getMembers().size() <= 1) { if (group.getMembers().size() <= 1) {
return; return;
@ -346,6 +275,7 @@ public class ControllerService {
promoteVeteranMember(account, group); promoteVeteranMember(account, group);
} }
//TODO: GroupService
public void changeRole(Account account, User user, Group group) { public void changeRole(Account account, User user, Group group) {
if (user.getId().equals(account.getName())) { if (user.getId().equals(account.getName())) {
if (group.getMembers().size() <= 1) { if (group.getMembers().size() <= 1) {

View File

@ -4,15 +4,24 @@ import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.dataformat.csv.CsvMapper; import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema; import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import mops.gruppen2.domain.User; import mops.gruppen2.domain.User;
import mops.gruppen2.domain.exception.EventException;
import mops.gruppen2.domain.exception.WrongFileException;
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.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
@Service @Service
public final class CsvService { public final class CsvService {
private static final Logger LOG = LoggerFactory.getLogger(CsvService.class);
private CsvService() {} private CsvService() {}
static List<User> read(InputStream stream) throws IOException { static List<User> read(InputStream stream) throws IOException {
@ -23,4 +32,21 @@ public final class CsvService {
return reader.<User>readValues(stream).readAll(); return reader.<User>readValues(stream).readAll();
} }
//TODO: CsvService
static List<User> readCsvFile(MultipartFile file) throws EventException {
if (file == null) {
return new ArrayList<>();
}
if (!file.isEmpty()) {
try {
List<User> userList = read(file.getInputStream());
return userList.stream().distinct().collect(Collectors.toList()); //filters duplicates from list
} catch (IOException ex) {
LOG.warn("File konnte nicht gelesen werden");
throw new WrongFileException(file.getOriginalFilename());
}
}
return new ArrayList<>();
}
} }

View File

@ -0,0 +1,4 @@
package mops.gruppen2.service;
public class EventBuilderService {
}

View File

@ -15,12 +15,12 @@ import java.util.stream.Collectors;
@Service @Service
//TODO: Evtl aufsplitten in EventRepoService und EventService? //TODO: Evtl aufsplitten in EventRepoService und EventService?
public class EventService { public class EventStoreService {
private static final Logger LOG = LoggerFactory.getLogger(EventService.class); private static final Logger LOG = LoggerFactory.getLogger(EventStoreService.class);
private final EventRepository eventStore; private final EventRepository eventStore;
public EventService(EventRepository eventStore) { public EventStoreService(EventRepository eventStore) {
this.eventStore = eventStore; this.eventStore = eventStore;
} }

View File

@ -3,32 +3,101 @@ 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.User;
import mops.gruppen2.domain.Visibility; import mops.gruppen2.domain.Visibility;
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.EventException;
import mops.gruppen2.repository.EventRepository; import mops.gruppen2.repository.EventRepository;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors;
/**
* Behandelt Aufgaben, welche sich auf eine Gruppe beziehen
*/
@Service @Service
public class GroupService { public class GroupService {
private final EventService eventService; private final EventStoreService eventStoreService;
private final EventRepository eventRepository; private final EventRepository eventRepository;
public GroupService(EventService eventService, EventRepository eventRepository) { public GroupService(EventStoreService eventStoreService, EventRepository eventRepository) {
this.eventService = eventService; this.eventStoreService = eventStoreService;
this.eventRepository = eventRepository; this.eventRepository = eventRepository;
} }
static User getVeteranMember(Account account, Group group) {
List<User> 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 new User(newAdminId, "", "", "");
}
/**
* Wenn die maximale Useranzahl unendlich ist, wird das Maximum auf 100000 gesetzt. Praktisch gibt es also Maximla 100000
* Nutzer pro Gruppe.
*
* @param isMaximumInfinite Gibt an ob es unendlich viele User geben soll
* @param userMaximum Das Maximum an Usern, falls es eins gibt
*
* @return Maximum an Usern
*/
static Long checkInfiniteUsers(Boolean isMaximumInfinite, Long userMaximum) {
isMaximumInfinite = isMaximumInfinite != null;
if (isMaximumInfinite) {
userMaximum = 100_000L;
}
return userMaximum;
}
static void removeOldUsersFromNewUsers(List<User> oldUsers, List<User> newUsers) {
for (User oldUser : oldUsers) {
newUsers.remove(oldUser);
}
}
static Long adjustUserMaximum(Long newUsers, Long oldUsers, Long maxUsers) {
if (oldUsers + newUsers > maxUsers) {
maxUsers = oldUsers + newUsers;
}
return maxUsers;
}
static Visibility setGroupVisibility(Boolean isVisibilityPrivate) {
isVisibilityPrivate = isVisibilityPrivate != null;
if (isVisibilityPrivate) {
return Visibility.PRIVATE;
} else {
return Visibility.PUBLIC;
}
}
static GroupType setGroupType(Boolean isLecture) {
isLecture = isLecture != null;
if (isLecture) {
return GroupType.LECTURE;
} else {
return GroupType.SIMPLE;
}
}
static boolean idIsEmpty(UUID id) {
if (id == null) {
return true;
}
return "00000000-0000-0000-0000-000000000000".equals(id.toString());
}
/** /**
* Sucht in der DB alle Zeilen raus welche eine der Gruppen_ids hat. * 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. * Wandelt die Zeilen in Events um und gibt davon eine Liste zurück.
@ -43,128 +112,7 @@ public class GroupService {
for (UUID groupId : groupIds) { for (UUID groupId : groupIds) {
eventDTOS.addAll(eventRepository.findEventDTOByGroupId(groupId.toString())); eventDTOS.addAll(eventRepository.findEventDTOByGroupId(groupId.toString()));
} }
return eventService.getEventsFromDTOs(eventDTOS); return eventStoreService.getEventsFromDTOs(eventDTOS);
} }
/**
* Wird verwendet beim Gruppe erstellen bei der Parent-Auswahl: nur Titel benötigt.
*
* @return List of groups
*/
@Cacheable("groups")
public List<Group> getAllLecturesWithVisibilityPublic() {
List<Event> createEvents = eventService.getEventsFromDTOs(eventRepository.findAllEventsByType("CreateGroupEvent"));
createEvents.addAll(eventService.getEventsFromDTOs(eventRepository.findAllEventsByType("DeleteGroupEvent")));
createEvents.addAll(eventService.getEventsFromDTOs(eventRepository.findAllEventsByType("UpdateGroupTitleEvent")));
createEvents.addAll(eventService.getEventsFromDTOs(eventRepository.findAllEventsByType("DeleteGroupEvent")));
List<Group> visibleGroups = projectEventList(createEvents);
return visibleGroups.stream()
.filter(group -> group.getType() == GroupType.LECTURE)
.filter(group -> group.getVisibility() == Visibility.PUBLIC)
.collect(Collectors.toList());
}
/**
* Erzeugt eine neue Map wo Gruppen aus den Events erzeugt und den Gruppen_ids zugeordnet werden.
* Die Gruppen werden als Liste zurückgegeben.
*
* @param events Liste an Events
*
* @return Liste an Projizierten Gruppen
*
* @throws EventException Projektionsfehler
*/
public List<Group> projectEventList(List<Event> events) throws EventException {
Map<UUID, Group> groupMap = new HashMap<>();
events.parallelStream()
.forEachOrdered(event -> event.apply(getOrCreateGroup(groupMap, event.getGroupId())));
return new ArrayList<>(groupMap.values());
}
/**
* Gibt die Gruppe mit der richtigen Id aus der übergebenen Map wieder, existiert diese nicht
* wird die Gruppe erstellt und der Map hizugefügt.
*
* @param groups Map aus GruppenIds und Gruppen
* @param groupId Die Id der Gruppe, die zurückgegeben werden soll
*
* @return Die gesuchte Gruppe
*/
private static Group getOrCreateGroup(Map<UUID, Group> groups, UUID groupId) {
if (!groups.containsKey(groupId)) {
groups.put(groupId, new Group());
}
return groups.get(groupId);
}
/**
* Filtert alle öffentliche Gruppen nach dem Suchbegriff und gibt diese als Liste von Gruppen zurück.
* Groß und Kleinschreibung wird nicht beachtet.
*
* @param search Der Suchstring
*
* @return Liste von projizierten Gruppen
*
* @throws EventException Projektionsfehler
*/
//Todo Rename
@Cacheable("groups")
public List<Group> findGroupWith(String search, Account account) throws EventException {
if (search.isEmpty()) {
return getAllGroupWithVisibilityPublic(account.getName());
}
return getAllGroupWithVisibilityPublic(account.getName()).parallelStream().filter(group -> group.getTitle().toLowerCase().contains(search.toLowerCase()) || group.getDescription().toLowerCase().contains(search.toLowerCase())).collect(Collectors.toList());
}
/**
* Wird verwendet bei der Suche nach Gruppen: Titel, Beschreibung werden benötigt.
* Außerdem wird beachtet, ob der eingeloggte User bereits in entsprechenden Gruppen mitglied ist.
*
* @return Liste von projizierten Gruppen
*
* @throws EventException Projektionsfehler
*/
//TODO Rename
@Cacheable("groups")
public List<Group> getAllGroupWithVisibilityPublic(String userId) throws EventException {
List<Event> groupEvents = eventService.getEventsFromDTOs(eventRepository.findAllEventsByType("CreateGroupEvent"));
groupEvents.addAll(eventService.getEventsFromDTOs(eventRepository.findAllEventsByType("UpdateGroupDescriptionEvent")));
groupEvents.addAll(eventService.getEventsFromDTOs(eventRepository.findAllEventsByType("UpdateGroupTitleEvent")));
groupEvents.addAll(eventService.getEventsFromDTOs(eventRepository.findAllEventsByType("DeleteGroupEvent")));
groupEvents.addAll(eventService.getEventsFromDTOs(eventRepository.findAllEventsByType("UpdateUserMaxEvent")));
List<Group> visibleGroups = projectEventList(groupEvents);
sortByGroupType(visibleGroups);
return visibleGroups.stream()
.filter(group -> group.getType() != null)
.filter(group -> !eventService.userInGroup(group.getId(), userId))
.filter(group -> group.getVisibility() == Visibility.PUBLIC)
.collect(Collectors.toList());
}
/**
* Sortiert die übergebene Liste an Gruppen, sodass Veranstaltungen am Anfang der Liste sind.
*
* @param groups Die Liste von Gruppen die sortiert werden soll
*/
void sortByGroupType(List<Group> groups) {
groups.sort((Group g1, Group g2) -> {
if (g1.getType() == GroupType.LECTURE) {
return -1;
}
if (g2.getType() == GroupType.LECTURE) {
return 0;
}
return 1;
});
}
} }

View File

@ -33,9 +33,8 @@ public class InviteService {
return UUID.fromString(inviteRepository.findGroupIdByLink(link)); return UUID.fromString(inviteRepository.findGroupIdByLink(link));
} catch (Exception e) { } catch (Exception e) {
LOG.error("Gruppe zu Link ({}) konnte nicht gefunden werden!", link); LOG.error("Gruppe zu Link ({}) konnte nicht gefunden werden!", link);
throw new InvalidInviteException(link);
} }
throw new InvalidInviteException(link);
} }
public String getLinkByGroupId(UUID groupId) { public String getLinkByGroupId(UUID groupId) {
@ -43,8 +42,7 @@ public class InviteService {
return inviteRepository.findLinkByGroupId(groupId.toString()); return inviteRepository.findLinkByGroupId(groupId.toString());
} catch (Exception e) { } catch (Exception e) {
LOG.error("Link zu Gruppe ({}) konnte nicht gefunden werden!", groupId); LOG.error("Link zu Gruppe ({}) konnte nicht gefunden werden!", groupId);
throw new NoInviteExistException(groupId.toString());
} }
throw new NoInviteExistException(groupId.toString());
} }
} }

View File

@ -0,0 +1,120 @@
package mops.gruppen2.service;
import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.GroupType;
import mops.gruppen2.domain.Visibility;
import mops.gruppen2.domain.event.Event;
import mops.gruppen2.domain.exception.EventException;
import mops.gruppen2.repository.EventRepository;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
/**
* Liefert verschiedene Projektionen auf Gruppen
*/
@Service
public class ProjectionService {
private final EventRepository eventRepository;
private final EventStoreService eventStoreService;
public ProjectionService(EventRepository eventRepository, EventStoreService eventStoreService) {
this.eventRepository = eventRepository;
this.eventStoreService = eventStoreService;
}
/**
* Gibt die Gruppe mit der richtigen Id aus der übergebenen Map wieder, existiert diese nicht
* wird die Gruppe erstellt und der Map hizugefügt.
*
* @param groups Map aus GruppenIds und Gruppen
* @param groupId Die Id der Gruppe, die zurückgegeben werden soll
*
* @return Die gesuchte Gruppe
*/
//TODO: ProjectionService
private static Group getOrCreateGroup(Map<UUID, Group> groups, UUID groupId) {
if (!groups.containsKey(groupId)) {
groups.put(groupId, new Group());
}
return groups.get(groupId);
}
/**
* Wird verwendet bei der Suche nach Gruppen: Titel, Beschreibung werden benötigt.
* Außerdem wird beachtet, ob der eingeloggte User bereits in entsprechenden Gruppen mitglied ist.
*
* @return Liste von projizierten Gruppen
*
* @throws EventException Projektionsfehler
*/
//TODO: ProjectionService
//TODO Rename
@Cacheable("groups")
public List<Group> getAllGroupWithVisibilityPublic(String userId) throws EventException {
List<Event> groupEvents = eventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("CreateGroupEvent"));
groupEvents.addAll(eventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("UpdateGroupDescriptionEvent")));
groupEvents.addAll(eventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("UpdateGroupTitleEvent")));
groupEvents.addAll(eventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("DeleteGroupEvent")));
groupEvents.addAll(eventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("UpdateUserMaxEvent")));
List<Group> visibleGroups = projectEventList(groupEvents);
SearchService.sortByGroupType(visibleGroups);
return visibleGroups.stream()
.filter(group -> group.getType() != null)
.filter(group -> !eventStoreService.userInGroup(group.getId(), userId))
.filter(group -> group.getVisibility() == Visibility.PUBLIC)
.collect(Collectors.toList());
}
/**
* Wird verwendet beim Gruppe erstellen bei der Parent-Auswahl: nur Titel benötigt.
*
* @return List of groups
*/
@Cacheable("groups")
//TODO: ProjectionService
public List<Group> getAllLecturesWithVisibilityPublic() {
List<Event> createEvents = eventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("CreateGroupEvent"));
createEvents.addAll(eventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("DeleteGroupEvent")));
createEvents.addAll(eventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("UpdateGroupTitleEvent")));
createEvents.addAll(eventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("DeleteGroupEvent")));
List<Group> visibleGroups = projectEventList(createEvents);
return visibleGroups.stream()
.filter(group -> group.getType() == GroupType.LECTURE)
.filter(group -> group.getVisibility() == Visibility.PUBLIC)
.collect(Collectors.toList());
}
/**
* Erzeugt eine neue Map wo Gruppen aus den Events erzeugt und den Gruppen_ids zugeordnet werden.
* Die Gruppen werden als Liste zurückgegeben.
*
* @param events Liste an Events
*
* @return Liste an Projizierten Gruppen
*
* @throws EventException Projektionsfehler
*/
//TODO: ProjectionService
public List<Group> projectEventList(List<Event> events) throws EventException {
Map<UUID, Group> groupMap = new HashMap<>();
events.parallelStream()
.forEachOrdered(event -> event.apply(getOrCreateGroup(groupMap, event.getGroupId())));
return new ArrayList<>(groupMap.values());
}
}

View File

@ -1,6 +1,59 @@
package mops.gruppen2.service; package mops.gruppen2.service;
import mops.gruppen2.domain.Account;
import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.GroupType;
import mops.gruppen2.domain.exception.EventException;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
@Service @Service
public class SearchService {} public class SearchService {
private final ProjectionService projectionService;
public SearchService(ProjectionService projectionService) {this.projectionService = projectionService;}
/**
* Sortiert die übergebene Liste an Gruppen, sodass Veranstaltungen am Anfang der Liste sind.
*
* @param groups Die Liste von Gruppen die sortiert werden soll
*/
//TODO: ProjectionService/SearchSortService
static void sortByGroupType(List<Group> groups) {
groups.sort((Group g1, Group g2) -> {
if (g1.getType() == GroupType.LECTURE) {
return -1;
}
if (g2.getType() == GroupType.LECTURE) {
return 0;
}
return 1;
});
}
/**
* Filtert alle öffentliche Gruppen nach dem Suchbegriff und gibt diese als Liste von Gruppen zurück.
* Groß und Kleinschreibung wird nicht beachtet.
*
* @param search Der Suchstring
*
* @return Liste von projizierten Gruppen
*
* @throws EventException Projektionsfehler
*/
//TODO: ProjectionService/SearchSortService
//Todo Rename
@Cacheable("groups")
public List<Group> findGroupWith(String search, Account account) throws EventException {
if (search.isEmpty()) {
return projectionService.getAllGroupWithVisibilityPublic(account.getName());
}
return projectionService.getAllGroupWithVisibilityPublic(account.getName()).parallelStream().filter(group -> group.getTitle().toLowerCase().contains(search.toLowerCase()) || group.getDescription().toLowerCase().contains(search.toLowerCase())).collect(Collectors.toList());
}
}

View File

@ -16,11 +16,13 @@ import java.util.UUID;
public class UserService { public class UserService {
private final GroupService groupService; private final GroupService groupService;
private final EventService eventService; private final EventStoreService eventStoreService;
private final ProjectionService projectionService;
public UserService(GroupService groupService, EventService eventService) { public UserService(GroupService groupService, EventStoreService eventStoreService, ProjectionService projectionService) {
this.groupService = groupService; this.groupService = groupService;
this.eventService = eventService; this.eventStoreService = eventStoreService;
this.projectionService = projectionService;
} }
@Cacheable("groups") @Cacheable("groups")
@ -38,9 +40,9 @@ public class UserService {
//TODO: Nur AddUserEvents + DeleteUserEvents betrachten //TODO: Nur AddUserEvents + DeleteUserEvents betrachten
@Cacheable("groups") @Cacheable("groups")
public List<Group> getUserGroups(User user) { public List<Group> getUserGroups(User user) {
List<UUID> groupIds = eventService.findGroupIdsByUser(user.getId()); List<UUID> groupIds = eventStoreService.findGroupIdsByUser(user.getId());
List<Event> events = groupService.getGroupEvents(groupIds); List<Event> events = groupService.getGroupEvents(groupIds);
List<Group> groups = groupService.projectEventList(events); List<Group> groups = projectionService.projectEventList(events);
List<Group> newGroups = new ArrayList<>(); List<Group> newGroups = new ArrayList<>();
for (Group group : groups) { for (Group group : groups) {
@ -48,7 +50,7 @@ public class UserService {
newGroups.add(group); newGroups.add(group);
} }
} }
groupService.sortByGroupType(newGroups); SearchService.sortByGroupType(newGroups);
return newGroups; return newGroups;
} }
@ -68,7 +70,7 @@ public class UserService {
try { try {
List<Event> events = groupService.getGroupEvents(groupIds); List<Event> events = groupService.getGroupEvents(groupIds);
return groupService.projectEventList(events).get(0); return projectionService.projectEventList(events).get(0);
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
throw new GroupNotFoundException("@UserService"); throw new GroupNotFoundException("@UserService");
} }

View File

@ -24,17 +24,17 @@ import static mops.gruppen2.domain.Role.ADMIN;
public class ValidationService { public class ValidationService {
private final UserService userService; private final UserService userService;
private final GroupService groupService; private final SearchService searchService;
public ValidationService(UserService userService, GroupService groupService) { public ValidationService(UserService userService, SearchService searchService) {
this.userService = userService; this.userService = userService;
this.groupService = groupService; this.searchService = searchService;
} }
//TODO: make static or change return + assignment //TODO: make static or change return + assignment
public List<Group> checkSearch(String search, List<Group> groups, Account account) { public List<Group> checkSearch(String search, List<Group> groups, Account account) {
if (search != null) { if (search != null) {
groups = groupService.findGroupWith(search, account); groups = searchService.findGroupWith(search, account);
} }
return groups; return groups;
} }

View File

@ -2,7 +2,7 @@ package mops.gruppen2.controller;
import mops.gruppen2.Gruppen2Application; import mops.gruppen2.Gruppen2Application;
import mops.gruppen2.repository.EventRepository; import mops.gruppen2.repository.EventRepository;
import mops.gruppen2.service.EventService; import mops.gruppen2.service.EventStoreService;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
@ -33,13 +33,13 @@ class APIControllerTest {
private EventRepository eventRepository; private EventRepository eventRepository;
@Autowired @Autowired
private APIController apiController; private APIController apiController;
private EventService eventService; private EventStoreService eventStoreService;
@Autowired @Autowired
private JdbcTemplate template; private JdbcTemplate template;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
eventService = new EventService(eventRepository); eventStoreService = new EventStoreService(eventRepository);
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");
@ -57,11 +57,11 @@ class APIControllerTest {
@Test @Test
@WithMockUser(username = "api_user", roles = "api_user") @WithMockUser(username = "api_user", roles = "api_user")
void updateGroup_singleGroup() { void updateGroup_singleGroup() {
eventService.saveAll(createPublicGroupEvent(uuidMock(0)), eventStoreService.saveAll(createPublicGroupEvent(uuidMock(0)),
addUserEvent(uuidMock(0)), addUserEvent(uuidMock(0)),
addUserEvent(uuidMock(0)), addUserEvent(uuidMock(0)),
addUserEvent(uuidMock(0)), addUserEvent(uuidMock(0)),
addUserEvent(uuidMock(0))); addUserEvent(uuidMock(0)));
assertThat(apiController.updateGroups(0L).getGroupList()).hasSize(1); assertThat(apiController.updateGroups(0L).getGroupList()).hasSize(1);
assertThat(apiController.updateGroups(4L).getGroupList()).hasSize(1); assertThat(apiController.updateGroups(4L).getGroupList()).hasSize(1);
@ -73,13 +73,13 @@ class APIControllerTest {
@Test @Test
@WithMockUser(username = "api_user", roles = "api_user") @WithMockUser(username = "api_user", roles = "api_user")
void updateGroup_multipleGroups() { void updateGroup_multipleGroups() {
eventService.saveAll(createPublicGroupEvent(uuidMock(0)), eventStoreService.saveAll(createPublicGroupEvent(uuidMock(0)),
addUserEvent(uuidMock(0)), addUserEvent(uuidMock(0)),
addUserEvent(uuidMock(0)), addUserEvent(uuidMock(0)),
createPrivateGroupEvent(uuidMock(1)), createPrivateGroupEvent(uuidMock(1)),
addUserEvent(uuidMock(1)), addUserEvent(uuidMock(1)),
addUserEvent(uuidMock(1)), addUserEvent(uuidMock(1)),
addUserEvent(uuidMock(1))); addUserEvent(uuidMock(1)));
assertThat(apiController.updateGroups(0L).getGroupList()).hasSize(2); assertThat(apiController.updateGroups(0L).getGroupList()).hasSize(2);
assertThat(apiController.updateGroups(4L).getGroupList()).hasSize(1); assertThat(apiController.updateGroups(4L).getGroupList()).hasSize(1);
@ -97,10 +97,10 @@ class APIControllerTest {
@Test @Test
@WithMockUser(username = "api_user", roles = "api_user") @WithMockUser(username = "api_user", roles = "api_user")
void getGroupsOfUser_singleGroup() { void getGroupsOfUser_singleGroup() {
eventService.saveAll(createPrivateGroupEvent(uuidMock(0)), eventStoreService.saveAll(createPrivateGroupEvent(uuidMock(0)),
createPrivateGroupEvent(uuidMock(1)), createPrivateGroupEvent(uuidMock(1)),
createPrivateGroupEvent(uuidMock(2)), createPrivateGroupEvent(uuidMock(2)),
addUserEvent(uuidMock(0), "A")); addUserEvent(uuidMock(0), "A"));
assertThat(apiController.getGroupIdsOfUser("A")).hasSize(1); assertThat(apiController.getGroupIdsOfUser("A")).hasSize(1);
} }
@ -108,9 +108,9 @@ class APIControllerTest {
@Test @Test
@WithMockUser(username = "api_user", roles = "api_user") @WithMockUser(username = "api_user", roles = "api_user")
void getGroupsOfUser_singleGroupDeletedUser() { void getGroupsOfUser_singleGroupDeletedUser() {
eventService.saveAll(createPrivateGroupEvent(uuidMock(0)), eventStoreService.saveAll(createPrivateGroupEvent(uuidMock(0)),
addUserEvent(uuidMock(0), "A"), addUserEvent(uuidMock(0), "A"),
deleteUserEvent(uuidMock(0), "A")); deleteUserEvent(uuidMock(0), "A"));
assertThat(apiController.getGroupIdsOfUser("A")).isEmpty(); assertThat(apiController.getGroupIdsOfUser("A")).isEmpty();
} }
@ -118,9 +118,9 @@ class APIControllerTest {
@Test @Test
@WithMockUser(username = "api_user", roles = "api_user") @WithMockUser(username = "api_user", roles = "api_user")
void getGroupsOfUser_singleDeletedGroup() { void getGroupsOfUser_singleDeletedGroup() {
eventService.saveAll(createPrivateGroupEvent(uuidMock(0)), eventStoreService.saveAll(createPrivateGroupEvent(uuidMock(0)),
addUserEvent(uuidMock(0), "A"), addUserEvent(uuidMock(0), "A"),
deleteGroupEvent(uuidMock(0))); deleteGroupEvent(uuidMock(0)));
assertThat(apiController.getGroupIdsOfUser("A")).isEmpty(); assertThat(apiController.getGroupIdsOfUser("A")).isEmpty();
} }
@ -128,14 +128,14 @@ class APIControllerTest {
@Test @Test
@WithMockUser(username = "api_user", roles = "api_user") @WithMockUser(username = "api_user", roles = "api_user")
void getGroupsOfUser_multipleGroups() { void getGroupsOfUser_multipleGroups() {
eventService.saveAll(createPrivateGroupEvent(uuidMock(0)), eventStoreService.saveAll(createPrivateGroupEvent(uuidMock(0)),
createPrivateGroupEvent(uuidMock(1)), createPrivateGroupEvent(uuidMock(1)),
createPrivateGroupEvent(uuidMock(2)), createPrivateGroupEvent(uuidMock(2)),
addUserEvent(uuidMock(0), "A"), addUserEvent(uuidMock(0), "A"),
addUserEvent(uuidMock(0), "B"), addUserEvent(uuidMock(0), "B"),
addUserEvent(uuidMock(1), "A"), addUserEvent(uuidMock(1), "A"),
addUserEvent(uuidMock(2), "A"), addUserEvent(uuidMock(2), "A"),
addUserEvent(uuidMock(2), "B")); addUserEvent(uuidMock(2), "B"));
assertThat(apiController.getGroupIdsOfUser("A")).hasSize(3); assertThat(apiController.getGroupIdsOfUser("A")).hasSize(3);
assertThat(apiController.getGroupIdsOfUser("B")).hasSize(2); assertThat(apiController.getGroupIdsOfUser("B")).hasSize(2);
@ -150,7 +150,7 @@ class APIControllerTest {
@Test @Test
@WithMockUser(username = "api_user", roles = "api_user") @WithMockUser(username = "api_user", roles = "api_user")
void getGroupFromId_singleGroup() { void getGroupFromId_singleGroup() {
eventService.saveAll(createPrivateGroupEvent(uuidMock(0))); eventStoreService.saveAll(createPrivateGroupEvent(uuidMock(0)));
assertThat(apiController.getGroupById(uuidMock(0).toString()).getId()).isEqualTo(uuidMock(0)); assertThat(apiController.getGroupById(uuidMock(0).toString()).getId()).isEqualTo(uuidMock(0));
} }
@ -158,9 +158,9 @@ class APIControllerTest {
@Test @Test
@WithMockUser(username = "api_user", roles = "api_user") @WithMockUser(username = "api_user", roles = "api_user")
void getGroupFromId_deletedGroup() { void getGroupFromId_deletedGroup() {
eventService.saveAll(createPrivateGroupEvent(uuidMock(0)), eventStoreService.saveAll(createPrivateGroupEvent(uuidMock(0)),
updateGroupTitleEvent(uuidMock(0)), updateGroupTitleEvent(uuidMock(0)),
deleteGroupEvent(uuidMock(0))); deleteGroupEvent(uuidMock(0)));
assertThat(apiController.getGroupById(uuidMock(0).toString()).getTitle()).isEqualTo(null); assertThat(apiController.getGroupById(uuidMock(0).toString()).getTitle()).isEqualTo(null);
} }

View File

@ -38,7 +38,7 @@ class ControllerServiceTest {
Account account2; Account account2;
Account account3; Account account3;
ControllerService controllerService; ControllerService controllerService;
EventService eventService; EventStoreService eventStoreService;
UserService userService; UserService userService;
ValidationService validationService; ValidationService validationService;
@Autowired @Autowired
@ -46,14 +46,18 @@ class ControllerServiceTest {
GroupService groupService; GroupService groupService;
@Autowired @Autowired
InviteService inviteService; InviteService inviteService;
@Autowired
SearchService searchService;
@Autowired
ProjectionService projectionService;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
eventService = new EventService(eventRepository); eventStoreService = new EventStoreService(eventRepository);
groupService = new GroupService(eventService, eventRepository); groupService = new GroupService(eventStoreService, eventRepository);
userService = new UserService(groupService, eventService); userService = new UserService(groupService, eventStoreService, projectionService);
validationService = new ValidationService(userService, groupService); validationService = new ValidationService(userService, searchService);
controllerService = new ControllerService(eventService, userService, validationService, inviteService); 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);

View File

@ -26,17 +26,17 @@ import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(classes = Gruppen2Application.class) @SpringBootTest(classes = Gruppen2Application.class)
@Transactional @Transactional
@Rollback @Rollback
class EventServiceTest { class EventStoreServiceTest {
@Autowired @Autowired
private EventRepository eventRepository; private EventRepository eventRepository;
private EventService eventService; private EventStoreService eventStoreService;
@Autowired @Autowired
private JdbcTemplate template; private JdbcTemplate template;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
eventService = new EventService(eventRepository); eventStoreService = new EventStoreService(eventRepository);
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");
@ -44,22 +44,22 @@ class EventServiceTest {
@Test @Test
void saveEvent() { void saveEvent() {
eventService.saveEvent(createPublicGroupEvent()); eventStoreService.saveEvent(createPublicGroupEvent());
assertThat(eventRepository.findAll()).hasSize(1); assertThat(eventRepository.findAll()).hasSize(1);
} }
@Test @Test
void saveAll() { void saveAll() {
eventService.saveAll(createPrivateGroupEvents(10)); eventStoreService.saveAll(createPrivateGroupEvents(10));
assertThat(eventRepository.findAll()).hasSize(10); assertThat(eventRepository.findAll()).hasSize(10);
} }
@Test @Test
void testSaveAll() { void testSaveAll() {
eventService.saveAll(createPublicGroupEvents(5), eventStoreService.saveAll(createPublicGroupEvents(5),
createPrivateGroupEvents(5)); createPrivateGroupEvents(5));
assertThat(eventRepository.findAll()).hasSize(10); assertThat(eventRepository.findAll()).hasSize(10);
} }
@ -68,7 +68,7 @@ class EventServiceTest {
void getDTO() { void getDTO() {
Event event = createPublicGroupEvent(); Event event = createPublicGroupEvent();
EventDTO dto = eventService.getDTOFromEvent(event); EventDTO dto = eventStoreService.getDTOFromEvent(event);
assertThat(dto.getGroup_id()).isEqualTo(event.getGroupId().toString()); assertThat(dto.getGroup_id()).isEqualTo(event.getGroupId().toString());
assertThat(dto.getUser_id()).isEqualTo(event.getUserId()); assertThat(dto.getUser_id()).isEqualTo(event.getUserId());
@ -78,22 +78,22 @@ class EventServiceTest {
@Test @Test
void getEventsOfGroup() { void getEventsOfGroup() {
eventService.saveAll(addUserEvents(10, uuidMock(0)), eventStoreService.saveAll(addUserEvents(10, uuidMock(0)),
addUserEvents(5, uuidMock(1))); addUserEvents(5, uuidMock(1)));
assertThat(eventService.getEventsOfGroup(uuidMock(0))).hasSize(10); assertThat(eventStoreService.getEventsOfGroup(uuidMock(0))).hasSize(10);
assertThat(eventService.getEventsOfGroup(uuidMock(1))).hasSize(5); assertThat(eventStoreService.getEventsOfGroup(uuidMock(1))).hasSize(5);
} }
@Test @Test
void findGroupIdsByUser() { void findGroupIdsByUser() {
eventService.saveAll(addUserEvent(uuidMock(0), "A"), eventStoreService.saveAll(addUserEvent(uuidMock(0), "A"),
addUserEvent(uuidMock(1), "A"), addUserEvent(uuidMock(1), "A"),
addUserEvent(uuidMock(2), "A"), addUserEvent(uuidMock(2), "A"),
addUserEvent(uuidMock(3), "A"), addUserEvent(uuidMock(3), "A"),
addUserEvent(uuidMock(3), "B")); addUserEvent(uuidMock(3), "B"));
assertThat(eventService.findGroupIdsByUser("A")).hasSize(4); assertThat(eventStoreService.findGroupIdsByUser("A")).hasSize(4);
assertThat(eventService.findGroupIdsByUser("B")).hasSize(1); assertThat(eventStoreService.findGroupIdsByUser("B")).hasSize(1);
} }
} }

View File

@ -43,45 +43,52 @@ class GroupServiceTest {
@Autowired @Autowired
private EventRepository eventRepository; private EventRepository eventRepository;
@Autowired @Autowired
private EventService eventService; SearchService searchService;
private GroupService groupService; private GroupService groupService;
@Autowired @Autowired
private JdbcTemplate template; private JdbcTemplate template;
@Autowired
ProjectionService projectionService;
@Autowired
private EventStoreService eventStoreService;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
groupService = new GroupService(eventService, eventRepository); groupService = new GroupService(eventStoreService, eventRepository);
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");
} }
//TODO: Wofür ist dieser Test? //TODO: Wofür ist dieser Test?
//TODO: ProjectionServiceTest
@Test @Test
void rightClassForSuccessfulGroup() { void rightClassForSuccessfulGroup() {
List<Event> eventList = completePrivateGroup(1); List<Event> eventList = completePrivateGroup(1);
List<Group> groups = groupService.projectEventList(eventList); List<Group> groups = projectionService.projectEventList(eventList);
assertThat(groups.get(0)).isInstanceOf(Group.class); assertThat(groups.get(0)).isInstanceOf(Group.class);
} }
//TODO: ProjectionServiceTest
@Test @Test
void projectEventList_SingleGroup() { void projectEventList_SingleGroup() {
List<Event> eventList = completePrivateGroup(5); List<Event> eventList = completePrivateGroup(5);
List<Group> groups = groupService.projectEventList(eventList); List<Group> groups = projectionService.projectEventList(eventList);
assertThat(groups).hasSize(1); assertThat(groups).hasSize(1);
assertThat(groups.get(0).getMembers()).hasSize(5); assertThat(groups.get(0).getMembers()).hasSize(5);
assertThat(groups.get(0).getVisibility()).isEqualTo(Visibility.PRIVATE); assertThat(groups.get(0).getVisibility()).isEqualTo(Visibility.PRIVATE);
} }
//TODO: ProjectionServiceTest
@Test @Test
void projectEventList_MultipleGroups() { void projectEventList_MultipleGroups() {
List<Event> eventList = completePrivateGroups(10, 2); List<Event> eventList = completePrivateGroups(10, 2);
eventList.addAll(completePublicGroups(10, 5)); eventList.addAll(completePublicGroups(10, 5));
List<Group> groups = groupService.projectEventList(eventList); List<Group> groups = projectionService.projectEventList(eventList);
assertThat(groups).hasSize(20); assertThat(groups).hasSize(20);
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);
@ -89,9 +96,9 @@ class GroupServiceTest {
@Test @Test
void getGroupEvents() { void getGroupEvents() {
eventService.saveAll(createPublicGroupEvent(uuidMock(0)), eventStoreService.saveAll(createPublicGroupEvent(uuidMock(0)),
createPublicGroupEvent(uuidMock(1)), createPublicGroupEvent(uuidMock(1)),
createPrivateGroupEvent(uuidMock(2))); createPrivateGroupEvent(uuidMock(2)));
List<UUID> groupIds = Arrays.asList(uuidMock(0), uuidMock(1)); List<UUID> groupIds = Arrays.asList(uuidMock(0), uuidMock(1));
@ -100,6 +107,7 @@ class GroupServiceTest {
assertThat(groupService.getGroupEvents(groupIds).get(1).getGroupId()).isEqualTo(uuidMock(1)); assertThat(groupService.getGroupEvents(groupIds).get(1).getGroupId()).isEqualTo(uuidMock(1));
} }
//TODO: ProjectionServiceTest
@Test @Test
void getAllGroupWithVisibilityPublicTestCreateAndDeleteSameGroup() { void getAllGroupWithVisibilityPublicTestCreateAndDeleteSameGroup() {
Event test1 = createPublicGroupEvent(uuidMock(0)); Event test1 = createPublicGroupEvent(uuidMock(0));
@ -109,83 +117,90 @@ class GroupServiceTest {
Group group = TestBuilder.apply(test1, test2); Group group = TestBuilder.apply(test1, test2);
assertThat(group.getType()).isEqualTo(null); assertThat(group.getType()).isEqualTo(null);
assertThat(groupService.getAllGroupWithVisibilityPublic("errer")).isEmpty(); assertThat(projectionService.getAllGroupWithVisibilityPublic("errer")).isEmpty();
} }
//TODO: ProjectionServiceTest
@Test @Test
void getAllGroupWithVisibilityPublicTestGroupPublic() { void getAllGroupWithVisibilityPublicTestGroupPublic() {
eventService.saveAll(createPublicGroupEvent(uuidMock(0)), eventStoreService.saveAll(createPublicGroupEvent(uuidMock(0)),
deleteGroupEvent(uuidMock(0)), deleteGroupEvent(uuidMock(0)),
createPublicGroupEvent()); createPublicGroupEvent());
assertThat(groupService.getAllGroupWithVisibilityPublic("test1").size()).isEqualTo(1); assertThat(projectionService.getAllGroupWithVisibilityPublic("test1").size()).isEqualTo(1);
} }
//TODO: ProjectionServiceTest
@Test @Test
void getAllGroupWithVisibilityPublicTestAddSomeEvents() { void getAllGroupWithVisibilityPublicTestAddSomeEvents() {
eventService.saveAll(createPublicGroupEvent(uuidMock(0)), eventStoreService.saveAll(createPublicGroupEvent(uuidMock(0)),
deleteGroupEvent(uuidMock(0)), deleteGroupEvent(uuidMock(0)),
createPublicGroupEvent(), createPublicGroupEvent(),
createPublicGroupEvent(), createPublicGroupEvent(),
createPublicGroupEvent(), createPublicGroupEvent(),
createPrivateGroupEvent()); createPrivateGroupEvent());
assertThat(groupService.getAllGroupWithVisibilityPublic("test1").size()).isEqualTo(3); assertThat(projectionService.getAllGroupWithVisibilityPublic("test1").size()).isEqualTo(3);
} }
//TODO: ProjectionServiceTest
@Test @Test
void getAllGroupWithVisibilityPublic_UserInGroup() { void getAllGroupWithVisibilityPublic_UserInGroup() {
eventService.saveAll(createPublicGroupEvent(uuidMock(0)), eventStoreService.saveAll(createPublicGroupEvent(uuidMock(0)),
addUserEvent(uuidMock(0), "kobold"), addUserEvent(uuidMock(0), "kobold"),
createPrivateGroupEvent(), createPrivateGroupEvent(),
createPublicGroupEvent()); createPublicGroupEvent());
assertThat(groupService.getAllGroupWithVisibilityPublic("kobold")).hasSize(1); assertThat(projectionService.getAllGroupWithVisibilityPublic("kobold")).hasSize(1);
assertThat(groupService.getAllGroupWithVisibilityPublic("peter")).hasSize(2); assertThat(projectionService.getAllGroupWithVisibilityPublic("peter")).hasSize(2);
} }
//TODO: ProjectionServiceTest
@Test @Test
void getAllLecturesWithVisibilityPublic() { void getAllLecturesWithVisibilityPublic() {
eventService.saveAll(createLectureEvent(), eventStoreService.saveAll(createLectureEvent(),
createPublicGroupEvent(), createPublicGroupEvent(),
createLectureEvent(), createLectureEvent(),
createLectureEvent(), createLectureEvent(),
createLectureEvent()); createLectureEvent());
assertThat(groupService.getAllLecturesWithVisibilityPublic().size()).isEqualTo(4); assertThat(projectionService.getAllLecturesWithVisibilityPublic().size()).isEqualTo(4);
} }
//TODO: SearchServiceTest
@Test @Test
void findGroupWith_UserMember_AllGroups() { void findGroupWith_UserMember_AllGroups() {
eventService.saveAll(createPublicGroupEvent(uuidMock(0)), eventStoreService.saveAll(createPublicGroupEvent(uuidMock(0)),
addUserEvent(uuidMock(0), "jens"), addUserEvent(uuidMock(0), "jens"),
updateGroupTitleEvent(uuidMock(0)), updateGroupTitleEvent(uuidMock(0)),
updateGroupDescriptionEvent(uuidMock(0))); updateGroupDescriptionEvent(uuidMock(0)));
assertThat(groupService.findGroupWith("", account("jens"))).isEmpty(); assertThat(searchService.findGroupWith("", account("jens"))).isEmpty();
} }
//TODO: SearchServiceTest
@Test @Test
void findGroupWith_UserNoMember_AllGroups() { void findGroupWith_UserNoMember_AllGroups() {
eventService.saveAll(completePublicGroups(10, 0), eventStoreService.saveAll(completePublicGroups(10, 0),
completePrivateGroups(10, 0)); completePrivateGroups(10, 0));
assertThat(groupService.findGroupWith("", account("jens"))).hasSize(10); assertThat(searchService.findGroupWith("", account("jens"))).hasSize(10);
} }
//TODO: SearchServiceTest
@Test @Test
void findGroupWith_FilterGroups() { void findGroupWith_FilterGroups() {
eventService.saveAll(createPublicGroupEvent(uuidMock(0)), eventStoreService.saveAll(createPublicGroupEvent(uuidMock(0)),
updateGroupTitleEvent(uuidMock(0), "KK"), updateGroupTitleEvent(uuidMock(0), "KK"),
updateGroupDescriptionEvent(uuidMock(0), "ABCDE"), updateGroupDescriptionEvent(uuidMock(0), "ABCDE"),
createPublicGroupEvent(uuidMock(1)), createPublicGroupEvent(uuidMock(1)),
updateGroupTitleEvent(uuidMock(1), "ABCDEFG"), updateGroupTitleEvent(uuidMock(1), "ABCDEFG"),
updateGroupDescriptionEvent(uuidMock(1), "KK"), updateGroupDescriptionEvent(uuidMock(1), "KK"),
createPrivateGroupEvent()); createPrivateGroupEvent());
assertThat(groupService.findGroupWith("A", account("jesus"))).hasSize(2); assertThat(searchService.findGroupWith("A", account("jesus"))).hasSize(2);
assertThat(groupService.findGroupWith("F", account("jesus"))).hasSize(1); assertThat(searchService.findGroupWith("F", account("jesus"))).hasSize(1);
assertThat(groupService.findGroupWith("Z", account("jesus"))).hasSize(0); assertThat(searchService.findGroupWith("Z", account("jesus"))).hasSize(0);
} }
} }