]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8134 use a single DbSession in ReportSubmitter
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Thu, 20 Oct 2016 16:40:16 +0000 (18:40 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 21 Oct 2016 13:58:39 +0000 (15:58 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/queue/ReportSubmitter.java
server/sonar-server/src/test/java/org/sonar/server/computation/queue/ReportSubmitterTest.java

index 92141581e99e6b5627aea650869fbad321303cb2..5780fedd6a0c39b207d186d2cf7ef1c414c1f894 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.computation.queue;
 
+import com.google.common.base.Optional;
 import java.io.InputStream;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
@@ -47,59 +48,50 @@ public class ReportSubmitter {
   private final CeQueue queue;
   private final UserSession userSession;
   private final ComponentService componentService;
-  private final PermissionService permissionService;
+  private final PermissionService permissionTemplateService;
   private final DbClient dbClient;
 
   public ReportSubmitter(CeQueue queue, UserSession userSession,
-    ComponentService componentService, PermissionService permissionService, DbClient dbClient) {
+                         ComponentService componentService, PermissionService permissionTemplateService, DbClient dbClient) {
     this.queue = queue;
     this.userSession = userSession;
     this.componentService = componentService;
-    this.permissionService = permissionService;
+    this.permissionTemplateService = permissionTemplateService;
     this.dbClient = dbClient;
   }
 
   public CeTask submit(String projectKey, @Nullable String projectBranch, @Nullable String projectName, InputStream reportInput) {
-    String effectiveProjectKey = ComponentKeys.createKey(projectKey, projectBranch);
-    ComponentDto project = componentService.getNullableByKey(effectiveProjectKey);
-    if (project == null) {
-      project = createProject(projectKey, projectBranch, projectName);
+    try (DbSession dbSession = dbClient.openSession(false)) {
+      String effectiveProjectKey = ComponentKeys.createKey(projectKey, projectBranch);
+      Optional<ComponentDto> opt = dbClient.componentDao().selectByKey(dbSession, effectiveProjectKey);
+      ComponentDto project = opt.or(() -> createProject(dbSession, projectKey, projectBranch, projectName));
+      userSession.checkComponentUuidPermission(SCAN_EXECUTION, project.uuid());
+      return submitReport(dbSession, reportInput, project);
     }
-
-    userSession.checkComponentPermission(SCAN_EXECUTION, projectKey);
-
-    return submitReport(reportInput, project);
   }
 
   @CheckForNull
-  private ComponentDto createProject(String projectKey, @Nullable String projectBranch, @Nullable String projectName) {
-    DbSession dbSession = dbClient.openSession(false);
-    try {
-      boolean wouldCurrentUserHaveScanPermission = permissionService.wouldCurrentUserHavePermissionWithDefaultTemplate(dbSession, SCAN_EXECUTION, projectBranch, projectKey,
-        Qualifiers.PROJECT);
-      if (!wouldCurrentUserHaveScanPermission) {
-        throw insufficientPrivilegesException();
-      }
-
-      NewComponent newProject = new NewComponent(projectKey, StringUtils.defaultIfBlank(projectName, projectKey));
-      newProject.setBranch(projectBranch);
-      newProject.setQualifier(Qualifiers.PROJECT);
-      // "provisioning" permission is check in ComponentService
-      ComponentDto project = componentService.create(dbSession, newProject);
-      permissionService.applyDefaultPermissionTemplate(dbSession, project.getKey());
-      return project;
-    } finally {
-      dbClient.closeSession(dbSession);
+  private ComponentDto createProject(DbSession dbSession, String projectKey, @Nullable String projectBranch, @Nullable String projectName) {
+    boolean wouldCurrentUserHaveScanPermission = permissionTemplateService.wouldCurrentUserHavePermissionWithDefaultTemplate(dbSession, SCAN_EXECUTION, projectBranch, projectKey,
+      Qualifiers.PROJECT);
+    if (!wouldCurrentUserHaveScanPermission) {
+      throw insufficientPrivilegesException();
     }
+
+    NewComponent newProject = new NewComponent(projectKey, StringUtils.defaultIfBlank(projectName, projectKey));
+    newProject.setBranch(projectBranch);
+    newProject.setQualifier(Qualifiers.PROJECT);
+    // "provisioning" permission is check in ComponentService
+    ComponentDto project = componentService.create(dbSession, newProject);
+    permissionTemplateService.applyDefaultPermissionTemplate(project.getKey());
+    return project;
   }
 
-  private CeTask submitReport(InputStream reportInput, ComponentDto project) {
+  private CeTask submitReport(DbSession dbSession, InputStream reportInput, ComponentDto project) {
     // the report file must be saved before submitting the task
     CeTaskSubmit.Builder submit = queue.prepareSubmit();
-    try (DbSession dbSession = dbClient.openSession(false)) {
-      dbClient.ceTaskInputDao().insert(dbSession, submit.getUuid(), reportInput);
-      dbSession.commit();
-    }
+    dbClient.ceTaskInputDao().insert(dbSession, submit.getUuid(), reportInput);
+    dbSession.commit();
 
     submit.setType(CeTaskTypes.REPORT);
     submit.setComponentUuid(project.uuid());
index 1de8f34a4bc0a24c64e3f0ec42b048172cdb5ddf..69265ea1655cddfc1603c6b36239a0b9b23bb5db 100644 (file)
@@ -55,10 +55,10 @@ import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION;
 
 public class ReportSubmitterTest {
 
-  static final String PROJECT_KEY = "MY_PROJECT";
-  static final String PROJECT_UUID = "P1";
-  static final String PROJECT_NAME = "My Project";
-  static final String TASK_UUID = "TASK_1";
+  private static final String PROJECT_KEY = "MY_PROJECT";
+  private static final String PROJECT_UUID = "P1";
+  private static final String PROJECT_NAME = "My Project";
+  private static final String TASK_UUID = "TASK_1";
 
   @Rule
   public ExpectedException thrown = ExpectedException.none();
@@ -77,18 +77,17 @@ public class ReportSubmitterTest {
   @Test
   public void submit_a_report_on_existing_project() {
     userSession.setGlobalPermissions(SCAN_EXECUTION);
-
+    ComponentDto project = dbTester.components().insertProject();
     when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder(TASK_UUID));
-    when(componentService.getNullableByKey(PROJECT_KEY)).thenReturn(new ComponentDto().setUuid(PROJECT_UUID));
 
-    underTest.submit(PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
+    underTest.submit(project.getKey(), null, project.name(), IOUtils.toInputStream("{binary}"));
 
     verifyReportIsPersisted(TASK_UUID);
     verifyZeroInteractions(permissionService);
     verify(queue).submit(argThat(new TypeSafeMatcher<CeTaskSubmit>() {
       @Override
       protected boolean matchesSafely(CeTaskSubmit submit) {
-        return submit.getType().equals(CeTaskTypes.REPORT) && submit.getComponentUuid().equals(PROJECT_UUID) &&
+        return submit.getType().equals(CeTaskTypes.REPORT) && submit.getComponentUuid().equals(project.uuid()) &&
           submit.getUuid().equals(TASK_UUID);
       }
 
@@ -104,7 +103,6 @@ public class ReportSubmitterTest {
     userSession.setGlobalPermissions(SCAN_EXECUTION, PROVISIONING);
 
     when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder(TASK_UUID));
-    when(componentService.getNullableByKey(PROJECT_KEY)).thenReturn(null);
     when(componentService.create(any(DbSession.class), any(NewComponent.class))).thenReturn(new ComponentDto().setUuid(PROJECT_UUID).setKey(PROJECT_KEY));
     when(permissionService.wouldCurrentUserHavePermissionWithDefaultTemplate(any(DbSession.class), eq(SCAN_EXECUTION), anyString(), eq(PROJECT_KEY), eq(Qualifiers.PROJECT)))
       .thenReturn(true);
@@ -132,7 +130,6 @@ public class ReportSubmitterTest {
     userSession.setGlobalPermissions(SCAN_EXECUTION, PROVISIONING);
 
     when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder(TASK_UUID));
-    when(componentService.getNullableByKey(PROJECT_KEY)).thenReturn(null);
     when(componentService.create(any(DbSession.class), any(NewComponent.class))).thenReturn(new ComponentDto().setUuid(PROJECT_UUID).setKey(PROJECT_KEY));
     when(permissionService.wouldCurrentUserHavePermissionWithDefaultTemplate(any(DbSession.class), eq(SCAN_EXECUTION), anyString(), eq(PROJECT_KEY), eq(Qualifiers.PROJECT)))
       .thenReturn(true);
@@ -144,24 +141,24 @@ public class ReportSubmitterTest {
 
   @Test
   public void submit_a_report_on_existing_project_with_global_scan_permission() {
+    ComponentDto project = dbTester.components().insertProject();
     userSession.setGlobalPermissions(SCAN_EXECUTION);
 
     when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder(TASK_UUID));
-    when(componentService.getNullableByKey(PROJECT_KEY)).thenReturn(new ComponentDto().setUuid(PROJECT_UUID));
 
-    underTest.submit(PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
+    underTest.submit(project.getKey(), null, project.name(), IOUtils.toInputStream("{binary}"));
 
     verify(queue).submit(any(CeTaskSubmit.class));
   }
 
   @Test
   public void submit_a_report_on_existing_project_with_project_scan_permission() {
-    userSession.addProjectPermissions(SCAN_EXECUTION, PROJECT_KEY);
+    ComponentDto project = dbTester.components().insertProject();
+    userSession.addProjectUuidPermissions(SCAN_EXECUTION, project.uuid());
 
     when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder(TASK_UUID));
-    when(componentService.getNullableByKey(PROJECT_KEY)).thenReturn(new ComponentDto().setUuid(PROJECT_UUID));
 
-    underTest.submit(PROJECT_KEY, null, PROJECT_NAME, IOUtils.toInputStream("{binary}"));
+    underTest.submit(project.getKey(), null, project.name(), IOUtils.toInputStream("{binary}"));
 
     verify(queue).submit(any(CeTaskSubmit.class));
   }
@@ -176,10 +173,9 @@ public class ReportSubmitterTest {
 
   @Test
   public void fail_with_forbidden_exception_on_new_project_when_only_project_scan_permission() {
-    userSession.addProjectPermissions(SCAN_EXECUTION, PROJECT_KEY);
+    userSession.addProjectUuidPermissions(SCAN_EXECUTION, PROJECT_UUID);
 
     when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder(TASK_UUID));
-    when(componentService.getNullableByKey(PROJECT_KEY)).thenReturn(null);
     when(componentService.create(any(DbSession.class), any(NewComponent.class))).thenReturn(new ComponentDto().setUuid(PROJECT_UUID).setKey(PROJECT_KEY));
 
     thrown.expect(ForbiddenException.class);