]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9016 Fail when no default group found
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 11 Apr 2017 13:02:21 +0000 (15:02 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 13 Apr 2017 09:51:55 +0000 (11:51 +0200)
13 files changed:
server/sonar-server/src/main/java/org/sonar/server/organization/ws/AddMemberAction.java
server/sonar-server/src/main/java/org/sonar/server/organization/ws/EnableSupportAction.java
server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java
server/sonar-server/src/main/java/org/sonar/server/user/ws/GroupsAction.java
server/sonar-server/src/main/java/org/sonar/server/usergroups/DefaultGroupFinder.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/usergroups/ws/GroupWsSupport.java
server/sonar-server/src/main/java/org/sonar/server/usergroups/ws/SearchAction.java
server/sonar-server/src/test/java/org/sonar/server/organization/ws/AddMemberActionTest.java
server/sonar-server/src/test/java/org/sonar/server/organization/ws/EnableSupportActionTest.java
server/sonar-server/src/test/java/org/sonar/server/user/ws/GroupsActionTest.java
server/sonar-server/src/test/java/org/sonar/server/usergroups/DefaultGroupFinderTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/usergroups/ws/SearchActionTest.java
server/sonar-server/src/test/java/org/sonar/server/usergroups/ws/UserGroupsWsTest.java

index 70a54acfec5ca83c0ddea566c878904de0d00158..30cbc1930aba28f1ed12764ea96589a4b6fc35c5 100644 (file)
@@ -33,6 +33,7 @@ import org.sonar.db.user.UserDto;
 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;
@@ -44,11 +45,13 @@ public class AddMemberAction implements OrganizationsWsAction {
   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
@@ -91,7 +94,8 @@ public class AddMemberAction implements OrganizationsWsAction {
       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());
     }
@@ -102,9 +106,4 @@ public class AddMemberAction implements OrganizationsWsAction {
     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())));
-  }
-
 }
index e2245de20d9b1506a1c7e93fd85a3f1f5be353cc..5d14c944ba7892ee6714ee893e38862afbc4a5a1 100644 (file)
@@ -35,6 +35,7 @@ import org.sonar.server.organization.DefaultOrganizationProvider;
 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;
 
@@ -46,14 +47,16 @@ public class EnableSupportAction implements OrganizationsWsAction {
   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
@@ -97,8 +100,7 @@ public class EnableSupportAction implements OrganizationsWsAction {
 
   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);
@@ -110,9 +112,9 @@ public class EnableSupportAction implements OrganizationsWsAction {
     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,
@@ -122,8 +124,9 @@ public class EnableSupportAction implements OrganizationsWsAction {
       });
   }
 
-  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()));
   }
index 5b48802e51ea1513654016c0b574a5a12afd7349..6bf1731e021a717617acaf150fe64505e53d15ab 100644 (file)
@@ -207,6 +207,7 @@ import org.sonar.server.user.index.UserIndexDefinition;
 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;
