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

@ -1,14 +1,10 @@
package mops.gruppen2.service;
import mops.gruppen2.Gruppen2Application;
import mops.gruppen2.domain.GroupType;
import mops.gruppen2.domain.Visibility;
import mops.gruppen2.domain.dto.EventDTO;
import mops.gruppen2.domain.event.CreateGroupEvent;
import mops.gruppen2.domain.event.Event;
import mops.gruppen2.repository.EventRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
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.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import static mops.gruppen2.TestBuilder.addUserEvent;
import static mops.gruppen2.TestBuilder.addUserEvents;
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.junit.jupiter.api.Assertions.assertEquals;
//TODO: Der ID autocounter wird nicht resettet -> Tests schlagen fehl beim nacheinanderausführen
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = Gruppen2Application.class)
@Rollback
@Transactional
@Rollback
class EventServiceTest {
@Autowired
@ -39,39 +39,90 @@ class EventServiceTest {
@BeforeEach
void setUp() {
eventService = new EventService(jsonService, eventRepository);
}
@Disabled
@Test
void getMaxID() {
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
void checkGroupReturnNextValue() {
eventRepository.deleteAll();
CreateGroupEvent createGroupEvent = new CreateGroupEvent(UUID.fromString("A"), "lol", null, GroupType.SIMPLE, Visibility.PUBLIC, 20L);
eventService.saveEvent(createGroupEvent);
assertEquals(2L, UUID.fromString("A")); // weil in DataSQL eine Gruppe erstellt wird
void saveEvent() {
eventService.saveEvent(createPublicGroupEvent());
assertThat(eventRepository.findAll()).hasSize(1);
}
@Test
void checkGroupReturnOneIfDBIsEmpty() {
//dafür muss data.sql weg
eventRepository.deleteAll();
assertEquals(1L, UUID.fromString("A"));
void saveAll() {
eventService.saveAll(createPrivateGroupEvents(10));
assertThat(eventRepository.findAll()).hasSize(10);
}
@Test
void translateEventDTOsTest() {
//EventDTO eventDTO1 = new EventDTO(1L,1L, "killerbert", "CreateGroupEvent", "{\"type\":\"CreateGroupEvent\",\"groupId\":1,\"userId\":\"orga\",\"groupVisibility\":\"PUBLIC\",\"groupParent\":null,\"groupType\":\"SIMPLE\",\"groupUserMaximum\":2}");
List<EventDTO> eventDTOS1 = new ArrayList<>();
//eventDTOS1.add(eventDTO1);
List<Event> events = eventService.translateEventDTOs(eventDTOS1);
assertThat(events.get(0)).isInstanceOf(CreateGroupEvent.class);
void testSaveAll() {
eventService.saveAll(createPublicGroupEvents(5),
createPrivateGroupEvents(5));
assertThat(eventRepository.findAll()).hasSize(10);
}
@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;
import mops.gruppen2.Gruppen2Application;
import mops.gruppen2.TestBuilder;
import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.GroupType;
import mops.gruppen2.domain.Role;
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.UpdateGroupDescriptionEvent;
import mops.gruppen2.domain.event.UpdateGroupTitleEvent;
import mops.gruppen2.domain.event.UpdateRoleEvent;
import mops.gruppen2.repository.EventRepository;
import mops.gruppen2.security.Account;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
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.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;
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = Gruppen2Application.class)
@Transactional
@Rollback
class GroupServiceTest {
@Autowired
private EventRepository eventRepository;
@Autowired
private JsonService jsonService;
@Autowired
private EventService eventService;
private GroupService groupService;
@ -47,91 +51,182 @@ class GroupServiceTest {
eventRepository.deleteAll();
}
//TODO: Wofür ist dieser Test?
@Test
void rightClassForSuccessfulGroup() {
List<Event> eventList = new ArrayList<>();
/*List<Event> eventList = new ArrayList<>();
UUID id = UUID.randomUUID();
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);
assertThat(groups.get(0)).isInstanceOf(Group.class);
}
@Test
void getGroupEventsTest() {
CreateGroupEvent test1 = new CreateGroupEvent(idFromNumber(0), "test1", null, GroupType.SIMPLE, Visibility.PUBLIC, 20L);
CreateGroupEvent test2 = new CreateGroupEvent(idFromNumber(1), "test2", null, GroupType.SIMPLE, Visibility.PUBLIC, 10L);
eventService.saveEvent(test1);
eventService.saveEvent(test2);
List<UUID> longs = new ArrayList<>();
longs.add(idFromNumber(0));
longs.add(idFromNumber(1));
assertThat(groupService.getGroupEvents(longs).get(0).getUserId()).isEqualTo("test1");
void projectEventList_SingleGroup() {
List<Event> eventList = completePrivateGroup(5);
List<Group> groups = groupService.projectEventList(eventList);
assertThat(groups).hasSize(1);
assertThat(groups.get(0).getMembers()).hasSize(5);
assertThat(groups.get(0).getVisibility()).isEqualTo(Visibility.PRIVATE);
}
@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
void getAllGroupWithVisibilityPublicTestCreateAndDeleteSameGroup() {
CreateGroupEvent test1 = new CreateGroupEvent(idFromNumber(0), "test1", null, GroupType.SIMPLE, Visibility.PUBLIC, 20L);
DeleteGroupEvent test2 = new DeleteGroupEvent(idFromNumber(0), "test1");
//CreateGroupEvent test1 = new CreateGroupEvent(uuidFromInt(0), "test1", null, GroupType.SIMPLE, Visibility.PUBLIC, 20L);
//DeleteGroupEvent test2 = new DeleteGroupEvent(uuidFromInt(0), "test1");
Event test1 = createPublicGroupEvent(uuidFromInt(0));
Event test2 = deleteGroupEvent(uuidFromInt(0));
Group group = new Group();
test1.apply(group);
test2.apply(group);
//Group group = new Group();
//test1.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
void getAllGroupWithVisibilityPublicTestGroupPublic() {
eventService.saveEvent(new CreateGroupEvent(idFromNumber(0), "test1", null, GroupType.SIMPLE, Visibility.PUBLIC, 20L));
eventService.saveEvent(new DeleteGroupEvent(idFromNumber(0), "test1"));
eventService.saveEvent(new CreateGroupEvent(idFromNumber(1), "test2", null, GroupType.LECTURE, Visibility.PUBLIC, 10L));
eventService.saveEvent(new UpdateRoleEvent(idFromNumber(1), "test2", Role.MEMBER));
// eventService.saveEvent(new CreateGroupEvent(uuidFromInt(0), "test1", null, GroupType.SIMPLE, Visibility.PUBLIC, 20L));
// eventService.saveEvent(new DeleteGroupEvent(uuidFromInt(0), "test1"));
// eventService.saveEvent(new CreateGroupEvent(uuidFromInt(1), "test2", null, GroupType.LECTURE, Visibility.PUBLIC, 10L));
// 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);
}
@Test
void getAllGroupWithVisibilityPublicTestAddSomeEvents() {
eventService.saveEvent(new CreateGroupEvent(idFromNumber(0), "test1", null, GroupType.SIMPLE, Visibility.PUBLIC, 20L));
eventService.saveEvent(new DeleteGroupEvent(idFromNumber(0), "test1"));
eventService.saveEvent(new CreateGroupEvent(idFromNumber(1), "test2", null, GroupType.LECTURE, Visibility.PUBLIC, 10L));
eventService.saveEvent(new UpdateRoleEvent(idFromNumber(1), "test2", Role.MEMBER));
eventService.saveEvent(new CreateGroupEvent(idFromNumber(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(idFromNumber(4), "test5", null, GroupType.LECTURE, Visibility.PUBLIC, 10L));
assertThat(groupService.getAllGroupWithVisibilityPublic("test1").size()).isEqualTo(4);
// eventService.saveEvent(new CreateGroupEvent(uuidFromInt(0), "test1", null, GroupType.SIMPLE, Visibility.PUBLIC, 20L));
// eventService.saveEvent(new DeleteGroupEvent(uuidFromInt(0), "test1"));
// eventService.saveEvent(new CreateGroupEvent(uuidFromInt(1), "test2", null, GroupType.LECTURE, Visibility.PUBLIC, 10L));
// eventService.saveEvent(new UpdateRoleEvent(uuidFromInt(1), "test2", Role.MEMBER)); // Wofür?
// eventService.saveEvent(new CreateGroupEvent(uuidFromInt(2), "test3", null, GroupType.LECTURE, Visibility.PUBLIC, 10L));
// eventService.saveEvent(new CreateGroupEvent(uuidFromInt(3), "test4", null, GroupType.LECTURE, Visibility.PUBLIC, 10L));
// eventService.saveEvent(new CreateGroupEvent(uuidFromInt(4), "test5", null, GroupType.LECTURE, Visibility.PUBLIC, 10L));
eventService.saveAll(createPublicGroupEvent(uuidFromInt(0)),
deleteGroupEvent(uuidFromInt(0)),
createPublicGroupEvent(),
createPublicGroupEvent(),
createPublicGroupEvent(),
createPrivateGroupEvent());
assertThat(groupService.getAllGroupWithVisibilityPublic("test1").size()).isEqualTo(3);
}
@Disabled
@Test
void getAllGroupWithVisibilityPublicTestIsUserInGroup() {
eventService.saveEvent(new CreateGroupEvent(idFromNumber(0), "test1", null, GroupType.SIMPLE, Visibility.PUBLIC, 20L));
eventService.saveEvent(new AddUserEvent(idFromNumber(0), "test1", "test", "test", "test@test"));
assertThat(groupService.getAllGroupWithVisibilityPublic("test2").get(0).getMembers().size()).isEqualTo(1);
// eventService.saveEvent(new CreateGroupEvent(uuidFromInt(0), "test1", null, GroupType.SIMPLE, Visibility.PUBLIC, 20L));
// eventService.saveEvent(new AddUserEvent(uuidFromInt(0), "test1", "test", "test", "test@test"));
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
void getAllLecturesWithVisibilityPublicTest() {
eventService.saveEvent(new CreateGroupEvent(idFromNumber(0), "test1", null, GroupType.SIMPLE, Visibility.PUBLIC, 20L));
eventService.saveEvent(new CreateGroupEvent(idFromNumber(1), "test2", null, GroupType.LECTURE, Visibility.PUBLIC, 10L));
eventService.saveEvent(new UpdateRoleEvent(idFromNumber(1), "test2", Role.MEMBER));
eventService.saveEvent(new CreateGroupEvent(idFromNumber(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(idFromNumber(4), "test5", null, GroupType.LECTURE, Visibility.PUBLIC, 10L));
void getAllLecturesWithVisibilityPublic() {
// eventService.saveEvent(new CreateGroupEvent(uuidFromInt(0), "test1", null, GroupType.SIMPLE, Visibility.PUBLIC, 20L));
// eventService.saveEvent(new CreateGroupEvent(uuidFromInt(1), "test2", null, GroupType.LECTURE, Visibility.PUBLIC, 10L));
// eventService.saveEvent(new UpdateRoleEvent(uuidFromInt(1), "test2", Role.MEMBER)); // Hä
// eventService.saveEvent(new CreateGroupEvent(uuidFromInt(2), "test3", null, GroupType.LECTURE, Visibility.PUBLIC, 10L));
// eventService.saveEvent(new CreateGroupEvent(uuidFromInt(3), "test4", 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);
}
@Disabled
@Test
void findGroupWithTest() {
eventService.saveEvent(new CreateGroupEvent(idFromNumber(0), "test1", null, GroupType.SIMPLE, Visibility.PUBLIC, 20L));
eventService.saveEvent(new AddUserEvent(idFromNumber(0), "test1", "test", "test", "test@test"));
eventService.saveEvent(new UpdateGroupTitleEvent(idFromNumber(0), "test1", "TestGroup"));
eventService.saveEvent(new UpdateGroupDescriptionEvent(idFromNumber(0), "test1", "TestDescription"));
eventService.saveEvent(new UpdateRoleEvent(idFromNumber(0), "test1", Role.MEMBER));
assertThat(groupService.findGroupWith("T", new Account("jens", "a@A", "test", "peter", "mueller", null)).size()).isEqualTo(1);
void findGroupWith_UserMember_AllGroups() {
// eventService.saveEvent(new CreateGroupEvent(uuidFromInt(0), "test1", null, GroupType.SIMPLE, Visibility.PUBLIC, 20L));
// eventService.saveEvent(new AddUserEvent(uuidFromInt(0), "test1", "test", "test", "test@test"));
// eventService.saveEvent(new UpdateGroupTitleEvent(uuidFromInt(0), "test1", "TestGroup"));
// eventService.saveEvent(new UpdateGroupDescriptionEvent(uuidFromInt(0), "test1", "TestDescription"));
// eventService.saveEvent(new UpdateRoleEvent(uuidFromInt(0), "test1", Role.MEMBER));
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);
}
}