From 2443179ad5de6279241e872da52f871345877db3 Mon Sep 17 00:00:00 2001 From: killerber4t Date: Thu, 26 Mar 2020 16:00:16 +0100 Subject: [PATCH 01/10] add Multiple tests And rename 1 Method --- .../mops/gruppen2/service/GroupService.java | 6 +- .../service/ControllerServiceTest.java | 100 +++++++++++++++--- .../gruppen2/service/GroupServiceTest.java | 10 +- 3 files changed, 91 insertions(+), 25 deletions(-) diff --git a/src/main/java/mops/gruppen2/service/GroupService.java b/src/main/java/mops/gruppen2/service/GroupService.java index 2cab31b..699ae0f 100644 --- a/src/main/java/mops/gruppen2/service/GroupService.java +++ b/src/main/java/mops/gruppen2/service/GroupService.java @@ -77,7 +77,7 @@ public class GroupService { * @throws EventException Projektionsfehler */ //TODO Rename - public List getAllGroupWithVisibilityPublic(String userId) throws EventException { + public List getAllGroupsWithVisibilityPublic(String userId) throws EventException { List groupEvents = eventService.translateEventDTOs(eventRepository.findAllEventsByType("CreateGroupEvent")); groupEvents.addAll(eventService.translateEventDTOs(eventRepository.findAllEventsByType("UpdateGroupDescriptionEvent"))); groupEvents.addAll(eventService.translateEventDTOs(eventRepository.findAllEventsByType("UpdateGroupTitleEvent"))); @@ -126,10 +126,10 @@ public class GroupService { //Todo Rename public List findGroupWith(String search, Account account) throws EventException { if (search.isEmpty()) { - return getAllGroupWithVisibilityPublic(account.getName()); + return getAllGroupsWithVisibilityPublic(account.getName()); } - return getAllGroupWithVisibilityPublic(account.getName()) + return getAllGroupsWithVisibilityPublic(account.getName()) .parallelStream() .filter(group -> group.getTitle().toLowerCase().contains(search.toLowerCase()) || group.getDescription().toLowerCase().contains(search.toLowerCase())) diff --git a/src/test/java/mops/gruppen2/service/ControllerServiceTest.java b/src/test/java/mops/gruppen2/service/ControllerServiceTest.java index 205edd9..47b85d9 100644 --- a/src/test/java/mops/gruppen2/service/ControllerServiceTest.java +++ b/src/test/java/mops/gruppen2/service/ControllerServiceTest.java @@ -1,32 +1,51 @@ 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.repository.EventRepository; import mops.gruppen2.security.Account; import org.junit.jupiter.api.BeforeEach; 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.io.IOException; import java.util.HashSet; +import java.util.List; import java.util.Set; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.mockito.Mockito.mock; +@ExtendWith(SpringExtension.class) +@SpringBootTest(classes = Gruppen2Application.class) +@Transactional +@Rollback class ControllerServiceTest { Faker faker; Account account; ControllerService controllerService; EventService eventService; UserService userService; + @Autowired EventRepository eventRepository; GroupService groupService; + @Autowired JsonService jsonService; @BeforeEach void setUp() { - jsonService = new JsonService(); - eventRepository = mock(EventRepository.class); eventService = new EventService(jsonService, eventRepository); groupService = new GroupService(eventService, eventRepository); userService = new UserService(groupService, eventService); @@ -37,41 +56,88 @@ class ControllerServiceTest { } @Test - void createGroupTest() { - + void createPublicGroupWithNoParentAndLimitedNumberTest() { + eventRepository.deleteAll(); + controllerService.createGroup(account,"test", "hi", null, null, 20L, null); + List groups= userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); + assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility()); + assertEquals(20L, groups.get(0).getUserMaximum()); + assertNull(groups.get(0).getParent()); } @Test - void createOrga() { + void createPublicGroupWithNoParentAndUnlimitedNumberTest() { + eventRepository.deleteAll(); + controllerService.createGroup(account,"test", "hi", null, true, null, null); + List groups= userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); + assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility()); + assertEquals(100000L, groups.get(0).getUserMaximum()); //100k ist "maximum" + assertNull(groups.get(0).getParent()); } @Test - void addUser() { + void createPrivateGroupWithNoParentAndUnlimitedNumberTest() { + eventRepository.deleteAll(); + controllerService.createGroup(account,"test", "hi", true, true, null, null); + List groups= userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); + assertEquals(Visibility.PRIVATE, groups.get(0).getVisibility()); + assertEquals(100000L, groups.get(0).getUserMaximum()); //100k ist "maximum" + assertNull(groups.get(0).getParent()); } @Test - void addUserList() { + void createPrivateGroupWithNoParentAndLimitedNumberTest() { + eventRepository.deleteAll(); + controllerService.createGroup(account,"test", "hi", true, null, 20L, null); + List groups= userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); + assertEquals(Visibility.PRIVATE, groups.get(0).getVisibility()); + assertEquals(20L, groups.get(0).getUserMaximum()); //100k ist "maximum" + assertNull(groups.get(0).getParent()); } @Test - void updateTitle() { + void createPublicOrgaGroupWithNoParentAndLimitedNumberTest() throws IOException { + eventRepository.deleteAll(); + controllerService.createOrga(account, "test", "hi", null, null, null, 20L, null); + List groups= userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); + assertEquals(GroupType.SIMPLE, groups.get(0).getType()); + assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility()); + assertEquals(20L, groups.get(0).getUserMaximum()); + assertNull(groups.get(0).getParent()); } @Test - void updateDescription() { + void createPublicOrgaGroupWithNoParentAndUnlimitedNumberTest() throws IOException { + eventRepository.deleteAll(); + controllerService.createOrga(account, "test", "hi", null, null, true, null, null); + List groups= userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); + assertEquals(GroupType.SIMPLE, groups.get(0).getType()); + assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility()); + assertEquals(100000L, groups.get(0).getUserMaximum()); + assertNull(groups.get(0).getParent()); } @Test - void updateRoleTest() { - + void createPrivateOrgaGroupWithNoParentAndLimitedNumberTest() throws IOException { + eventRepository.deleteAll(); + controllerService.createOrga(account, "test", "hi", true, null, null, 20L, null); + List groups= userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); + assertEquals(GroupType.SIMPLE, groups.get(0).getType()); + assertEquals(Visibility.PRIVATE, groups.get(0).getVisibility()); + assertEquals(20L, groups.get(0).getUserMaximum()); + assertNull(groups.get(0).getParent()); } - @Test - void deleteUser() { - } - - @Test - void deleteGroupEvent() { + void testTitleAndDescription(String title, String description) { + assertEquals("test", title); + assertEquals("hi", description); } @Test diff --git a/src/test/java/mops/gruppen2/service/GroupServiceTest.java b/src/test/java/mops/gruppen2/service/GroupServiceTest.java index e656f61..f830253 100644 --- a/src/test/java/mops/gruppen2/service/GroupServiceTest.java +++ b/src/test/java/mops/gruppen2/service/GroupServiceTest.java @@ -117,7 +117,7 @@ class GroupServiceTest { Group group = TestBuilder.apply(test1, test2); assertThat(group.getType()).isEqualTo(null); - assertThat(groupService.getAllGroupWithVisibilityPublic("test1")).isEmpty(); + assertThat(groupService.getAllGroupsWithVisibilityPublic("test1")).isEmpty(); } @Test @@ -131,7 +131,7 @@ class GroupServiceTest { deleteGroupEvent(uuidFromInt(0)), createPublicGroupEvent()); - assertThat(groupService.getAllGroupWithVisibilityPublic("test1").size()).isEqualTo(1); + assertThat(groupService.getAllGroupsWithVisibilityPublic("test1").size()).isEqualTo(1); } @Test @@ -151,7 +151,7 @@ class GroupServiceTest { createPublicGroupEvent(), createPrivateGroupEvent()); - assertThat(groupService.getAllGroupWithVisibilityPublic("test1").size()).isEqualTo(3); + assertThat(groupService.getAllGroupsWithVisibilityPublic("test1").size()).isEqualTo(3); } @Test @@ -167,8 +167,8 @@ class GroupServiceTest { //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); + assertThat(groupService.getAllGroupsWithVisibilityPublic("kobold")).hasSize(1); + assertThat(groupService.getAllGroupsWithVisibilityPublic("peter")).hasSize(2); } @Test From a79ab6473bcfaf783e185f309937029095f23c60 Mon Sep 17 00:00:00 2001 From: killerber4t Date: Thu, 26 Mar 2020 16:26:07 +0100 Subject: [PATCH 02/10] add more testcases --- .../service/ControllerServiceTest.java | 98 ++++++++++++++++++- 1 file changed, 94 insertions(+), 4 deletions(-) diff --git a/src/test/java/mops/gruppen2/service/ControllerServiceTest.java b/src/test/java/mops/gruppen2/service/ControllerServiceTest.java index 47b85d9..c2b3107 100644 --- a/src/test/java/mops/gruppen2/service/ControllerServiceTest.java +++ b/src/test/java/mops/gruppen2/service/ControllerServiceTest.java @@ -31,8 +31,7 @@ import static org.mockito.Mockito.mock; @Transactional @Rollback class ControllerServiceTest { - Faker faker; - Account account; + Account account, account2; ControllerService controllerService; EventService eventService; UserService userService; @@ -42,8 +41,6 @@ class ControllerServiceTest { @Autowired JsonService jsonService; - - @BeforeEach void setUp() { eventService = new EventService(jsonService, eventRepository); @@ -53,6 +50,7 @@ class ControllerServiceTest { Set roles = new HashSet<>(); 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); } @Test @@ -99,6 +97,58 @@ class ControllerServiceTest { assertNull(groups.get(0).getParent()); } + @Test + void createPrivateGroupWithParentAndLimitedNumberTest() throws IOException { + eventRepository.deleteAll(); + controllerService.createOrga(account2, "test", "hi", null, null, true, null, null); + List 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()); + List groups = userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); + assertEquals(Visibility.PRIVATE, groups.get(0).getVisibility()); + assertEquals(20L, groups.get(0).getUserMaximum()); //100k ist "maximum" + assertEquals(groups1.get(0).getId(),groups.get(0).getParent()); + } + + @Test + void createPublicGroupWithParentAndLimitedNumberTest() throws IOException { + eventRepository.deleteAll(); + controllerService.createOrga(account2, "test", "hi", null, null, true, null, null); + List 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()); + List groups = userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); + assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility()); + assertEquals(20L, groups.get(0).getUserMaximum()); //100k ist "maximum" + assertEquals(groups1.get(0).getId(),groups.get(0).getParent()); + } + + @Test + void createPublicGroupWithParentAndUnlimitedNumberTest() throws IOException { + eventRepository.deleteAll(); + controllerService.createOrga(account2, "test", "hi", null, null, true, null, null); + List 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()); + List groups = userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); + assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility()); + assertEquals(100000L, groups.get(0).getUserMaximum()); //100k ist "maximum" + assertEquals(groups1.get(0).getId(),groups.get(0).getParent()); + } + + @Test + void createPrivateGroupWithParentAndUnlimitedNumberTest() throws IOException { + eventRepository.deleteAll(); + controllerService.createOrga(account2, "test", "hi", null, null, true, null, null); + List 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()); + List groups = userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); + assertEquals(Visibility.PRIVATE, groups.get(0).getVisibility()); + assertEquals(100000L, groups.get(0).getUserMaximum()); //100k ist "maximum" + assertEquals(groups1.get(0).getId(),groups.get(0).getParent()); + } + @Test void createPublicOrgaGroupWithNoParentAndLimitedNumberTest() throws IOException { eventRepository.deleteAll(); @@ -135,6 +185,46 @@ class ControllerServiceTest { assertNull(groups.get(0).getParent()); } + @Test + void createPrivateOrgaGroupWithNoParentAndUnlimitedNumberTest() throws IOException { + eventRepository.deleteAll(); + controllerService.createOrga(account, "test", "hi", true, null, true, null, null); + List groups= userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); + assertEquals(GroupType.SIMPLE, groups.get(0).getType()); + assertEquals(Visibility.PRIVATE, groups.get(0).getVisibility()); + assertEquals(100000L, groups.get(0).getUserMaximum()); + assertNull(groups.get(0).getParent()); + } + + @Test + void createOrgaLectureGroupAndLimitedNumberTest() throws IOException { + eventRepository.deleteAll(); + controllerService.createOrga(account, "test", "hi", null, true, null, 20L, null); + List groups= userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); + assertEquals(GroupType.LECTURE, groups.get(0).getType()); + assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility()); + assertEquals(20L, groups.get(0).getUserMaximum()); + assertNull(groups.get(0).getParent()); + } + + @Test + void createOrgaLectureGroupAndUnlimitedNumberTest() throws IOException { + eventRepository.deleteAll(); + controllerService.createOrga(account, "test", "hi", null, true, true, null, null); + List groups= userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); + assertEquals(GroupType.LECTURE, groups.get(0).getType()); + assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility()); + assertEquals(100000L, groups.get(0).getUserMaximum()); + assertNull(groups.get(0).getParent()); + } + + + + + void testTitleAndDescription(String title, String description) { assertEquals("test", title); assertEquals("hi", description); From 12eb4b95344b11e082977e3d7ea278660a8c5f10 Mon Sep 17 00:00:00 2001 From: killerber4t Date: Thu, 26 Mar 2020 16:59:50 +0100 Subject: [PATCH 03/10] add more testcases --- .../service/ControllerServiceTest.java | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/test/java/mops/gruppen2/service/ControllerServiceTest.java b/src/test/java/mops/gruppen2/service/ControllerServiceTest.java index c2b3107..849772b 100644 --- a/src/test/java/mops/gruppen2/service/ControllerServiceTest.java +++ b/src/test/java/mops/gruppen2/service/ControllerServiceTest.java @@ -6,13 +6,16 @@ import mops.gruppen2.domain.Group; import mops.gruppen2.domain.GroupType; import mops.gruppen2.domain.User; import mops.gruppen2.domain.Visibility; +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; @@ -22,8 +25,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.mock; @ExtendWith(SpringExtension.class) @@ -213,7 +215,7 @@ class ControllerServiceTest { void createOrgaLectureGroupAndUnlimitedNumberTest() throws IOException { eventRepository.deleteAll(); controllerService.createOrga(account, "test", "hi", null, true, true, null, null); - List groups= userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + List groups=userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); assertEquals(GroupType.LECTURE, groups.get(0).getType()); assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility()); @@ -221,8 +223,26 @@ class ControllerServiceTest { assertNull(groups.get(0).getParent()); } + @Test + public void deleteUserTest() { + eventRepository.deleteAll(); + controllerService.createGroup(account,"test", "hi", true, true, null, null); + List groups=userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + controllerService.addUser(account2,groups.get(0).getId()); + controllerService.deleteUser(account.getName(), groups.get(0).getId()); + assertTrue(userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())).isEmpty()); + } + @Disabled + @Test + public void deleteNonUserTest() { + eventRepository.deleteAll(); + controllerService.createGroup(account,"test", "hi", true, true, null, null); + List 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 + } void testTitleAndDescription(String title, String description) { From ee1beaeccbe1f4489e5e766bd03b8d27cfb58a59 Mon Sep 17 00:00:00 2001 From: killerber4t Date: Fri, 27 Mar 2020 13:59:30 +0100 Subject: [PATCH 04/10] Update ControllerServiceTest.java Co-Authored-By: tomvahl Co-Authored-By: kasch309 --- .../service/ControllerServiceTest.java | 101 ++++++++++++------ 1 file changed, 68 insertions(+), 33 deletions(-) diff --git a/src/test/java/mops/gruppen2/service/ControllerServiceTest.java b/src/test/java/mops/gruppen2/service/ControllerServiceTest.java index 849772b..9e9fcbd 100644 --- a/src/test/java/mops/gruppen2/service/ControllerServiceTest.java +++ b/src/test/java/mops/gruppen2/service/ControllerServiceTest.java @@ -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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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())); } } From 30abcc408534d70e5f26ea1b6ace5ccd32a41134 Mon Sep 17 00:00:00 2001 From: killerber4t Date: Fri, 27 Mar 2020 14:21:50 +0100 Subject: [PATCH 05/10] fix checkstyle errors --- .../service/ControllerServiceTest.java | 161 ++++++++++-------- 1 file changed, 86 insertions(+), 75 deletions(-) diff --git a/src/test/java/mops/gruppen2/service/ControllerServiceTest.java b/src/test/java/mops/gruppen2/service/ControllerServiceTest.java index 9e9fcbd..e59f73d 100644 --- a/src/test/java/mops/gruppen2/service/ControllerServiceTest.java +++ b/src/test/java/mops/gruppen2/service/ControllerServiceTest.java @@ -1,7 +1,11 @@ package mops.gruppen2.service; import mops.gruppen2.Gruppen2Application; -import mops.gruppen2.domain.*; +import mops.gruppen2.domain.User; +import mops.gruppen2.domain.Group; +import mops.gruppen2.domain.Role; +import mops.gruppen2.domain.Visibility; +import mops.gruppen2.domain.GroupType; import mops.gruppen2.domain.exception.UserNotFoundException; import mops.gruppen2.repository.EventRepository; import mops.gruppen2.security.Account; @@ -13,10 +17,12 @@ 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.io.IOException; import java.util.HashSet; import java.util.List; import java.util.Set; + import static org.junit.jupiter.api.Assertions.*; @ExtendWith(SpringExtension.class) @@ -50,8 +56,8 @@ class ControllerServiceTest { @Test void createPublicGroupWithNoParentAndLimitedNumberTest() { - controllerService.createGroup(account,"test", "hi", null, null, 20L, null); - List groups= userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + controllerService.createGroup(account, "test", "hi", null, null, 20L, null); + List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility()); assertEquals(20L, groups.get(0).getUserMaximum()); @@ -60,86 +66,91 @@ class ControllerServiceTest { @Test void createPublicGroupWithNoParentAndUnlimitedNumberTest() { - controllerService.createGroup(account,"test", "hi", null, true, null, null); - List groups= userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + controllerService.createGroup(account, "test", "hi", null, true, null, null); + User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); + List groups = userService.getUserGroups(user); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility()); - assertEquals(100000L, groups.get(0).getUserMaximum()); //100k ist "maximum" + assertEquals(100000L, groups.get(0).getUserMaximum()); assertNull(groups.get(0).getParent()); } @Test void createPrivateGroupWithNoParentAndUnlimitedNumberTest() { - controllerService.createGroup(account,"test", "hi", true, true, null, null); - List groups= userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + controllerService.createGroup(account, "test", "hi", true, true, null, null); + User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); + List groups = userService.getUserGroups(user); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); assertEquals(Visibility.PRIVATE, groups.get(0).getVisibility()); - assertEquals(100000L, groups.get(0).getUserMaximum()); //100k ist "maximum" + assertEquals(100000L, groups.get(0).getUserMaximum()); assertNull(groups.get(0).getParent()); } @Test void createPrivateGroupWithNoParentAndLimitedNumberTest() { - controllerService.createGroup(account,"test", "hi", true, null, 20L, null); - List groups= userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + controllerService.createGroup(account, "test", "hi", true, null, 20L, null); + User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); + List groups = userService.getUserGroups(user); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); assertEquals(Visibility.PRIVATE, groups.get(0).getVisibility()); - assertEquals(20L, groups.get(0).getUserMaximum()); //100k ist "maximum" + assertEquals(20L, groups.get(0).getUserMaximum()); assertNull(groups.get(0).getParent()); } @Test void createPrivateGroupWithParentAndLimitedNumberTest() throws IOException { controllerService.createOrga(account2, "test", "hi", null, null, true, null, null); - List 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()); - List groups = userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); + List groups1 = userService.getUserGroups(user); + controllerService.createGroup(account, "test", "hi", true, null, 20L, groups1.get(0).getId()); + User user2 = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); + List groups = userService.getUserGroups(user2); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); assertEquals(Visibility.PRIVATE, groups.get(0).getVisibility()); - assertEquals(20L, groups.get(0).getUserMaximum()); //100k ist "maximum" - assertEquals(groups1.get(0).getId(),groups.get(0).getParent()); + assertEquals(20L, groups.get(0).getUserMaximum()); + assertEquals(groups1.get(0).getId(), groups.get(0).getParent()); } @Test void createPublicGroupWithParentAndLimitedNumberTest() throws IOException { controllerService.createOrga(account2, "test", "hi", null, null, true, null, null); - List 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()); - List groups = userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + List 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()); + List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility()); - assertEquals(20L, groups.get(0).getUserMaximum()); //100k ist "maximum" - assertEquals(groups1.get(0).getId(),groups.get(0).getParent()); + assertEquals(20L, groups.get(0).getUserMaximum()); + assertEquals(groups1.get(0).getId(), groups.get(0).getParent()); } @Test void createPublicGroupWithParentAndUnlimitedNumberTest() throws IOException { controllerService.createOrga(account2, "test", "hi", null, null, true, null, null); - List 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()); - List groups = userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + List 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()); + List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility()); - assertEquals(100000L, groups.get(0).getUserMaximum()); //100k ist "maximum" - assertEquals(groups1.get(0).getId(),groups.get(0).getParent()); + assertEquals(100000L, groups.get(0).getUserMaximum()); + assertEquals(groups1.get(0).getId(), groups.get(0).getParent()); } @Test void createPrivateGroupWithParentAndUnlimitedNumberTest() throws IOException { controllerService.createOrga(account2, "test", "hi", null, null, true, null, null); - List 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()); - List groups = userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + List 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()); + List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); assertEquals(Visibility.PRIVATE, groups.get(0).getVisibility()); - assertEquals(100000L, groups.get(0).getUserMaximum()); //100k ist "maximum" - assertEquals(groups1.get(0).getId(),groups.get(0).getParent()); + assertEquals(100000L, groups.get(0).getUserMaximum()); + assertEquals(groups1.get(0).getId(), groups.get(0).getParent()); } @Test void createPublicOrgaGroupWithNoParentAndLimitedNumberTest() throws IOException { controllerService.createOrga(account, "test", "hi", null, null, null, 20L, null); - List groups= userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); assertEquals(GroupType.SIMPLE, groups.get(0).getType()); assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility()); @@ -150,7 +161,7 @@ class ControllerServiceTest { @Test void createPublicOrgaGroupWithNoParentAndUnlimitedNumberTest() throws IOException { controllerService.createOrga(account, "test", "hi", null, null, true, null, null); - List groups= userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); assertEquals(GroupType.SIMPLE, groups.get(0).getType()); assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility()); @@ -161,7 +172,7 @@ class ControllerServiceTest { @Test void createPrivateOrgaGroupWithNoParentAndLimitedNumberTest() throws IOException { controllerService.createOrga(account, "test", "hi", true, null, null, 20L, null); - List groups= userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); assertEquals(GroupType.SIMPLE, groups.get(0).getType()); assertEquals(Visibility.PRIVATE, groups.get(0).getVisibility()); @@ -172,7 +183,7 @@ class ControllerServiceTest { @Test void createPrivateOrgaGroupWithNoParentAndUnlimitedNumberTest() throws IOException { controllerService.createOrga(account, "test", "hi", true, null, true, null, null); - List groups= userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); assertEquals(GroupType.SIMPLE, groups.get(0).getType()); assertEquals(Visibility.PRIVATE, groups.get(0).getVisibility()); @@ -183,7 +194,7 @@ class ControllerServiceTest { @Test void createOrgaLectureGroupAndLimitedNumberTest() throws IOException { controllerService.createOrga(account, "test", "hi", null, true, null, 20L, null); - List groups= userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); assertEquals(GroupType.LECTURE, groups.get(0).getType()); assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility()); @@ -194,7 +205,7 @@ class ControllerServiceTest { @Test void createOrgaLectureGroupAndUnlimitedNumberTest() throws IOException { controllerService.createOrga(account, "test", "hi", null, true, true, null, null); - List groups=userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); + List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); assertEquals(GroupType.LECTURE, groups.get(0).getType()); assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility()); @@ -204,47 +215,47 @@ class ControllerServiceTest { @Test public void deleteUserTest() { - controllerService.createGroup(account,"test", "hi", true, true, null, null); - List groups=userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); - controllerService.addUser(account2,groups.get(0).getId()); + controllerService.createGroup(account, "test", "hi", true, true, null, null); + List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); + controllerService.addUser(account2, groups.get(0).getId()); controllerService.deleteUser(account.getName(), groups.get(0).getId()); - assertTrue(userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())).isEmpty()); + assertTrue(userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())).isEmpty()); } @Test public void updateRoleAdminTest() { - controllerService.createGroup(account,"test", "hi", true, true, null, null); - List groups=userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); - controllerService.addUser(account2,groups.get(0).getId()); + controllerService.createGroup(account, "test", "hi", true, true, null, null); + List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); + 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())); + 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 groups=userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); - controllerService.addUser(account2,groups.get(0).getId()); + controllerService.createGroup(account, "test", "hi", true, true, null, null); + List 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())); + 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 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 + controllerService.createGroup(account, "test", "hi", true, true, null, null); + List 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()); } @Test public void deleteNonUserTest() { - controllerService.createGroup(account,"test", "hi", true, true, null, null); - List 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 + controllerService.createGroup(account, "test", "hi", true, true, null, null); + List 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()); } void testTitleAndDescription(String title, String description) { @@ -254,38 +265,38 @@ class ControllerServiceTest { @Test void passIfLastAdminTest() { - controllerService.createGroup(account,"test", "hi", true, true, null, null); - List groups=userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); - controllerService.addUser(account2,groups.get(0).getId()); + controllerService.createGroup(account, "test", "hi", true, true, null, null); + List 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())); + 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 groups=userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); - controllerService.addUser(account2,groups.get(0).getId()); + controllerService.createGroup(account, "test", "hi", true, true, null, null); + List 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())); + 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 groups=userService.getUserGroups(new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail())); - controllerService.addUser(account2,groups.get(0).getId()); + void getVeteranMemberTest() { + controllerService.createGroup(account, "test", "hi", true, true, null, null); + List 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())); + 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())); } } From 244f3fa6752e9a35a2b52028b25204f5bac90568 Mon Sep 17 00:00:00 2001 From: killerber4t Date: Fri, 27 Mar 2020 14:46:09 +0100 Subject: [PATCH 06/10] fix checkstyle errors --- src/main/java/mops/gruppen2/security/SecurityConfig.java | 3 +-- src/main/java/mops/gruppen2/service/ControllerService.java | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/mops/gruppen2/security/SecurityConfig.java b/src/main/java/mops/gruppen2/security/SecurityConfig.java index f597e47..37ce690 100644 --- a/src/main/java/mops/gruppen2/security/SecurityConfig.java +++ b/src/main/java/mops/gruppen2/security/SecurityConfig.java @@ -47,8 +47,7 @@ class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter { } @Bean - @Scope(scopeName = WebApplicationContext.SCOPE_REQUEST, - proxyMode = ScopedProxyMode.TARGET_CLASS) + @Scope(scopeName = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS) public AccessToken getAccessToken() { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder diff --git a/src/main/java/mops/gruppen2/service/ControllerService.java b/src/main/java/mops/gruppen2/service/ControllerService.java index 3d5263f..8df5b1f 100644 --- a/src/main/java/mops/gruppen2/service/ControllerService.java +++ b/src/main/java/mops/gruppen2/service/ControllerService.java @@ -49,6 +49,7 @@ public class ControllerService { * @param title Gruppentitel * @param description Gruppenbeschreibung */ + public void createGroup(Account account, String title, String description, Boolean visibility, Boolean maxInfiniteUsers, Long userMaximum, UUID parent) throws EventException { Visibility visibility1; maxInfiniteUsers = maxInfiniteUsers != null; From 3f192c9b817468ffd0f531fea361ab4d6aabfb1e Mon Sep 17 00:00:00 2001 From: Christoph Date: Fri, 27 Mar 2020 14:53:31 +0100 Subject: [PATCH 07/10] fix Co-authored-by: Christoph --- src/main/java/mops/gruppen2/repository/InviteRepository.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/mops/gruppen2/repository/InviteRepository.java b/src/main/java/mops/gruppen2/repository/InviteRepository.java index fdecb4d..7899c29 100644 --- a/src/main/java/mops/gruppen2/repository/InviteRepository.java +++ b/src/main/java/mops/gruppen2/repository/InviteRepository.java @@ -1,6 +1,7 @@ package mops.gruppen2.repository; import mops.gruppen2.domain.dto.InviteLinkDTO; +import org.springframework.data.jdbc.repository.query.Modifying; import org.springframework.data.jdbc.repository.query.Query; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.query.Param; @@ -12,6 +13,7 @@ public interface InviteRepository extends CrudRepository { @Query("SELECT group_id FROM invite WHERE invite_link = :link") String findGroupIdByLink(@Param("link") String link); + @Modifying @Query("DELETE FROM invite WHERE group_id = :group") void deleteLinkOfGroup(@Param("group") String group); From 8a24aee6de8d341c3d93c76be53d2ff1c90c5646 Mon Sep 17 00:00:00 2001 From: Christoph Date: Fri, 27 Mar 2020 15:04:52 +0100 Subject: [PATCH 08/10] fix Co-authored-by: Christoph --- src/main/java/mops/gruppen2/controller/WebController.java | 4 +--- src/main/java/mops/gruppen2/service/ValidationService.java | 5 +++++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/mops/gruppen2/controller/WebController.java b/src/main/java/mops/gruppen2/controller/WebController.java index 5a09ce9..0e1f1c6 100644 --- a/src/main/java/mops/gruppen2/controller/WebController.java +++ b/src/main/java/mops/gruppen2/controller/WebController.java @@ -281,8 +281,6 @@ public class WebController { validationService.checkGroup(group.getTitle()); model.addAttribute("group", group); - //controllerService.addUser(keyCloakService.createAccountFromPrincipal(token), group.getId()); - if (group.getVisibility() == Visibility.PUBLIC) { return "redirect:/gruppen2/details/" + group.getId(); } @@ -300,7 +298,7 @@ public class WebController { User user = new User(acc.getName(), acc.getGivenname(), acc.getFamilyname(), acc.getEmail()); - if (!validationService.checkIfUserInGroup(userService.getGroupById(UUID.fromString(groupId)), user)) { + if (!validationService.checkIfUserInGroupWithoutNoAccessAcception(userService.getGroupById(UUID.fromString(groupId)), user)) { controllerService.addUser(keyCloakService.createAccountFromPrincipal(token), UUID.fromString(groupId)); } diff --git a/src/main/java/mops/gruppen2/service/ValidationService.java b/src/main/java/mops/gruppen2/service/ValidationService.java index 94148c7..da1269b 100644 --- a/src/main/java/mops/gruppen2/service/ValidationService.java +++ b/src/main/java/mops/gruppen2/service/ValidationService.java @@ -62,6 +62,11 @@ public class ValidationService { } } + //Warum ist das überhaupt nötig smh + public boolean checkIfUserInGroupWithoutNoAccessAcception(Group group, User user) { + return group.getMembers().contains(user); + } + public Group checkParent(UUID parentId) { Group parent = new Group(); if (!controllerService.idIsEmpty(parentId)) { From e2b40e5aec70ccec69d3f3f9086bf59fb55c94ca Mon Sep 17 00:00:00 2001 From: killerber4t Date: Fri, 27 Mar 2020 15:13:38 +0100 Subject: [PATCH 09/10] refactor tests after merging master Co-Authored-By: tomvahl Co-Authored-By: kasch309 --- .../gruppen2/service/ControllerService.java | 1 + .../service/ControllerServiceTest.java | 58 ++++++++++--------- 2 files changed, 31 insertions(+), 28 deletions(-) diff --git a/src/main/java/mops/gruppen2/service/ControllerService.java b/src/main/java/mops/gruppen2/service/ControllerService.java index 7fd9261..0cf1aaa 100644 --- a/src/main/java/mops/gruppen2/service/ControllerService.java +++ b/src/main/java/mops/gruppen2/service/ControllerService.java @@ -133,6 +133,7 @@ public class ControllerService { } private List readCsvFile(MultipartFile file) throws EventException, IOException { + if(file == null) return new ArrayList<>(); if (!file.isEmpty()) { try { List userList = CsvService.read(file.getInputStream()); diff --git a/src/test/java/mops/gruppen2/service/ControllerServiceTest.java b/src/test/java/mops/gruppen2/service/ControllerServiceTest.java index e59f73d..b83bdaf 100644 --- a/src/test/java/mops/gruppen2/service/ControllerServiceTest.java +++ b/src/test/java/mops/gruppen2/service/ControllerServiceTest.java @@ -39,13 +39,15 @@ class ControllerServiceTest { GroupService groupService; @Autowired JsonService jsonService; + @Autowired + InviteService inviteService; @BeforeEach void setUp() { eventService = new EventService(jsonService, eventRepository); groupService = new GroupService(eventService, eventRepository); userService = new UserService(groupService, eventService); - controllerService = new ControllerService(eventService, userService); + controllerService = new ControllerService(eventService, userService, inviteService); Set roles = new HashSet<>(); roles.add("l"); account = new Account("ich", "ich@hhu.de", "l", "ichdude", "jap", roles); @@ -56,7 +58,7 @@ class ControllerServiceTest { @Test void createPublicGroupWithNoParentAndLimitedNumberTest() { - controllerService.createGroup(account, "test", "hi", null, null, 20L, null); + controllerService.createGroup(account, "test", "hi", null, null, null, 20L, null); List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility()); @@ -66,7 +68,7 @@ class ControllerServiceTest { @Test void createPublicGroupWithNoParentAndUnlimitedNumberTest() { - controllerService.createGroup(account, "test", "hi", null, true, null, null); + controllerService.createGroup(account, "test", "hi", null, null, true, null, null); User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); List groups = userService.getUserGroups(user); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); @@ -77,7 +79,7 @@ class ControllerServiceTest { @Test void createPrivateGroupWithNoParentAndUnlimitedNumberTest() { - controllerService.createGroup(account, "test", "hi", true, true, null, null); + controllerService.createGroup(account, "test", "hi", true, null, true, null, null); User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); List groups = userService.getUserGroups(user); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); @@ -88,7 +90,7 @@ class ControllerServiceTest { @Test void createPrivateGroupWithNoParentAndLimitedNumberTest() { - controllerService.createGroup(account, "test", "hi", true, null, 20L, null); + controllerService.createGroup(account, "test", "hi", true, null, null, 20L, null); User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); List groups = userService.getUserGroups(user); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); @@ -99,10 +101,10 @@ class ControllerServiceTest { @Test void createPrivateGroupWithParentAndLimitedNumberTest() throws IOException { - controllerService.createOrga(account2, "test", "hi", null, null, true, null, null); - User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); + controllerService.createGroupAsOrga(account2, "test", "hi", null, true, true, null, null, null); + User user = new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail()); List groups1 = userService.getUserGroups(user); - controllerService.createGroup(account, "test", "hi", true, null, 20L, groups1.get(0).getId()); + controllerService.createGroup(account, "test", "hi", true, null, null, 20L, groups1.get(0).getId()); User user2 = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); List groups = userService.getUserGroups(user2); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); @@ -113,9 +115,9 @@ class ControllerServiceTest { @Test void createPublicGroupWithParentAndLimitedNumberTest() throws IOException { - controllerService.createOrga(account2, "test", "hi", null, null, true, null, null); + controllerService.createGroupAsOrga(account2, "test", "hi", null, null, true, null, null, null); List 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()); + controllerService.createGroup(account, "test", "hi", null, null, null, 20L, groups1.get(0).getId()); List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility()); @@ -125,9 +127,9 @@ class ControllerServiceTest { @Test void createPublicGroupWithParentAndUnlimitedNumberTest() throws IOException { - controllerService.createOrga(account2, "test", "hi", null, null, true, null, null); + controllerService.createGroupAsOrga(account2, "test", "hi", null, null, true, null, null, null); List 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()); + controllerService.createGroup(account, "test", "hi", null, true, true, null,groups1.get(0).getId()); List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility()); @@ -137,9 +139,9 @@ class ControllerServiceTest { @Test void createPrivateGroupWithParentAndUnlimitedNumberTest() throws IOException { - controllerService.createOrga(account2, "test", "hi", null, null, true, null, null); + controllerService.createGroupAsOrga(account2, "test", "hi", null, null, true, null, null, null); List 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()); + controllerService.createGroup(account, "test", "hi", true, true, true, null, groups1.get(0).getId()); List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); assertEquals(Visibility.PRIVATE, groups.get(0).getVisibility()); @@ -149,7 +151,7 @@ class ControllerServiceTest { @Test void createPublicOrgaGroupWithNoParentAndLimitedNumberTest() throws IOException { - controllerService.createOrga(account, "test", "hi", null, null, null, 20L, null); + controllerService.createGroupAsOrga(account, "test", "hi", null, null, null, 20L, null, null); List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); assertEquals(GroupType.SIMPLE, groups.get(0).getType()); @@ -160,7 +162,7 @@ class ControllerServiceTest { @Test void createPublicOrgaGroupWithNoParentAndUnlimitedNumberTest() throws IOException { - controllerService.createOrga(account, "test", "hi", null, null, true, null, null); + controllerService.createGroupAsOrga(account, "test", "hi", null, null, true, null, null, null); List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); assertEquals(GroupType.SIMPLE, groups.get(0).getType()); @@ -171,7 +173,7 @@ class ControllerServiceTest { @Test void createPrivateOrgaGroupWithNoParentAndLimitedNumberTest() throws IOException { - controllerService.createOrga(account, "test", "hi", true, null, null, 20L, null); + controllerService.createGroupAsOrga(account, "test", "hi", true, null, null, 20L, null, null); List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); assertEquals(GroupType.SIMPLE, groups.get(0).getType()); @@ -182,7 +184,7 @@ class ControllerServiceTest { @Test void createPrivateOrgaGroupWithNoParentAndUnlimitedNumberTest() throws IOException { - controllerService.createOrga(account, "test", "hi", true, null, true, null, null); + controllerService.createGroupAsOrga(account, "test", "hi", true, null, true, null, null, null); List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); assertEquals(GroupType.SIMPLE, groups.get(0).getType()); @@ -193,7 +195,7 @@ class ControllerServiceTest { @Test void createOrgaLectureGroupAndLimitedNumberTest() throws IOException { - controllerService.createOrga(account, "test", "hi", null, true, null, 20L, null); + controllerService.createGroupAsOrga(account, "test", "hi", null, true, null, 20L, null, null); List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); assertEquals(GroupType.LECTURE, groups.get(0).getType()); @@ -204,7 +206,7 @@ class ControllerServiceTest { @Test void createOrgaLectureGroupAndUnlimitedNumberTest() throws IOException { - controllerService.createOrga(account, "test", "hi", null, true, true, null, null); + controllerService.createGroupAsOrga(account, "test", "hi", null, true, true, null, null, null); List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription()); assertEquals(GroupType.LECTURE, groups.get(0).getType()); @@ -215,7 +217,7 @@ class ControllerServiceTest { @Test public void deleteUserTest() { - controllerService.createGroup(account, "test", "hi", true, true, null, null); + controllerService.createGroup(account, "test", "hi", true, true, true, null, null); List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); controllerService.addUser(account2, groups.get(0).getId()); controllerService.deleteUser(account.getName(), groups.get(0).getId()); @@ -224,7 +226,7 @@ class ControllerServiceTest { @Test public void updateRoleAdminTest() { - controllerService.createGroup(account, "test", "hi", true, true, null, null); + controllerService.createGroup(account, "test", "hi", null, null, true, null, null); List groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())); controllerService.addUser(account2, groups.get(0).getId()); controllerService.updateRole(account.getName(), groups.get(0).getId()); @@ -234,7 +236,7 @@ class ControllerServiceTest { @Test public void updateRoleMemberTest() { - controllerService.createGroup(account, "test", "hi", true, true, null, null); + controllerService.createGroup(account, "test", "hi", null, null, true, null, null); List 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()); @@ -244,7 +246,7 @@ class ControllerServiceTest { @Test public void updateRoleNonUserTest() { - controllerService.createGroup(account, "test", "hi", true, true, null, null); + controllerService.createGroup(account, "test", "hi", null, null, true, null, null); List 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()); @@ -252,7 +254,7 @@ class ControllerServiceTest { @Test public void deleteNonUserTest() { - controllerService.createGroup(account, "test", "hi", true, true, null, null); + controllerService.createGroup(account, "test", "hi", true, null, true, null, null); List 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()); @@ -265,7 +267,7 @@ class ControllerServiceTest { @Test void passIfLastAdminTest() { - controllerService.createGroup(account, "test", "hi", true, true, null, null); + controllerService.createGroup(account, "test", "hi", null, null, true, null, null); List 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()); @@ -276,7 +278,7 @@ class ControllerServiceTest { @Test void dontPassIfNotLastAdminTest() { - controllerService.createGroup(account, "test", "hi", true, true, null, null); + controllerService.createGroup(account, "test", "hi", null, null, true, null, null); List 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()); @@ -289,7 +291,7 @@ class ControllerServiceTest { @Test void getVeteranMemberTest() { - controllerService.createGroup(account, "test", "hi", true, true, null, null); + controllerService.createGroup(account, "test", "hi", null, null, true, null, null); List 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()); From 3350edb8877bdfd1c5bfb909668399fa51645603 Mon Sep 17 00:00:00 2001 From: Christoph Date: Fri, 27 Mar 2020 15:15:43 +0100 Subject: [PATCH 10/10] docking Co-authored-by: Christoph --- Dockerfile | 6 +- pull-wait-for-it.sh | 5 -- wait-for-it.sh | 182 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 183 insertions(+), 10 deletions(-) delete mode 100644 pull-wait-for-it.sh create mode 100644 wait-for-it.sh diff --git a/Dockerfile b/Dockerfile index a18d8b7..46b5ffd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,16 +1,12 @@ FROM gradle:jdk11 AS build COPY --chown=gradle:gradle . /home/gradle/src WORKDIR /home/gradle/src - -RUN chmod +x ./pull-wait-for-it.sh -RUN ./pull-wait-for-it.sh -RUN chmod +x ./wait-for-it.sh - RUN gradle bootJar --no-daemon FROM openjdk:11-jre-slim RUN mkdir /app COPY --from=build /home/gradle/src/build/libs/*.jar /app/gruppen2.jar COPY --from=build /home/gradle/src/wait-for-it.sh /app/wait-for-it.sh +RUN chmod +x /app/wait-for-it.sh #ENTRYPOINT ["java"] #CMD ["-Dspring.profiles.active=docker", "-jar", "/app/gruppen2.jar"] diff --git a/pull-wait-for-it.sh b/pull-wait-for-it.sh deleted file mode 100644 index 773c9fe..0000000 --- a/pull-wait-for-it.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh - -if [ ! -f wait-for-it.sh ]; then - wget https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh -fi diff --git a/wait-for-it.sh b/wait-for-it.sh new file mode 100644 index 0000000..5e8679e --- /dev/null +++ b/wait-for-it.sh @@ -0,0 +1,182 @@ +#!/usr/bin/env bash +# Use this script to test if a given TCP host/port are available + +WAITFORIT_cmdname=${0##*/} + +echoerr() { if [[ $WAITFORIT_QUIET -ne 1 ]]; then echo "$@" 1>&2; fi } + +usage() +{ + cat << USAGE >&2 +Usage: + $WAITFORIT_cmdname host:port [-s] [-t timeout] [-- command args] + -h HOST | --host=HOST Host or IP under test + -p PORT | --port=PORT TCP port under test + Alternatively, you specify the host and port as host:port + -s | --strict Only execute subcommand if the test succeeds + -q | --quiet Don't output any status messages + -t TIMEOUT | --timeout=TIMEOUT + Timeout in seconds, zero for no timeout + -- COMMAND ARGS Execute command with args after the test finishes +USAGE + exit 1 +} + +wait_for() +{ + if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then + echoerr "$WAITFORIT_cmdname: waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT" + else + echoerr "$WAITFORIT_cmdname: waiting for $WAITFORIT_HOST:$WAITFORIT_PORT without a timeout" + fi + WAITFORIT_start_ts=$(date +%s) + while : + do + if [[ $WAITFORIT_ISBUSY -eq 1 ]]; then + nc -z $WAITFORIT_HOST $WAITFORIT_PORT + WAITFORIT_result=$? + else + (echo > /dev/tcp/$WAITFORIT_HOST/$WAITFORIT_PORT) >/dev/null 2>&1 + WAITFORIT_result=$? + fi + if [[ $WAITFORIT_result -eq 0 ]]; then + WAITFORIT_end_ts=$(date +%s) + echoerr "$WAITFORIT_cmdname: $WAITFORIT_HOST:$WAITFORIT_PORT is available after $((WAITFORIT_end_ts - WAITFORIT_start_ts)) seconds" + break + fi + sleep 1 + done + return $WAITFORIT_result +} + +wait_for_wrapper() +{ + # In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692 + if [[ $WAITFORIT_QUIET -eq 1 ]]; then + timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --quiet --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT & + else + timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT & + fi + WAITFORIT_PID=$! + trap "kill -INT -$WAITFORIT_PID" INT + wait $WAITFORIT_PID + WAITFORIT_RESULT=$? + if [[ $WAITFORIT_RESULT -ne 0 ]]; then + echoerr "$WAITFORIT_cmdname: timeout occurred after waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT" + fi + return $WAITFORIT_RESULT +} + +# process arguments +while [[ $# -gt 0 ]] +do + case "$1" in + *:* ) + WAITFORIT_hostport=(${1//:/ }) + WAITFORIT_HOST=${WAITFORIT_hostport[0]} + WAITFORIT_PORT=${WAITFORIT_hostport[1]} + shift 1 + ;; + --child) + WAITFORIT_CHILD=1 + shift 1 + ;; + -q | --quiet) + WAITFORIT_QUIET=1 + shift 1 + ;; + -s | --strict) + WAITFORIT_STRICT=1 + shift 1 + ;; + -h) + WAITFORIT_HOST="$2" + if [[ $WAITFORIT_HOST == "" ]]; then break; fi + shift 2 + ;; + --host=*) + WAITFORIT_HOST="${1#*=}" + shift 1 + ;; + -p) + WAITFORIT_PORT="$2" + if [[ $WAITFORIT_PORT == "" ]]; then break; fi + shift 2 + ;; + --port=*) + WAITFORIT_PORT="${1#*=}" + shift 1 + ;; + -t) + WAITFORIT_TIMEOUT="$2" + if [[ $WAITFORIT_TIMEOUT == "" ]]; then break; fi + shift 2 + ;; + --timeout=*) + WAITFORIT_TIMEOUT="${1#*=}" + shift 1 + ;; + --) + shift + WAITFORIT_CLI=("$@") + break + ;; + --help) + usage + ;; + *) + echoerr "Unknown argument: $1" + usage + ;; + esac +done + +if [[ "$WAITFORIT_HOST" == "" || "$WAITFORIT_PORT" == "" ]]; then + echoerr "Error: you need to provide a host and port to test." + usage +fi + +WAITFORIT_TIMEOUT=${WAITFORIT_TIMEOUT:-15} +WAITFORIT_STRICT=${WAITFORIT_STRICT:-0} +WAITFORIT_CHILD=${WAITFORIT_CHILD:-0} +WAITFORIT_QUIET=${WAITFORIT_QUIET:-0} + +# Check to see if timeout is from busybox? +WAITFORIT_TIMEOUT_PATH=$(type -p timeout) +WAITFORIT_TIMEOUT_PATH=$(realpath $WAITFORIT_TIMEOUT_PATH 2>/dev/null || readlink -f $WAITFORIT_TIMEOUT_PATH) + +WAITFORIT_BUSYTIMEFLAG="" +if [[ $WAITFORIT_TIMEOUT_PATH =~ "busybox" ]]; then + WAITFORIT_ISBUSY=1 + # Check if busybox timeout uses -t flag + # (recent Alpine versions don't support -t anymore) + if timeout &>/dev/stdout | grep -q -e '-t '; then + WAITFORIT_BUSYTIMEFLAG="-t" + fi +else + WAITFORIT_ISBUSY=0 +fi + +if [[ $WAITFORIT_CHILD -gt 0 ]]; then + wait_for + WAITFORIT_RESULT=$? + exit $WAITFORIT_RESULT +else + if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then + wait_for_wrapper + WAITFORIT_RESULT=$? + else + wait_for + WAITFORIT_RESULT=$? + fi +fi + +if [[ $WAITFORIT_CLI != "" ]]; then + if [[ $WAITFORIT_RESULT -ne 0 && $WAITFORIT_STRICT -eq 1 ]]; then + echoerr "$WAITFORIT_cmdname: strict mode, refusing to execute subprocess" + exit $WAITFORIT_RESULT + fi + exec "${WAITFORIT_CLI[@]}" +else + exit $WAITFORIT_RESULT +fi