]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9087 PersistComponentsStep ensures consistency of private flag
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 11 Apr 2017 16:18:27 +0000 (18:18 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 27 Apr 2017 12:25:54 +0000 (14:25 +0200)
across the whole tree: all components of the tree have the same private flag

server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistComponentsStep.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ReportPersistComponentsStepTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ViewsPersistComponentsStepTest.java

index c32afa645098a4a4517558c65aae37a5ba5a7a84..8b76b57cdedb6bde6112bf3140650fda379da7eb 100644 (file)
@@ -92,12 +92,14 @@ public class PersistComponentsStep implements ComputationStep {
       dbClient.componentDao().resetBChangedForRootComponentUuid(dbSession, projectUuid);
 
       Map<String, ComponentDto> existingDtosByKeys = indexExistingDtosByKey(dbSession);
+      boolean isRootPrivate = isRootPrivate(treeRootHolder.getRoot().getKey(), existingDtosByKeys);
       // Insert or update the components in database. They are removed from existingDtosByKeys
       // at the same time.
       new PathAwareCrawler<>(new PersistComponentStepsVisitor(existingDtosByKeys, dbSession))
         .visit(treeRootHolder.getRoot());
 
       disableRemainingComponents(dbSession, existingDtosByKeys.values());
+      ensureConsistentVisibility(dbSession, projectUuid, isRootPrivate);
 
       dbSession.commit();
     }
@@ -112,6 +114,15 @@ public class PersistComponentsStep implements ComputationStep {
     disabledComponentsHolder.setUuids(uuids);
   }
 
+  private void ensureConsistentVisibility(DbSession dbSession, String projectUuid, boolean isRootPrivate) {
+    dbClient.componentDao().setPrivateForRootComponentUuid(dbSession, projectUuid, isRootPrivate);
+  }
+
+  private static boolean isRootPrivate(String rootKey, Map<String, ComponentDto> existingDtosByKeys) {
+    ComponentDto projectDto = existingDtosByKeys.get(rootKey);
+    return projectDto == null ? false /*FIXME actually use default configured for current organization*/ : projectDto.isPrivate();
+  }
+
   /**
    * Returns a mutable map of the components currently persisted in database for the project, including
    * disabled components.
index 264025d667c8999a64353ea2e25ad6bf3580390f..48558fc203e2a996dcfcb52032d37047fc35dde4 100644 (file)
@@ -22,6 +22,8 @@ package org.sonar.server.computation.task.projectanalysis.step;
 import com.google.common.base.Optional;
 import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.Random;
+import java.util.stream.Stream;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -31,11 +33,13 @@ 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.organization.OrganizationDto;
 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
 import org.sonar.server.computation.task.projectanalysis.component.Component;
 import org.sonar.server.computation.task.projectanalysis.component.FileAttributes;
 import org.sonar.server.computation.task.projectanalysis.component.MutableDbIdsRepositoryRule;
 import org.sonar.server.computation.task.projectanalysis.component.MutableDisabledComponentsHolder;
+import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
 import org.sonar.server.computation.task.step.ComputationStep;
 
@@ -46,6 +50,9 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 import static org.sonar.db.component.ComponentDto.UUID_PATH_OF_ROOT;
 import static org.sonar.db.component.ComponentDto.UUID_PATH_SEPARATOR;
+import static org.sonar.db.component.ComponentTesting.newDirectory;
+import static org.sonar.db.component.ComponentTesting.newModuleDto;
+import static org.sonar.db.component.ComponentTesting.newProjectDto;
 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.DIRECTORY;
 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.FILE;
 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT;
@@ -233,7 +240,7 @@ public class ReportPersistComponentsStepTest extends BaseStepTest {
   @Test
   public void persist_only_new_components() {
     // Project and module already exists
-    ComponentDto project = ComponentTesting.newProjectDto(dbTester.getDefaultOrganization(), "ABCD").setKey(PROJECT_KEY).setName("Project");
+    ComponentDto project = newProjectDto(dbTester.getDefaultOrganization(), "ABCD").setKey(PROJECT_KEY).setName("Project");
     dbClient.componentDao().insert(dbTester.getSession(), project);
     ComponentDto module = ComponentTesting.newModuleDto("BCDE", project).setKey(MODULE_KEY).setName("Module");
     dbClient.componentDao().insert(dbTester.getSession(), module);
@@ -387,7 +394,7 @@ public class ReportPersistComponentsStepTest extends BaseStepTest {
 
   @Test
   public void nothing_to_persist() {
-    ComponentDto project = ComponentTesting.newProjectDto(dbTester.organizations().insert(), "ABCD").setKey(PROJECT_KEY).setName("Project");
+    ComponentDto project = newProjectDto(dbTester.organizations().insert(), "ABCD").setKey(PROJECT_KEY).setName("Project");
     dbClient.componentDao().insert(dbTester.getSession(), project);
     ComponentDto module = ComponentTesting.newModuleDto("BCDE", project).setKey(MODULE_KEY).setName("Module");
     dbClient.componentDao().insert(dbTester.getSession(), module);
@@ -462,7 +469,7 @@ public class ReportPersistComponentsStepTest extends BaseStepTest {
 
   @Test
   public void update_module_name_and_description() {
-    ComponentDto project = ComponentTesting.newProjectDto(dbTester.getDefaultOrganization(), "ABCD").setKey(PROJECT_KEY).setName("Project").setDescription("Project description");
+    ComponentDto project = newProjectDto(dbTester.getDefaultOrganization(), "ABCD").setKey(PROJECT_KEY).setName("Project").setDescription("Project description");
     dbClient.componentDao().insert(dbTester.getSession(), project);
     ComponentDto module = ComponentTesting.newModuleDto("BCDE", project).setKey(MODULE_KEY).setName("Module");
     dbClient.componentDao().insert(dbTester.getSession(), module);
@@ -501,7 +508,7 @@ public class ReportPersistComponentsStepTest extends BaseStepTest {
 
   @Test
   public void update_module_path() {
-    ComponentDto project = ComponentTesting.newProjectDto(dbTester.organizations().insert(), "ABCD").setKey(PROJECT_KEY).setName("Project");
+    ComponentDto project = newProjectDto(dbTester.organizations().insert(), "ABCD").setKey(PROJECT_KEY).setName("Project");
     dbClient.componentDao().insert(dbTester.getSession(), project);
     ComponentDto module = ComponentTesting.newModuleDto("BCDE", project).setKey(MODULE_KEY).setName("Module").setPath("path");
     dbClient.componentDao().insert(dbTester.getSession(), module);
@@ -528,7 +535,7 @@ public class ReportPersistComponentsStepTest extends BaseStepTest {
 
   @Test
   public void update_module_uuid_when_moving_a_module() {
-    ComponentDto project = ComponentTesting.newProjectDto(dbTester.getDefaultOrganization(), "ABCD").setKey(PROJECT_KEY).setName("Project");
+    ComponentDto project = newProjectDto(dbTester.getDefaultOrganization(), "ABCD").setKey(PROJECT_KEY).setName("Project");
     dbClient.componentDao().insert(dbTester.getSession(), project);
     ComponentDto moduleA = ComponentTesting.newModuleDto("EDCB", project)
       .setKey("MODULE_A")
@@ -605,7 +612,7 @@ public class ReportPersistComponentsStepTest extends BaseStepTest {
   @Test
   public void do_not_update_created_at_on_existing_component() {
     Date oldDate = DateUtils.parseDate("2015-01-01");
-    ComponentDto project = ComponentTesting.newProjectDto(dbTester.getDefaultOrganization(), "ABCD").setKey(PROJECT_KEY).setName("Project").setCreatedAt(oldDate);
+    ComponentDto project = newProjectDto(dbTester.getDefaultOrganization(), "ABCD").setKey(PROJECT_KEY).setName("Project").setCreatedAt(oldDate);
     dbClient.componentDao().insert(dbTester.getSession(), project);
     ComponentDto module = ComponentTesting.newModuleDto("BCDE", project).setKey(MODULE_KEY).setName("Module").setPath("path").setCreatedAt(oldDate);
     dbClient.componentDao().insert(dbTester.getSession(), module);
@@ -623,7 +630,7 @@ public class ReportPersistComponentsStepTest extends BaseStepTest {
 
   @Test
   public void persist_components_that_were_previously_removed() {
-    ComponentDto project = ComponentTesting.newProjectDto(dbTester.organizations().insert(), "ABCD").setKey(PROJECT_KEY).setName("Project");
+    ComponentDto project = newProjectDto(dbTester.organizations().insert(), "ABCD").setKey(PROJECT_KEY).setName("Project");
     dbClient.componentDao().insert(dbTester.getSession(), project);
     ComponentDto removedModule = ComponentTesting.newModuleDto("BCDE", project)
       .setKey(MODULE_KEY)
@@ -725,7 +732,7 @@ public class ReportPersistComponentsStepTest extends BaseStepTest {
 
   @Test
   public void update_module_uuid_when_reactivating_removed_component() {
-    ComponentDto project = ComponentTesting.newProjectDto(dbTester.getDefaultOrganization(), "ABCD").setKey(PROJECT_KEY).setName("Project");
+    ComponentDto project = newProjectDto(dbTester.getDefaultOrganization(), "ABCD").setKey(PROJECT_KEY).setName("Project");
     dbClient.componentDao().insert(dbTester.getSession(), project);
     ComponentDto module = ComponentTesting.newModuleDto("BCDE", project).setKey(MODULE_KEY).setName("Module");
     ComponentDto removedModule = ComponentTesting.newModuleDto("EDCD", project).setKey("REMOVED_MODULE_KEY").setName("Removed Module").setEnabled(false);
@@ -777,4 +784,70 @@ public class ReportPersistComponentsStepTest extends BaseStepTest {
     assertThat(fileReloaded.isEnabled()).isTrue();
   }
 
+  @Test
+  public void persists_new_components_as_private_if_root_does_not_exist_yet_out_of_functional_transaction() {
+    treeRootHolder.setRoot(createSampleProjectComponentTree("ABCD", PROJECT_KEY));
+
+    underTest.execute();
+
+    Stream.of("ABCD", "BCDE", "DEFG", "CDEF")
+      .forEach(uuid -> assertThat(dbClient.componentDao().selectByUuid(dbTester.getSession(), uuid).get().isPrivate()).isFalse());
+  }
+
+  @Test
+  public void persists_new_components_with_visibility_of_root_in_db_out_of_functional_transaction() {
+    boolean isRootPrivate = new Random().nextBoolean();
+    OrganizationDto organization = dbTester.organizations().insert();
+    ComponentDto project = newProjectDto(organization, "ABCD").setKey(PROJECT_KEY).setName("Project").setPrivate(isRootPrivate);
+    dbTester.components().insertComponent(project);
+    treeRootHolder.setRoot(createSampleProjectComponentTree(project));
+
+    underTest.execute();
+
+    Stream.of("ABCD", "BCDE", "DEFG", "CDEF")
+      .forEach(uuid -> assertThat(dbClient.componentDao().selectByUuid(dbTester.getSession(), uuid).get().isPrivate())
+        .describedAs("for uuid " + uuid)
+        .isEqualTo(isRootPrivate));
+  }
+
+  @Test
+  public void persists_existing_components_with_visibility_of_root_in_db_out_of_functional_transaction() {
+    boolean isRootPrivate = new Random().nextBoolean();
+    OrganizationDto organization = dbTester.organizations().insert();
+    ComponentDto project = newProjectDto(organization, "ABCD").setKey(PROJECT_KEY).setName("Project").setPrivate(isRootPrivate);
+    dbTester.components().insertComponent(project);
+    ComponentDto module = newModuleDto(project).setUuid("BCDE").setKey("MODULE").setPrivate(!isRootPrivate);
+    dbTester.components().insertComponent(module);
+    dbTester.components().insertComponent(newDirectory(module, "DEFG", "Directory").setKey("DIR").setPrivate(isRootPrivate));
+    treeRootHolder.setRoot(createSampleProjectComponentTree(project));
+
+    underTest.execute();
+
+    Stream.of("ABCD", "BCDE", "BCDE", "BCDE")
+      .forEach(uuid -> assertThat(dbClient.componentDao().selectByUuid(dbTester.getSession(), uuid).get().isPrivate())
+        .describedAs("for uuid " + uuid)
+        .isEqualTo(isRootPrivate));
+  }
+
+  private ReportComponent createSampleProjectComponentTree(ComponentDto project) {
+    return createSampleProjectComponentTree(project.uuid(), project.key());
+  }
+
+  private ReportComponent createSampleProjectComponentTree(String projectUuid, String projectKey) {
+    return builder(PROJECT, 1).setUuid(projectUuid).setKey(projectKey)
+      .setName("Project")
+      .addChildren(
+        builder(Component.Type.MODULE, 2).setUuid("BCDE").setKey("MODULE")
+          .setName("Module")
+          .addChildren(
+            builder(Component.Type.DIRECTORY, 3).setUuid("DEFG").setKey("DIR")
+              .setPath("Directory")
+              .addChildren(
+                builder(FILE, 4).setUuid("CDEF").setKey("FILE")
+                  .setPath("file")
+                  .build())
+              .build())
+          .build())
+      .build();
+  }
 }
index ebb89f81bf210eff6aab4cc5c0c0d8a8e77e61f6..b230172d5097c81d7b817e0cb9925d5db55b3e86 100644 (file)
@@ -21,6 +21,8 @@ package org.sonar.server.computation.task.projectanalysis.step;
 
 import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.Random;
+import java.util.stream.Stream;
 import javax.annotation.Nullable;
 import org.junit.Before;
 import org.junit.Rule;
@@ -47,7 +49,9 @@ import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
+import static org.sonar.db.component.ComponentTesting.newProjectCopy;
 import static org.sonar.db.component.ComponentTesting.newProjectDto;
+import static org.sonar.db.component.ComponentTesting.newSubView;
 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT_VIEW;
 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.SUBVIEW;
 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.VIEW;
@@ -348,6 +352,75 @@ public class ViewsPersistComponentsStepTest extends BaseStepTest {
     assertThat(subViewReloaded.getCopyResourceUuid()).isEqualTo("NEW_COPY");
   }
 
+  @Test
+  public void persists_new_components_as_public_if_root_does_not_exist_yet_out_of_functional_transaction() {
+    ComponentDto project = dbTester.components().insertComponent(newProjectDto(dbTester.organizations().insert()));
+    treeRootHolder.setRoot(
+      createViewBuilder()
+        .addChildren(
+          createSubView1Builder(null)
+            .addChildren(
+              createProjectView1Builder(project, null).build())
+            .build())
+        .build());
+
+    underTest.execute();
+
+    Stream.of(VIEW_UUID, SUBVIEW_1_UUID, PROJECT_VIEW_1_UUID)
+      .forEach(uuid -> assertThat(dbClient.componentDao().selectByUuid(dbTester.getSession(), uuid).get().isPrivate()).isFalse());
+  }
+
+  @Test
+  public void persists_new_components_with_visibility_of_root_in_db_out_of_functional_transaction() {
+    boolean isRootPrivate = new Random().nextBoolean();
+    ComponentDto project = dbTester.components().insertComponent(newProjectDto(dbTester.organizations().insert()));
+    OrganizationDto organization = dbTester.organizations().insert();
+    ComponentDto view = newViewDto(organization).setUuid(VIEW_UUID).setKey(VIEW_KEY).setName("View").setPrivate(isRootPrivate);
+    dbTester.components().insertComponent(view);
+    treeRootHolder.setRoot(
+      createViewBuilder()
+        .addChildren(
+          createSubView1Builder(null)
+            .addChildren(
+              createProjectView1Builder(project, null).build())
+            .build())
+        .build());
+
+    underTest.execute();
+
+    Stream.of(VIEW_UUID, SUBVIEW_1_UUID, PROJECT_VIEW_1_UUID)
+      .forEach(uuid -> assertThat(dbClient.componentDao().selectByUuid(dbTester.getSession(), uuid).get().isPrivate())
+        .describedAs("for uuid " + uuid)
+        .isEqualTo(isRootPrivate));
+  }
+
+  @Test
+  public void persists_existing_components_with_visibility_of_root_in_db_out_of_functional_transaction() {
+    boolean isRootPrivate = new Random().nextBoolean();
+    ComponentDto project = dbTester.components().insertComponent(newProjectDto(dbTester.organizations().insert()));
+    OrganizationDto organization = dbTester.organizations().insert();
+    ComponentDto view = newViewDto(organization).setUuid(VIEW_UUID).setKey(VIEW_KEY).setName("View").setPrivate(isRootPrivate);
+    dbTester.components().insertComponent(view);
+    ComponentDto subView = newSubView(view).setUuid("BCDE").setKey("MODULE").setPrivate(!isRootPrivate);
+    dbTester.components().insertComponent(subView);
+    dbTester.components().insertComponent(newProjectCopy("DEFG", project, view).setKey("DIR").setPrivate(isRootPrivate));
+    treeRootHolder.setRoot(
+      createViewBuilder()
+        .addChildren(
+          createSubView1Builder(null)
+            .addChildren(
+              createProjectView1Builder(project, null).build())
+            .build())
+        .build());
+
+    underTest.execute();
+
+    Stream.of(VIEW_UUID, SUBVIEW_1_UUID, PROJECT_VIEW_1_UUID, subView.uuid(), "DEFG")
+      .forEach(uuid -> assertThat(dbClient.componentDao().selectByUuid(dbTester.getSession(), uuid).get().isPrivate())
+        .describedAs("for uuid " + uuid)
+        .isEqualTo(isRootPrivate));
+  }
+
   private static ViewsComponent.Builder createViewBuilder() {
     return builder(VIEW, VIEW_KEY)
       .setUuid(VIEW_UUID)