refactor aggregate build process
This commit is contained in:
@ -4,6 +4,7 @@ import lombok.Getter;
|
||||
import mops.gruppen2.domain.event.Event;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Repräsentiert viele Events als aggregiertes Objekt.
|
||||
@ -13,12 +14,20 @@ public abstract class Aggregate {
|
||||
|
||||
protected long id;
|
||||
|
||||
public void apply(List<Event> events) {
|
||||
events.forEach(this::applyEvent);
|
||||
}
|
||||
|
||||
public void apply(Event event) {
|
||||
applyEvent(event);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ruft die spezifische applyEvent-Methode im entsprechenden Aggregat auf.
|
||||
*
|
||||
* @param event Event, welches aggregiert wird
|
||||
*/
|
||||
public void applyEvent(Event event) {
|
||||
private void applyEvent(Event event) {
|
||||
try {
|
||||
Method method = this.getClass().getDeclaredMethod("applyEvent", event.getClass());
|
||||
method.setAccessible(true);
|
||||
|
||||
@ -18,8 +18,7 @@ public class GroupService {
|
||||
*/
|
||||
Group buildGroupFromEvents(List<Event> eventList) {
|
||||
Group newGroup = new Group();
|
||||
|
||||
eventList.forEach(newGroup::applyEvent);
|
||||
newGroup.apply(eventList);
|
||||
|
||||
return newGroup;
|
||||
}
|
||||
|
||||
@ -26,7 +26,7 @@ class GroupTest {
|
||||
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1,2, "asd", "hello", "foo");
|
||||
|
||||
Group group = new Group();
|
||||
group.applyEvent(createGroupEvent);
|
||||
group.apply(createGroupEvent);
|
||||
|
||||
assertThat(group.getDescription()).isEqualTo("foo");
|
||||
assertThat(group.getTitle()).isEqualTo("hello");
|
||||
@ -38,11 +38,11 @@ class GroupTest {
|
||||
void addSingleUser() {
|
||||
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1,1,"prof1", "hi", "foo");
|
||||
Group group = new Group();
|
||||
group.applyEvent(createGroupEvent);
|
||||
group.apply(createGroupEvent);
|
||||
|
||||
User user = new User("prof", "jens", "bendi", "hi@gmail.com");
|
||||
AddUserEvent addUserEvent = new AddUserEvent(1,1, user);
|
||||
group.applyEvent(addUserEvent);
|
||||
group.apply(addUserEvent);
|
||||
|
||||
assertThat(group.getMembers().get(0)).isEqualTo(user);
|
||||
}
|
||||
@ -55,13 +55,13 @@ class GroupTest {
|
||||
AddUserEvent addUserEvent = new AddUserEvent(1L, 1L, "5L", "Peter", "Pan", "123@mail.de");
|
||||
|
||||
Group group = new Group();
|
||||
group.applyEvent(createGroupEvent);
|
||||
group.applyEvent(addUserEvent);
|
||||
group.apply(createGroupEvent);
|
||||
group.apply(addUserEvent);
|
||||
|
||||
UpdateRoleEvent updateRoleEvent = new UpdateRoleEvent(1L, 1L, "5L", Role.ORGA);
|
||||
|
||||
// Act
|
||||
group.applyEvent(updateRoleEvent);
|
||||
group.apply(updateRoleEvent);
|
||||
|
||||
// Assert
|
||||
assertThat(group.getRoles())
|
||||
|
||||
Reference in New Issue
Block a user