From: Aurelien Poscia Date: Mon, 6 Mar 2023 12:40:53 +0000 (+0100) Subject: SONAR-18525 fix POST support X-Git-Tag: 10.0.0.68432~107 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=57babadd5aa81dde39c573da6b6d1b732c6d8e4e;p=sonarqube.git SONAR-18525 fix POST support --- diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/usergroups/ws/DeleteActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/usergroups/ws/DeleteActionIT.java index 7fafc8b8a09..46fe34bfde0 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/usergroups/ws/DeleteActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/usergroups/ws/DeleteActionIT.java @@ -35,12 +35,14 @@ import org.sonar.db.qualitygate.QualityGateDto; import org.sonar.db.qualityprofile.QProfileDto; import org.sonar.db.user.GroupDto; import org.sonar.db.user.UserDto; +import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.TestRequest; import org.sonar.server.ws.TestResponse; import org.sonar.server.ws.WsActionTester; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.tuple; import static org.sonar.db.permission.GlobalPermission.ADMINISTER; @@ -97,6 +99,19 @@ public class DeleteActionIT { assertThat(db.users().selectGroupByUuid(group.getUuid())).isNull(); } + @Test + public void delete_ifNotGroupFound_throwsNotFoundException() { + addAdmin(); + insertDefaultGroup(); + GroupDto group = db.users().insertGroup(); + loginAsAdmin(); + + TestRequest groupDeletionRequest = newRequest().setParam(PARAM_GROUP_NAME, group.getName() + "_toto"); + assertThatExceptionOfType(NotFoundException.class) + .isThrownBy(groupDeletionRequest::execute) + .withMessageStartingWith("No group with name "); + } + @Test public void delete_members() { addAdmin(); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/usergroups/ws/DeleteAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/usergroups/ws/DeleteAction.java index e9da2748bda..91f6e62b2f0 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/usergroups/ws/DeleteAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/usergroups/ws/DeleteAction.java @@ -28,6 +28,7 @@ import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.permission.GlobalPermission; import org.sonar.db.user.GroupDto; +import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.user.UserSession; import static java.lang.String.format; @@ -67,11 +68,17 @@ public class DeleteAction implements UserGroupsWsAction { try (DbSession dbSession = dbClient.openSession(false)) { userSession.checkPermission(GlobalPermission.ADMINISTER); - GroupDto groupDto = groupService.findGroupDtoOrThrow(dbSession, request.mandatoryParam(PARAM_GROUP_NAME)); - groupService.delete(dbSession, groupDto); + GroupDto group = findGroupOrThrow(request, dbSession); + groupService.delete(dbSession, group); dbSession.commit(); response.noContent(); } } + + private GroupDto findGroupOrThrow(Request request, DbSession dbSession) { + String groupName = request.mandatoryParam(PARAM_GROUP_NAME); + return groupService.findGroup(dbSession, groupName) + .orElseThrow(() -> new NotFoundException(format("No group with name '%s'", groupName))); + } } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/usergroups/ws/GroupService.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/usergroups/ws/GroupService.java index 2f726464559..d8a9cbe68dc 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/usergroups/ws/GroupService.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/usergroups/ws/GroupService.java @@ -19,6 +19,8 @@ */ package org.sonar.server.usergroups.ws; +import java.util.Optional; +import java.util.Set; import javax.annotation.Nullable; import org.sonar.api.security.DefaultGroups; import org.sonar.api.server.ServerSide; @@ -29,10 +31,8 @@ import org.sonar.db.DbSession; import org.sonar.db.permission.GlobalPermission; import org.sonar.db.user.GroupDto; import org.sonar.server.exceptions.BadRequestException; -import org.sonar.server.exceptions.NotFoundException; import static com.google.common.base.Preconditions.checkArgument; -import static java.lang.String.format; import static org.sonar.server.exceptions.BadRequestException.checkRequest; @ServerSide @@ -46,10 +46,8 @@ public class GroupService { this.uuidFactory = uuidFactory; } - public GroupDto findGroupDtoOrThrow(DbSession dbSession, String groupName) { - return dbClient.groupDao() - .selectByName(dbSession, groupName) - .orElseThrow(() -> new NotFoundException(format("No group with name '%s'", groupName))); + public Optional findGroup(DbSession dbSession, String groupName) { + return dbClient.groupDao().selectByName(dbSession, groupName); } public void delete(DbSession dbSession, GroupDto group) { diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/usergroups/ws/GroupServiceTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/usergroups/ws/GroupServiceTest.java index f35b424a697..ce1c489f895 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/usergroups/ws/GroupServiceTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/usergroups/ws/GroupServiceTest.java @@ -47,7 +47,6 @@ import org.sonar.db.user.GroupDto; import org.sonar.db.user.RoleDao; import org.sonar.db.user.UserGroupDao; import org.sonar.server.exceptions.BadRequestException; -import org.sonar.server.exceptions.NotFoundException; import static java.lang.String.format; import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; @@ -103,8 +102,7 @@ public class GroupServiceTest { when(dbClient.groupDao().selectByName(dbSession, GROUP_NAME)) .thenReturn(Optional.of(groupDto)); - assertThat(groupService.findGroupDtoOrThrow(dbSession, GROUP_NAME)) - .isEqualTo(groupDto); + assertThat(groupService.findGroup(dbSession, GROUP_NAME)).contains(groupDto); } @Test @@ -112,9 +110,7 @@ public class GroupServiceTest { when(dbClient.groupDao().selectByName(dbSession, GROUP_NAME)) .thenReturn(Optional.empty()); - assertThatThrownBy(() -> groupService.findGroupDtoOrThrow(dbSession, GROUP_NAME)) - .isInstanceOf(NotFoundException.class) - .hasMessage(format("No group with name '%s'", GROUP_NAME)); + assertThat(groupService.findGroup(dbSession, GROUP_NAME)).isEmpty(); } @Test