]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10223 add organizationsEnabled flag to Organization
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 27 Dec 2017 13:38:27 +0000 (14:38 +0100)
committerJanos Gyerik <janos.gyerik@sonarsource.com>
Fri, 5 Jan 2018 14:49:51 +0000 (15:49 +0100)
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/Organization.java
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStep.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImplTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderRule.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/OrganizationTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/DefaultAssigneeTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStepTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/VerifyBillingStepTest.java

index f2de3e7a4231bcc68d6ab08db8a80a23b7895ca8..c35b7ce398a64e70f24bdd0dbd714b1a3140defd 100644 (file)
@@ -30,12 +30,14 @@ public class Organization {
   private final String key;
   private final String name;
   private final String defaultQualityGateUuid;
+  private final boolean organizationsEnabled;
 
-  private Organization(String uuid, String key, String name, String defaultQualityGateUuid) {
+  private Organization(String uuid, String key, String name, String defaultQualityGateUuid, boolean organizationsEnabled) {
     this.uuid = requireNonNull(uuid, "uuid can't be null");
     this.key = requireNonNull(key, "key can't be null");
     this.name = requireNonNull(name, "name can't be null");
     this.defaultQualityGateUuid = requireNonNull(defaultQualityGateUuid, "defaultQualityGateUuid can't be null");
+    this.organizationsEnabled = organizationsEnabled;
   }
 
   public String getUuid() {
@@ -54,6 +56,10 @@ public class Organization {
     return defaultQualityGateUuid;
   }
 
+  public boolean isOrganizationsEnabled() {
+    return organizationsEnabled;
+  }
+
   @Override
   public boolean equals(Object o) {
     if (this == o) {
@@ -78,6 +84,7 @@ public class Organization {
       ", key='" + key + '\'' +
       ", name='" + name + '\'' +
       ", defaultQualityGateUuid='" + defaultQualityGateUuid + '\'' +
+      ", organizationsEnabled=" + organizationsEnabled +
       '}';
   }
 
@@ -89,8 +96,13 @@ public class Organization {
       .setDefaultQualityGateUuid(defaultQualityGateUuid);
   }
 
-  public static Organization from(OrganizationDto organizationDto) {
-    return new Organization(organizationDto.getUuid(), organizationDto.getKey(), organizationDto.getName(), organizationDto.getDefaultQualityGateUuid());
+  public static Organization from(OrganizationDto organizationDto, boolean organizationsEnabled) {
+    return new Organization(
+        organizationDto.getUuid(),
+        organizationDto.getKey(),
+        organizationDto.getName(),
+        organizationDto.getDefaultQualityGateUuid(),
+        organizationsEnabled);
   }
 
 }
index 1010269638c43787f177e99504323e69ac9bf027..be38b184e74526a1bb167979c5b5e1ce81995dd1 100644 (file)
@@ -48,6 +48,7 @@ import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader
 import org.sonar.server.computation.task.projectanalysis.component.BranchLoader;
 import org.sonar.server.computation.task.step.ComputationStep;
 import org.sonar.server.organization.DefaultOrganizationProvider;
+import org.sonar.server.organization.OrganizationFlags;
 import org.sonar.server.qualityprofile.QualityProfile;
 
 import static com.google.common.base.Preconditions.checkState;
@@ -66,9 +67,10 @@ public class LoadReportAnalysisMetadataHolderStep implements ComputationStep {
   private final DbClient dbClient;
   private final BranchLoader branchLoader;
   private final PluginRepository pluginRepository;
+  private final OrganizationFlags organizationFlags;
 
   public LoadReportAnalysisMetadataHolderStep(CeTask ceTask, BatchReportReader reportReader, MutableAnalysisMetadataHolder analysisMetadata,
-    DefaultOrganizationProvider defaultOrganizationProvider, DbClient dbClient, BranchLoader branchLoader, PluginRepository pluginRepository) {
+    DefaultOrganizationProvider defaultOrganizationProvider, DbClient dbClient, BranchLoader branchLoader, PluginRepository pluginRepository, OrganizationFlags organizationFlags) {
     this.ceTask = ceTask;
     this.reportReader = reportReader;
     this.analysisMetadata = analysisMetadata;
@@ -76,6 +78,7 @@ public class LoadReportAnalysisMetadataHolderStep implements ComputationStep {
     this.dbClient = dbClient;
     this.branchLoader = branchLoader;
     this.pluginRepository = pluginRepository;
+    this.organizationFlags = organizationFlags;
   }
 
   @Override
@@ -205,7 +208,7 @@ public class LoadReportAnalysisMetadataHolderStep implements ComputationStep {
     try (DbSession dbSession = dbClient.openSession(false)) {
       Optional<OrganizationDto> organizationDto = dbClient.organizationDao().selectByUuid(dbSession, organizationUuid);
       checkState(organizationDto.isPresent(), "Organization with uuid '%s' can't be found", organizationUuid);
-      return Organization.from(organizationDto.get());
+      return Organization.from(organizationDto.get(), organizationFlags.isEnabled(dbSession));
     }
   }
 
index 513fa54b3a5dfd7796ea4b3d8a388e3f865d89c4..a8f9b13dccbd260ecb507d17f8385d3fd7175b10 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.computation.task.projectanalysis.analysis;
 
+import java.util.Random;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
@@ -62,7 +63,9 @@ public class AnalysisMetadataHolderImplTest {
 
   @Test
   public void setOrganization_throws_ISE_if_called_twice() {
-    Organization organization = Organization.from(new OrganizationDto().setUuid("uuid").setKey("key").setName("name").setDefaultQualityGateUuid("anyuuidr"));
+    Organization organization = Organization.from(
+      new OrganizationDto().setUuid("uuid").setKey("key").setName("name").setDefaultQualityGateUuid("anyuuidr"),
+      new Random().nextBoolean());
     underTest.setOrganization(organization);
 
     expectedException.expect(IllegalStateException.class);
index 1be5bbb5db0d1323e987640f2f81fa565c8044e3..c1b27fce6a22c769f3a8604eec1502da43e29fc3 100644 (file)
@@ -64,7 +64,13 @@ public class AnalysisMetadataHolderRule extends ExternalResource implements Muta
 
   public AnalysisMetadataHolderRule setOrganizationUuid(String uuid, String defaultQualityGateUuid) {
     requireNonNull(uuid, "organization uuid can't be null");
-    this.organization.setProperty(Organization.from(new OrganizationDto().setUuid(uuid).setKey("key_" + uuid).setName("name_" + uuid).setDefaultQualityGateUuid(defaultQualityGateUuid)));
+    this.organization.setProperty(Organization.from(
+      new OrganizationDto()
+        .setUuid(uuid)
+        .setKey("key_" + uuid)
+        .setName("name_" + uuid)
+        .setDefaultQualityGateUuid(defaultQualityGateUuid),
+      false));
     return this;
   }
 
index 75c251936a8b07f6cca33e346d57f319ba42118e..d139d768f659191a40fbb15eae0eb0c6c7e8c4a6 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.computation.task.projectanalysis.analysis;
 
+import java.util.Random;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
@@ -36,7 +37,7 @@ public class OrganizationTest {
   public void build_throws_NPE_if_dto_is_null() {
     expectedException.expect(NullPointerException.class);
 
-    Organization.from(null);
+    Organization.from(null, new Random().nextBoolean());
   }
 
   @Test
@@ -44,17 +45,17 @@ public class OrganizationTest {
     expectedException.expect(NullPointerException.class);
     expectedException.expectMessage("uuid can't be null");
 
-    Organization.from(underTest);
+    Organization.from(underTest, new Random().nextBoolean());
   }
 
   @Test
   public void build_throws_NPE_if_key_is_null() {
     underTest.setUuid("uuid");
-    
+
     expectedException.expect(NullPointerException.class);
     expectedException.expectMessage("key can't be null");
 
-    Organization.from(underTest);
+    Organization.from(underTest, new Random().nextBoolean());
   }
 
   @Test
@@ -64,52 +65,68 @@ public class OrganizationTest {
     expectedException.expect(NullPointerException.class);
     expectedException.expectMessage("defaultQualityGateUuid can't be null");
 
-    Organization.from(underTest);
+    Organization.from(underTest, new Random().nextBoolean());
   }
 
   @Test
   public void build_throws_NPE_if_name_is_null() {
     underTest.setUuid("uuid").setKey("key");
-    
+
     expectedException.expect(NullPointerException.class);
     expectedException.expectMessage("name can't be null");
 
-    Organization.from(underTest);
+    Organization.from(underTest, new Random().nextBoolean());
   }
 
   @Test
   public void verify_getters() {
-    Organization organization = Organization.from(underTest.setUuid("uuid").setKey("key").setName("name").setDefaultQualityGateUuid("uuid1"));
+    boolean organizationsEnabled = new Random().nextBoolean();
+    Organization organization = Organization.from(
+      underTest.setUuid("uuid").setKey("key").setName("name").setDefaultQualityGateUuid("uuid1"),
+        organizationsEnabled);
 
     assertThat(organization.getUuid()).isEqualTo("uuid");
     assertThat(organization.getKey()).isEqualTo("key");
     assertThat(organization.getName()).isEqualTo("name");
     assertThat(organization.getDefaultQualityGateUuid()).isEqualTo("uuid1");
+    assertThat(organization.isOrganizationsEnabled()).isEqualTo(organizationsEnabled);
   }
 
   @Test
   public void verify_toString() {
-    Organization organization = Organization.from(underTest.setUuid("uuid").setKey("key").setName("name").setDefaultQualityGateUuid("uuid1"));
+    Organization organization = Organization.from(
+      underTest.setUuid("uuid").setKey("key").setName("name").setDefaultQualityGateUuid("uuid1"),
+      false);
 
-    assertThat(organization.toString()).isEqualTo("Organization{uuid='uuid', key='key', name='name', defaultQualityGateUuid='uuid1'}");
+    assertThat(organization.toString()).isEqualTo("Organization{uuid='uuid', key='key', name='name', defaultQualityGateUuid='uuid1', organizationsEnabled=false}");
   }
 
   @Test
   public void equals_is_based_on_uuid_only() {
-    Organization organization = Organization.from(underTest.setUuid("uuid").setKey("key").setName("name").setDefaultQualityGateUuid("uuid1"));
-
-    assertThat(organization).isEqualTo(Organization.from(underTest.setUuid("uuid").setKey("key").setName("name").setDefaultQualityGateUuid("uuid1")));
-    assertThat(organization).isEqualTo(Organization.from(underTest.setUuid("uuid").setKey("other key").setName("name").setDefaultQualityGateUuid("uuid1")));
-    assertThat(organization).isEqualTo(Organization.from(underTest.setUuid("uuid").setKey("key").setName("other name").setDefaultQualityGateUuid("uuid1")));
-    assertThat(organization).isEqualTo(Organization.from(underTest.setUuid("uuid").setKey("key").setName("name").setDefaultQualityGateUuid("other uuid")));
-    assertThat(organization).isNotEqualTo(Organization.from(underTest.setUuid("other uuid").setKey("key").setName("name").setDefaultQualityGateUuid("uuid1")));
+    boolean organizationsEnabled = new Random().nextBoolean();
+    Organization organization = Organization.from(
+      underTest.setUuid("uuid").setKey("key").setName("name").setDefaultQualityGateUuid("uuid1"),
+      organizationsEnabled);
+
+    assertThat(organization).isEqualTo(Organization.from(underTest.setUuid("uuid").setKey("key").setName("name").setDefaultQualityGateUuid("uuid1"), organizationsEnabled));
+    assertThat(organization).isEqualTo(Organization.from(underTest.setUuid("uuid").setKey("key").setName("name").setDefaultQualityGateUuid("uuid1"), !organizationsEnabled));
+    assertThat(organization).isEqualTo(Organization.from(underTest.setUuid("uuid").setKey("other key").setName("name").setDefaultQualityGateUuid("uuid1"), organizationsEnabled));
+    assertThat(organization).isEqualTo(Organization.from(underTest.setUuid("uuid").setKey("other key").setName("name").setDefaultQualityGateUuid("uuid1"), !organizationsEnabled));
+    assertThat(organization).isEqualTo(Organization.from(underTest.setUuid("uuid").setKey("key").setName("other name").setDefaultQualityGateUuid("uuid1"), organizationsEnabled));
+    assertThat(organization).isEqualTo(Organization.from(underTest.setUuid("uuid").setKey("key").setName("other name").setDefaultQualityGateUuid("uuid1"), !organizationsEnabled));
+    assertThat(organization).isEqualTo(Organization.from(underTest.setUuid("uuid").setKey("key").setName("name").setDefaultQualityGateUuid("other uuid"), organizationsEnabled));
+    assertThat(organization).isEqualTo(Organization.from(underTest.setUuid("uuid").setKey("key").setName("name").setDefaultQualityGateUuid("other uuid"), !organizationsEnabled));
+    assertThat(organization)
+      .isNotEqualTo(Organization.from(underTest.setUuid("other uuid").setKey("key").setName("name").setDefaultQualityGateUuid("uuid1"), organizationsEnabled));
+    assertThat(organization).isNotEqualTo(
+      Organization.from(underTest.setUuid("other uuid").setKey("key").setName("name").setDefaultQualityGateUuid("uuid1"), !organizationsEnabled));
     assertThat(organization).isNotEqualTo(null);
     assertThat(organization).isNotEqualTo("toto");
   }
 
   @Test
   public void hashcode_is_based_on_uuid_only() {
-    Organization organization = Organization.from(underTest.setUuid("uuid").setKey("key").setName("name").setDefaultQualityGateUuid("uuid1"));
+    Organization organization = Organization.from(underTest.setUuid("uuid").setKey("key").setName("name").setDefaultQualityGateUuid("uuid1"), new Random().nextBoolean());
 
     assertThat(organization.hashCode()).isEqualTo("uuid".hashCode());
   }
index 39eab252ffe7bd0ee3084324c74d511975fe4400..00ab0c67cd6aec1e0a48a09a446dc7fe22a33279 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.computation.task.projectanalysis.issue;
 
+import java.util.Random;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -54,7 +55,8 @@ public class DefaultAssigneeTest {
   public void setUp() throws Exception {
     organizationDto = db.organizations().insertForUuid(ORGANIZATION_UUID);
     analysisMetadataHolder.setOrganization(Organization.from(
-      new OrganizationDto().setUuid(ORGANIZATION_UUID).setKey("Organization key").setName("Organization name").setDefaultQualityGateUuid(QUALITY_GATE_UUID)));
+      new OrganizationDto().setUuid(ORGANIZATION_UUID).setKey("Organization key").setName("Organization name").setDefaultQualityGateUuid(QUALITY_GATE_UUID),
+      new Random().nextBoolean()));
   }
 
   @Test
index ba8bf417e1e8cd9d492a2f0b7f752e4c586f302e..32b4d7dde2a1566adcdbfef175662a1afd386356 100644 (file)
  */
 package org.sonar.server.computation.task.projectanalysis.step;
 
+import com.tngtech.java.junit.dataprovider.DataProvider;
+import com.tngtech.java.junit.dataprovider.DataProviderRunner;
+import com.tngtech.java.junit.dataprovider.UseDataProvider;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
 import org.sonar.api.utils.MessageException;
 import org.sonar.api.utils.System2;
 import org.sonar.ce.queue.CeTask;
@@ -41,12 +45,14 @@ import org.sonar.server.computation.task.projectanalysis.component.BranchLoader;
 import org.sonar.server.computation.task.step.ComputationStep;
 import org.sonar.server.organization.DefaultOrganizationProvider;
 import org.sonar.server.organization.TestDefaultOrganizationProvider;
+import org.sonar.server.organization.TestOrganizationFlags;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.tuple;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+@RunWith(DataProviderRunner.class)
 public class LoadReportAnalysisMetadataHolderStepTest {
 
   private static final String PROJECT_KEY = "project_key";
@@ -63,6 +69,7 @@ public class LoadReportAnalysisMetadataHolderStepTest {
 
   private DbClient dbClient = db.getDbClient();
   private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
+  private TestOrganizationFlags organizationFlags = TestOrganizationFlags.standalone();
   private PluginRepository pluginRepository = mock(PluginRepository.class);
   private ComputationStep underTest;
 
@@ -207,11 +214,13 @@ public class LoadReportAnalysisMetadataHolderStepTest {
   }
 
   @Test
-  public void execute_set_organization_from_ce_task_when_organizationKey_is_set_in_report() {
+  @UseDataProvider("organizationEnabledFlags")
+  public void execute_set_organization_from_ce_task_when_organizationKey_is_set_in_report(boolean organizationEnabled) {
     reportReader.setMetadata(
       newBatchReportBuilder()
         .setOrganizationKey(db.getDefaultOrganization().getKey())
         .build());
+    organizationFlags.setEnabled(organizationEnabled);
 
     underTest.execute();
 
@@ -220,10 +229,12 @@ public class LoadReportAnalysisMetadataHolderStepTest {
     assertThat(organization.getUuid()).isEqualTo(defaultOrganization.getUuid());
     assertThat(organization.getKey()).isEqualTo(defaultOrganization.getKey());
     assertThat(organization.getName()).isEqualTo(defaultOrganization.getName());
+    assertThat(organization.isOrganizationsEnabled()).isEqualTo(organizationEnabled);
   }
 
   @Test
-  public void execute_set_non_default_organization_from_ce_task() {
+  @UseDataProvider("organizationEnabledFlags")
+  public void execute_set_non_default_organization_from_ce_task(boolean organizationEnabled) {
     OrganizationDto nonDefaultOrganizationDto = db.organizations().insert();
     ComponentDto project = db.components().insertPublicProject(nonDefaultOrganizationDto);
     reportReader.setMetadata(
@@ -231,6 +242,7 @@ public class LoadReportAnalysisMetadataHolderStepTest {
         .setOrganizationKey(nonDefaultOrganizationDto.getKey())
         .setProjectKey(project.getDbKey())
         .build());
+    organizationFlags.setEnabled(organizationEnabled);
 
     ComputationStep underTest = createStep(createCeTask(project.getDbKey(), nonDefaultOrganizationDto.getUuid()));
 
@@ -240,6 +252,15 @@ public class LoadReportAnalysisMetadataHolderStepTest {
     assertThat(organization.getUuid()).isEqualTo(nonDefaultOrganizationDto.getUuid());
     assertThat(organization.getKey()).isEqualTo(nonDefaultOrganizationDto.getKey());
     assertThat(organization.getName()).isEqualTo(nonDefaultOrganizationDto.getName());
+    assertThat(organization.isOrganizationsEnabled()).isEqualTo(organizationEnabled);
+  }
+
+  @DataProvider
+  public static Object[][] organizationEnabledFlags() {
+    return new Object[][] {
+        { true },
+        { false }
+    };
   }
 
   @Test
@@ -323,7 +344,7 @@ public class LoadReportAnalysisMetadataHolderStepTest {
 
   private LoadReportAnalysisMetadataHolderStep createStep(CeTask ceTask) {
     return new LoadReportAnalysisMetadataHolderStep(ceTask, reportReader, analysisMetadataHolder,
-      defaultOrganizationProvider, dbClient, new BranchLoader(analysisMetadataHolder), pluginRepository);
+      defaultOrganizationProvider, dbClient, new BranchLoader(analysisMetadataHolder), pluginRepository, organizationFlags);
   }
 
   private static ScannerReport.Metadata.Builder newBatchReportBuilder() {
index 5007d2d87c9cfd3afdba0ea689a36e6a2b83c94f..1bf9349d77da5198bbe8a307731289188db11225 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.computation.task.projectanalysis.step;
 
+import java.util.Random;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
@@ -46,7 +47,7 @@ public class VerifyBillingStepTest {
   private OrganizationDto organization = newOrganizationDto();
   @Rule
   public MutableAnalysisMetadataHolderRule analysisMetadata = new MutableAnalysisMetadataHolderRule()
-    .setOrganization(Organization.from(organization));
+    .setOrganization(Organization.from(organization, new Random().nextBoolean()));
 
   private BillingValidationsProxy validations = mock(BillingValidationsProxy.class);