1

improved testing

This commit is contained in:
Christoph
2020-03-24 22:28:56 +01:00
parent cdb22828aa
commit a0d6243b5c
8 changed files with 409 additions and 140 deletions

View File

@ -39,7 +39,7 @@ public class AddUserEvent extends Event {
throw new UserAlreadyExistsException(this.getClass().toString()); throw new UserAlreadyExistsException(this.getClass().toString());
} }
if (group.getMembers().size() == group.getUserMaximum()) { if (group.getMembers().size() >= group.getUserMaximum()) {
throw new GroupFullException(this.getClass().toString()); throw new GroupFullException(this.getClass().toString());
} }

View File

@ -5,7 +5,7 @@ import org.springframework.http.HttpStatus;
public class GroupFullException extends EventException { public class GroupFullException extends EventException {
public GroupFullException(String info) { public GroupFullException(String info) {
super(HttpStatus.INTERNAL_SERVER_ERROR, "Der User existiert bereits.", info); super(HttpStatus.INTERNAL_SERVER_ERROR, "Die Gruppe ist voll.", info);
} }
} }

View File

@ -9,6 +9,7 @@ import org.springframework.stereotype.Repository;
import java.util.List; import java.util.List;
@Repository @Repository
//TODO Rename Queries + Formatting
public interface EventRepository extends CrudRepository<EventDTO, Long> { public interface EventRepository extends CrudRepository<EventDTO, Long> {
@Query("select distinct group_id from event where user_id =:id") @Query("select distinct group_id from event where user_id =:id")

View File

@ -12,6 +12,7 @@ import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Service @Service
//TODO: Evtl aufsplitten in EventRepoService und EventService?
public class EventService { public class EventService {
private final JsonService jsonService; private final JsonService jsonService;
@ -28,8 +29,22 @@ public class EventService {
* @param event Event, welches gespeichert wird * @param event Event, welches gespeichert wird
*/ */
public void saveEvent(Event event) { public void saveEvent(Event event) {
EventDTO eventDTO = getDTO(event); eventStore.save(getDTO(event));
eventStore.save(eventDTO); }
public void saveAll(Event... events) {
for (Event event : events) {
eventStore.save(getDTO(event));
}
}
@SafeVarargs
public final void saveAll(List<Event>... events) {
for (List<Event> eventlist : events) {
for (Event event : eventlist) {
eventStore.save(getDTO(event));
}
}
} }
/** /**
@ -39,6 +54,7 @@ public class EventService {
* @param event Event, welches in DTO übersetzt wird * @param event Event, welches in DTO übersetzt wird
* @return EventDTO Neues DTO * @return EventDTO Neues DTO
*/ */
//TODO Rename: getDTOFromEvent?
public EventDTO getDTO(Event event) { public EventDTO getDTO(Event event) {
String payload = ""; String payload = "";
try { try {
@ -58,6 +74,7 @@ public class EventService {
/** /**
* Findet alle Events welche ab dem neuen Status hinzugekommen sind. * Findet alle Events welche ab dem neuen Status hinzugekommen sind.
* Sucht alle Events mit event_id > status
* *
* @param status Die Id des zuletzt gespeicherten Events * @param status Die Id des zuletzt gespeicherten Events
* @return Liste von neueren Events * @return Liste von neueren Events
@ -75,6 +92,7 @@ public class EventService {
* @param eventDTOS Liste von DTOs * @param eventDTOS Liste von DTOs
* @return Liste von Events * @return Liste von Events
*/ */
//TODO Rename: getEventsFromDTO?
public List<Event> translateEventDTOs(Iterable<EventDTO> eventDTOS) { public List<Event> translateEventDTOs(Iterable<EventDTO> eventDTOS) {
List<Event> events = new ArrayList<>(); List<Event> events = new ArrayList<>();
@ -88,7 +106,6 @@ public class EventService {
return events; return events;
} }
public Long getMaxEvent_id() { public Long getMaxEvent_id() {
return eventStore.getHighesEvent_ID(); return eventStore.getHighesEvent_ID();
} }
@ -98,6 +115,7 @@ public class EventService {
return translateEventDTOs(eventDTOList); return translateEventDTOs(eventDTOList);
} }
//TODO: Nur AddUserEvents betrachten
public List<UUID> findGroupIdsByUser(String userId) { public List<UUID> findGroupIdsByUser(String userId) {
List<String> groupIDs = eventStore.findGroup_idsWhereUser_id(userId); List<String> groupIDs = eventStore.findGroup_idsWhereUser_id(userId);

View File

@ -35,6 +35,7 @@ public class GroupService {
* @param groupIds Liste an IDs * @param groupIds Liste an IDs
* @return Liste an Events * @return Liste an Events
*/ */
//TODO Das vielleicht in den EventRepoService?
public List<Event> getGroupEvents(List<UUID> groupIds) { public List<Event> getGroupEvents(List<UUID> groupIds) {
List<EventDTO> eventDTOS = new ArrayList<>(); List<EventDTO> eventDTOS = new ArrayList<>();
for (UUID groupId : groupIds) { for (UUID groupId : groupIds) {
@ -54,8 +55,6 @@ public class GroupService {
public List<Group> projectEventList(List<Event> events) throws EventException { public List<Group> projectEventList(List<Event> events) throws EventException {
Map<UUID, Group> groupMap = new HashMap<>(); Map<UUID, Group> groupMap = new HashMap<>();
events.forEach(System.out::println);
events.parallelStream() events.parallelStream()
.forEachOrdered(event -> event.apply(getOrCreateGroup(groupMap, event.getGroupId()))); .forEachOrdered(event -> event.apply(getOrCreateGroup(groupMap, event.getGroupId())));
@ -77,6 +76,7 @@ public class GroupService {
* @return Liste von projizierten Gruppen * @return Liste von projizierten Gruppen
* @throws EventException Projektionsfehler * @throws EventException Projektionsfehler
*/ */
//TODO Rename
public List<Group> getAllGroupWithVisibilityPublic(String userId) throws EventException { public List<Group> getAllGroupWithVisibilityPublic(String userId) throws EventException {
List<Event> createEvents = eventService.translateEventDTOs(eventRepository.findAllEventsByType("CreateGroupEvent")); List<Event> createEvents = eventService.translateEventDTOs(eventRepository.findAllEventsByType("CreateGroupEvent"));
createEvents.addAll(eventService.translateEventDTOs(eventRepository.findAllEventsByType("UpdateGroupDescriptionEvent"))); createEvents.addAll(eventService.translateEventDTOs(eventRepository.findAllEventsByType("UpdateGroupDescriptionEvent")));
@ -95,6 +95,7 @@ public class GroupService {
} }
//TODO Rename
public List<Group> getAllLecturesWithVisibilityPublic() throws EventException { public List<Group> getAllLecturesWithVisibilityPublic() throws EventException {
List<Event> createEvents = eventService.translateEventDTOs(eventRepository.findAllEventsByType("CreateGroupEvent")); List<Event> createEvents = eventService.translateEventDTOs(eventRepository.findAllEventsByType("CreateGroupEvent"));
createEvents.addAll(eventService.translateEventDTOs(eventRepository.findAllEventsByType("UpdateGroupTitleEvent"))); createEvents.addAll(eventService.translateEventDTOs(eventRepository.findAllEventsByType("UpdateGroupTitleEvent")));
@ -103,7 +104,6 @@ public class GroupService {
List<Group> visibleGroups = projectEventList(createEvents); List<Group> visibleGroups = projectEventList(createEvents);
return visibleGroups.parallelStream() return visibleGroups.parallelStream()
.filter(group -> group.getType() != null)
.filter(group -> group.getType() == GroupType.LECTURE) .filter(group -> group.getType() == GroupType.LECTURE)
.filter(group -> group.getVisibility() == Visibility.PUBLIC) .filter(group -> group.getVisibility() == Visibility.PUBLIC)
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -118,12 +118,16 @@ public class GroupService {
* @return Liste von projizierten Gruppen * @return Liste von projizierten Gruppen
* @throws EventException Projektionsfehler * @throws EventException Projektionsfehler
*/ */
//Todo Rename
public List<Group> findGroupWith(String search, Account account) throws EventException { public List<Group> findGroupWith(String search, Account account) throws EventException {
if (search.isEmpty()) {
return getAllGroupWithVisibilityPublic(account.getName());
}
return getAllGroupWithVisibilityPublic(account.getName()) return getAllGroupWithVisibilityPublic(account.getName())
.parallelStream() .parallelStream()
.filter(group -> .filter(group -> group.getTitle().toLowerCase().contains(search.toLowerCase())
group.getTitle().toLowerCase().contains(search.toLowerCase()) || || group.getDescription().toLowerCase().contains(search.toLowerCase()))
group.getDescription().toLowerCase().contains(search.toLowerCase()))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
} }

View File

@ -7,11 +7,13 @@ import mops.gruppen2.domain.Role;
import mops.gruppen2.domain.Visibility; import mops.gruppen2.domain.Visibility;
import mops.gruppen2.domain.event.AddUserEvent; import mops.gruppen2.domain.event.AddUserEvent;
import mops.gruppen2.domain.event.CreateGroupEvent; import mops.gruppen2.domain.event.CreateGroupEvent;
import mops.gruppen2.domain.event.DeleteGroupEvent;
import mops.gruppen2.domain.event.DeleteUserEvent; import mops.gruppen2.domain.event.DeleteUserEvent;
import mops.gruppen2.domain.event.Event; import mops.gruppen2.domain.event.Event;
import mops.gruppen2.domain.event.UpdateGroupDescriptionEvent; import mops.gruppen2.domain.event.UpdateGroupDescriptionEvent;
import mops.gruppen2.domain.event.UpdateGroupTitleEvent; import mops.gruppen2.domain.event.UpdateGroupTitleEvent;
import mops.gruppen2.domain.event.UpdateRoleEvent; import mops.gruppen2.domain.event.UpdateRoleEvent;
import mops.gruppen2.security.Account;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@ -25,41 +27,67 @@ public class TestBuilder {
private static final Faker faker = new Faker(); private static final Faker faker = new Faker();
public static Account account(String name) {
return new Account(name,
"",
"",
"",
"",
null);
}
public static Group apply(Group group, Event... events) {
for (Event event : events) {
event.apply(group);
}
return group;
}
public static Group apply(Event... events) {
return apply(new Group(), events);
}
/** /**
* Baut eine UUID. * Baut eine UUID.
* *
* @param i Zahl von 0 bis 9 * @param id Integer id
* @return UUID * @return UUID
*/ */
public static UUID idFromNumber(int i) { public static UUID uuidFromInt(int id) {
if (i > 9) { return UUID.fromString("00000000-0000-0000-0000-"
return null; + "0".repeat(11 - String.valueOf(id).length())
} + id);
return UUID.fromString("00000000-0000-0000-0000-00000000000" + i);
} }
/** /**
* Generiert ein EventLog mit mehreren Gruppen und Usern. * Generiert ein EventLog mit mehreren Gruppen und Usern.
* *
* @param count Gruppenanzahl * @param count Gruppenanzahl
* @param membercount Gesamte Mitgliederanzahl * @param membercount Mitgliederanzahl pro Gruppe
* @return Eventliste * @return Eventliste
*/ */
public static List<Event> completeGroups(int count, int membercount) { public static List<Event> completePublicGroups(int count, int membercount) {
int memPerGroup = membercount / count; return IntStream.range(0, count)
return IntStream.rangeClosed(0, count)
.parallel() .parallel()
.mapToObj(i -> completeGroup(memPerGroup)) .mapToObj(i -> completePublicGroup(membercount))
.flatMap(Collection::stream) .flatMap(Collection::stream)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
public static List<Event> completeGroup(int membercount) { public static List<Event> completePrivateGroups(int count, int membercount) {
return IntStream.range(0, count)
.parallel()
.mapToObj(i -> completePrivateGroup(membercount))
.flatMap(Collection::stream)
.collect(Collectors.toList());
}
public static List<Event> completePublicGroup(int membercount) {
List<Event> eventList = new ArrayList<>(); List<Event> eventList = new ArrayList<>();
UUID groupId = UUID.randomUUID(); UUID groupId = UUID.randomUUID();
eventList.add(createGroupEvent(groupId)); eventList.add(createPublicGroupEvent(groupId));
eventList.add(updateGroupTitleEvent(groupId)); eventList.add(updateGroupTitleEvent(groupId));
eventList.add(updateGroupDescriptionEvent(groupId)); eventList.add(updateGroupDescriptionEvent(groupId));
eventList.addAll(addUserEvents(membercount, groupId)); eventList.addAll(addUserEvents(membercount, groupId));
@ -67,8 +95,24 @@ public class TestBuilder {
return eventList; return eventList;
} }
public static List<Event> completeGroup() { public static List<Event> completePrivateGroup(int membercount) {
return completeGroup(100); List<Event> eventList = new ArrayList<>();
UUID groupId = UUID.randomUUID();
eventList.add(createPrivateGroupEvent(groupId));
eventList.add(updateGroupTitleEvent(groupId));
eventList.add(updateGroupDescriptionEvent(groupId));
eventList.addAll(addUserEvents(membercount, groupId));
return eventList;
}
public static List<Event> completePublicGroup() {
return completePublicGroup(100);
}
public static List<Event> completePrivateGroup() {
return completePrivateGroup(100);
} }
/** /**
@ -77,26 +121,69 @@ public class TestBuilder {
* @param count Anzahl der verschiedenen Gruppen * @param count Anzahl der verschiedenen Gruppen
* @return Eventliste * @return Eventliste
*/ */
public static List<CreateGroupEvent> createGroupEvents(int count) { public static List<Event> createPublicGroupEvents(int count) {
return IntStream.rangeClosed(0, count) return IntStream.range(0, count)
.parallel() .parallel()
.mapToObj(i -> createGroupEvent()) .mapToObj(i -> createPublicGroupEvent())
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
public static CreateGroupEvent createGroupEvent(UUID groupId) { public static List<Event> createPrivateGroupEvents(int count) {
return IntStream.range(0, count)
.parallel()
.mapToObj(i -> createPublicGroupEvent())
.collect(Collectors.toList());
}
public static List<Event> createMixedGroupEvents(int count) {
return IntStream.range(0, count)
.parallel()
.mapToObj(i -> faker.random().nextInt(0, 1) > 0.5
? createPublicGroupEvent()
: createPrivateGroupEvent())
.collect(Collectors.toList());
}
public static Event createPrivateGroupEvent(UUID groupId) {
return createGroupEvent(groupId, Visibility.PRIVATE);
}
public static Event createPrivateGroupEvent() {
return createPrivateGroupEvent(UUID.randomUUID());
}
public static Event createPublicGroupEvent(UUID groupId) {
return createGroupEvent(groupId, Visibility.PUBLIC);
}
public static Event createPublicGroupEvent() {
return createPublicGroupEvent(UUID.randomUUID());
}
public static Event createGroupEvent(UUID groupId, Visibility visibility) {
return new CreateGroupEvent( return new CreateGroupEvent(
groupId, groupId,
faker.random().hex(), faker.random().hex(),
null, null,
GroupType.SIMPLE, GroupType.SIMPLE,
Visibility.PUBLIC, visibility,
10000000L 10000000L
); );
} }
public static CreateGroupEvent createGroupEvent() { public static Event createLectureEvent() {
return createGroupEvent(UUID.randomUUID()); return createLectureEvent(UUID.randomUUID());
}
public static Event createLectureEvent(UUID groupId) {
return new CreateGroupEvent(
groupId,
faker.random().hex(),
null,
GroupType.LECTURE,
Visibility.PUBLIC,
10000000L
);
} }
/** /**
@ -107,13 +194,13 @@ public class TestBuilder {
* @return Eventliste * @return Eventliste
*/ */
public static List<Event> addUserEvents(int count, UUID groupId) { public static List<Event> addUserEvents(int count, UUID groupId) {
return IntStream.rangeClosed(1, count) return IntStream.range(0, count)
.parallel() .parallel()
.mapToObj(i -> addUserEvent(groupId, String.valueOf(i))) .mapToObj(i -> addUserEvent(groupId, String.valueOf(i)))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
public static AddUserEvent addUserEvent(UUID groupId, String userId) { public static Event addUserEvent(UUID groupId, String userId) {
String firstname = firstname(); String firstname = firstname();
String lastname = lastname(); String lastname = lastname();
@ -126,10 +213,11 @@ public class TestBuilder {
); );
} }
public static AddUserEvent addUserEvent(UUID groupId) { public static Event addUserEvent(UUID groupId) {
return addUserEvent(groupId, faker.random().hex()); return addUserEvent(groupId, faker.random().hex());
} }
// Am besten einfach nicht benutzen
public static List<Event> deleteUserEvents(int count, List<Event> eventList) { public static List<Event> deleteUserEvents(int count, List<Event> eventList) {
List<Event> removeEvents = new ArrayList<>(); List<Event> removeEvents = new ArrayList<>();
List<Event> shuffle = eventList.parallelStream() List<Event> shuffle = eventList.parallelStream()
@ -155,36 +243,44 @@ public class TestBuilder {
* @param group Gruppe welche geleert wird * @param group Gruppe welche geleert wird
* @return Eventliste * @return Eventliste
*/ */
public static List<DeleteUserEvent> deleteUserEvents(Group group) { public static List<Event> deleteUserEvents(Group group) {
return group.getMembers().parallelStream() return group.getMembers().parallelStream()
.map(user -> deleteUserEvent(group.getId(), user.getId())) .map(user -> deleteUserEvent(group.getId(), user.getId()))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
public static DeleteUserEvent deleteUserEvent(UUID groupId, String userId) { public static Event deleteUserEvent(UUID groupId, String userId) {
return new DeleteUserEvent( return new DeleteUserEvent(
groupId, groupId,
userId userId
); );
} }
public static UpdateGroupDescriptionEvent updateGroupDescriptionEvent(UUID groupId) { public static Event updateGroupDescriptionEvent(UUID groupId) {
return updateGroupDescriptionEvent(groupId, quote());
}
public static Event updateGroupDescriptionEvent(UUID groupId, String description) {
return new UpdateGroupDescriptionEvent( return new UpdateGroupDescriptionEvent(
groupId, groupId,
faker.random().hex(), faker.random().hex(),
quote() description
); );
} }
public static UpdateGroupTitleEvent updateGroupTitleEvent(UUID groupId) { public static Event updateGroupTitleEvent(UUID groupId) {
return updateGroupTitleEvent(groupId, champion());
}
public static Event updateGroupTitleEvent(UUID groupId, String title) {
return new UpdateGroupTitleEvent( return new UpdateGroupTitleEvent(
groupId, groupId,
faker.random().hex(), faker.random().hex(),
champion() title
); );
} }
public static UpdateRoleEvent randomUpdateRoleEvent(UUID groupId, String userId, Role role) { public static Event updateRoleEvent(UUID groupId, String userId, Role role) {
return new UpdateRoleEvent( return new UpdateRoleEvent(
groupId, groupId,
userId, userId,
@ -192,6 +288,10 @@ public class TestBuilder {
); );
} }
public static Event deleteGroupEvent(UUID groupId) {
return new DeleteGroupEvent(groupId, faker.random().hex());
}
private static String firstname() { private static String firstname() {
return clean(faker.name().firstName()); return clean(faker.name().firstName());
} }

View File

@ -1,14 +1,10 @@
package mops.gruppen2.service; package mops.gruppen2.service;
import mops.gruppen2.Gruppen2Application; import mops.gruppen2.Gruppen2Application;
import mops.gruppen2.domain.GroupType;
import mops.gruppen2.domain.Visibility;
import mops.gruppen2.domain.dto.EventDTO; import mops.gruppen2.domain.dto.EventDTO;
import mops.gruppen2.domain.event.CreateGroupEvent;
import mops.gruppen2.domain.event.Event; import mops.gruppen2.domain.event.Event;
import mops.gruppen2.repository.EventRepository; import mops.gruppen2.repository.EventRepository;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -17,17 +13,21 @@ import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList; import static mops.gruppen2.TestBuilder.addUserEvent;
import java.util.List; import static mops.gruppen2.TestBuilder.addUserEvents;
import java.util.UUID; import static mops.gruppen2.TestBuilder.createPrivateGroupEvent;
import static mops.gruppen2.TestBuilder.createPrivateGroupEvents;
import static mops.gruppen2.TestBuilder.createPublicGroupEvent;
import static mops.gruppen2.TestBuilder.createPublicGroupEvents;
import static mops.gruppen2.TestBuilder.updateGroupDescriptionEvent;
import static mops.gruppen2.TestBuilder.uuidFromInt;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
//TODO: Der ID autocounter wird nicht resettet -> Tests schlagen fehl beim nacheinanderausführen
@ExtendWith(SpringExtension.class) @ExtendWith(SpringExtension.class)
@SpringBootTest(classes = Gruppen2Application.class) @SpringBootTest(classes = Gruppen2Application.class)
@Rollback
@Transactional @Transactional
@Rollback
class EventServiceTest { class EventServiceTest {
@Autowired @Autowired
@ -39,39 +39,90 @@ class EventServiceTest {
@BeforeEach @BeforeEach
void setUp() { void setUp() {
eventService = new EventService(jsonService, eventRepository); eventService = new EventService(jsonService, eventRepository);
}
@Disabled
@Test
void getMaxID() {
eventRepository.deleteAll(); eventRepository.deleteAll();
//UpdateRoleEvent updateRoleEvent = new UpdateRoleEvent(2L, "hi",Role.ADMIN);
//eventService.saveEvent(updateRoleEvent);
assertEquals(1L, eventService.getMaxEvent_id()); // funzt noch net richtig weil Autoincrement hochaddiert auch wenn DB leer
} }
@Test @Test
void checkGroupReturnNextValue() { void saveEvent() {
eventRepository.deleteAll(); eventService.saveEvent(createPublicGroupEvent());
CreateGroupEvent createGroupEvent = new CreateGroupEvent(UUID.fromString("A"), "lol", null, GroupType.SIMPLE, Visibility.PUBLIC, 20L);
eventService.saveEvent(createGroupEvent); assertThat(eventRepository.findAll()).hasSize(1);
assertEquals(2L, UUID.fromString("A")); // weil in DataSQL eine Gruppe erstellt wird
} }
@Test @Test
void checkGroupReturnOneIfDBIsEmpty() { void saveAll() {
//dafür muss data.sql weg eventService.saveAll(createPrivateGroupEvents(10));
eventRepository.deleteAll();
assertEquals(1L, UUID.fromString("A")); assertThat(eventRepository.findAll()).hasSize(10);
} }
@Test @Test
void translateEventDTOsTest() { void testSaveAll() {
//EventDTO eventDTO1 = new EventDTO(1L,1L, "killerbert", "CreateGroupEvent", "{\"type\":\"CreateGroupEvent\",\"groupId\":1,\"userId\":\"orga\",\"groupVisibility\":\"PUBLIC\",\"groupParent\":null,\"groupType\":\"SIMPLE\",\"groupUserMaximum\":2}"); eventService.saveAll(createPublicGroupEvents(5),
List<EventDTO> eventDTOS1 = new ArrayList<>(); createPrivateGroupEvents(5));
//eventDTOS1.add(eventDTO1);
List<Event> events = eventService.translateEventDTOs(eventDTOS1); assertThat(eventRepository.findAll()).hasSize(10);
assertThat(events.get(0)).isInstanceOf(CreateGroupEvent.class);
} }
@Test
void getDTO() {
Event event = createPublicGroupEvent();
EventDTO dto = eventService.getDTO(event);
assertThat(dto.getGroup_id()).isEqualTo(event.getGroupId().toString());
assertThat(dto.getUser_id()).isEqualTo(event.getUserId());
assertThat(dto.getEvent_id()).isEqualTo(null);
assertThat(dto.getEvent_type()).isEqualTo("CreateGroupEvent");
}
@Test
void getNewEvents_createGroupEvents() {
eventService.saveAll(createPrivateGroupEvents(10));
assertThat(eventService.getNewEvents(0L)).hasSize(10);
assertThat(eventService.getNewEvents(5L)).hasSize(5);
assertThat(eventService.getNewEvents(10L)).isEmpty();
}
@Test
void getNewEvents_mixedEvents() {
eventService.saveAll(createPrivateGroupEvent(uuidFromInt(0)),
updateGroupDescriptionEvent(uuidFromInt(0)),
createPrivateGroupEvent(uuidFromInt(1)),
updateGroupDescriptionEvent(uuidFromInt(1)));
assertThat(eventService.getNewEvents(0L)).hasSize(4);
assertThat(eventService.getNewEvents(1L)).hasSize(4);
assertThat(eventService.getNewEvents(2L)).hasSize(2);
assertThat(eventService.getNewEvents(3L)).hasSize(2);
}
@Test
void getMaxEvent_id() {
eventService.saveAll(createPrivateGroupEvents(10));
assertThat(eventService.getMaxEvent_id()).isEqualTo(10);
}
@Test
void getEventsOfGroup() {
eventService.saveAll(addUserEvents(10, uuidFromInt(0)),
addUserEvents(5, uuidFromInt(1)));
assertThat(eventService.getEventsOfGroup(uuidFromInt(0))).hasSize(10);
assertThat(eventService.getEventsOfGroup(uuidFromInt(1))).hasSize(5);
}
@Test
void findGroupIdsByUser() {
eventService.saveAll(addUserEvent(uuidFromInt(0), "A"),
addUserEvent(uuidFromInt(1), "A"),
addUserEvent(uuidFromInt(2), "A"),
addUserEvent(uuidFromInt(3), "A"),
addUserEvent(uuidFromInt(3), "B"));
assertThat(eventService.findGroupIdsByUser("A")).hasSize(4);
assertThat(eventService.findGroupIdsByUser("B")).hasSize(1);
}
} }

View File

@ -1,43 +1,47 @@
package mops.gruppen2.service; package mops.gruppen2.service;
import mops.gruppen2.Gruppen2Application; import mops.gruppen2.Gruppen2Application;
import mops.gruppen2.TestBuilder;
import mops.gruppen2.domain.Group; import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.GroupType;
import mops.gruppen2.domain.Role;
import mops.gruppen2.domain.Visibility; import mops.gruppen2.domain.Visibility;
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 mops.gruppen2.domain.event.Event;
import mops.gruppen2.domain.event.UpdateGroupDescriptionEvent;
import mops.gruppen2.domain.event.UpdateGroupTitleEvent;
import mops.gruppen2.domain.event.UpdateRoleEvent;
import mops.gruppen2.repository.EventRepository; import mops.gruppen2.repository.EventRepository;
import mops.gruppen2.security.Account;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import static mops.gruppen2.TestBuilder.idFromNumber; import static mops.gruppen2.TestBuilder.account;
import static mops.gruppen2.TestBuilder.addUserEvent;
import static mops.gruppen2.TestBuilder.completePrivateGroup;
import static mops.gruppen2.TestBuilder.completePrivateGroups;
import static mops.gruppen2.TestBuilder.completePublicGroups;
import static mops.gruppen2.TestBuilder.createLectureEvent;
import static mops.gruppen2.TestBuilder.createPrivateGroupEvent;
import static mops.gruppen2.TestBuilder.createPublicGroupEvent;
import static mops.gruppen2.TestBuilder.deleteGroupEvent;
import static mops.gruppen2.TestBuilder.updateGroupDescriptionEvent;
import static mops.gruppen2.TestBuilder.updateGroupTitleEvent;
import static mops.gruppen2.TestBuilder.uuidFromInt;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ExtendWith(SpringExtension.class) @ExtendWith(SpringExtension.class)
@SpringBootTest(classes = Gruppen2Application.class) @SpringBootTest(classes = Gruppen2Application.class)
@Transactional
@Rollback
class GroupServiceTest { class GroupServiceTest {
@Autowired @Autowired
private EventRepository eventRepository; private EventRepository eventRepository;
@Autowired @Autowired
private JsonService jsonService;
@Autowired
private EventService eventService; private EventService eventService;
private GroupService groupService; private GroupService groupService;
@ -47,91 +51,182 @@ class GroupServiceTest {
eventRepository.deleteAll(); eventRepository.deleteAll();
} }
//TODO: Wofür ist dieser Test?
@Test @Test
void rightClassForSuccessfulGroup() { void rightClassForSuccessfulGroup() {
List<Event> eventList = new ArrayList<>(); /*List<Event> eventList = new ArrayList<>();
UUID id = UUID.randomUUID(); UUID id = UUID.randomUUID();
eventList.add(new CreateGroupEvent(id, "Prof", null, GroupType.LECTURE, Visibility.PRIVATE, 1000L)); eventList.add(new CreateGroupEvent(id, "Prof", null, GroupType.LECTURE, Visibility.PRIVATE, 1000L));
eventList.add(new AddUserEvent(id, "Ulli", "Ulli", "Honnis", "FC@B.de")); eventList.add(new AddUserEvent(id, "Ulli", "Ulli", "Honnis", "FC@B.de"));*/
List<Event> eventList = completePrivateGroup(1);
List<Group> groups = groupService.projectEventList(eventList); List<Group> groups = groupService.projectEventList(eventList);
assertThat(groups.get(0)).isInstanceOf(Group.class); assertThat(groups.get(0)).isInstanceOf(Group.class);
} }
@Test @Test
void getGroupEventsTest() { void projectEventList_SingleGroup() {
CreateGroupEvent test1 = new CreateGroupEvent(idFromNumber(0), "test1", null, GroupType.SIMPLE, Visibility.PUBLIC, 20L); List<Event> eventList = completePrivateGroup(5);
CreateGroupEvent test2 = new CreateGroupEvent(idFromNumber(1), "test2", null, GroupType.SIMPLE, Visibility.PUBLIC, 10L);
eventService.saveEvent(test1); List<Group> groups = groupService.projectEventList(eventList);
eventService.saveEvent(test2);
List<UUID> longs = new ArrayList<>(); assertThat(groups).hasSize(1);
longs.add(idFromNumber(0)); assertThat(groups.get(0).getMembers()).hasSize(5);
longs.add(idFromNumber(1)); assertThat(groups.get(0).getVisibility()).isEqualTo(Visibility.PRIVATE);
assertThat(groupService.getGroupEvents(longs).get(0).getUserId()).isEqualTo("test1"); }
@Test
void projectEventList_MultipleGroups() {
List<Event> eventList = completePrivateGroups(10, 2);
eventList.addAll(completePublicGroups(10, 5));
List<Group> groups = groupService.projectEventList(eventList);
assertThat(groups).hasSize(20);
assertThat(groups.stream().map(group -> group.getMembers().size()).reduce(Integer::sum).get()).isEqualTo(70);
}
@Test
void getGroupEvents() {
// CreateGroupEvent test1 = new CreateGroupEvent(uuidFromInt(0), "test1", null, GroupType.SIMPLE, Visibility.PUBLIC, 20L);
// CreateGroupEvent test2 = new CreateGroupEvent(uuidFromInt(1), "test2", null, GroupType.SIMPLE, Visibility.PUBLIC, 10L);
eventService.saveAll(createPublicGroupEvent(uuidFromInt(0)),
createPublicGroupEvent(uuidFromInt(1)),
createPrivateGroupEvent(uuidFromInt(2)));
List<UUID> groupIds = Arrays.asList(uuidFromInt(0), uuidFromInt(1));
assertThat(groupService.getGroupEvents(groupIds)).hasSize(2);
assertThat(groupService.getGroupEvents(groupIds).get(0).getGroupId()).isEqualTo(uuidFromInt(0));
assertThat(groupService.getGroupEvents(groupIds).get(1).getGroupId()).isEqualTo(uuidFromInt(1));
} }
@Test @Test
void getAllGroupWithVisibilityPublicTestCreateAndDeleteSameGroup() { void getAllGroupWithVisibilityPublicTestCreateAndDeleteSameGroup() {
CreateGroupEvent test1 = new CreateGroupEvent(idFromNumber(0), "test1", null, GroupType.SIMPLE, Visibility.PUBLIC, 20L); //CreateGroupEvent test1 = new CreateGroupEvent(uuidFromInt(0), "test1", null, GroupType.SIMPLE, Visibility.PUBLIC, 20L);
DeleteGroupEvent test2 = new DeleteGroupEvent(idFromNumber(0), "test1"); //DeleteGroupEvent test2 = new DeleteGroupEvent(uuidFromInt(0), "test1");
Event test1 = createPublicGroupEvent(uuidFromInt(0));
Event test2 = deleteGroupEvent(uuidFromInt(0));
Group group = new Group(); //Group group = new Group();
test1.apply(group); //test1.apply(group);
test2.apply(group); //test2.apply(group);
//assertThat(group.getType()).isEqualTo(null); //TODO: Hier projectEventlist()?
Group group = TestBuilder.apply(test1, test2);
assertThat(groupService.getAllGroupWithVisibilityPublic("test1").size()).isEqualTo(0); assertThat(group.getType()).isEqualTo(null);
assertThat(groupService.getAllGroupWithVisibilityPublic("test1")).isEmpty();
} }
@Test @Test
void getAllGroupWithVisibilityPublicTestGroupPublic() { void getAllGroupWithVisibilityPublicTestGroupPublic() {
eventService.saveEvent(new CreateGroupEvent(idFromNumber(0), "test1", null, GroupType.SIMPLE, Visibility.PUBLIC, 20L)); // eventService.saveEvent(new CreateGroupEvent(uuidFromInt(0), "test1", null, GroupType.SIMPLE, Visibility.PUBLIC, 20L));
eventService.saveEvent(new DeleteGroupEvent(idFromNumber(0), "test1")); // eventService.saveEvent(new DeleteGroupEvent(uuidFromInt(0), "test1"));
eventService.saveEvent(new CreateGroupEvent(idFromNumber(1), "test2", null, GroupType.LECTURE, Visibility.PUBLIC, 10L)); // eventService.saveEvent(new CreateGroupEvent(uuidFromInt(1), "test2", null, GroupType.LECTURE, Visibility.PUBLIC, 10L));
eventService.saveEvent(new UpdateRoleEvent(idFromNumber(1), "test2", Role.MEMBER)); // eventService.saveEvent(new UpdateRoleEvent(uuidFromInt(1), "test2", Role.MEMBER)); //Wofür ist das
eventService.saveAll(createPublicGroupEvent(uuidFromInt(0)),
deleteGroupEvent(uuidFromInt(0)),
createPublicGroupEvent());
assertThat(groupService.getAllGroupWithVisibilityPublic("test1").size()).isEqualTo(1); assertThat(groupService.getAllGroupWithVisibilityPublic("test1").size()).isEqualTo(1);
} }
@Test @Test
void getAllGroupWithVisibilityPublicTestAddSomeEvents() { void getAllGroupWithVisibilityPublicTestAddSomeEvents() {
eventService.saveEvent(new CreateGroupEvent(idFromNumber(0), "test1", null, GroupType.SIMPLE, Visibility.PUBLIC, 20L)); // eventService.saveEvent(new CreateGroupEvent(uuidFromInt(0), "test1", null, GroupType.SIMPLE, Visibility.PUBLIC, 20L));
eventService.saveEvent(new DeleteGroupEvent(idFromNumber(0), "test1")); // eventService.saveEvent(new DeleteGroupEvent(uuidFromInt(0), "test1"));
eventService.saveEvent(new CreateGroupEvent(idFromNumber(1), "test2", null, GroupType.LECTURE, Visibility.PUBLIC, 10L)); // eventService.saveEvent(new CreateGroupEvent(uuidFromInt(1), "test2", null, GroupType.LECTURE, Visibility.PUBLIC, 10L));
eventService.saveEvent(new UpdateRoleEvent(idFromNumber(1), "test2", Role.MEMBER)); // eventService.saveEvent(new UpdateRoleEvent(uuidFromInt(1), "test2", Role.MEMBER)); // Wofür?
eventService.saveEvent(new CreateGroupEvent(idFromNumber(2), "test3", null, GroupType.LECTURE, Visibility.PUBLIC, 10L)); // eventService.saveEvent(new CreateGroupEvent(uuidFromInt(2), "test3", null, GroupType.LECTURE, Visibility.PUBLIC, 10L));
eventService.saveEvent(new CreateGroupEvent(idFromNumber(3), "test4", null, GroupType.LECTURE, Visibility.PUBLIC, 10L)); // eventService.saveEvent(new CreateGroupEvent(uuidFromInt(3), "test4", null, GroupType.LECTURE, Visibility.PUBLIC, 10L));
eventService.saveEvent(new CreateGroupEvent(idFromNumber(4), "test5", null, GroupType.LECTURE, Visibility.PUBLIC, 10L)); // eventService.saveEvent(new CreateGroupEvent(uuidFromInt(4), "test5", null, GroupType.LECTURE, Visibility.PUBLIC, 10L));
assertThat(groupService.getAllGroupWithVisibilityPublic("test1").size()).isEqualTo(4);
eventService.saveAll(createPublicGroupEvent(uuidFromInt(0)),
deleteGroupEvent(uuidFromInt(0)),
createPublicGroupEvent(),
createPublicGroupEvent(),
createPublicGroupEvent(),
createPrivateGroupEvent());
assertThat(groupService.getAllGroupWithVisibilityPublic("test1").size()).isEqualTo(3);
} }
@Disabled
@Test @Test
void getAllGroupWithVisibilityPublicTestIsUserInGroup() { void getAllGroupWithVisibilityPublicTestIsUserInGroup() {
eventService.saveEvent(new CreateGroupEvent(idFromNumber(0), "test1", null, GroupType.SIMPLE, Visibility.PUBLIC, 20L)); // eventService.saveEvent(new CreateGroupEvent(uuidFromInt(0), "test1", null, GroupType.SIMPLE, Visibility.PUBLIC, 20L));
eventService.saveEvent(new AddUserEvent(idFromNumber(0), "test1", "test", "test", "test@test")); // eventService.saveEvent(new AddUserEvent(uuidFromInt(0), "test1", "test", "test", "test@test"));
assertThat(groupService.getAllGroupWithVisibilityPublic("test2").get(0).getMembers().size()).isEqualTo(1);
eventService.saveAll(createPublicGroupEvent(uuidFromInt(0)),
addUserEvent(uuidFromInt(0), "kobold"),
createPrivateGroupEvent(),
createPublicGroupEvent());
//Das kommt glaube ich eher in einen Test für die Projektion
//assertThat(groupService.getAllGroupWithVisibilityPublic("test2").get(0).getMembers().size()).isEqualTo(1);
assertThat(groupService.getAllGroupWithVisibilityPublic("kobold")).hasSize(1);
assertThat(groupService.getAllGroupWithVisibilityPublic("peter")).hasSize(2);
} }
@Test @Test
void getAllLecturesWithVisibilityPublicTest() { void getAllLecturesWithVisibilityPublic() {
eventService.saveEvent(new CreateGroupEvent(idFromNumber(0), "test1", null, GroupType.SIMPLE, Visibility.PUBLIC, 20L)); // eventService.saveEvent(new CreateGroupEvent(uuidFromInt(0), "test1", null, GroupType.SIMPLE, Visibility.PUBLIC, 20L));
eventService.saveEvent(new CreateGroupEvent(idFromNumber(1), "test2", null, GroupType.LECTURE, Visibility.PUBLIC, 10L)); // eventService.saveEvent(new CreateGroupEvent(uuidFromInt(1), "test2", null, GroupType.LECTURE, Visibility.PUBLIC, 10L));
eventService.saveEvent(new UpdateRoleEvent(idFromNumber(1), "test2", Role.MEMBER)); // eventService.saveEvent(new UpdateRoleEvent(uuidFromInt(1), "test2", Role.MEMBER)); // Hä
eventService.saveEvent(new CreateGroupEvent(idFromNumber(2), "test3", null, GroupType.LECTURE, Visibility.PUBLIC, 10L)); // eventService.saveEvent(new CreateGroupEvent(uuidFromInt(2), "test3", null, GroupType.LECTURE, Visibility.PUBLIC, 10L));
eventService.saveEvent(new CreateGroupEvent(idFromNumber(3), "test4", null, GroupType.LECTURE, Visibility.PUBLIC, 10L)); // eventService.saveEvent(new CreateGroupEvent(uuidFromInt(3), "test4", null, GroupType.LECTURE, Visibility.PUBLIC, 10L));
eventService.saveEvent(new CreateGroupEvent(idFromNumber(4), "test5", null, GroupType.LECTURE, Visibility.PUBLIC, 10L)); // eventService.saveEvent(new CreateGroupEvent(uuidFromInt(4), "test5", null, GroupType.LECTURE, Visibility.PUBLIC, 10L));
eventService.saveAll(createLectureEvent(),
createPublicGroupEvent(),
createLectureEvent(),
createLectureEvent(),
createLectureEvent());
assertThat(groupService.getAllLecturesWithVisibilityPublic().size()).isEqualTo(4); assertThat(groupService.getAllLecturesWithVisibilityPublic().size()).isEqualTo(4);
} }
@Disabled
@Test @Test
void findGroupWithTest() { void findGroupWith_UserMember_AllGroups() {
eventService.saveEvent(new CreateGroupEvent(idFromNumber(0), "test1", null, GroupType.SIMPLE, Visibility.PUBLIC, 20L)); // eventService.saveEvent(new CreateGroupEvent(uuidFromInt(0), "test1", null, GroupType.SIMPLE, Visibility.PUBLIC, 20L));
eventService.saveEvent(new AddUserEvent(idFromNumber(0), "test1", "test", "test", "test@test")); // eventService.saveEvent(new AddUserEvent(uuidFromInt(0), "test1", "test", "test", "test@test"));
eventService.saveEvent(new UpdateGroupTitleEvent(idFromNumber(0), "test1", "TestGroup")); // eventService.saveEvent(new UpdateGroupTitleEvent(uuidFromInt(0), "test1", "TestGroup"));
eventService.saveEvent(new UpdateGroupDescriptionEvent(idFromNumber(0), "test1", "TestDescription")); // eventService.saveEvent(new UpdateGroupDescriptionEvent(uuidFromInt(0), "test1", "TestDescription"));
eventService.saveEvent(new UpdateRoleEvent(idFromNumber(0), "test1", Role.MEMBER)); // eventService.saveEvent(new UpdateRoleEvent(uuidFromInt(0), "test1", Role.MEMBER));
assertThat(groupService.findGroupWith("T", new Account("jens", "a@A", "test", "peter", "mueller", null)).size()).isEqualTo(1);
eventService.saveAll(createPublicGroupEvent(uuidFromInt(0)),
addUserEvent(uuidFromInt(0), "jens"),
updateGroupTitleEvent(uuidFromInt(0)),
updateGroupDescriptionEvent(uuidFromInt(0)));
//assertThat(groupService.findGroupWith("T", new Account("jens", "a@A", "test", "peter", "mueller", null)).size()).isEqualTo(1);
assertThat(groupService.findGroupWith("", account("jens"))).isEmpty();
}
@Test
void findGroupWith_UserNoMember_AllGroups() {
eventService.saveAll(completePublicGroups(10, 0),
completePrivateGroups(10, 0));
assertThat(groupService.findGroupWith("", account("jens"))).hasSize(10);
}
@Test
void findGroupWith_FilterGroups() {
eventService.saveAll(createPublicGroupEvent(uuidFromInt(0)),
updateGroupTitleEvent(uuidFromInt(0), "KK"),
updateGroupDescriptionEvent(uuidFromInt(0), "ABCDE"),
createPublicGroupEvent(uuidFromInt(1)),
updateGroupTitleEvent(uuidFromInt(1), "ABCDEFG"),
updateGroupDescriptionEvent(uuidFromInt(1), "KK"),
createPrivateGroupEvent());
assertThat(groupService.findGroupWith("A", account("jesus"))).hasSize(2);
assertThat(groupService.findGroupWith("F", account("jesus"))).hasSize(1);
assertThat(groupService.findGroupWith("Z", account("jesus"))).hasSize(0);
} }
} }