]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-18525 fix POST support
authorAurelien Poscia <aurelien.poscia@sonarsource.com>
Mon, 6 Mar 2023 12:40:53 +0000 (13:40 +0100)
committersonartech <sonartech@sonarsource.com>
Wed, 22 Mar 2023 20:04:06 +0000 (20:04 +0000)
server/sonar-webserver-webapi/src/it/java/org/sonar/server/usergroups/ws/DeleteActionIT.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/usergroups/ws/DeleteAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/usergroups/ws/GroupService.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/usergroups/ws/GroupServiceTest.java

index 7fafc8b8a0957bcca4d1d5a47344a9153dbf8b8e..46fe34bfde035026fbdfc3eed9a8f1937a00f4e0 100644 (file)
@@ -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();
index e9da2748bda070afb44a3151211f8d5c4434872b..91f6e62b2f090d1b937e6cda78b69add59534d88 100644 (file)
@@ -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)));
+  }
 }
index 2f72646455944daf69f29766a8f2c8fe9362b0f1..d8a9cbe68dc19483fbffe540f4de733301473922 100644 (file)
@@ -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<GroupDto> findGroup(DbSession dbSession, String groupName) {
+    return dbClient.groupDao().selectByName(dbSession, groupName);
   }
 
   public void delete(DbSession dbSession, GroupDto group) {
index f35b424a6979ffd7d0725bd5eaed336722d0f1ba..ce1c489f895fac979020297d96ae85f3bc1c1cdb 100644 (file)
@@ -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