1

Added DeleteGroupEvent and GroupDoesNotExistException

This commit is contained in:
Mahgs
2020-03-10 15:09:35 +01:00
parent 3425e1fcec
commit c0d2bbbf7f
8 changed files with 162 additions and 30 deletions

View File

@ -29,7 +29,8 @@ public class Gruppen2Controller {
this.groupService = groupService;
}
/**Zeigt die index.html an.
/**
* Zeigt die index.html an.
*
* @param token toller token
* @param model tolles model

View File

@ -30,8 +30,7 @@ public abstract class Aggregate {
Method method = this.getClass().getDeclaredMethod("applyEvent", event.getClass());
method.setAccessible(true);
method.invoke(this, event);
}
catch (IllegalAccessException e) {
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();

View File

@ -0,0 +1,9 @@
package mops.gruppen2.domain.Exceptions;
import mops.gruppen2.domain.event.Event;
public class GroupDoesNotExistException extends EventException {
public GroupDoesNotExistException(String msg) {
super(msg);
}
}

View File

@ -68,8 +68,8 @@ public class Group extends Aggregate {
this.description = event.getNewGroupDescription();
}
private void applyEvent(DeleteUserEvent event) throws UserNotFoundException{
User user = new User(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);
@ -77,4 +77,8 @@ public class Group extends Aggregate {
throw new UserNotFoundException("Nutzer wurde nicht gefunden!");
}
}
private void applyEvent(DeleteGroupEvent event) {
this.id = this.id * -1;
}
}

View File

@ -0,0 +1,13 @@
package mops.gruppen2.domain.event;
import lombok.EqualsAndHashCode;
import lombok.Value;
@EqualsAndHashCode(callSuper = true)
@Value
public class DeleteGroupEvent extends Event {
public DeleteGroupEvent(long event_id, long group_id, String user_id) {
super(event_id, group_id, user_id);
}
}

View File

@ -1,8 +1,8 @@
package mops.gruppen2.service;
import mops.gruppen2.domain.Exceptions.EventException;
import mops.gruppen2.domain.Exceptions.GroupDoesNotExistException;
import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.event.AddUserEvent;
import mops.gruppen2.domain.event.CreateGroupEvent;
import mops.gruppen2.domain.event.Event;
import org.springframework.stereotype.Service;
@ -18,17 +18,32 @@ public class GroupService {
* @param eventList Die restlichen Events für diese Gruppe
* @return Gruppe auf aktuellem Stand
*/
Group buildGroupFromEvents(List<Event> eventList) {
Group buildGroupFromEvents(List<Event> eventList) throws EventException {
Group newGroup = new Group();
try {
if (!(eventList.get(0) instanceof CreateGroupEvent)) {
throw new GroupDoesNotExistException("Die Gruppe existiert nicht");
} 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);
}
}catch (EventException e){
} catch (EventException e) {
if (e instanceof GroupDoesNotExistException) {
throw e;
}
e.printStackTrace();
}
if (newGroup.getId() < 0) {
return null;
}
return newGroup;
}
}

View File

