1

Merge pull request #57 from hhu-propra2/GroupLogicProjection

Group logic projection
This commit is contained in:
Talha Caliskan
2020-03-11 14:04:23 +01:00
committed by GitHub
6 changed files with 69 additions and 66 deletions

View File

@ -77,8 +77,4 @@ public class Group extends Aggregate {
throw new UserNotFoundException("Nutzer wurde nicht gefunden!"); throw new UserNotFoundException("Nutzer wurde nicht gefunden!");
} }
} }
private void applyEvent(DeleteGroupEvent event) {
this.id = this.id * -1;
}
} }

View File

@ -6,6 +6,9 @@ import mops.gruppen2.domain.event.Event;
import mops.gruppen2.repository.EventRepository; import mops.gruppen2.repository.EventRepository;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service @Service
public class EventService { public class EventService {
private final SerializationService serializationService; private final SerializationService serializationService;
@ -48,4 +51,18 @@ public class EventService {
} }
return tmpId; 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;
}
} }

View File

@ -1,49 +1,58 @@
package mops.gruppen2.service; package mops.gruppen2.service;
import lombok.EqualsAndHashCode;
import mops.gruppen2.domain.Exceptions.EventException; import mops.gruppen2.domain.Exceptions.EventException;
import mops.gruppen2.domain.Exceptions.GroupDoesNotExistException; import mops.gruppen2.domain.Exceptions.GroupDoesNotExistException;
import mops.gruppen2.domain.Group; import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.event.CreateGroupEvent; import mops.gruppen2.domain.event.CreateGroupEvent;
import mops.gruppen2.domain.event.DeleteGroupEvent;
import mops.gruppen2.domain.event.Event; import mops.gruppen2.domain.event.Event;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
@Service @Service
public class GroupService { public class GroupService {
/** private final EventService eventService;
* 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();
try { public GroupService(EventService eventService) {
if (!(eventList.get(0) instanceof CreateGroupEvent)) { this.eventService = eventService;
throw new GroupDoesNotExistException("Die Gruppe existiert nicht"); }
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 { } else {
newGroup.applyEvent(eventList.get(0)); try {
eventList.remove(0); Group group = groupMap.get(event.getGroup_id());
}
for (Event event : eventList) { if (group == null) {
if (!(newGroup.getId() > 0)) { throw new GroupDoesNotExistException("Gruppe " + event.getGroup_id() + " existiert nicht");
throw new GroupDoesNotExistException("Die Gruppe existiert nicht"); }
group.applyEvent(event);
} catch (EventException e) {
if (e instanceof GroupDoesNotExistException) {
throw e;
}
e.printStackTrace();
} }
newGroup.applyEvent(event);
} }
} catch (EventException e) {
if (e instanceof GroupDoesNotExistException) {
throw e;
}
e.printStackTrace();
} }
if (newGroup.getId() < 0) { return new ArrayList<>(groupMap.values());
return null; }
}
return newGroup; public List<Group> projectEventList(List<Event> events) throws EventException {
return projectEventList(new HashMap<>(), events);
} }
} }

View File

@ -21,7 +21,7 @@ public class EventBuilder {
); );
} }
public static AddUserEvent randomAddUserEvent() { public static AddUserEvent randomAddUserEvent(long group_id) {
Faker faker = new Faker(); Faker faker = new Faker();
String firstname = faker.name().firstName(); String firstname = faker.name().firstName();
@ -29,7 +29,7 @@ public class EventBuilder {
return new AddUserEvent( return new AddUserEvent(
faker.random().nextLong(), faker.random().nextLong(),
faker.random().nextLong(), group_id,
faker.random().hex(), faker.random().hex(),
firstname, firstname,
lastname, 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<>(); List<Event> eventList = new ArrayList<>();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
eventList.add(EventBuilder.randomAddUserEvent()); eventList.add(EventBuilder.randomAddUserEvent(group_id));
} }
return eventList; return eventList;

View File

@ -1,6 +1,5 @@
package mops.gruppen2.domain; package mops.gruppen2.domain;
import mops.gruppen2.domain.Exceptions.GroupDoesNotExistException;
import mops.gruppen2.domain.Exceptions.UserAlreadyExistsException; import mops.gruppen2.domain.Exceptions.UserAlreadyExistsException;
import mops.gruppen2.domain.Exceptions.UserNotFoundException; import mops.gruppen2.domain.Exceptions.UserNotFoundException;
import mops.gruppen2.domain.event.*; import mops.gruppen2.domain.event.*;
@ -154,17 +153,4 @@ class GroupTest {
assertThat(group.getDescription()).isEqualTo("Tolle Beschreibung"); 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);
}
} }

View File

@ -10,18 +10,23 @@ import mops.gruppen2.domain.event.Event;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
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.mockito.configuration.IMockitoConfiguration;
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 static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
class GroupServiceTest { class GroupServiceTest {
GroupService groupService = new GroupService(); GroupService groupService;
@BeforeEach @BeforeEach
public void setUp() { public void setUp() {
groupService = new GroupService(mock(EventService.class));
} }
@Test @Test
@ -36,7 +41,7 @@ class GroupServiceTest {
Assertions.assertThrows(GroupDoesNotExistException.class, () -> { Assertions.assertThrows(GroupDoesNotExistException.class, () -> {
groupService.buildGroupFromEvents(eventList); groupService.projectEventList(eventList);
}); });
} }
@ -48,30 +53,20 @@ class GroupServiceTest {
eventList.add(new DeleteGroupEvent(44, 10, "loescher78")); 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 @Test
void firstEventNotCreateGroup() throws Exception { void rightClassForSucsessfulGroup() 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 {
List<Event> eventList = new ArrayList<>(); List<Event> eventList = new ArrayList<>();
eventList.add(new CreateGroupEvent(1, 10L, "prof1", "hi", "foo")); eventList.add(new CreateGroupEvent(1, 10L, "prof1", "hi", "foo"));
eventList.add(new AddUserEvent(900L, 10L, "Ulli", "Ulli", "Honnis", "FC@B.de")); 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);
} }
} }