import org.sonar.db.user.UserGroupDto;
import org.sonar.server.user.UserSession;
import org.sonar.server.user.index.UserIndexer;
+import org.sonar.server.usergroups.DefaultGroupFinder;
import static org.sonar.server.organization.ws.OrganizationsWsSupport.PARAM_LOGIN;
import static org.sonar.server.organization.ws.OrganizationsWsSupport.PARAM_ORGANIZATION;
private final DbClient dbClient;
private final UserSession userSession;
private final UserIndexer userIndexer;
+ private final DefaultGroupFinder defaultGroupFinder;
- public AddMemberAction(DbClient dbClient, UserSession userSession, UserIndexer userIndexer) {
+ public AddMemberAction(DbClient dbClient, UserSession userSession, UserIndexer userIndexer, DefaultGroupFinder defaultGroupFinder) {
this.dbClient = dbClient;
this.userSession = userSession;
this.userIndexer = userIndexer;
+ this.defaultGroupFinder = defaultGroupFinder;
}
@Override
dbClient.organizationMemberDao().insert(dbSession, new OrganizationMemberDto()
.setOrganizationUuid(organization.getUuid())
.setUserId(user.getId()));
- dbClient.userGroupDao().insert(dbSession, new UserGroupDto().setGroupId(getDefaultGroupId(dbSession, organization)).setUserId(user.getId()));
+ dbClient.userGroupDao().insert(dbSession,
+ new UserGroupDto().setGroupId(defaultGroupFinder.findDefaultGroup(dbSession, organization.getUuid()).getId()).setUserId(user.getId()));
dbSession.commit();
userIndexer.index(user.getLogin());
}
return dbClient.organizationMemberDao().select(dbSession, organizationDto.getUuid(), userDto.getId()).isPresent();
}
- int getDefaultGroupId(DbSession dbSession, OrganizationDto organizationDto) {
- return dbClient.organizationDao().getDefaultGroupId(dbSession, organizationDto.getUuid())
- .orElseThrow(() -> new IllegalStateException(String.format("Default group doesn't exist on default organization '%s'", organizationDto.getKey())));
- }
-
}
import org.sonar.server.organization.OrganizationFlags;
import org.sonar.server.user.UserSession;
import org.sonar.server.usergroups.DefaultGroupCreator;
+import org.sonar.server.usergroups.DefaultGroupFinder;
import static java.util.Objects.requireNonNull;
private final DefaultOrganizationProvider defaultOrganizationProvider;
private final OrganizationFlags organizationFlags;
private final DefaultGroupCreator defaultGroupCreator;
+ private final DefaultGroupFinder defaultGroupFinder;
public EnableSupportAction(UserSession userSession, DbClient dbClient, DefaultOrganizationProvider defaultOrganizationProvider,
- OrganizationFlags organizationFlags, DefaultGroupCreator defaultGroupCreator) {
+ OrganizationFlags organizationFlags, DefaultGroupCreator defaultGroupCreator, DefaultGroupFinder defaultGroupFinder) {
this.userSession = userSession;
this.dbClient = dbClient;
this.defaultOrganizationProvider = defaultOrganizationProvider;
this.organizationFlags = organizationFlags;
this.defaultGroupCreator = defaultGroupCreator;
+ this.defaultGroupFinder = defaultGroupFinder;
}
@Override
private void createDefaultMembersGroup(DbSession dbSession) {
String defaultOrganizationUuid = defaultOrganizationProvider.get().getUuid();
- int sonarUsersGroupId = dbClient.organizationDao().getDefaultGroupId(dbSession, defaultOrganizationUuid)
- .orElseThrow(() -> new IllegalStateException(String.format("Default group doesn't exist on default organization '%s'", defaultOrganizationProvider.get().getKey())));
+ GroupDto sonarUsersGroupId = defaultGroupFinder.findDefaultGroup(dbSession, defaultOrganizationUuid);
GroupDto members = defaultGroupCreator.create(dbSession, defaultOrganizationUuid);
copySonarUsersGroupPermissionsToMembersGroup(dbSession, sonarUsersGroupId, members);
copySonarUsersGroupPermissionTemplatesToMembersGroup(dbSession, sonarUsersGroupId, members);
organizationMembers.forEach(member -> dbClient.userGroupDao().insert(dbSession, new UserGroupDto().setGroupId(membersGroup.getId()).setUserId(member)));
}
- private void copySonarUsersGroupPermissionsToMembersGroup(DbSession dbSession, int sonarUsersGroupId, GroupDto membersGroup) {
+ private void copySonarUsersGroupPermissionsToMembersGroup(DbSession dbSession, GroupDto sonarUsersGroup, GroupDto membersGroup) {
String defaultOrganizationUuid = defaultOrganizationProvider.get().getUuid();
- dbClient.groupPermissionDao().selectAllPermissionsByGroupId(dbSession, defaultOrganizationUuid, sonarUsersGroupId,
+ dbClient.groupPermissionDao().selectAllPermissionsByGroupId(dbSession, defaultOrganizationUuid, sonarUsersGroup.getId(),
context -> {
GroupPermissionDto groupPermissionDto = (GroupPermissionDto) context.getResultObject();
dbClient.groupPermissionDao().insert(dbSession,
});
}
- private void copySonarUsersGroupPermissionTemplatesToMembersGroup(DbSession dbSession, int sonarUsersGroupId, GroupDto membersGroup) {
- List<PermissionTemplateGroupDto> sonarUsersPermissionTemplates = dbClient.permissionTemplateDao().selectAllGroupPermissionTemplatesByGroupId(dbSession, sonarUsersGroupId);
+ private void copySonarUsersGroupPermissionTemplatesToMembersGroup(DbSession dbSession, GroupDto sonarUsersGroup, GroupDto membersGroup) {
+ List<PermissionTemplateGroupDto> sonarUsersPermissionTemplates = dbClient.permissionTemplateDao().selectAllGroupPermissionTemplatesByGroupId(dbSession,
+ sonarUsersGroup.getId());
sonarUsersPermissionTemplates.forEach(permissionTemplateGroup -> dbClient.permissionTemplateDao().insertGroupPermission(dbSession,
permissionTemplateGroup.getTemplateId(), membersGroup.getId(), permissionTemplateGroup.getPermission()));
}
import org.sonar.server.user.index.UserIndexer;
import org.sonar.server.user.ws.UsersWsModule;
import org.sonar.server.usergroups.DefaultGroupCreatorImpl;
+import org.sonar.server.usergroups.DefaultGroupFinder;
import org.sonar.server.usergroups.ws.UserGroupsModule;
import org.sonar.server.usertoken.UserTokenModule;
import org.sonar.server.util.TypeValidationModule;
// groups
UserGroupsModule.class,
DefaultGroupCreatorImpl.class,
+ DefaultGroupFinder.class,
// permissions
DefaultTemplatesResolverImpl.class,
import org.sonar.db.DbSession;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.permission.OrganizationPermission;
+import org.sonar.db.user.GroupDto;
import org.sonar.db.user.GroupMembershipDto;
import org.sonar.db.user.GroupMembershipQuery;
import org.sonar.db.user.UserDto;
import org.sonar.server.organization.DefaultOrganizationProvider;
import org.sonar.server.user.UserSession;
+import org.sonar.server.usergroups.DefaultGroupFinder;
import org.sonarqube.ws.WsUsers.GroupsWsResponse;
import org.sonarqube.ws.WsUsers.GroupsWsResponse.Group;
import org.sonarqube.ws.client.user.GroupsRequest;
private final DbClient dbClient;
private final UserSession userSession;
private final DefaultOrganizationProvider defaultOrganizationProvider;
+ private final DefaultGroupFinder defaultGroupFinder;
- public GroupsAction(DbClient dbClient, UserSession userSession, DefaultOrganizationProvider defaultOrganizationProvider) {
+ public GroupsAction(DbClient dbClient, UserSession userSession, DefaultOrganizationProvider defaultOrganizationProvider, DefaultGroupFinder defaultGroupFinder) {
this.dbClient = dbClient;
this.userSession = userSession;
this.defaultOrganizationProvider = defaultOrganizationProvider;
+ this.defaultGroupFinder = defaultGroupFinder;
}
@Override
try (DbSession dbSession = dbClient.openSession(false)) {
OrganizationDto organization = findOrganizationByKey(dbSession, request.getOrganization());
userSession.checkPermission(OrganizationPermission.ADMINISTER, organization);
- Optional<Integer> defaultGroupId = dbClient.organizationDao().getDefaultGroupId(dbSession, organization.getUuid());
String login = request.getLogin();
GroupMembershipQuery query = GroupMembershipQuery.builder()
int total = dbClient.groupMembershipDao().countGroups(dbSession, query, user.getId());
Paging paging = forPageIndex(query.pageIndex()).withPageSize(query.pageSize()).andTotal(total);
List<GroupMembershipDto> groups = dbClient.groupMembershipDao().selectGroups(dbSession, query, user.getId(), paging.offset(), query.pageSize());
- return buildResponse(groups, defaultGroupId, paging);
+ return buildResponse(groups, defaultGroupFinder.findDefaultGroup(dbSession, organization.getUuid()), paging);
}
}
return membership;
}
- private static GroupsWsResponse buildResponse(List<GroupMembershipDto> groups, Optional<Integer> defaultGroupId, Paging paging) {
+ private static GroupsWsResponse buildResponse(List<GroupMembershipDto> groups, GroupDto defaultGroup, Paging paging) {
GroupsWsResponse.Builder responseBuilder = GroupsWsResponse.newBuilder();
- groups.forEach(group -> responseBuilder.addGroups(toWsGroup(group, defaultGroupId)));
+ groups.forEach(group -> responseBuilder.addGroups(toWsGroup(group, defaultGroup)));
responseBuilder.getPagingBuilder()
.setPageIndex(paging.pageIndex())
.setPageSize(paging.pageSize())
return responseBuilder.build();
}
- private static Group toWsGroup(GroupMembershipDto group, Optional<Integer> defaultGroupId) {
+ private static Group toWsGroup(GroupMembershipDto group, GroupDto defaultGroup) {
Group.Builder groupBuilder = Group.newBuilder()
.setId(group.getId())
.setName(group.getName())
- .setSelected(group.getUserId() != null);
- defaultGroupId.ifPresent(defaultGroup -> groupBuilder.setDefault(defaultGroup.longValue() == group.getId()));
+ .setSelected(group.getUserId() != null)
+ .setDefault(defaultGroup.getId().longValue() == group.getId());
Protobuf.setNullable(group.getDescription(), groupBuilder::setDescription);
return groupBuilder.build();
}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.server.usergroups;
+
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.user.GroupDto;
+
+import static java.lang.String.format;
+import static java.util.Objects.requireNonNull;
+
+public class DefaultGroupFinder {
+
+ private final DbClient dbClient;
+
+ public DefaultGroupFinder(DbClient dbClient) {
+ this.dbClient = dbClient;
+ }
+
+ public GroupDto findDefaultGroup(DbSession dbSession, String organizationUuid) {
+ int defaultGroupId = dbClient.organizationDao().getDefaultGroupId(dbSession, organizationUuid)
+ .orElseThrow(() -> new IllegalStateException(format("Default group cannot be found on organization '%s'", organizationUuid)));
+ return requireNonNull(dbClient.groupDao().selectById(dbSession, defaultGroupId), format("Group '%s' cannot be found", defaultGroupId));
+ }
+
+}
return org.get();
}
- public Optional<Integer> findDefaultGroupId(DbSession dbSession, OrganizationDto organization) {
- return dbClient.organizationDao().getDefaultGroupId(dbSession, organization.getUuid());
- }
-
/**
* Similar to {@link UserGroupValidation#validateGroupName(String)} but kept internal. No need to publish
* this method in public API.
import java.util.Arrays;
import java.util.List;
import java.util.Map;
-import java.util.Optional;
import java.util.Set;
import org.sonar.api.server.ws.Change;
import org.sonar.api.server.ws.Request;
import org.sonar.db.user.GroupDto;
import org.sonar.server.es.SearchOptions;
import org.sonar.server.user.UserSession;
+import org.sonar.server.usergroups.DefaultGroupFinder;
import static org.apache.commons.lang.StringUtils.defaultIfBlank;
import static org.sonar.api.utils.Paging.forPageIndex;
private final DbClient dbClient;
private final UserSession userSession;
private final GroupWsSupport groupWsSupport;
+ private final DefaultGroupFinder defaultGroupFinder;
- public SearchAction(DbClient dbClient, UserSession userSession, GroupWsSupport groupWsSupport) {
+ public SearchAction(DbClient dbClient, UserSession userSession, GroupWsSupport groupWsSupport, DefaultGroupFinder defaultGroupFinder) {
this.dbClient = dbClient;
this.userSession = userSession;
this.groupWsSupport = groupWsSupport;
+ this.defaultGroupFinder = defaultGroupFinder;
}
@Override
try (DbSession dbSession = dbClient.openSession(false)) {
OrganizationDto organization = groupWsSupport.findOrganizationByKey(dbSession, request.param(PARAM_ORGANIZATION_KEY));
userSession.checkLoggedIn().checkPermission(ADMINISTER, organization);
- Optional<Integer> defaultGroupId = groupWsSupport.findDefaultGroupId(dbSession, organization);
+ GroupDto defaultGroup = defaultGroupFinder.findDefaultGroup(dbSession, organization.getUuid());
int limit = dbClient.groupDao().countByQuery(dbSession, organization.getUuid(), query);
Paging paging = forPageIndex(page).withPageSize(pageSize).andTotal(limit);
List<GroupDto> groups = dbClient.groupDao().selectByQuery(dbSession, organization.getUuid(), query, options.getOffset(), pageSize);
List<Integer> groupIds = groups.stream().map(GroupDto::getId).collect(MoreCollectors.toList(groups.size()));
Map<String, Integer> userCountByGroup = dbClient.groupMembershipDao().countUsersByGroups(dbSession, groupIds);
- writeProtobuf(buildResponse(groups, userCountByGroup, fields, paging, defaultGroupId), request, response);
+ writeProtobuf(buildResponse(groups, userCountByGroup, fields, paging, defaultGroup), request, response);
}
}
return fields;
}
- private static SearchWsResponse buildResponse(List<GroupDto> groups, Map<String, Integer> userCountByGroup, Set<String> fields, Paging paging, Optional<Integer> defaultGroupId) {
+ private static SearchWsResponse buildResponse(List<GroupDto> groups, Map<String, Integer> userCountByGroup, Set<String> fields, Paging paging, GroupDto defaultGroup) {
SearchWsResponse.Builder responseBuilder = SearchWsResponse.newBuilder();
groups.forEach(group -> responseBuilder
- .addGroups(toWsGroup(group, userCountByGroup.get(group.getName()), fields, defaultGroupId.isPresent() && defaultGroupId.get().equals(group.getId()))));
+ .addGroups(toWsGroup(group, userCountByGroup.get(group.getName()), fields, defaultGroup.getId().equals(group.getId()))));
responseBuilder.getPagingBuilder()
.setPageIndex(paging.pageIndex())
.setPageSize(paging.pageSize())
import org.sonar.server.user.index.UserIndexDefinition;
import org.sonar.server.user.index.UserIndexer;
import org.sonar.server.user.index.UserQuery;
+import org.sonar.server.usergroups.DefaultGroupFinder;
import org.sonar.server.ws.TestRequest;
import org.sonar.server.ws.TestResponse;
import org.sonar.server.ws.WsActionTester;
private DbClient dbClient = db.getDbClient();
private DbSession dbSession = db.getSession();
- private WsActionTester ws = new WsActionTester(new AddMemberAction(dbClient, userSession, new UserIndexer(dbClient, es.client())));
+ private WsActionTester ws = new WsActionTester(new AddMemberAction(dbClient, userSession, new UserIndexer(dbClient, es.client()), new DefaultGroupFinder(dbClient)));
@Test
public void definition() {
UserDto user = db.users().insertUser();
expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage(format("Default group doesn't exist on default organization '%s'", organization.getKey()));
+ expectedException.expectMessage(format("Default group cannot be found on organization '%s'", organization.getUuid()));
call(organization.getKey(), user.getLogin());
}
import org.sonar.server.organization.TestDefaultOrganizationProvider;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.usergroups.DefaultGroupCreatorImpl;
+import org.sonar.server.usergroups.DefaultGroupFinder;
import org.sonar.server.ws.TestResponse;
import org.sonar.server.ws.WsActionTester;
private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
private OrganizationFlags organizationFlags = new OrganizationFlagsImpl(db.getDbClient());
private EnableSupportAction underTest = new EnableSupportAction(userSession, db.getDbClient(), defaultOrganizationProvider, organizationFlags,
- new DefaultGroupCreatorImpl(db.getDbClient()));
+ new DefaultGroupCreatorImpl(db.getDbClient()), new DefaultGroupFinder(db.getDbClient()));
private WsActionTester tester = new WsActionTester(underTest);
@Test
logInAsSystemAdministrator(user.getLogin());
expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage(String.format("Default group doesn't exist on default organization '%s'", defaultOrganizationProvider.get().getKey()));
+ expectedException.expectMessage(String.format("Default group cannot be found on organization '%s'", defaultOrganizationProvider.get().getUuid()));
call();
}
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.organization.TestDefaultOrganizationProvider;
import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.usergroups.DefaultGroupFinder;
import org.sonar.server.ws.TestRequest;
import org.sonar.server.ws.WsActionTester;
import org.sonarqube.ws.MediaTypes;
import org.sonarqube.ws.WsUsers.GroupsWsResponse;
+import static java.lang.String.format;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
private TestDefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
- private WsActionTester ws = new WsActionTester(new GroupsAction(db.getDbClient(), userSession, defaultOrganizationProvider));
+ private WsActionTester ws = new WsActionTester(new GroupsAction(db.getDbClient(), userSession, defaultOrganizationProvider, new DefaultGroupFinder(db.getDbClient())));
@Test
public void empty_groups() throws Exception {
tuple(adminGroup.getId().longValue(), adminGroup.getName(), false));
}
- @Test
- public void return_default_to_false_when_organization_has_no_default_group() throws Exception {
- UserDto user = insertUser();
- OrganizationDto organizationDto = db.organizations().insert(organization -> organization.setKey("OrgKey"));
- GroupDto group = db.users().insertGroup(organizationDto, "group1");
- addUserToGroup(user, group);
-
- GroupsWsResponse response = call(ws.newRequest().setParam("login", USER_LOGIN).setParam("organization", organizationDto.getKey()));
-
- assertThat(response.getGroupsList())
- .extracting(GroupsWsResponse.Group::getId, GroupsWsResponse.Group::getName, GroupsWsResponse.Group::getDefault)
- .containsOnly(
- tuple(group.getId().longValue(), group.getName(), false));
- }
-
@Test
public void return_groups_from_given_organization() throws Exception {
UserDto user = insertUser();
.containsOnly(tuple(group.getId().longValue(), group.getName(), group.getDescription(), true, true));
}
+ @Test
+ public void fail_when_organization_has_no_default_group() throws Exception {
+ UserDto user = insertUser();
+ OrganizationDto organizationDto = db.organizations().insert(organization -> organization.setKey("OrgKey"));
+ GroupDto group = db.users().insertGroup(organizationDto, "group1");
+ addUserToGroup(user, group);
+
+ expectedException.expect(IllegalStateException.class);
+ expectedException.expectMessage(format("Default group cannot be found on organization '%s'", organizationDto.getUuid()));
+
+ call(ws.newRequest().setParam("login", USER_LOGIN).setParam("organization", organizationDto.getKey()));
+ }
+
@Test
public void fail_on_unknown_user() throws Exception {
insertDefaultGroup("sonar-users", "Sonar Users");
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.server.usergroups;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.db.DbTester;
+import org.sonar.db.organization.OrganizationDto;
+import org.sonar.db.user.GroupDto;
+
+import static java.lang.String.format;
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class DefaultGroupFinderTest {
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ @Rule
+ public DbTester db = DbTester.create();
+
+ private DefaultGroupFinder underTest = new DefaultGroupFinder(db.getDbClient());
+
+ @Test
+ public void find_default_group() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ GroupDto defaultGroup = db.users().insertDefaultGroup(organization, "default");
+
+ GroupDto result = underTest.findDefaultGroup(db.getSession(), organization.getUuid());
+
+ assertThat(result.getId()).isEqualTo(defaultGroup.getId());
+ assertThat(result.getName()).isEqualTo("default");
+ }
+
+ @Test
+ public void fail_with_ISE_when_no_default_group_on_org() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ db.users().insertGroup(organization);
+
+ expectedException.expect(IllegalStateException.class);
+ expectedException.expectMessage(format("Default group cannot be found on organization '%s'", organization.getUuid()));
+
+ underTest.findDefaultGroup(db.getSession(), organization.getUuid());
+ }
+
+ @Test
+ public void fail_with_NPE_when_default_group_does_not_exist() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ GroupDto defaultGroup = db.users().insertDefaultGroup(organization, "default");
+ db.getDbClient().groupDao().deleteById(db.getSession(), defaultGroup.getId());
+
+ expectedException.expect(NullPointerException.class);
+ expectedException.expectMessage(format("Group '%s' cannot be found", defaultGroup.getId()));
+
+ underTest.findDefaultGroup(db.getSession(), organization.getUuid());
+ }
+}
import org.sonar.server.exceptions.UnauthorizedException;
import org.sonar.server.organization.TestDefaultOrganizationProvider;
import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.usergroups.DefaultGroupFinder;
import org.sonar.server.ws.TestRequest;
import org.sonar.server.ws.WsActionTester;
import org.sonarqube.ws.Common.Paging;
import org.sonarqube.ws.MediaTypes;
+import static java.lang.String.format;
import static org.apache.commons.lang.StringUtils.capitalize;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
@Rule
public ExpectedException expectedException = ExpectedException.none();
- private WsActionTester ws = new WsActionTester(new SearchAction(db.getDbClient(), userSession, newGroupWsSupport()));
+ private WsActionTester ws = new WsActionTester(new SearchAction(db.getDbClient(), userSession, newGroupWsSupport(), new DefaultGroupFinder(db.getDbClient())));
@Test
public void search_without_parameters() throws Exception {
assertThat(response.getGroupsList()).extracting(Group::getId, Group::getName).containsOnly(tuple(group.getId().longValue(), "users"));
}
-
@Test
public void return_default_group() throws Exception {
db.users().insertDefaultGroup(db.getDefaultOrganization(), "default");
}
@Test
- public void return_no_default_group() throws Exception {
+ public void fail_when_no_default_group() throws Exception {
db.users().insertGroup(db.getDefaultOrganization(), "users");
loginAsDefaultOrgAdmin();
- SearchWsResponse response = call(ws.newRequest());
+ expectedException.expect(IllegalStateException.class);
+ expectedException.expectMessage(format("Default group cannot be found on organization '%s'", db.getDefaultOrganization().getUuid()));
- assertThat(response.getGroupsList()).extracting(Group::getName, Group::getDefault).containsOnly(tuple("users", false));
+ call(ws.newRequest());
}
@Test
import org.sonar.db.DbClient;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.user.UserSession;
+import org.sonar.server.usergroups.DefaultGroupFinder;
import org.sonar.server.ws.WsTester;
import static org.assertj.core.api.Assertions.assertThat;
public void setUp() {
GroupWsSupport wsSupport = mock(GroupWsSupport.class);
WsTester tester = new WsTester(new UserGroupsWs(
- new SearchAction(mock(DbClient.class), mock(UserSession.class), wsSupport),
+ new SearchAction(mock(DbClient.class), mock(UserSession.class), wsSupport, mock(DefaultGroupFinder.class)),
new CreateAction(mock(DbClient.class), mock(UserSession.class), wsSupport)));
controller = tester.controller("api/user_groups");
}