@ -1,5 +1,6 @@
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.*;
@ -12,7 +13,7 @@ import static org.assertj.core.api.Assertions.assertThat;
class GroupTest {
@BeforeEach
public void setUp(){
public void setUp() {
}
@ -21,8 +22,8 @@ class GroupTest {
}
@Test
void createSingleGroup() throws Exception{
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1,2L, "asd", "hello", "foo");
void createSingleGroup() throws Exception {
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1, 2L, "asd", "hello", "foo");
Group group = new Group();
@ -36,31 +37,31 @@ class GroupTest {
// Verwendet CreateGroupEvent
@Test
void addSingleUser() throws Exception{
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1,1L,"prof1", "hi", "foo");
void addSingleUser() throws Exception {
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1, 1L, "prof1", "hi", "foo");
Group group = new Group();
group.applyEvent(createGroupEvent);
User user = new User("prof", "jens", "bendi", "hi@gmail.com");
AddUserEvent addUserEvent = new AddUserEvent(1L,1L, user);
AddUserEvent addUserEvent = new AddUserEvent(1L, 1L, user);
group.applyEvent(addUserEvent);
assertThat(group.getMembers().get(0)).isEqualTo(user);
}
@Test
void addExistingUser() throws Exception{
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1L,1L,"prof1", "hi", "foo");
void addExistingUser() throws Exception {
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1L, 1L, "prof1", "hi", "foo");
Group group = new Group();
group.applyEvent(createGroupEvent);
User user1 = new User("prof", "jens", "bendi", "hi@gmail.com");
AddUserEvent addUserEvent1 = new AddUserEvent(2L,1L, user1);
AddUserEvent addUserEvent1 = new AddUserEvent(2L, 1L, user1);
group.applyEvent(addUserEvent1);
User user2 = new User("prof", "olga", "bendi", "hi@gmail.com");
AddUserEvent addUserEvent2 = new AddUserEvent(3L,1L, user2);
Assertions.assertThrows(UserAlreadyExistsException.class, () ->{
AddUserEvent addUserEvent2 = new AddUserEvent(3L, 1L, user2);
Assertions.assertThrows(UserAlreadyExistsException.class, () -> {
group.applyEvent(addUserEvent2);
});
@ -69,7 +70,7 @@ class GroupTest {
}
@Test
void deleteSingleUser() throws Exception{
void deleteSingleUser() throws Exception {
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1, 2L, "Prof", "Tolle Gruppe", "Tolle Beshreibung");
User user = new User("Prof", "Pro", "fessor", "pro@fessor.de");
AddUserEvent addUserEvent = new AddUserEvent(2L, 2L, user);
@ -84,21 +85,21 @@ class GroupTest {
}
@Test
void deleteUserThatDoesNotExists() throws Exception{
void deleteUserThatDoesNotExists() throws Exception {
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1, 2L, "Prof", "Tolle Gruppe", "Tolle Beshreibung");
Group group = new Group();
group.applyEvent(createGroupEvent);
DeleteUserEvent deleteUserEvent = new DeleteUserEvent(3L, 2L, "Prof");
Assertions.assertThrows(UserNotFoundException.class, () ->{
Assertions.assertThrows(UserNotFoundException.class, () -> {
group.applyEvent(deleteUserEvent);
});
}
// Verwendet CreateGroupEvent und AddUserEvent
@Test
void updateRoleForExistingUser() throws Exception{
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");
@ -119,20 +120,20 @@ class GroupTest {
}
@Test
void updateRoleForNonExistingUser() throws Exception{
void updateRoleForNonExistingUser() throws Exception {
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1L, 1L, "1L", "gruppe1", "Eine Testgruppe");
UpdateRoleEvent updateRoleEvent = new UpdateRoleEvent(345L, 33L , "coolerUser", Role.ADMIN);
UpdateRoleEvent updateRoleEvent = new UpdateRoleEvent(345L, 33L, "coolerUser", Role.ADMIN);
Group group = new Group();
group.applyEvent(createGroupEvent);
Assertions.assertThrows(UserNotFoundException.class, () ->{
Assertions.assertThrows(UserNotFoundException.class, () -> {
group.applyEvent(updateRoleEvent);
});
}
@Test
void updateTitle() throws Exception{
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1,1L,"prof1", "hi", "foo");
void updateTitle() throws Exception {
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1, 1L, "prof1", "hi", "foo");
Group group = new Group();
group.applyEvent(createGroupEvent);
@ -143,8 +144,8 @@ class GroupTest {
}
@Test
void updateBeschreibung() throws Exception{
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1,1L,"prof1", "hi", "foo");
void updateBeschreibung() throws Exception {
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1, 1L, "prof1", "hi", "foo");
Group group = new Group();
group.applyEvent(createGroupEvent);
@ -153,4 +154,17 @@ 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);
}
}

View File

@ -0,0 +1,77 @@
package mops.gruppen2.service;
import mops.gruppen2.domain.Exceptions.GroupDoesNotExistException;
import mops.gruppen2.domain.Exceptions.UserNotFoundException;
import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.event.AddUserEvent;
import mops.gruppen2.domain.event.CreateGroupEvent;
import mops.gruppen2.domain.event.DeleteGroupEvent;
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 java.util.ArrayList;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
class GroupServiceTest {
GroupService groupService = new GroupService();
@BeforeEach
public void setUp() {
}
@Test
void applyEventOnGroupThatIsDeleted() throws Exception {
List<Event> eventList = new ArrayList<>();
eventList.add(new CreateGroupEvent(1, 10L, "prof1", "hi", "foo"));
eventList.add(new DeleteGroupEvent(44, 10, "loescher78"));
eventList.add(new AddUserEvent(900L, 10L, "Ulli", "Ulli", "Honnis", "FC@B.de"));
Assertions.assertThrows(GroupDoesNotExistException.class, () -> {
groupService.buildGroupFromEvents(eventList);
});
}
@Test
void returnDeletedGroup() throws Exception {
List<Event> eventList = new ArrayList<>();
eventList.add(new CreateGroupEvent(1, 10L, "prof1", "hi", "foo"));
eventList.add(new DeleteGroupEvent(44, 10, "loescher78"));
assertThat(groupService.buildGroupFromEvents(eventList)).isEqualTo(null);
}
@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 {
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);
}
}