1

Update ControllerServiceTest.java

Co-Authored-By: tomvahl <tomvahl@users.noreply.github.com>
Co-Authored-By: kasch309 <kasch309@users.noreply.github.com>
This commit is contained in:
killerber4t
2020-03-27 13:59:30 +01:00
parent 12eb4b9534
commit ee1beaeccb

View File

@ -1,39 +1,30 @@
package mops.gruppen2.service;
import com.github.javafaker.Faker;
import mops.gruppen2.Gruppen2Application;
import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.GroupType;
import mops.gruppen2.domain.User;
import mops.gruppen2.domain.Visibility;
import mops.gruppen2.domain.*;
import mops.gruppen2.domain.exception.UserNotFoundException;
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.http.HttpStatus;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = Gruppen2Application.class)
@Transactional
@Rollback
class ControllerServiceTest {
Account account, account2;
Account account, account2, account3;
ControllerService controllerService;
EventService eventService;
UserService userService;
@ -53,11 +44,12 @@ class ControllerServiceTest {
roles.add("l");
account = new Account("ich", "ich@hhu.de", "l", "ichdude", "jap", roles);
account2 = new Account("ich2", "ich2@hhu.de", "l", "ichdude2", "jap2", roles);
account3 = new Account("ich3", "ich3@hhu.de", "l", "ichdude3", "jap3", roles);
eventRepository.deleteAll();
}
@Test
void createPublicGroupWithNoParentAndLimitedNumberTest() {
eventRepository.deleteAll();
controllerService.createGroup(account,"test", "hi", null, null, 20L, null);
List<Group> groups= userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail()));
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
@ -68,7 +60,6 @@ class ControllerServiceTest {
@Test
void createPublicGroupWithNoParentAndUnlimitedNumberTest() {
eventRepository.deleteAll();
controllerService.createGroup(account,"test", "hi", null, true, null, null);
List<Group> groups= userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail()));
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
@ -79,7 +70,6 @@ class ControllerServiceTest {
@Test
void createPrivateGroupWithNoParentAndUnlimitedNumberTest() {
eventRepository.deleteAll();
controllerService.createGroup(account,"test", "hi", true, true, null, null);
List<Group> groups= userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail()));
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
@ -90,7 +80,6 @@ class ControllerServiceTest {
@Test
void createPrivateGroupWithNoParentAndLimitedNumberTest() {
eventRepository.deleteAll();
controllerService.createGroup(account,"test", "hi", true, null, 20L, null);
List<Group> groups= userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail()));
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
@ -101,7 +90,6 @@ class ControllerServiceTest {
@Test
void createPrivateGroupWithParentAndLimitedNumberTest() throws IOException {
eventRepository.deleteAll();
controllerService.createOrga(account2, "test", "hi", null, null, true, null, null);
List<Group> groups1= userService.getUserGroups(new User(account2.getName(),account2.getGivenname(),account2.getFamilyname(),account2.getEmail()));
controllerService.createGroup(account,"test", "hi", true, null, 20L, groups1.get(0).getId());
@ -114,7 +102,6 @@ class ControllerServiceTest {
@Test
void createPublicGroupWithParentAndLimitedNumberTest() throws IOException {
eventRepository.deleteAll();
controllerService.createOrga(account2, "test", "hi", null, null, true, null, null);
List<Group> groups1= userService.getUserGroups(new User(account2.getName(),account2.getGivenname(),account2.getFamilyname(),account2.getEmail()));
controllerService.createGroup(account,"test", "hi", null, null, 20L, groups1.get(0).getId());
@ -127,7 +114,6 @@ class ControllerServiceTest {
@Test
void createPublicGroupWithParentAndUnlimitedNumberTest() throws IOException {
eventRepository.deleteAll();
controllerService.createOrga(account2, "test", "hi", null, null, true, null, null);
List<Group> groups1= userService.getUserGroups(new User(account2.getName(),account2.getGivenname(),account2.getFamilyname(),account2.getEmail()));
controllerService.createGroup(account,"test", "hi", null, true, null, groups1.get(0).getId());
@ -140,7 +126,6 @@ class ControllerServiceTest {
@Test
void createPrivateGroupWithParentAndUnlimitedNumberTest() throws IOException {
eventRepository.deleteAll();
controllerService.createOrga(account2, "test", "hi", null, null, true, null, null);
List<Group> groups1= userService.getUserGroups(new User(account2.getName(),account2.getGivenname(),account2.getFamilyname(),account2.getEmail()));
controllerService.createGroup(account,"test", "hi", true, true, null, groups1.get(0).getId());
@ -153,7 +138,6 @@ class ControllerServiceTest {
@Test
void createPublicOrgaGroupWithNoParentAndLimitedNumberTest() throws IOException {
eventRepository.deleteAll();
controllerService.createOrga(account, "test", "hi", null, null, null, 20L, null);
List<Group> groups= userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail()));
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
@ -165,7 +149,6 @@ class ControllerServiceTest {
@Test
void createPublicOrgaGroupWithNoParentAndUnlimitedNumberTest() throws IOException {
eventRepository.deleteAll();
controllerService.createOrga(account, "test", "hi", null, null, true, null, null);
List<Group> groups= userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail()));
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
@ -177,7 +160,6 @@ class ControllerServiceTest {
@Test
void createPrivateOrgaGroupWithNoParentAndLimitedNumberTest() throws IOException {
eventRepository.deleteAll();
controllerService.createOrga(account, "test", "hi", true, null, null, 20L, null);
List<Group> groups= userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail()));
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
@ -189,7 +171,6 @@ class ControllerServiceTest {
@Test
void createPrivateOrgaGroupWithNoParentAndUnlimitedNumberTest() throws IOException {
eventRepository.deleteAll();
controllerService.createOrga(account, "test", "hi", true, null, true, null, null);
List<Group> groups= userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail()));
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
@ -201,7 +182,6 @@ class ControllerServiceTest {
@Test
void createOrgaLectureGroupAndLimitedNumberTest() throws IOException {
eventRepository.deleteAll();
controllerService.createOrga(account, "test", "hi", null, true, null, 20L, null);
List<Group> groups= userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail()));
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
@ -213,7 +193,6 @@ class ControllerServiceTest {
@Test
void createOrgaLectureGroupAndUnlimitedNumberTest() throws IOException {
eventRepository.deleteAll();
controllerService.createOrga(account, "test", "hi", null, true, true, null, null);
List<Group> groups=userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail()));
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
@ -225,7 +204,6 @@ class ControllerServiceTest {
@Test
public void deleteUserTest() {
eventRepository.deleteAll();
controllerService.createGroup(account,"test", "hi", true, true, null, null);
List<Group> groups=userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail()));
controllerService.addUser(account2,groups.get(0).getId());
@ -233,17 +211,41 @@ class ControllerServiceTest {
assertTrue(userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())).isEmpty());
}
@Disabled
@Test
public void deleteNonUserTest() {
eventRepository.deleteAll();
public void updateRoleAdminTest() {
controllerService.createGroup(account,"test", "hi", true, true, null, null);
List<Group> groups=userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail()));
Throwable exception = assertThrows(UserNotFoundException.class, () -> controllerService.deleteUser(account2.getName(), groups.get(0).getId()));
assertEquals(HttpStatus.NOT_FOUND, "Der User wurde nicht gefunden. (class mops.gruppen2.service.ControllerService)", exception.getMessage()); //noch fixen
controllerService.addUser(account2,groups.get(0).getId());
controllerService.updateRole(account.getName(), groups.get(0).getId());
groups = userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail()));
assertEquals(Role.MEMBER,groups.get(0).getRoles().get(account.getName()));
}
@Test
public void updateRoleMemberTest() {
controllerService.createGroup(account,"test", "hi", true, true, null, null);
List<Group> groups=userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail()));
controllerService.addUser(account2,groups.get(0).getId());
controllerService.updateRole(account2.getName(), groups.get(0).getId());
groups = userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail()));
assertEquals(Role.ADMIN,groups.get(0).getRoles().get(account2.getName()));
}
@Test
public void updateRoleNonUserTest() {
controllerService.createGroup(account,"test", "hi", true, true, null, null);
List<Group> groups=userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail()));
Throwable exception=assertThrows(UserNotFoundException.class, () -> controllerService.updateRole(account2.getName(), groups.get(0).getId()));
assertEquals("404 NOT_FOUND \"Der User wurde nicht gefunden. (class mops.gruppen2.service.ControllerService)\"", exception.getMessage()); //noch fixen
}
@Test
public void deleteNonUserTest() {
controllerService.createGroup(account,"test", "hi", true, true, null, null);
List<Group> groups=userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail()));
Throwable exception=assertThrows(UserNotFoundException.class, () -> controllerService.deleteUser(account2.getName(), groups.get(0).getId()));
assertEquals("404 NOT_FOUND \"Der User wurde nicht gefunden. (class mops.gruppen2.service.ControllerService)\"", exception.getMessage()); //noch fixen
}
void testTitleAndDescription(String title, String description) {
assertEquals("test", title);
@ -251,6 +253,39 @@ class ControllerServiceTest {
}
@Test
void passIfLastAdmin() {
void passIfLastAdminTest() {
controllerService.createGroup(account,"test", "hi", true, true, null, null);
List<Group> groups=userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail()));
controllerService.addUser(account2,groups.get(0).getId());
controllerService.passIfLastAdmin(account, groups.get(0).getId());
controllerService.deleteUser(account.getName(), groups.get(0).getId());
groups = userService.getUserGroups(new User(account2.getName(),account2.getGivenname(),account2.getFamilyname(),account2.getEmail()));
assertEquals(Role.ADMIN,groups.get(0).getRoles().get(account2.getName()));
}
@Test
void dontPassIfNotLastAdminTest() {
controllerService.createGroup(account,"test", "hi", true, true, null, null);
List<Group> groups=userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail()));
controllerService.addUser(account2,groups.get(0).getId());
controllerService.updateRole(account2.getName(), groups.get(0).getId());
controllerService.addUser(account3, groups.get(0).getId());
controllerService.passIfLastAdmin(account, groups.get(0).getId());
controllerService.deleteUser(account.getName(), groups.get(0).getId());
groups = userService.getUserGroups(new User(account2.getName(),account2.getGivenname(),account2.getFamilyname(),account2.getEmail()));
assertEquals(Role.MEMBER, groups.get(0).getRoles().get(account3.getName()));
}
@Test
void getVeteranMemberTest(){
controllerService.createGroup(account,"test", "hi", true, true, null, null);
List<Group> groups=userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail()));
controllerService.addUser(account2,groups.get(0).getId());
controllerService.addUser(account3, groups.get(0).getId());
controllerService.passIfLastAdmin(account, groups.get(0).getId());
controllerService.deleteUser(account.getName(), groups.get(0).getId());
groups = userService.getUserGroups(new User(account2.getName(),account2.getGivenname(),account2.getFamilyname(),account2.getEmail()));
assertEquals(Role.ADMIN,groups.get(0).getRoles().get(account2.getName()));
assertEquals(Role.MEMBER, groups.get(0).getRoles().get(account3.getName()));
}
}