]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9551 Use view templates for applications
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 17 Jul 2017 16:19:48 +0000 (18:19 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 31 Jul 2017 09:27:51 +0000 (11:27 +0200)
server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDbTester.java
server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentTesting.java
server/sonar-server/src/main/java/org/sonar/server/permission/PermissionTemplateService.java
server/sonar-server/src/test/java/org/sonar/server/component/ComponentUpdaterTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/PermissionTemplateServiceTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/DefaultTemplatesResolverRule.java

index 5d1f6b7973f9d0756ac1d67a6d0ea97866608544..e434b210da766d11b9b9231cb427b1d439dd5f88 100644 (file)
@@ -29,6 +29,7 @@ import org.sonar.db.organization.OrganizationDto;
 
 import static com.google.common.base.Preconditions.checkState;
 import static java.util.Arrays.asList;
+import static org.sonar.db.component.ComponentTesting.newApplication;
 import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
 import static org.sonar.db.component.ComponentTesting.newPublicProjectDto;
 import static org.sonar.db.component.ComponentTesting.newView;
@@ -133,6 +134,14 @@ public class ComponentDbTester {
     return insertComponentImpl(newView(organizationDto, uuid), false, noExtraConfiguration());
   }
 
+  public ComponentDto insertApplication(OrganizationDto organizationDto) {
+    return insertComponentImpl(newApplication(organizationDto), false, noExtraConfiguration());
+  }
+
+  public ComponentDto insertApplication(OrganizationDto organizationDto, Consumer<ComponentDto> dtoPopulator) {
+    return insertComponentImpl(newApplication(organizationDto), false, dtoPopulator);
+  }
+
   private static <T> Consumer<T> noExtraConfiguration() {
     return (t) -> {
     };
index 3445b4778b4d1c89fd0c41738daee9bdfe031c9b..45c6707cc64df977c0e68de4c6eb0d76057e97c4 100644 (file)
@@ -153,6 +153,11 @@ public class ComponentTesting {
       .setQualifier(Qualifiers.VIEW);
   }
 
+  public static ComponentDto newApplication(OrganizationDto organizationDto) {
+    return newView(organizationDto.getUuid(), Uuids.createFast())
+      .setQualifier(Qualifiers.APP);
+  }
+
   public static ComponentDto newProjectCopy(String uuid, ComponentDto project, ComponentDto view) {
     checkNotNull(project.getId(), "The project need to be persisted before creating this technical project.");
     return newChildComponent(uuid, view, view)
index ebcdb291e0db70230b424cf2c4a760423e7f88e1..d0cc6054c19f865ea1de5cae36bae2fcfc0bedad 100644 (file)
@@ -205,14 +205,16 @@ public class PermissionTemplateService {
 
     String qualifier = component.qualifier();
     DefaultTemplatesResolverImpl.ResolvedDefaultTemplates resolvedDefaultTemplates = defaultTemplatesResolver.resolve(defaultTemplates);
-    if (Qualifiers.PROJECT.equals(qualifier)) {
-      return dbClient.permissionTemplateDao().selectByUuid(dbSession, resolvedDefaultTemplates.getProject());
-    } else if (Qualifiers.VIEW.equals(qualifier)) {
-      String viewDefaultTemplateUuid = resolvedDefaultTemplates.getView().orElseThrow(
-        () -> new IllegalStateException("Attempt to create a view when Governance plugin is not installed"));
-      return dbClient.permissionTemplateDao().selectByUuid(dbSession, viewDefaultTemplateUuid);
-    } else {
-      throw new IllegalArgumentException(format("Qualifier '%s' is not supported", qualifier));
+    switch (qualifier) {
+      case Qualifiers.PROJECT:
+        return dbClient.permissionTemplateDao().selectByUuid(dbSession, resolvedDefaultTemplates.getProject());
+      case Qualifiers.VIEW:
+      case Qualifiers.APP:
+        String viewDefaultTemplateUuid = resolvedDefaultTemplates.getView().orElseThrow(
+          () -> new IllegalStateException("Attempt to create a view when Governance plugin is not installed"));
+        return dbClient.permissionTemplateDao().selectByUuid(dbSession, viewDefaultTemplateUuid);
+      default:
+        throw new IllegalArgumentException(format("Qualifier '%s' is not supported", qualifier));
     }
   }
 
index 868e7bf706684373b78fcdae4d346f8b9ec7ff19..46e6fb57617ce2ec677a270502ace88409d1575a 100644 (file)
@@ -42,6 +42,7 @@ import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
+import static org.sonar.api.resources.Qualifiers.APP;
 import static org.sonar.api.resources.Qualifiers.VIEW;
 
 public class ComponentUpdaterTest {
@@ -67,7 +68,7 @@ public class ComponentUpdaterTest {
     projectIndexers);
 
   @Test
-  public void should_persist_and_index_when_creating_project() throws Exception {
+  public void persist_and_index_when_creating_project() throws Exception {
     NewComponent project = NewComponent.newComponentBuilder()
       .setKey(DEFAULT_PROJECT_KEY)
       .setName(DEFAULT_PROJECT_NAME)
@@ -96,7 +97,7 @@ public class ComponentUpdaterTest {
   }
 
   @Test
-  public void should_persist_private_flag_true_when_creating_project() throws Exception {
+  public void persist_private_flag_true_when_creating_project() throws Exception {
     OrganizationDto organization = db.organizations().insert();
     NewComponent project = NewComponent.newComponentBuilder()
       .setKey(DEFAULT_PROJECT_KEY)
@@ -110,7 +111,7 @@ public class ComponentUpdaterTest {
   }
 
   @Test
-  public void should_persist_private_flag_false_when_creating_project() throws Exception {
+  public void persist_private_flag_false_when_creating_project() throws Exception {
     OrganizationDto organization = db.organizations().insert();
     NewComponent project = NewComponent.newComponentBuilder()
       .setKey(DEFAULT_PROJECT_KEY)
@@ -138,7 +139,43 @@ public class ComponentUpdaterTest {
   }
 
   @Test
-  public void should_apply_default_permission_template() throws Exception {
+  public void persist_and_index_when_creating_view() {
+    NewComponent view = NewComponent.newComponentBuilder()
+      .setKey("view-key")
+      .setName("view-name")
+      .setQualifier(VIEW)
+      .setOrganizationUuid(db.getDefaultOrganization().getUuid())
+      .build();
+
+    ComponentDto returned = underTest.create(db.getSession(), view, null);
+
+    ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.uuid());
+    assertThat(loaded.getKey()).isEqualTo("view-key");
+    assertThat(loaded.name()).isEqualTo("view-name");
+    assertThat(loaded.qualifier()).isEqualTo("VW");
+    verify(projectIndexers).hasBeenCalled(loaded.uuid(), ProjectIndexer.Cause.PROJECT_CREATION);
+  }
+
+  @Test
+  public void create_application() {
+    NewComponent view = NewComponent.newComponentBuilder()
+      .setKey("app-key")
+      .setName("app-name")
+      .setQualifier(APP)
+      .setOrganizationUuid(db.getDefaultOrganization().getUuid())
+      .build();
+
+    ComponentDto returned = underTest.create(db.getSession(), view, null);
+
+    ComponentDto loaded = db.getDbClient().componentDao().selectByKey(db.getSession(), returned.key()).get();
+    assertThat(loaded.getKey()).isEqualTo("app-key");
+    assertThat(loaded.name()).isEqualTo("app-name");
+    assertThat(loaded.qualifier()).isEqualTo("APP");
+    verify(projectIndexers).hasBeenCalled(loaded.uuid(), ProjectIndexer.Cause.PROJECT_CREATION);
+  }
+
+  @Test
+  public void apply_default_permission_template() throws Exception {
     int userId = 42;
     NewComponent project = NewComponent.newComponentBuilder()
       .setKey(DEFAULT_PROJECT_KEY)
@@ -151,7 +188,7 @@ public class ComponentUpdaterTest {
   }
 
   @Test
-  public void should_add_project_to_user_favorites_if_project_creator_is_defined_in_permission_template() throws Exception {
+  public void add_project_to_user_favorites_if_project_creator_is_defined_in_permission_template() throws Exception {
     UserDto userDto = db.users().insertUser();
     NewComponent project = NewComponent.newComponentBuilder()
       .setKey(DEFAULT_PROJECT_KEY)
@@ -268,23 +305,4 @@ public class ComponentUpdaterTest {
         .build(),
       null);
   }
-
-  @Test
-  public void persist_and_index_when_creating_view() {
-    NewComponent view = NewComponent.newComponentBuilder()
-      .setKey("view-key")
-      .setName("view-name")
-      .setQualifier(VIEW)
-      .setOrganizationUuid(db.getDefaultOrganization().getUuid())
-      .build();
-
-    ComponentDto returned = underTest.create(db.getSession(), view, null);
-
-    ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.uuid());
-    assertThat(loaded.getKey()).isEqualTo("view-key");
-    assertThat(loaded.name()).isEqualTo("view-name");
-    assertThat(loaded.qualifier()).isEqualTo("VW");
-    assertThat(projectIndexers.hasBeenCalled(loaded.uuid(), ProjectIndexer.Cause.PROJECT_CREATION)).isTrue();
-  }
-
 }
index 007c7eb62d200aba74738fe420cacaf53e535587..6b673dfadd53536d1e8c857f04f5b3efffc9f262 100644 (file)
@@ -45,6 +45,8 @@ import org.sonar.server.tester.UserSessionRule;
 import static java.util.Collections.singletonList;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION;
+import static org.sonar.db.permission.OrganizationPermission.ADMINISTER;
+import static org.sonar.db.permission.OrganizationPermission.PROVISION_PROJECTS;
 
 public class PermissionTemplateServiceTest {
 
@@ -258,14 +260,14 @@ public class PermissionTemplateServiceTest {
     ComponentDto publicProject = dbTester.components().insertPublicProject(organization);
     UserDto user = dbTester.users().insertUser();
     ProjectPermissions.ALL
-        .forEach(perm -> dbTester.permissionTemplates().addProjectCreatorToTemplate(permissionTemplate, perm));
+      .forEach(perm -> dbTester.permissionTemplates().addProjectCreatorToTemplate(permissionTemplate, perm));
     dbTester.permissionTemplates().addProjectCreatorToTemplate(permissionTemplate, "p1");
     dbTester.organizations().setDefaultTemplates(organization, permissionTemplate.getUuid(), null);
 
     underTest.applyDefault(session, organization.getUuid(), publicProject, user.getId());
 
     assertThat(selectProjectPermissionsOfUser(user, publicProject))
-        .containsOnly("p1", UserRole.ADMIN, UserRole.ISSUE_ADMIN, GlobalPermissions.SCAN_EXECUTION);
+      .containsOnly("p1", UserRole.ADMIN, UserRole.ISSUE_ADMIN, GlobalPermissions.SCAN_EXECUTION);
   }
 
   @Test
@@ -285,6 +287,100 @@ public class PermissionTemplateServiceTest {
       .containsOnly("p1", UserRole.USER, UserRole.CODEVIEWER, UserRole.ADMIN, UserRole.ISSUE_ADMIN, GlobalPermissions.SCAN_EXECUTION);
   }
 
+  @Test
+  public void apply_template_on_view() {
+    OrganizationDto organization = dbTester.organizations().insert();
+    ComponentDto view = dbTester.components().insertView(organization);
+    PermissionTemplateDto permissionTemplate = dbTester.permissionTemplates().insertTemplate(organization);
+    GroupDto group = dbTester.users().insertGroup(organization);
+    dbTester.permissionTemplates().addGroupToTemplate(permissionTemplate, group, ADMINISTER.getKey());
+    dbTester.permissionTemplates().addGroupToTemplate(permissionTemplate, group, PROVISION_PROJECTS.getKey());
+    dbTester.organizations().setDefaultTemplates(organization, permissionTemplate.getUuid(), null);
+
+    underTest.applyDefault(session, organization.getUuid(), view, null);
+
+    assertThat(selectProjectPermissionsOfGroup(organization, group, view))
+      .containsOnly(ADMINISTER.getKey(), PROVISION_PROJECTS.getKey());
+  }
+
+  @Test
+  public void apply_default_template_on_view() {
+    OrganizationDto organization = dbTester.organizations().insert();
+    ComponentDto view = dbTester.components().insertView(organization);
+    PermissionTemplateDto projectPermissionTemplate = dbTester.permissionTemplates().insertTemplate(organization);
+    PermissionTemplateDto viewPermissionTemplate = dbTester.permissionTemplates().insertTemplate(organization);
+    GroupDto group = dbTester.users().insertGroup(organization);
+    dbTester.permissionTemplates().addGroupToTemplate(viewPermissionTemplate, group, ADMINISTER.getKey());
+    dbTester.permissionTemplates().addGroupToTemplate(viewPermissionTemplate, group, PROVISION_PROJECTS.getKey());
+    dbTester.organizations().setDefaultTemplates(organization, projectPermissionTemplate.getUuid(), viewPermissionTemplate.getUuid());
+
+    underTest.applyDefault(session, organization.getUuid(), view, null);
+
+    assertThat(selectProjectPermissionsOfGroup(organization, group, view))
+      .containsOnly(ADMINISTER.getKey(), PROVISION_PROJECTS.getKey());
+  }
+
+  @Test
+  public void apply_project_default_template_on_view_when_no_view_default_template() {
+    OrganizationDto organization = dbTester.organizations().insert();
+    ComponentDto view = dbTester.components().insertView(organization);
+    PermissionTemplateDto projectPermissionTemplate = dbTester.permissionTemplates().insertTemplate(organization);
+    GroupDto group = dbTester.users().insertGroup(organization);
+    dbTester.permissionTemplates().addGroupToTemplate(projectPermissionTemplate, group, PROVISION_PROJECTS.getKey());
+    dbTester.organizations().setDefaultTemplates(organization, projectPermissionTemplate.getUuid(), null);
+
+    underTest.applyDefault(session, organization.getUuid(), view, null);
+
+    assertThat(selectProjectPermissionsOfGroup(organization, group, view)).containsOnly(PROVISION_PROJECTS.getKey());
+  }
+
+  @Test
+  public void apply_template_on_applications() {
+    OrganizationDto organization = dbTester.organizations().insert();
+    ComponentDto application = dbTester.components().insertApplication(organization);
+    PermissionTemplateDto permissionTemplate = dbTester.permissionTemplates().insertTemplate(organization);
+    GroupDto group = dbTester.users().insertGroup(organization);
+    dbTester.permissionTemplates().addGroupToTemplate(permissionTemplate, group, ADMINISTER.getKey());
+    dbTester.permissionTemplates().addGroupToTemplate(permissionTemplate, group, PROVISION_PROJECTS.getKey());
+    dbTester.organizations().setDefaultTemplates(organization, permissionTemplate.getUuid(), null);
+
+    underTest.applyDefault(session, organization.getUuid(), application, null);
+
+    assertThat(selectProjectPermissionsOfGroup(organization, group, application))
+      .containsOnly(ADMINISTER.getKey(), PROVISION_PROJECTS.getKey());
+  }
+
+  @Test
+  public void apply_default_view_template_on_application() {
+    OrganizationDto organization = dbTester.organizations().insert();
+    ComponentDto application = dbTester.components().insertApplication(organization);
+    PermissionTemplateDto projectPermissionTemplate = dbTester.permissionTemplates().insertTemplate(organization);
+    PermissionTemplateDto viewPermissionTemplate = dbTester.permissionTemplates().insertTemplate(organization);
+    GroupDto group = dbTester.users().insertGroup(organization);
+    dbTester.permissionTemplates().addGroupToTemplate(viewPermissionTemplate, group, ADMINISTER.getKey());
+    dbTester.permissionTemplates().addGroupToTemplate(viewPermissionTemplate, group, PROVISION_PROJECTS.getKey());
+    dbTester.organizations().setDefaultTemplates(organization, projectPermissionTemplate.getUuid(), viewPermissionTemplate.getUuid());
+
+    underTest.applyDefault(session, organization.getUuid(), application, null);
+
+    assertThat(selectProjectPermissionsOfGroup(organization, group, application))
+      .containsOnly(ADMINISTER.getKey(), PROVISION_PROJECTS.getKey());
+  }
+
+  @Test
+  public void apply_project_default_template_on_application_when_no_application_default_template() {
+    OrganizationDto organization = dbTester.organizations().insert();
+    ComponentDto application = dbTester.components().insertApplication(organization);
+    PermissionTemplateDto projectPermissionTemplate = dbTester.permissionTemplates().insertTemplate(organization);
+    GroupDto group = dbTester.users().insertGroup(organization);
+    dbTester.permissionTemplates().addGroupToTemplate(projectPermissionTemplate, group, PROVISION_PROJECTS.getKey());
+    dbTester.organizations().setDefaultTemplates(organization, projectPermissionTemplate.getUuid(), null);
+
+    underTest.applyDefault(session, organization.getUuid(), application, null);
+
+    assertThat(selectProjectPermissionsOfGroup(organization, group, application)).containsOnly(PROVISION_PROJECTS.getKey());
+  }
+
   @Test
   public void apply_permission_template() {
     OrganizationDto organization = dbTester.organizations().insert();
index 392cc9b8bc285ea346278171ce1e4644c3b15c0a..4ca7b954a45686e0f97eeadd5df14902144d1c38 100644 (file)
 package org.sonar.server.permission.ws.template;
 
 import org.junit.rules.ExternalResource;
-import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.resources.ResourceType;
 import org.sonar.api.resources.ResourceTypeTree;
 import org.sonar.api.resources.ResourceTypes;
 import org.sonar.db.organization.DefaultTemplates;
 
+import static org.sonar.api.resources.Qualifiers.APP;
+import static org.sonar.api.resources.Qualifiers.PROJECT;
+import static org.sonar.api.resources.Qualifiers.VIEW;
+
 public class DefaultTemplatesResolverRule extends ExternalResource implements DefaultTemplatesResolver {
-  private static final DefaultTemplatesResolver WITH_VIEWS = new DefaultTemplatesResolverImpl(
+  private static final DefaultTemplatesResolver WITH_GOV = new DefaultTemplatesResolverImpl(
     new ResourceTypes(new ResourceTypeTree[] {
       ResourceTypeTree.builder()
-        .addType(ResourceType.builder(Qualifiers.PROJECT).build())
+        .addType(ResourceType.builder(PROJECT).build())
+        .build(),
+      ResourceTypeTree.builder()
+        .addType(ResourceType.builder(VIEW).build())
         .build(),
       ResourceTypeTree.builder()
-        .addType(ResourceType.builder(Qualifiers.VIEW).build())
-        .build()}));
-  private static final DefaultTemplatesResolver WITHOUT_VIEWS = new DefaultTemplatesResolverImpl(
+        .addType(ResourceType.builder(APP).build())
+        .build()
+    }));
+  private static final DefaultTemplatesResolver WITHOUT_GOV = new DefaultTemplatesResolverImpl(
     new ResourceTypes(new ResourceTypeTree[] {ResourceTypeTree.builder()
-      .addType(ResourceType.builder(Qualifiers.PROJECT).build())
+      .addType(ResourceType.builder(PROJECT).build())
       .build()}));
 
   private final boolean governanceInitiallyInstalled;
@@ -72,8 +79,8 @@ public class DefaultTemplatesResolverRule extends ExternalResource implements De
   @Override
   public DefaultTemplatesResolverImpl.ResolvedDefaultTemplates resolve(DefaultTemplates defaultTemplates) {
     if (governanceInstalled) {
-      return WITH_VIEWS.resolve(defaultTemplates);
+      return WITH_GOV.resolve(defaultTemplates);
     }
-    return WITHOUT_VIEWS.resolve(defaultTemplates);
+    return WITHOUT_GOV.resolve(defaultTemplates);
   }
 }