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;
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();
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;
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)));
+ }
}
*/
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;
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
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) {
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;
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
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