Merge pull request #38 from hhu-propra2/Eventrifinighandling
EventRifinigHandling
This commit is contained in:
@ -1,10 +1,17 @@
|
||||
package mops.gruppen2.domain;
|
||||
|
||||
import com.google.common.base.Throwables;
|
||||
import lombok.Getter;
|
||||
import mops.gruppen2.domain.Exceptions.EventException;
|
||||
import mops.gruppen2.domain.Exceptions.UserAlreadyExistsException;
|
||||
import mops.gruppen2.domain.event.Event;
|
||||
|
||||
import javax.swing.table.TableRowSorter;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import static java.lang.String.format;
|
||||
|
||||
/**
|
||||
* Repräsentiert viele Events als aggregiertes Objekt.
|
||||
*/
|
||||
@ -18,13 +25,19 @@ public abstract class Aggregate {
|
||||
*
|
||||
* @param event Event, welches aggregiert wird
|
||||
*/
|
||||
public void applyEvent(Event event) {
|
||||
public void applyEvent(Event event) throws EventException {
|
||||
try {
|
||||
Method method = this.getClass().getDeclaredMethod("applyEvent", event.getClass());
|
||||
method.setAccessible(true);
|
||||
method.invoke(this, event);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
EventException f = (EventException) e.getTargetException();
|
||||
throw f;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,14 @@
|
||||
package mops.gruppen2.domain.Exceptions;
|
||||
|
||||
public class EventException extends Exception {
|
||||
private String msg;
|
||||
|
||||
public EventException(String msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
package mops.gruppen2.domain.Exceptions;
|
||||
|
||||
public class UserAlreadyExistsException extends EventException {
|
||||
public UserAlreadyExistsException(String msg){
|
||||
super(msg);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
package mops.gruppen2.domain.Exceptions;
|
||||
|
||||
public class UserNotFoundException extends EventException{
|
||||
public UserNotFoundException(String msg){
|
||||
super(msg);
|
||||
}
|
||||
}
|
||||
@ -2,6 +2,8 @@ package mops.gruppen2.domain;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import mops.gruppen2.domain.Exceptions.UserAlreadyExistsException;
|
||||
import mops.gruppen2.domain.Exceptions.UserNotFoundException;
|
||||
import mops.gruppen2.domain.event.*;
|
||||
|
||||
import java.util.*;
|
||||
@ -28,7 +30,7 @@ public class Group extends Aggregate {
|
||||
id = event.getGroup_id();
|
||||
}
|
||||
|
||||
private void applyEvent(UpdateRoleEvent event) {
|
||||
private void applyEvent(UpdateRoleEvent event) throws UserNotFoundException {
|
||||
User user;
|
||||
|
||||
Optional<User> userOptional = members.stream()
|
||||
@ -38,8 +40,7 @@ public class Group extends Aggregate {
|
||||
if (userOptional.isPresent()) {
|
||||
user = userOptional.get();
|
||||
} else {
|
||||
System.out.println("UserNotFoundException");
|
||||
return;
|
||||
throw new UserNotFoundException("Nutzer wurde nicht gefunden!");
|
||||
}
|
||||
|
||||
if (roles.containsKey(user) && event.getNewRole() == Role.STUDENT) {
|
||||
@ -49,10 +50,14 @@ public class Group extends Aggregate {
|
||||
}
|
||||
}
|
||||
|
||||
private void applyEvent(AddUserEvent event) {
|
||||
private void applyEvent(AddUserEvent event) throws UserAlreadyExistsException {
|
||||
User user = new User(event.getUser_id(), event.getGivenname(), event.getFamilyname(), event.getEmail());
|
||||
|
||||
if (!this.members.contains(user)) {
|
||||
this.members.add(user);
|
||||
} else {
|
||||
throw new UserAlreadyExistsException("Nutzer bereits in Gruppe vorhanden!");
|
||||
}
|
||||
}
|
||||
|
||||
private void applyEvent(UpdateGroupTitleEvent event) {
|
||||
@ -63,12 +68,13 @@ public class Group extends Aggregate {
|
||||
this.description = event.getNewGroupDescription();
|
||||
}
|
||||
|
||||
private void applyEvent(DeleteUserEvent event) {
|
||||
for (User user : members) {
|
||||
if (user.getUser_id().equals(event.getUser_id())) {
|
||||
private void applyEvent(DeleteUserEvent event) throws UserNotFoundException{
|
||||
User user = new User(event.getUser_id(), "","","");
|
||||
|
||||
if (this.members.contains(user)) {
|
||||
this.members.remove(user);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
throw new UserNotFoundException("Nutzer wurde nicht gefunden!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,12 +2,16 @@ package mops.gruppen2.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Value;
|
||||
|
||||
@Data
|
||||
@Value
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode(exclude = {"givenname", "familyname", "email"})
|
||||
public class User {
|
||||
|
||||
String user_id;
|
||||
|
||||
String givenname;
|
||||
String familyname;
|
||||
String email;
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package mops.gruppen2.service;
|
||||
|
||||
import mops.gruppen2.domain.Exceptions.EventException;
|
||||
import mops.gruppen2.domain.Group;
|
||||
import mops.gruppen2.domain.event.Event;
|
||||
import org.springframework.stereotype.Service;
|
||||
@ -19,7 +20,13 @@ public class GroupService {
|
||||
Group buildGroupFromEvents(List<Event> eventList) {
|
||||
Group newGroup = new Group();
|
||||
|
||||
eventList.forEach(newGroup::applyEvent);
|
||||
try {
|
||||
for (Event event : eventList) {
|
||||
newGroup.applyEvent(event);
|
||||
}
|
||||
}catch (EventException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return newGroup;
|
||||
}
|
||||
|
||||
@ -1,8 +1,9 @@
|
||||
package mops.gruppen2.domain;
|
||||
|
||||
import mops.gruppen2.domain.event.AddUserEvent;
|
||||
import mops.gruppen2.domain.event.CreateGroupEvent;
|
||||
import mops.gruppen2.domain.event.UpdateRoleEvent;
|
||||
import mops.gruppen2.domain.Exceptions.UserAlreadyExistsException;
|
||||
import mops.gruppen2.domain.Exceptions.UserNotFoundException;
|
||||
import mops.gruppen2.domain.event.*;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -16,18 +17,19 @@ class GroupTest {
|
||||
}
|
||||
|
||||
|
||||
@Disabled
|
||||
@Test
|
||||
void applyEvent() {
|
||||
}
|
||||
|
||||
@Test
|
||||
void createSingleGroup() {
|
||||
void createSingleGroup() throws Exception{
|
||||
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1,2, "asd", "hello", "foo");
|
||||
|
||||
Group group = new Group();
|
||||
|
||||
group.applyEvent(createGroupEvent);
|
||||
|
||||
|
||||
assertThat(group.getDescription()).isEqualTo("foo");
|
||||
assertThat(group.getTitle()).isEqualTo("hello");
|
||||
assertThat(group.getId()).isEqualTo(2);
|
||||
@ -35,7 +37,7 @@ class GroupTest {
|
||||
|
||||
// Verwendet CreateGroupEvent
|
||||
@Test
|
||||
void addSingleUser() {
|
||||
void addSingleUser() throws Exception{
|
||||
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1,1,"prof1", "hi", "foo");
|
||||
Group group = new Group();
|
||||
group.applyEvent(createGroupEvent);
|
||||
@ -47,9 +49,57 @@ class GroupTest {
|
||||
assertThat(group.getMembers().get(0)).isEqualTo(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
void addExistingUser() throws Exception{
|
||||
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1,1,"prof1", "hi", "foo");
|
||||
Group group = new Group();
|
||||
group.applyEvent(createGroupEvent);
|
||||
|
||||
User user1 = new User("prof", "jens", "bendi", "hi@gmail.com");
|
||||
AddUserEvent addUserEvent1 = new AddUserEvent(2,1, user1);
|
||||
group.applyEvent(addUserEvent1);
|
||||
|
||||
User user2 = new User("prof", "olga", "bendi", "hi@gmail.com");
|
||||
AddUserEvent addUserEvent2 = new AddUserEvent(3,1, user2);
|
||||
Assertions.assertThrows(UserAlreadyExistsException.class, () ->{
|
||||
group.applyEvent(addUserEvent2);
|
||||
});
|
||||
|
||||
|
||||
assertThat(group.getMembers().size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteSingleUser() throws Exception{
|
||||
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1, 2, "Prof", "Tolle Gruppe", "Tolle Beshreibung");
|
||||
User user = new User("Prof", "Pro", "fessor", "pro@fessor.de");
|
||||
AddUserEvent addUserEvent = new AddUserEvent(2, 2, user);
|
||||
Group group = new Group();
|
||||
group.applyEvent(createGroupEvent);
|
||||
group.applyEvent(addUserEvent);
|
||||
|
||||
DeleteUserEvent deleteUserEvent = new DeleteUserEvent(3, 2, "Prof");
|
||||
group.applyEvent(deleteUserEvent);
|
||||
|
||||
assertThat(group.getMembers().size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteUserThatDoesNotExists() throws Exception{
|
||||
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1, 2, "Prof", "Tolle Gruppe", "Tolle Beshreibung");
|
||||
Group group = new Group();
|
||||
group.applyEvent(createGroupEvent);
|
||||
|
||||
DeleteUserEvent deleteUserEvent = new DeleteUserEvent(3, 2, "Prof");
|
||||
|
||||
Assertions.assertThrows(UserNotFoundException.class, () ->{
|
||||
group.applyEvent(deleteUserEvent);
|
||||
});
|
||||
}
|
||||
|
||||
// Verwendet CreateGroupEvent und AddUserEvent
|
||||
@Test
|
||||
void updateRoleForExistingUser() {
|
||||
void updateRoleForExistingUser() throws Exception{
|
||||
// Arrange
|
||||
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1L, 1L, "1L", "gruppe1", "Eine Testgruppe");
|
||||
AddUserEvent addUserEvent = new AddUserEvent(1L, 1L, "5L", "Peter", "Pan", "123@mail.de");
|
||||
@ -69,10 +119,39 @@ class GroupTest {
|
||||
.containsValue(Role.ORGA);
|
||||
}
|
||||
|
||||
@Disabled
|
||||
@Test
|
||||
void updateRoleForNonExistingUser() {
|
||||
void updateRoleForNonExistingUser() throws Exception{
|
||||
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1L, 1L, "1L", "gruppe1", "Eine Testgruppe");
|
||||
UpdateRoleEvent updateRoleEvent = new UpdateRoleEvent(345L, 33 , "coolerUser", Role.ADMIN);
|
||||
|
||||
Group group = new Group();
|
||||
group.applyEvent(createGroupEvent);
|
||||
Assertions.assertThrows(UserNotFoundException.class, () ->{
|
||||
group.applyEvent(updateRoleEvent);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateTitle() throws Exception{
|
||||
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1,1,"prof1", "hi", "foo");
|
||||
Group group = new Group();
|
||||
group.applyEvent(createGroupEvent);
|
||||
|
||||
UpdateGroupTitleEvent updateGroupTitleEvent = new UpdateGroupTitleEvent(2, 1, "Klaus", "Toller Titel");
|
||||
group.applyEvent(updateGroupTitleEvent);
|
||||
|
||||
assertThat(group.getTitle()).isEqualTo("Toller Titel");
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateBeschreibung() throws Exception{
|
||||
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1,1,"prof1", "hi", "foo");
|
||||
Group group = new Group();
|
||||
group.applyEvent(createGroupEvent);
|
||||
|
||||
UpdateGroupDescriptionEvent updateGroupDescriptionEvent = new UpdateGroupDescriptionEvent(2, 1, "Peter", "Tolle Beschreibung");
|
||||
group.applyEvent(updateGroupDescriptionEvent);
|
||||
|
||||
assertThat(group.getDescription()).isEqualTo("Tolle Beschreibung");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user