]> source.dussan.org Git - sonarqube.git/commitdiff
WS of permission domain are compatible with Views and Developer Cockpit
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Fri, 2 Oct 2015 14:36:45 +0000 (16:36 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Fri, 2 Oct 2015 14:36:56 +0000 (16:36 +0200)
22 files changed:
server/sonar-server/src/main/java/org/sonar/server/component/ComponentFinder.java
server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionDependenciesFinder.java
server/sonar-server/src/main/java/org/sonar/server/permission/ws/SearchProjectPermissionsDataLoader.java
server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/ApplyTemplateAction.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/AddGroupActionTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/AddUserActionTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/GroupsActionTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/RemoveGroupActionTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/RemoveUserActionTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/SearchProjectPermissionsActionTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/TemplateGroupsActionTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/TemplateUsersActionTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/UsersActionTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/AddGroupToTemplateActionTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/AddUserToTemplateActionTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/ApplyTemplateActionTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/DeleteTemplateActionTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/RemoveGroupFromTemplateActionTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/RemoveUserFromTemplateActionTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/SetDefaultTemplateActionTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/UpdateTemplateActionTest.java
sonar-db/src/test/java/org/sonar/db/component/ResourceTypesRule.java [new file with mode: 0644]

index 19c594d22891300aa851d775491eadde204e5405..2dbe0d3611ddeca5ceb9a4a7a95e7087c34a0167 100644 (file)
 package org.sonar.server.component;
 
 import com.google.common.base.Optional;
+import java.util.Set;
 import javax.annotation.Nullable;
 import org.sonar.api.resources.Qualifiers;
+import org.sonar.api.resources.ResourceTypes;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.component.ComponentDto;
-import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.exceptions.NotFoundException;
 
 import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.collect.FluentIterable.from;
+import static java.lang.String.format;
+import static org.sonar.server.component.ResourceTypeFunctions.RESOURCE_TYPE_TO_QUALIFIER;
+import static org.sonar.server.ws.WsUtils.checkRequest;
 
 public class ComponentFinder {
 
@@ -49,21 +54,29 @@ public class ComponentFinder {
   }
 
   public ComponentDto getByKey(DbSession dbSession, String key) {
-    return getIfPresentOrFail(dbClient.componentDao().selectByKey(dbSession, key), String.format("Component key '%s' not found", key));
+    return getIfPresentOrFail(dbClient.componentDao().selectByKey(dbSession, key), format("Component key '%s' not found", key));
   }
 
   public ComponentDto getByUuid(DbSession dbSession, String uuid) {
-    return getIfPresentOrFail(dbClient.componentDao().selectByUuid(dbSession, uuid), String.format("Component id '%s' not found", uuid));
+    return getIfPresentOrFail(dbClient.componentDao().selectByUuid(dbSession, uuid), format("Component id '%s' not found", uuid));
   }
 
-  public ComponentDto getProjectByUuidOrKey(DbSession dbSession, @Nullable String projectUuid, @Nullable String projectKey) {
+  /**
+   * A project can be:
+   * <ul>
+   *   <li>a project – ex: SonarQube</li>
+   *   <li>a view – ex: Language Team</li>
+   *   <li>a developer – ex: Simon Brandhof</li>
+   * </ul>
+   */
+  public ComponentDto getRootComponentOrModuleByUuidOrKey(DbSession dbSession, @Nullable String projectUuid, @Nullable String projectKey, ResourceTypes resourceTypes) {
     ComponentDto project;
     if (projectUuid != null) {
-      project = getIfPresentOrFail(dbClient.componentDao().selectByUuid(dbSession, projectUuid), String.format("Project id '%s' not found", projectUuid));
+      project = getIfPresentOrFail(dbClient.componentDao().selectByUuid(dbSession, projectUuid), format("Project id '%s' not found", projectUuid));
     } else {
-      project = getIfPresentOrFail(dbClient.componentDao().selectByKey(dbSession, projectKey), String.format("Project key '%s' not found", projectKey));
+      project = getIfPresentOrFail(dbClient.componentDao().selectByKey(dbSession, projectKey), format("Project key '%s' not found", projectKey));
     }
-    checkIsProjectOrModule(project);
+    checkIsProjectOrModule(project, resourceTypes);
 
     return project;
   }
@@ -75,9 +88,13 @@ public class ComponentFinder {
     return component.get();
   }
 
-  private static void checkIsProjectOrModule(ComponentDto component) {
-    if (!Qualifiers.PROJECT.equals(component.qualifier()) && !Qualifiers.MODULE.equals(component.qualifier())) {
-      throw new BadRequestException(String.format("Component '%s' (id: %s) must be a project or a module.", component.key(), component.uuid()));
-    }
+  private void checkIsProjectOrModule(ComponentDto component, ResourceTypes resourceTypes) {
+    Set<String> rootQualifiers = from(resourceTypes.getRoots())
+      .transform(RESOURCE_TYPE_TO_QUALIFIER)
+      .toSet();
+    String qualifier = component.qualifier();
+
+    checkRequest(rootQualifiers.contains(qualifier) || Qualifiers.MODULE.equals(qualifier),
+      format("Component '%s' (id: %s) must be a project or a module.", component.key(), component.uuid()));
   }
 }
index 9838e66f73364aac1fea8f65a791c1d35f26e367..dbcc8f6ec96f9e285c598137159b341e092b3701 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.server.permission.ws;
 
 import com.google.common.base.Optional;
 import javax.annotation.CheckForNull;
+import org.sonar.api.resources.ResourceTypes;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.component.ComponentDto;
@@ -38,10 +39,12 @@ import static org.sonar.server.ws.WsUtils.checkFound;
 public class PermissionDependenciesFinder {
   private final DbClient dbClient;
   private final ComponentFinder componentFinder;
+  private final ResourceTypes resourceTypes;
 
-  public PermissionDependenciesFinder(DbClient dbClient, ComponentFinder componentFinder) {
+  public PermissionDependenciesFinder(DbClient dbClient, ComponentFinder componentFinder, ResourceTypes resourceTypes) {
     this.dbClient = dbClient;
     this.componentFinder = componentFinder;
+    this.resourceTypes = resourceTypes;
   }
 
   /**
@@ -53,11 +56,11 @@ public class PermissionDependenciesFinder {
     }
 
     WsProjectRef wsProjectRef = request.project().get();
-    return Optional.of(componentFinder.getProjectByUuidOrKey(dbSession, wsProjectRef.uuid(), wsProjectRef.key()));
+    return Optional.of(componentFinder.getRootComponentOrModuleByUuidOrKey(dbSession, wsProjectRef.uuid(), wsProjectRef.key(), resourceTypes));
   }
 
-  public ComponentDto getProject(DbSession dbSession, WsProjectRef projectRef) {
-    return componentFinder.getProjectByUuidOrKey(dbSession, projectRef.uuid(), projectRef.key());
+  public ComponentDto getRootComponentOrModule(DbSession dbSession, WsProjectRef projectRef) {
+    return componentFinder.getRootComponentOrModuleByUuidOrKey(dbSession, projectRef.uuid(), projectRef.key(), resourceTypes);
   }
 
   public String getGroupName(DbSession dbSession, PermissionRequest request) {
index bbdca2994dd0a5bc66412cb34636991505f8a7ca..0e91bce2eb551523ad24b3c3a3979c89333b7070 100644 (file)
@@ -92,7 +92,7 @@ public class SearchProjectPermissionsDataLoader {
     Optional<WsProjectRef> project = WsProjectRef.optionalFromRequest(wsRequest);
 
     if (project.isPresent()) {
-      return singletonList(finder.getProject(dbSession, project.get()));
+      return singletonList(finder.getRootComponentOrModule(dbSession, project.get()));
     }
 
     return dbClient.componentDao().selectComponents(dbSession, rootQualifiers, paging.offset(), paging.pageSize(), query);
index ab97c359cc8a68b1712bbe646f334cc62a257cbf..11111c34762c3483cbfe572e7c11f77d32974b21 100644 (file)
@@ -68,7 +68,7 @@ public class ApplyTemplateAction implements PermissionsWsAction {
     DbSession dbSession = dbClient.openSession(false);
     try {
       PermissionTemplateDto template = finder.getTemplate(dbSession, WsTemplateRef.fromRequest(wsRequest));
-      ComponentDto project = finder.getProject(dbSession, WsProjectRef.fromRequest(wsRequest));
+      ComponentDto project = finder.getRootComponentOrModule(dbSession, WsProjectRef.fromRequest(wsRequest));
 
       ApplyPermissionTemplateQuery query = ApplyPermissionTemplateQuery.create(
         template.getUuid(),
index 2544e3dc145d1071a0e05b68b721794ffdbcf13c..3c3779189d8ad435627814d949199510403d6761 100644 (file)
@@ -26,12 +26,14 @@ import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.junit.rules.ExpectedException;
 import org.mockito.ArgumentCaptor;
+import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.utils.System2;
 import org.sonar.api.web.UserRole;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.ComponentTesting;
+import org.sonar.db.component.ResourceTypesRule;
 import org.sonar.db.user.GroupDto;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.exceptions.BadRequestException;
@@ -48,6 +50,7 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
 import static org.sonar.db.component.ComponentTesting.newProjectDto;
+import static org.sonar.db.component.ComponentTesting.newView;
 import static org.sonar.server.permission.ws.AddGroupAction.ACTION;
 import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_GROUP_ID;
 import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_GROUP_NAME;
@@ -64,6 +67,7 @@ public class AddGroupActionTest {
   public DbTester db = DbTester.create(System2.INSTANCE);
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
+  ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
   private PermissionUpdater permissionUpdater;
   private DbClient dbClient;
   private ArgumentCaptor<PermissionChange> permissionChangeCaptor;
@@ -75,7 +79,7 @@ public class AddGroupActionTest {
     dbClient = db.getDbClient();
     ComponentFinder componentFinder = new ComponentFinder(dbClient);
     ws = new WsTester(new PermissionsWs(
-      new AddGroupAction(dbClient, new PermissionChangeBuilder(new PermissionDependenciesFinder(dbClient, componentFinder)), permissionUpdater)));
+      new AddGroupAction(dbClient, new PermissionChangeBuilder(new PermissionDependenciesFinder(dbClient, componentFinder, resourceTypes)), permissionUpdater)));
     userSession.login("admin").setGlobalPermissions(SYSTEM_ADMIN);
   }
 
@@ -146,6 +150,24 @@ public class AddGroupActionTest {
     assertThat(permissionChange.groupName()).isEqualTo("sonar-administrators");
   }
 
+  @Test
+  public void add_with_view_uuid() throws Exception {
+    insertGroup("sonar-administrators");
+    insertComponent(newView("view-uuid").setKey("view-key"));
+    commit();
+
+    newRequest()
+      .setParam(PARAM_GROUP_NAME, "sonar-administrators")
+      .setParam(PARAM_PROJECT_ID, "view-uuid")
+      .setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
+      .execute();
+
+    verify(permissionUpdater).addPermission(permissionChangeCaptor.capture());
+    PermissionChange permissionChange = permissionChangeCaptor.getValue();
+    assertThat(permissionChange.componentKey()).isEqualTo("view-key");
+    assertThat(permissionChange.groupName()).isEqualTo("sonar-administrators");
+  }
+
   @Test
   public void fail_if_project_uuid_not_found() throws Exception {
     expectedException.expect(NotFoundException.class);
index 7b6e00e49f1e76b7eb78b89da640215ea02f056f..48e3b6d7bc8e1dc81ec5b567ae54854a444a89e7 100644 (file)
@@ -26,12 +26,14 @@ import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.junit.rules.ExpectedException;
 import org.mockito.ArgumentCaptor;
+import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.utils.System2;
 import org.sonar.api.web.UserRole;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ResourceTypesRule;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.exceptions.NotFoundException;
@@ -48,25 +50,29 @@ import static org.mockito.Mockito.verify;
 import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
 import static org.sonar.db.component.ComponentTesting.newFileDto;
 import static org.sonar.db.component.ComponentTesting.newProjectDto;
+import static org.sonar.db.component.ComponentTesting.newView;
 import static org.sonar.server.permission.ws.AddUserAction.ACTION;
+import static org.sonar.server.permission.ws.PermissionsWs.ENDPOINT;
 import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PERMISSION;
-import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PROJECT_KEY;
 import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PROJECT_ID;
+import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PROJECT_KEY;
 import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_USER_LOGIN;
-import static org.sonar.server.permission.ws.PermissionsWs.ENDPOINT;
 
 @Category(DbTests.class)
 public class AddUserActionTest {
-  UserSessionRule userSession = UserSessionRule.standalone();
-  WsTester ws;
   @Rule
   public DbTester db = DbTester.create(System2.INSTANCE);
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
-  private PermissionUpdater permissionUpdater;
-  private DbClient dbClient;
-  private DbSession dbSession;
-  private ArgumentCaptor<PermissionChange> permissionChangeCaptor = ArgumentCaptor.forClass(PermissionChange.class);
+  ResourceTypesRule resourceTypes = new ResourceTypesRule()
+    .setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
+
+  UserSessionRule userSession = UserSessionRule.standalone();
+  WsTester ws;
+  PermissionUpdater permissionUpdater;
+  DbClient dbClient;
+  DbSession dbSession;
+  ArgumentCaptor<PermissionChange> permissionChangeCaptor = ArgumentCaptor.forClass(PermissionChange.class);
 
   @Before
   public void setUp() {
@@ -75,7 +81,7 @@ public class AddUserActionTest {
     dbSession = db.getSession();
     ComponentFinder componentFinder = new ComponentFinder(dbClient);
     ws = new WsTester(new PermissionsWs(
-      new AddUserAction(dbClient, permissionUpdater, new PermissionChangeBuilder(new PermissionDependenciesFinder(dbClient, componentFinder)))));
+      new AddUserAction(dbClient, permissionUpdater, new PermissionChangeBuilder(new PermissionDependenciesFinder(dbClient, componentFinder, resourceTypes)))));
     userSession.login("admin").setGlobalPermissions(SYSTEM_ADMIN);
   }
 
@@ -124,6 +130,22 @@ public class AddUserActionTest {
     assertThat(permissionChange.componentKey()).isEqualTo("project-key");
   }
 
+  @Test
+  public void add_user_permission_with_view_uuid() throws Exception {
+    dbClient.componentDao().insert(dbSession, newView("view-uuid").setKey("view-key"));
+    commit();
+
+    ws.newPostRequest(ENDPOINT, ACTION)
+      .setParam(PARAM_USER_LOGIN, "ray.bradbury")
+      .setParam(PARAM_PROJECT_ID, "view-uuid")
+      .setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
+      .execute();
+
+    verify(permissionUpdater).addPermission(permissionChangeCaptor.capture());
+    PermissionChange permissionChange = permissionChangeCaptor.getValue();
+    assertThat(permissionChange.componentKey()).isEqualTo("view-key");
+  }
+
   @Test
   public void fail_when_project_uuid_is_unknown() throws Exception {
     expectedException.expect(NotFoundException.class);
index 3ca4b95c5de367909eb090efc18ecf1dfb1f9a0a..3d5ea0755a2dd242125d79e5be7b40102ff6365a 100644 (file)
@@ -26,6 +26,7 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.junit.rules.ExpectedException;
+import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.security.DefaultGroups;
 import org.sonar.api.server.ws.WebService.Param;
 import org.sonar.api.server.ws.WebService.SelectionMode;
@@ -35,7 +36,9 @@ import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
+import org.sonar.db.component.ComponentDbTester;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ResourceTypesRule;
 import org.sonar.db.user.GroupDto;
 import org.sonar.db.user.GroupRoleDto;
 import org.sonar.server.component.ComponentFinder;
@@ -53,6 +56,7 @@ import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
 import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION;
 import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
 import static org.sonar.db.component.ComponentTesting.newProjectDto;
+import static org.sonar.db.component.ComponentTesting.newView;
 import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PERMISSION;
 import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PROJECT_KEY;
 import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PROJECT_ID;
@@ -67,6 +71,8 @@ public class GroupsActionTest {
   public UserSessionRule userSession = UserSessionRule.standalone();
   @Rule
   public DbTester db = DbTester.create(System2.INSTANCE);
+  ComponentDbTester componentDb = new ComponentDbTester(db);
+  ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
 
   DbClient dbClient;
   DbSession dbSession;
@@ -79,7 +85,7 @@ public class GroupsActionTest {
     dbClient = db.getDbClient();
     dbSession = db.getSession();
     PermissionFinder permissionFinder = new PermissionFinder(dbClient);
-    underTest = new GroupsAction(dbClient, userSession, permissionFinder, new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient)));
+    underTest = new GroupsAction(dbClient, userSession, permissionFinder, new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), resourceTypes));
     ws = new WsActionTester(underTest);
 
     userSession.login("login").setGlobalPermissions(SYSTEM_ADMIN);
@@ -169,6 +175,26 @@ public class GroupsActionTest {
       .doesNotContain("group-3");
   }
 
+  @Test
+  public void search_groups_on_views() {
+    ComponentDto view = componentDb.insertComponent(newView("view-uuid").setKey("view-key"));
+    GroupDto group = insertGroup(new GroupDto().setName("project-group-name"));
+    insertGroupRole(new GroupRoleDto()
+      .setGroupId(group.getId())
+      .setRole(ISSUE_ADMIN)
+      .setResourceId(view.getId()));
+
+    String result = ws.newRequest()
+      .setParam(PARAM_PERMISSION, ISSUE_ADMIN)
+      .setParam(PARAM_PROJECT_ID, "view-uuid")
+      .execute().getInput();
+
+    assertThat(result).contains("project-group-name")
+      .doesNotContain("group-1")
+      .doesNotContain("group-2")
+      .doesNotContain("group-3");
+  }
+
   @Test
   public void fail_if_project_permission_without_project() {
     expectedException.expect(BadRequestException.class);
index 0add9e68cd83048d76435489297520a82f8809c0..7a485b6660cebe730839772a26a3f6393760b1a4 100644 (file)
@@ -26,12 +26,14 @@ import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.junit.rules.ExpectedException;
 import org.mockito.ArgumentCaptor;
+import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.utils.System2;
 import org.sonar.api.web.UserRole;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ResourceTypesRule;
 import org.sonar.db.user.GroupDto;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.exceptions.BadRequestException;
@@ -49,6 +51,7 @@ import static org.mockito.Mockito.verify;
 import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
 import static org.sonar.db.component.ComponentTesting.newFileDto;
 import static org.sonar.db.component.ComponentTesting.newProjectDto;
+import static org.sonar.db.component.ComponentTesting.newView;
 import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_GROUP_ID;
 import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_GROUP_NAME;
 import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PERMISSION;
@@ -64,6 +67,7 @@ public class RemoveGroupActionTest {
   public DbTester db = DbTester.create(System2.INSTANCE);
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
+  ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
   private PermissionUpdater permissionUpdater;
   private ArgumentCaptor<PermissionChange> permissionChangeCaptor = ArgumentCaptor.forClass(PermissionChange.class);
   private DbSession dbSession = db.getSession();
@@ -74,7 +78,7 @@ public class RemoveGroupActionTest {
     DbClient dbClient = db.getDbClient();
     ComponentFinder componentFinder = new ComponentFinder(dbClient);
     ws = new WsTester(new PermissionsWs(
-      new RemoveGroupAction(dbClient, new PermissionChangeBuilder(new PermissionDependenciesFinder(dbClient, componentFinder)), permissionUpdater)));
+      new RemoveGroupAction(dbClient, new PermissionChangeBuilder(new PermissionDependenciesFinder(dbClient, componentFinder, resourceTypes)), permissionUpdater)));
     userSession.login("admin").setGlobalPermissions(SYSTEM_ADMIN);
   }
 
@@ -126,11 +130,28 @@ public class RemoveGroupActionTest {
     assertThat(permissionChange.componentKey()).isEqualTo("project-key");
   }
 
+  @Test
+  public void remove_with_view_uuid() throws Exception {
+    insertComponent(newView("view-uuid").setKey("view-key"));
+    insertGroup("sonar-administrators");
+    db.commit();
+
+    newRequest()
+      .setParam(PARAM_GROUP_NAME, "sonar-administrators")
+      .setParam(PARAM_PROJECT_ID, "view-uuid")
+      .setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
+      .execute();
+
+    verify(permissionUpdater).removePermission(permissionChangeCaptor.capture());
+    PermissionChange permissionChange = permissionChangeCaptor.getValue();
+    assertThat(permissionChange.componentKey()).isEqualTo("view-key");
+  }
+
   @Test
   public void remove_with_project_key() throws Exception {
     insertComponent(newProjectDto("project-uuid").setKey("project-key"));
     insertGroup("sonar-administrators");
-    commit();
+    db.commit();
 
     newRequest()
       .setParam(PARAM_GROUP_NAME, "sonar-administrators")
index ff47ab463ec4f0afd7dee47a0aa2308a087e21d7..6557350bc681d581ac4ab03d8dd8d9886783b798 100644 (file)
@@ -26,12 +26,14 @@ import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.junit.rules.ExpectedException;
 import org.mockito.ArgumentCaptor;
+import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.utils.System2;
 import org.sonar.api.web.UserRole;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ResourceTypesRule;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.exceptions.NotFoundException;
@@ -48,6 +50,7 @@ import static org.mockito.Mockito.verify;
 import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
 import static org.sonar.db.component.ComponentTesting.newFileDto;
 import static org.sonar.db.component.ComponentTesting.newProjectDto;
+import static org.sonar.db.component.ComponentTesting.newView;
 import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PERMISSION;
 import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PROJECT_KEY;
 import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PROJECT_ID;
@@ -61,6 +64,7 @@ public class RemoveUserActionTest {
   public DbTester db = DbTester.create(System2.INSTANCE);
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
+  ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
   UserSessionRule userSession = UserSessionRule.standalone();
   WsTester ws;
   PermissionUpdater permissionUpdater;
@@ -75,7 +79,7 @@ public class RemoveUserActionTest {
     dbSession = db.getSession();
     ComponentFinder componentFinder = new ComponentFinder(dbClient);
     ws = new WsTester(new PermissionsWs(
-      new RemoveUserAction(dbClient, permissionUpdater, new PermissionChangeBuilder(new PermissionDependenciesFinder(dbClient, componentFinder)))));
+      new RemoveUserAction(dbClient, permissionUpdater, new PermissionChangeBuilder(new PermissionDependenciesFinder(dbClient, componentFinder, resourceTypes)))));
     userSession.login("admin").setGlobalPermissions(SYSTEM_ADMIN);
   }
 
@@ -122,6 +126,21 @@ public class RemoveUserActionTest {
     assertThat(permissionChange.componentKey()).isEqualTo("project-key");
   }
 
+  @Test
+  public void remove_with_view_uuid() throws Exception {
+    insertComponent(newView("view-uuid").setKey("view-key"));
+
+    ws.newPostRequest(PermissionsWs.ENDPOINT, ACTION)
+      .setParam(PARAM_USER_LOGIN, "ray.bradbury")
+      .setParam(PARAM_PROJECT_ID, "view-uuid")
+      .setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
+      .execute();
+
+    verify(permissionUpdater).removePermission(permissionChangeCaptor.capture());
+    PermissionChange permissionChange = permissionChangeCaptor.getValue();
+    assertThat(permissionChange.componentKey()).isEqualTo("view-key");
+  }
+
   @Test
   public void fail_when_project_does_not_exist() throws Exception {
     expectedException.expect(NotFoundException.class);
index a2c2649fa7ac929b8119a3ed2e6fb3d01b33b5a9..8368c1276cdc9e06a9aee8f376dd3c81510c7319 100644 (file)
@@ -85,7 +85,7 @@ public class SearchProjectPermissionsActionTest {
     resourceTypes = mock(ResourceTypes.class);
     when(resourceTypes.getRoots()).thenReturn(rootResourceTypes());
     ComponentFinder componentFinder = new ComponentFinder(dbClient);
-    PermissionDependenciesFinder finder = new PermissionDependenciesFinder(dbClient, componentFinder);
+    PermissionDependenciesFinder finder = new PermissionDependenciesFinder(dbClient, componentFinder, resourceTypes);
     i18n.setProjectPermissions();
 
     dataLoader = new SearchProjectPermissionsDataLoader(dbClient, finder, resourceTypes);
@@ -225,7 +225,7 @@ public class SearchProjectPermissionsActionTest {
     insertComponent(newDeveloper("developer-name"));
     insertComponent(newProjectDto("project-uuid"));
     commit();
-    dataLoader = new SearchProjectPermissionsDataLoader(dbClient, new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient)), resourceTypes);
+    dataLoader = new SearchProjectPermissionsDataLoader(dbClient, new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), resourceTypes), resourceTypes);
     underTest = new SearchProjectPermissionsAction(dbClient, userSession, i18n, dataLoader);
     ws = new WsActionTester(underTest);
 
index 8c461c2372737f07184143288504ee36c40d2a9d..53ed5888aa543ad58507107279b08ac8159c5641 100644 (file)
@@ -28,6 +28,7 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.junit.rules.ExpectedException;
+import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.api.utils.System2;
 import org.sonar.api.web.UserRole;
@@ -35,6 +36,7 @@ import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
+import org.sonar.db.component.ResourceTypesRule;
 import org.sonar.db.permission.PermissionTemplateDto;
 import org.sonar.db.permission.PermissionTemplateGroupDto;
 import org.sonar.db.user.GroupDto;
@@ -71,6 +73,7 @@ public class TemplateGroupsActionTest {
   public UserSessionRule userSession = UserSessionRule.standalone();
   @Rule
   public DbTester db = DbTester.create(System2.INSTANCE);
+  ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
 
   DbClient dbClient;
   DbSession dbSession;
@@ -85,7 +88,7 @@ public class TemplateGroupsActionTest {
   public void setUp() {
     dbClient = db.getDbClient();
     dbSession = db.getSession();
-    underTest = new TemplateGroupsAction(dbClient, userSession, new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient)));
+    underTest = new TemplateGroupsAction(dbClient, userSession, new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), resourceTypes));
     ws = new WsActionTester(underTest);
 
     userSession.login("login").setGlobalPermissions(SYSTEM_ADMIN);
index 5991d5796d2e4e6def257c6640552db9e84844e1..bfd1f1f51e7e16b251d3c9afcaa0a130218204f2 100644 (file)
@@ -28,6 +28,7 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.junit.rules.ExpectedException;
+import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.api.utils.System2;
 import org.sonar.api.web.UserRole;
@@ -35,6 +36,7 @@ import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
+import org.sonar.db.component.ResourceTypesRule;
 import org.sonar.db.permission.PermissionTemplateDto;
 import org.sonar.db.permission.PermissionTemplateUserDto;
 import org.sonar.db.user.UserDto;
@@ -66,6 +68,7 @@ public class TemplateUsersActionTest {
   public UserSessionRule userSession = UserSessionRule.standalone();
   @Rule
   public DbTester db = DbTester.create(System2.INSTANCE);
+  ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
   DbClient dbClient = db.getDbClient();
   DbSession dbSession = db.getSession();
   WsActionTester ws;
@@ -77,7 +80,7 @@ public class TemplateUsersActionTest {
 
   @Before
   public void setUp() {
-    PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient));
+    PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), resourceTypes);
     underTest = new TemplateUsersAction(dbClient, userSession, dependenciesFinder);
     ws = new WsActionTester(underTest);
 
index 864de4c130a55222ce5fe17a5e6116a3c691157a..9b24ebd01f3d933ddd595ec4d0968473da9d410f 100644 (file)
@@ -25,6 +25,7 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.junit.rules.ExpectedException;
+import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.server.ws.WebService.Param;
 import org.sonar.api.server.ws.WebService.SelectionMode;
 import org.sonar.api.utils.System2;
@@ -33,6 +34,7 @@ import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ResourceTypesRule;
 import org.sonar.db.user.UserDto;
 import org.sonar.db.user.UserRoleDto;
 import org.sonar.server.component.ComponentFinder;
@@ -51,8 +53,8 @@ import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
 import static org.sonar.db.component.ComponentTesting.newProjectDto;
 import static org.sonar.db.user.UserTesting.newUserDto;
 import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PERMISSION;
-import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PROJECT_KEY;
 import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PROJECT_ID;
+import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PROJECT_KEY;
 import static org.sonar.test.JsonAssert.assertJson;
 
 @Category(DbTests.class)
@@ -64,6 +66,7 @@ public class UsersActionTest {
   public UserSessionRule userSession = UserSessionRule.standalone();
   @Rule
   public DbTester db = DbTester.create(System2.INSTANCE);
+  ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
   DbClient dbClient = db.getDbClient();
   DbSession dbSession = db.getSession();
   WsActionTester ws;
@@ -73,7 +76,7 @@ public class UsersActionTest {
   @Before
   public void setUp() {
     PermissionFinder permissionFinder = new PermissionFinder(dbClient);
-    PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient));
+    PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), resourceTypes);
     underTest = new UsersAction(dbClient, userSession, permissionFinder, dependenciesFinder);
     ws = new WsActionTester(underTest);
 
index b447951d92b7ff5645f078b082e49a92fb669310..1d26b5ae5d821901a3dc6e83f240e16c305555c5 100644 (file)
@@ -29,12 +29,14 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.junit.rules.ExpectedException;
+import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.utils.System2;
 import org.sonar.api.web.UserRole;
 import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
+import org.sonar.db.component.ResourceTypesRule;
 import org.sonar.db.permission.GroupWithPermissionDto;
 import org.sonar.db.permission.PermissionQuery;
 import org.sonar.db.permission.PermissionTemplateDto;
@@ -76,6 +78,7 @@ public class AddGroupToTemplateActionTest {
   public ExpectedException expectedException = ExpectedException.none();
   @Rule
   public UserSessionRule userSession = UserSessionRule.standalone();
+  ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
 
   WsActionTester ws;
   DbClient dbClient;
@@ -89,7 +92,7 @@ public class AddGroupToTemplateActionTest {
     dbSession = db.getSession();
     userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
 
-    PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient));
+    PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), resourceTypes);
     ws = new WsActionTester(new AddGroupToTemplateAction(dbClient, dependenciesFinder, userSession));
 
     group = insertGroup(newGroupDto().setName(GROUP_NAME));
index 826dd24e4a3af2e647e2490dc7bb30c3b793366b..6bc89246c15a17faefc1368c9173d86163115b6b 100644 (file)
@@ -29,11 +29,13 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.junit.rules.ExpectedException;
+import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.utils.System2;
 import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
+import org.sonar.db.component.ResourceTypesRule;
 import org.sonar.db.permission.PermissionQuery;
 import org.sonar.db.permission.PermissionTemplateDto;
 import org.sonar.db.permission.UserWithPermissionDto;
@@ -71,6 +73,7 @@ public class AddUserToTemplateActionTest {
   public ExpectedException expectedException = ExpectedException.none();
   @Rule
   public UserSessionRule userSession = UserSessionRule.standalone();
+  ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
 
   WsActionTester ws;
   DbClient dbClient;
@@ -84,7 +87,7 @@ public class AddUserToTemplateActionTest {
     dbSession = db.getSession();
     userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
 
-    PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient));
+    PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), resourceTypes);
     ws = new WsActionTester(new AddUserToTemplateAction(dbClient, dependenciesFinder, userSession));
 
     user = insertUser(newUserDto().setLogin(USER_LOGIN));
index cbe4c96b6a5030aafa7057815bd22e668e683401..5f6764949668044ec1cc74c284ec4644f386cc58 100644 (file)
@@ -30,6 +30,7 @@ import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.junit.rules.ExpectedException;
 import org.sonar.api.config.Settings;
+import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.utils.System2;
 import org.sonar.api.web.UserRole;
 import org.sonar.core.permission.GlobalPermissions;
@@ -37,6 +38,7 @@ import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ResourceTypesRule;
 import org.sonar.db.permission.GroupWithPermissionDto;
 import org.sonar.db.permission.PermissionQuery;
 import org.sonar.db.permission.PermissionRepository;
@@ -83,6 +85,7 @@ public class ApplyTemplateActionTest {
   public UserSessionRule userSession = UserSessionRule.standalone();
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
+  ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
 
   WsActionTester ws;
   DbClient dbClient;
@@ -107,7 +110,7 @@ public class ApplyTemplateActionTest {
     PermissionFinder permissionFinder = new PermissionFinder(dbClient);
     ComponentFinder componentFinder = new ComponentFinder(dbClient);
     PermissionService permissionService = new PermissionService(dbClient, repository, issueAuthorizationIndexer, userSession, componentFinder);
-    PermissionDependenciesFinder permissionDependenciesFinder = new PermissionDependenciesFinder(dbClient, componentFinder);
+    PermissionDependenciesFinder permissionDependenciesFinder = new PermissionDependenciesFinder(dbClient, componentFinder, resourceTypes);
 
     ApplyTemplateAction underTest = new ApplyTemplateAction(dbClient, permissionService, permissionDependenciesFinder);
     ws = new WsActionTester(underTest);
index 5b824c94637288fe78ab57385967793a504d9544..2a6ecc9d7b008e58d8f8c23f4eb82ce622859ede 100644 (file)
@@ -26,12 +26,14 @@ import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
+import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.utils.System2;
 import org.sonar.api.web.UserRole;
 import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
+import org.sonar.db.component.ResourceTypesRule;
 import org.sonar.db.permission.PermissionTemplateDto;
 import org.sonar.db.user.GroupDto;
 import org.sonar.db.user.GroupTesting;
@@ -67,6 +69,7 @@ public class DeleteTemplateActionTest {
   public UserSessionRule userSession = UserSessionRule.standalone();
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
+  ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
 
   WsActionTester ws;
   DbClient dbClient;
@@ -83,7 +86,7 @@ public class DeleteTemplateActionTest {
     dbSession = db.getSession();
     defaultTemplatePermissionFinder = mock(DefaultPermissionTemplateFinder.class);
     when(defaultTemplatePermissionFinder.getDefaultTemplateUuids()).thenReturn(Collections.<String>emptySet());
-    PermissionDependenciesFinder finder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient));
+    PermissionDependenciesFinder finder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), resourceTypes);
     ws = new WsActionTester(new DeleteTemplateAction(dbClient, userSession, finder, defaultTemplatePermissionFinder));
 
     permissionTemplate = insertTemplateAndAssociatedPermissions(newPermissionTemplateDto().setUuid(TEMPLATE_UUID));
index d35995d9a4507a4b6aec3dd04c61928796d3aecb..cbb9772b6c4ff90250c84ddb2d1a5caa492f28b6 100644 (file)
@@ -29,11 +29,13 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.junit.rules.ExpectedException;
+import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.utils.System2;
 import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
+import org.sonar.db.component.ResourceTypesRule;
 import org.sonar.db.permission.GroupWithPermissionDto;
 import org.sonar.db.permission.PermissionQuery;
 import org.sonar.db.permission.PermissionTemplateDto;
@@ -74,6 +76,7 @@ public class RemoveGroupFromTemplateActionTest {
   public ExpectedException expectedException = ExpectedException.none();
   @Rule
   public UserSessionRule userSession = UserSessionRule.standalone();
+  ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
 
   WsActionTester ws;
   DbClient dbClient;
@@ -87,7 +90,7 @@ public class RemoveGroupFromTemplateActionTest {
     dbSession = db.getSession();
     userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
 
-    PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient));
+    PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), resourceTypes);
     ws = new WsActionTester(new RemoveGroupFromTemplateAction(dbClient, dependenciesFinder, userSession));
 
     group = insertGroup(newGroupDto().setName(GROUP_NAME));
index 406bb72f18de9e9bb1b10c69ba22c30b6f062d19..7bda0f44f05ac27f4dc2901645c14cb61d5bf979 100644 (file)
@@ -29,11 +29,13 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.junit.rules.ExpectedException;
+import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.utils.System2;
 import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
+import org.sonar.db.component.ResourceTypesRule;
 import org.sonar.db.permission.PermissionQuery;
 import org.sonar.db.permission.PermissionTemplateDto;
 import org.sonar.db.permission.UserWithPermissionDto;
@@ -72,6 +74,7 @@ public class RemoveUserFromTemplateActionTest {
   public ExpectedException expectedException = ExpectedException.none();
   @Rule
   public UserSessionRule userSession = UserSessionRule.standalone();
+  ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
 
   WsActionTester ws;
   DbClient dbClient;
@@ -85,7 +88,7 @@ public class RemoveUserFromTemplateActionTest {
     dbSession = db.getSession();
     userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
 
-    PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient));
+    PermissionDependenciesFinder dependenciesFinder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), resourceTypes);
     ws = new WsActionTester(new RemoveUserFromTemplateAction(dbClient, dependenciesFinder, userSession));
 
     user = insertUser(newUserDto().setLogin(USER_LOGIN));
index 327c667003c14aecef766e1fccda1bfe025303a5..cd7dde8c148fbc9fc458e10f30a572cf5d61b953 100644 (file)
@@ -94,7 +94,7 @@ public class SetDefaultTemplateActionTest {
 
     ws = new WsActionTester(new SetDefaultTemplateAction(
       dbClient,
-      new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient)),
+      new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), resourceTypes),
       resourceTypes,
       persistentSettings,
       userSession, i18n));
index 0246c5a9eecf82173bd441d09e63aff1655a3cd9..b882b9f82e5ff97162751a3b46eeb2dfb577f2eb 100644 (file)
@@ -26,11 +26,13 @@ import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
+import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.utils.System2;
 import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
+import org.sonar.db.component.ResourceTypesRule;
 import org.sonar.db.permission.PermissionTemplateDto;
 import org.sonar.db.user.GroupDto;
 import org.sonar.server.component.ComponentFinder;
@@ -66,6 +68,7 @@ public class UpdateTemplateActionTest {
   WsActionTester ws;
   DbClient dbClient;
   DbSession dbSession;
+  ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
 
   PermissionTemplateDto templateDto;
 
@@ -77,7 +80,7 @@ public class UpdateTemplateActionTest {
 
     dbClient = db.getDbClient();
     dbSession = db.getSession();
-    PermissionDependenciesFinder finder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient));
+    PermissionDependenciesFinder finder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), resourceTypes);
 
     ws = new WsActionTester(new UpdateTemplateAction(dbClient, userSession, system, finder));
 
diff --git a/sonar-db/src/test/java/org/sonar/db/component/ResourceTypesRule.java b/sonar-db/src/test/java/org/sonar/db/component/ResourceTypesRule.java
new file mode 100644 (file)
index 0000000..e305ca6
--- /dev/null
@@ -0,0 +1,107 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.db.component;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import org.sonar.api.resources.ResourceType;
+import org.sonar.api.resources.ResourceTypeTree;
+import org.sonar.api.resources.ResourceTypes;
+
+public class ResourceTypesRule extends ResourceTypes {
+  private final Set<ResourceType> allResourceTypes = new HashSet<>();
+  private final Set<ResourceType> rootResourceTypes = new HashSet<>();
+
+  @Override
+  public Collection<ResourceType> getAll() {
+    return allResourceTypes;
+  }
+
+  @Override
+  public Collection<ResourceType> getRoots() {
+    return rootResourceTypes;
+  }
+
+  public ResourceTypesRule setRootQualifiers(String... qualifiers) {
+    rootResourceTypes.clear();
+    for (String qualifier : qualifiers) {
+      rootResourceTypes.add(ResourceType.builder(qualifier).build());
+    }
+
+    return this;
+  }
+
+  public ResourceTypesRule setAllQualifiers(String... qualifiers) {
+    allResourceTypes.clear();
+    for (String qualifier : qualifiers) {
+      allResourceTypes.add(ResourceType.builder(qualifier).build());
+    }
+
+    return this;
+  }
+
+  @Override
+  public ResourceType get(String qualifier) {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public Collection<ResourceType> getAllWithPropertyKey(String propertyKey) {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public Collection<ResourceType> getAllWithPropertyValue(String propertyKey, String propertyValue) {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public Collection<ResourceType> getAllWithPropertyValue(String propertyKey, boolean propertyValue) {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public List<String> getChildrenQualifiers(String qualifier) {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public List<ResourceType> getChildren(String qualifier) {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public List<String> getLeavesQualifiers(String qualifier) {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public ResourceTypeTree getTree(String qualifier) {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public ResourceType getRoot(String qualifier) {
+    throw new UnsupportedOperationException();
+  }
+}