@@ -354,6 +355,7 @@ public class PlatformLevel4 extends PlatformLevel {
       // groups
       UserGroupsModule.class,
       DefaultGroupCreatorImpl.class,
+      DefaultGroupFinder.class,
 
       // permissions
       DefaultTemplatesResolverImpl.class,
index 5dfd0173f5fda64e1fe9660fb167140556f950ea..d3b052bcfeacc4f511ec9fff3e3d9b03d0765265 100644 (file)
@@ -34,11 +34,13 @@ import org.sonar.db.DbClient;
 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;
@@ -62,11 +64,13 @@ public class GroupsAction implements UsersWsAction {
   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
@@ -106,7 +110,6 @@ public class GroupsAction implements UsersWsAction {
     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()
@@ -120,7 +123,7 @@ public class GroupsAction implements UsersWsAction {
       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);
     }
   }
 
@@ -155,9 +158,9 @@ public class GroupsAction implements UsersWsAction {
     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())
@@ -166,12 +169,12 @@ public class GroupsAction implements UsersWsAction {
     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();
   }
diff --git a/server/sonar-server/src/main/java/org/sonar/server/usergroups/DefaultGroupFinder.java b/server/sonar-server/src/main/java/org/sonar/server/usergroups/DefaultGroupFinder.java
new file mode 100644 (file)
index 0000000..dbfb126
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * 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));
+  }
+
+}
index d6c35e275c07dfe69ea41597551c7370277b9312..f088eb21373c7f97508962f01a7430ba2dc5354f 100644 (file)
@@ -130,10 +130,6 @@ public class GroupWsSupport {
     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.
index 84380e52c25a459430e6b7a588dde88f5a2a1d12..cb0d1120da7a2a97012a22473c6241c74966e6cf 100644 (file)
@@ -23,7 +23,6 @@ import com.google.common.collect.Sets;
 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;
@@ -39,6 +38,7 @@ import org.sonar.db.organization.OrganizationDto;
 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;
@@ -60,11 +60,13 @@ public class SearchAction implements UserGroupsWsAction {
   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
@@ -101,14 +103,14 @@ public class SearchAction implements UserGroupsWsAction {
     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);
     }
   }
 
@@ -123,10 +125,10 @@ public class SearchAction implements UserGroupsWsAction {
     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())
index ef9d2d7a2fa6e8370816a48ce9a15bb74ac25179..a3ec76d7a6a1a0734bc884b770a4a5d252e0a631 100644 (file)
@@ -45,6 +45,7 @@ import org.sonar.server.user.index.UserIndex;
 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;
@@ -71,7 +72,7 @@ public class AddMemberActionTest {
   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() {
@@ -166,7 +167,7 @@ public class AddMemberActionTest {
     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());
   }
index 074b6c22c471ddbc1d1f7c61e1885ecea72bd866..12df1741e2dfa2fedac7b4ab533d5783687c567e 100644 (file)
@@ -43,6 +43,7 @@ import org.sonar.server.organization.OrganizationFlagsImpl;
 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;
 
@@ -62,7 +63,7 @@ public class EnableSupportActionTest {
   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
@@ -191,7 +192,7 @@ public class EnableSupportActionTest {
     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();
   }
index 5e2b073987e525875eba81acacd34176c4f2151c..00388fad5d9d5a8ef9ed8d9f09deda6f27e00d4f 100644 (file)
@@ -34,11 +34,13 @@ import org.sonar.server.exceptions.ForbiddenException;
 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;
@@ -65,7 +67,7 @@ public class GroupsActionTest {
 
   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 {
@@ -195,21 +197,6 @@ public class GroupsActionTest {
         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();
@@ -230,6 +217,19 @@ public class GroupsActionTest {
       .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");
diff --git a/server/sonar-server/src/test/java/org/sonar/server/usergroups/DefaultGroupFinderTest.java b/server/sonar-server/src/test/java/org/sonar/server/usergroups/DefaultGroupFinderTest.java
new file mode 100644 (file)
index 0000000..bec8bf7
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * 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());
+  }
+}
index 9746603e54c775c1f2709cd30a0bd1d0c1d3cf75..30e8eea273b4140d9a76e6d90aed1a587b62f0b9 100644 (file)
@@ -33,11 +33,13 @@ import org.sonar.db.user.UserDto;
 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;
@@ -62,7 +64,7 @@ public class SearchActionTest {
   @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 {
@@ -177,7 +179,6 @@ public class SearchActionTest {
     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");
@@ -189,13 +190,14 @@ public class SearchActionTest {
   }
 
   @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
index 5c9ba6a57e9df61dae599c7f3d9f4c211e76e25a..f814cbc1a37f0d1a444bb292e45f9ec372a3c8cd 100644 (file)
@@ -26,6 +26,7 @@ import org.sonar.api.server.ws.WebService;
 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;
@@ -42,7 +43,7 @@ public class UserGroupsWsTest {
   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");
   }