Merge pull request #57 from hhu-propra2/GroupLogicProjection
Group logic projection
This commit is contained in:
@ -77,8 +77,4 @@ public class Group extends Aggregate {
|
||||
throw new UserNotFoundException("Nutzer wurde nicht gefunden!");
|
||||
}
|
||||
}
|
||||
|
||||
private void applyEvent(DeleteGroupEvent event) {
|
||||
this.id = this.id * -1;
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,6 +6,9 @@ import mops.gruppen2.domain.event.Event;
|
||||
import mops.gruppen2.repository.EventRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class EventService {
|
||||
private final SerializationService serializationService;
|
||||
@ -48,4 +51,18 @@ public class EventService {
|
||||
}
|
||||
return tmpId;
|
||||
}
|
||||
|
||||
public List<Event> findAllEvents() {
|
||||
Iterable<EventDTO> eventDTOS = eventStore.findAll();
|
||||
List<Event> events = new ArrayList<>();
|
||||
eventDTOS.forEach(eventDTO -> {
|
||||
try {
|
||||
events.add(serializationService.deserializeEvent(eventDTO.getEvent_payload()));
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
return events;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,49 +1,58 @@
|
||||
package mops.gruppen2.service;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import mops.gruppen2.domain.Exceptions.EventException;
|
||||
import mops.gruppen2.domain.Exceptions.GroupDoesNotExistException;
|
||||
import mops.gruppen2.domain.Group;
|
||||
import mops.gruppen2.domain.event.CreateGroupEvent;
|
||||
import mops.gruppen2.domain.event.DeleteGroupEvent;
|
||||
import mops.gruppen2.domain.event.Event;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class GroupService {
|
||||
|
||||
/**
|
||||
* Konstruiert eine vollständige Gruppe aus Events, welche dieselbe Gruppe betreffen.
|
||||
*
|
||||
* @param eventList Die restlichen Events für diese Gruppe
|
||||
* @return Gruppe auf aktuellem Stand
|
||||
*/
|
||||
Group buildGroupFromEvents(List<Event> eventList) throws EventException {
|
||||
Group newGroup = new Group();
|
||||
private final EventService eventService;
|
||||
|
||||
try {
|
||||
if (!(eventList.get(0) instanceof CreateGroupEvent)) {
|
||||
throw new GroupDoesNotExistException("Die Gruppe existiert nicht");
|
||||
public GroupService(EventService eventService) {
|
||||
this.eventService = eventService;
|
||||
}
|
||||
|
||||
public List<Group> projectEventList(Map<Long, Group> groupMap, List<Event> events) throws EventException {
|
||||
for (Event event : events) {
|
||||
if (event instanceof CreateGroupEvent) {
|
||||
groupMap.put(event.getGroup_id(), new Group());
|
||||
}
|
||||
if (event instanceof DeleteGroupEvent) {
|
||||
groupMap.remove(event.getGroup_id());
|
||||
} else {
|
||||
newGroup.applyEvent(eventList.get(0));
|
||||
eventList.remove(0);
|
||||
}
|
||||
for (Event event : eventList) {
|
||||
if (!(newGroup.getId() > 0)) {
|
||||
throw new GroupDoesNotExistException("Die Gruppe existiert nicht");
|
||||
}
|
||||
newGroup.applyEvent(event);
|
||||
try {
|
||||
Group group = groupMap.get(event.getGroup_id());
|
||||
|
||||
if (group == null) {
|
||||
throw new GroupDoesNotExistException("Gruppe " + event.getGroup_id() + " existiert nicht");
|
||||
}
|
||||
|
||||
group.applyEvent(event);
|
||||
} catch (EventException e) {
|
||||
if (e instanceof GroupDoesNotExistException) {
|
||||
throw e;
|
||||
}
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
if (newGroup.getId() < 0) {
|
||||
return null;
|
||||
}
|
||||
return newGroup;
|
||||
|
||||
return new ArrayList<>(groupMap.values());
|
||||
}
|
||||
|
||||
public List<Group> projectEventList(List<Event> events) throws EventException {
|
||||
return projectEventList(new HashMap<>(), events);
|
||||
}
|
||||
}
|
||||
|
||||
@ -21,7 +21,7 @@ public class EventBuilder {
|
||||
);
|
||||
}
|
||||
|
||||
public static AddUserEvent randomAddUserEvent() {
|
||||
public static AddUserEvent randomAddUserEvent(long group_id) {
|
||||
Faker faker = new Faker();
|
||||
|
||||
String firstname = faker.name().firstName();
|
||||
@ -29,7 +29,7 @@ public class EventBuilder {
|
||||
|
||||
return new AddUserEvent(
|
||||
faker.random().nextLong(),
|
||||
faker.random().nextLong(),
|
||||
group_id,
|
||||
faker.random().hex(),
|
||||
firstname,
|
||||
lastname,
|
||||
@ -37,11 +37,11 @@ public class EventBuilder {
|
||||
);
|
||||
}
|
||||
|
||||
public static List<Event> randomAddUserEvents(int count) {
|
||||
public static List<Event> randomAddUserEvents(int count, long group_id) {
|
||||
List<Event> eventList = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
eventList.add(EventBuilder.randomAddUserEvent());
|
||||
eventList.add(EventBuilder.randomAddUserEvent(group_id));
|
||||
}
|
||||
|
||||
return eventList;
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
package mops.gruppen2.domain;
|
||||
|
||||
import mops.gruppen2.domain.Exceptions.GroupDoesNotExistException;
|
||||
import mops.gruppen2.domain.Exceptions.UserAlreadyExistsException;
|
||||
import mops.gruppen2.domain.Exceptions.UserNotFoundException;
|
||||
import mops.gruppen2.domain.event.*;
|
||||
@ -154,17 +153,4 @@ class GroupTest {
|
||||
|
||||
assertThat(group.getDescription()).isEqualTo("Tolle Beschreibung");
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteGroup() throws Exception {
|
||||
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1, 1L, "prof1", "hi", "foo");
|
||||
Group group = new Group();
|
||||
group.applyEvent(createGroupEvent);
|
||||
|
||||
DeleteGroupEvent deleteGroupEvent = new DeleteGroupEvent(44, 1, "loescher78");
|
||||
group.applyEvent(deleteGroupEvent);
|
||||
|
||||
assertThat(group.id).isEqualTo(-1L);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -10,18 +10,23 @@ import mops.gruppen2.domain.event.Event;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.configuration.IMockitoConfiguration;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
class GroupServiceTest {
|
||||
GroupService groupService = new GroupService();
|
||||
GroupService groupService;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
groupService = new GroupService(mock(EventService.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -36,7 +41,7 @@ class GroupServiceTest {
|
||||
|
||||
|
||||
Assertions.assertThrows(GroupDoesNotExistException.class, () -> {
|
||||
groupService.buildGroupFromEvents(eventList);
|
||||
groupService.projectEventList(eventList);
|
||||
});
|
||||
}
|
||||
|
||||
@ -48,30 +53,20 @@ class GroupServiceTest {
|
||||
|
||||
eventList.add(new DeleteGroupEvent(44, 10, "loescher78"));
|
||||
|
||||
assertThat(groupService.buildGroupFromEvents(eventList)).isEqualTo(null);
|
||||
List<Group> list = new ArrayList<>();
|
||||
|
||||
assertThat(groupService.projectEventList(eventList)).isEqualTo(list);
|
||||
}
|
||||
|
||||
@Test
|
||||
void firstEventNotCreateGroup() throws Exception {
|
||||
List<Event> eventList = new ArrayList<>();
|
||||
|
||||
eventList.add(new DeleteGroupEvent(44, 10, "loescher78"));
|
||||
eventList.add(new CreateGroupEvent(1, 10L, "prof1", "hi", "foo"));
|
||||
|
||||
Assertions.assertThrows(GroupDoesNotExistException.class, () -> {
|
||||
groupService.buildGroupFromEvents(eventList);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void sucsessfullReturnGroup() throws Exception {
|
||||
void rightClassForSucsessfulGroup() throws Exception {
|
||||
List<Event> eventList = new ArrayList<>();
|
||||
|
||||
eventList.add(new CreateGroupEvent(1, 10L, "prof1", "hi", "foo"));
|
||||
|
||||
eventList.add(new AddUserEvent(900L, 10L, "Ulli", "Ulli", "Honnis", "FC@B.de"));
|
||||
|
||||
assertThat(groupService.buildGroupFromEvents(eventList)).isInstanceOf(Group.class);
|
||||
assertThat(groupService.projectEventList(eventList).get(0)).isInstanceOf(Group.class);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user