Implemented Exeption and their handling
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,11 +50,13 @@ 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!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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,9 +1,11 @@
|
||||
package mops.gruppen2.domain;
|
||||
|
||||
import mops.gruppen2.domain.Exceptions.UserAlreadyExistsException;
|
||||
import mops.gruppen2.domain.event.AddUserEvent;
|
||||
import mops.gruppen2.domain.event.CreateGroupEvent;
|
||||
import mops.gruppen2.domain.event.DeleteUserEvent;
|
||||
import mops.gruppen2.domain.event.UpdateRoleEvent;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -23,12 +25,14 @@ class GroupTest {
|
||||
}
|
||||
|
||||
@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);
|
||||
@ -36,7 +40,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);
|
||||
@ -49,7 +53,7 @@ class GroupTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void addExistingUser() {
|
||||
void addExistingUser() throws Exception{
|
||||
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1,1,"prof1", "hi", "foo");
|
||||
Group group = new Group();
|
||||
group.applyEvent(createGroupEvent);
|
||||
@ -60,13 +64,16 @@ class GroupTest {
|
||||
|
||||
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);
|
||||
|
||||
//assertThat(group.getMembers().size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteSingleUser() {
|
||||
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);
|
||||
@ -82,7 +89,7 @@ class GroupTest {
|
||||
|
||||
// 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");
|
||||
|
||||
Reference in New Issue
Block a user