From: Sébastien Lesaint Date: Wed, 27 Dec 2017 13:38:27 +0000 (+0100) Subject: SONAR-10223 add organizationsEnabled flag to Organization X-Git-Tag: 7.0-RC1~60 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2227e7444e6004e86b4bde3b0ad950f8947287f7;p=sonarqube.git SONAR-10223 add organizationsEnabled flag to Organization --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/Organization.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/Organization.java index f2de3e7a423..c35b7ce398a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/Organization.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/Organization.java @@ -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); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStep.java index 1010269638c..be38b184e74 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStep.java @@ -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 = 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)); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImplTest.java index 513fa54b3a5..a8f9b13dccb 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImplTest.java @@ -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); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderRule.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderRule.java index 1be5bbb5db0..c1b27fce6a2 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderRule.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderRule.java @@ -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; } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/OrganizationTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/OrganizationTest.java index 75c251936a8..d139d768f65 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/OrganizationTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/OrganizationTest.java @@ -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()); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/DefaultAssigneeTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/DefaultAssigneeTest.java index 39eab252ffe..00ab0c67cd6 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/DefaultAssigneeTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/DefaultAssigneeTest.java @@ -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 diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStepTest.java index ba8bf417e1e..32b4d7dde2a 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadReportAnalysisMetadataHolderStepTest.java @@ -19,10 +19,14 @@ */ 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() { diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/VerifyBillingStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/VerifyBillingStepTest.java index 5007d2d87c9..1bf9349d77d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/VerifyBillingStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/VerifyBillingStepTest.java @@ -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);