From ab36b2522d87866097eb247fa16dd941442229e7 Mon Sep 17 00:00:00 2001 From: Eric Hartmann Date: Fri, 8 Dec 2017 16:28:41 +0100 Subject: [PATCH] SONAR-10147 Drop usage of global 'sonar.qualitygate' property in CE --- .../analysis/Organization.java | 19 ++++++- .../qualitygate/QualityGateService.java | 11 +++- .../qualitygate/QualityGateServiceImpl.java | 16 +++++- .../step/LoadQualityGateStep.java | 54 ++++++++++++------- .../AnalysisMetadataHolderImplTest.java | 2 +- .../analysis/AnalysisMetadataHolderRule.java | 4 +- .../analysis/OrganizationTest.java | 30 +++++++---- .../issue/DefaultAssigneeTest.java | 4 +- .../issue/RuleRepositoryImplTest.java | 3 +- .../QualityGateServiceImplTest.java | 13 +++-- .../step/LoadQualityGateStepTest.java | 21 ++++---- .../step/PersistIssuesStepTest.java | 2 +- .../step/ReportPersistComponentsStepTest.java | 3 +- .../step/ViewsPersistComponentsStepTest.java | 3 +- 14 files changed, 123 insertions(+), 62 deletions(-) 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 c53ec627eb8..f2de3e7a423 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 @@ -29,11 +29,13 @@ public class Organization { private final String uuid; private final String key; private final String name; + private final String defaultQualityGateUuid; - private Organization(String uuid, String key, String name) { + private Organization(String uuid, String key, String name, String defaultQualityGateUuid) { 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"); } public String getUuid() { @@ -48,6 +50,10 @@ public class Organization { return name; } + public String getDefaultQualityGateUuid() { + return defaultQualityGateUuid; + } + @Override public boolean equals(Object o) { if (this == o) { @@ -71,11 +77,20 @@ public class Organization { "uuid='" + uuid + '\'' + ", key='" + key + '\'' + ", name='" + name + '\'' + + ", defaultQualityGateUuid='" + defaultQualityGateUuid + '\'' + '}'; } + public OrganizationDto toDto() { + return new OrganizationDto() + .setName(name) + .setKey(key) + .setUuid(uuid) + .setDefaultQualityGateUuid(defaultQualityGateUuid); + } + public static Organization from(OrganizationDto organizationDto) { - return new Organization(organizationDto.getUuid(), organizationDto.getKey(), organizationDto.getName()); + return new Organization(organizationDto.getUuid(), organizationDto.getKey(), organizationDto.getName(), organizationDto.getDefaultQualityGateUuid()); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/qualitygate/QualityGateService.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/qualitygate/QualityGateService.java index 2a457118b2f..4eb9d158986 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/qualitygate/QualityGateService.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/qualitygate/QualityGateService.java @@ -19,12 +19,19 @@ */ package org.sonar.server.computation.task.projectanalysis.qualitygate; -import com.google.common.base.Optional; +import java.util.Optional; +import org.sonar.server.computation.task.projectanalysis.analysis.Organization; public interface QualityGateService { /** - * Retrieve the {@link QualityGate} from the database with the specified id, it it exists. + * Retrieve the {@link QualityGate} from the database with the specified id, if it exists. */ Optional findById(long id); + + /** + * Retrieve the {@link QualityGate} from the database with the specified uuid, if it exists. + */ + Optional findDefaultQualityGate(Organization organizationDto); + } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/qualitygate/QualityGateServiceImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/qualitygate/QualityGateServiceImpl.java index f01810b5eab..f12eb5e3004 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/qualitygate/QualityGateServiceImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/qualitygate/QualityGateServiceImpl.java @@ -19,13 +19,14 @@ */ package org.sonar.server.computation.task.projectanalysis.qualitygate; -import com.google.common.base.Optional; import java.util.Collection; import java.util.Objects; +import java.util.Optional; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.qualitygate.QualityGateConditionDto; import org.sonar.db.qualitygate.QualityGateDto; +import org.sonar.server.computation.task.projectanalysis.analysis.Organization; import org.sonar.server.computation.task.projectanalysis.metric.MetricRepository; import org.sonar.server.qualitygate.ShortLivingBranchQualityGate; @@ -49,7 +50,18 @@ public class QualityGateServiceImpl implements QualityGateService { try (DbSession dbSession = dbClient.openSession(false)) { QualityGateDto qualityGateDto = dbClient.qualityGateDao().selectById(dbSession, id); if (qualityGateDto == null) { - return Optional.absent(); + return Optional.empty(); + } + return Optional.of(toQualityGate(dbSession, qualityGateDto)); + } + } + + @Override + public Optional findDefaultQualityGate(Organization organization) { + try (DbSession dbSession = dbClient.openSession(false)) { + QualityGateDto qualityGateDto = dbClient.qualityGateDao().selectByOrganizationAndUuid(dbSession, organization.toDto(), organization.getDefaultQualityGateUuid()); + if (qualityGateDto == null) { + return Optional.empty(); } return Optional.of(toQualityGate(dbSession, qualityGateDto)); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadQualityGateStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadQualityGateStep.java index 68abc8c8b62..dcb01bd4695 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadQualityGateStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadQualityGateStep.java @@ -19,10 +19,8 @@ */ package org.sonar.server.computation.task.projectanalysis.step; -import com.google.common.base.Optional; +import java.util.Optional; import org.sonar.api.config.Configuration; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder; import org.sonar.server.computation.task.projectanalysis.component.ConfigurationRepository; import org.sonar.server.computation.task.projectanalysis.qualitygate.MutableQualityGateHolder; @@ -38,9 +36,7 @@ import static org.apache.commons.lang.StringUtils.isBlank; * {@link MutableQualityGateHolder}. */ public class LoadQualityGateStep implements ComputationStep { - private static final Logger LOGGER = Loggers.get(LoadQualityGateStep.class); - - private static final String PROPERTY_QUALITY_GATE = "sonar.qualitygate"; + private static final String PROPERTY_PROJECT_QUALITY_GATE = "sonar.qualitygate"; private final ConfigurationRepository configRepository; private final QualityGateService qualityGateService; @@ -57,39 +53,57 @@ public class LoadQualityGateStep implements ComputationStep { @Override public void execute() { + Optional qualityGate = getShortLivingBranchQualityGate(); + if (!qualityGate.isPresent()) { + // Not on a short living branch, let's retrieve the QG of the project + qualityGate = getProjectQualityGate(); + if (!qualityGate.isPresent()) { + // No QG defined for the project, let's retrieve the QG on the organization + qualityGate = getOrganizationDefaultQualityGate(); + } + } + + if (qualityGate.isPresent()) { + qualityGateHolder.setQualityGate(qualityGate.get()); + } else { + qualityGateHolder.setNoQualityGate(); + } + } + + private Optional getShortLivingBranchQualityGate() { if (analysisMetadataHolder.isShortLivingBranch()) { Optional qualityGate = qualityGateService.findById(ShortLivingBranchQualityGate.ID); if (qualityGate.isPresent()) { - qualityGateHolder.setQualityGate(qualityGate.get()); + return qualityGate; } else { throw new IllegalStateException("Failed to retrieve hardcoded short living branch Quality Gate"); } - return; + } else { + return Optional.empty(); } + } + + private Optional getProjectQualityGate() { Configuration config = configRepository.getConfiguration(); - String qualityGateSetting = config.get(PROPERTY_QUALITY_GATE).orElse(null); + String qualityGateSetting = config.get(PROPERTY_PROJECT_QUALITY_GATE).orElse(null); if (isBlank(qualityGateSetting)) { - LOGGER.debug("No quality gate is configured"); - qualityGateHolder.setNoQualityGate(); - return; + return Optional.empty(); } try { long qualityGateId = Long.parseLong(qualityGateSetting); - Optional qualityGate = qualityGateService.findById(qualityGateId); - if (qualityGate.isPresent()) { - qualityGateHolder.setQualityGate(qualityGate.get()); - } else { - qualityGateHolder.setNoQualityGate(); - } + return qualityGateService.findById(qualityGateId); } catch (NumberFormatException e) { throw new IllegalStateException( - String.format("Unsupported value (%s) in property %s", qualityGateSetting, PROPERTY_QUALITY_GATE), - e); + String.format("Unsupported value (%s) in property %s", qualityGateSetting, PROPERTY_PROJECT_QUALITY_GATE), e); } } + private Optional getOrganizationDefaultQualityGate() { + return qualityGateService.findDefaultQualityGate(analysisMetadataHolder.getOrganization()); + } + @Override public String getDescription() { return "Load Quality gate"; 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 ec05e37eba3..513fa54b3a5 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 @@ -62,7 +62,7 @@ public class AnalysisMetadataHolderImplTest { @Test public void setOrganization_throws_ISE_if_called_twice() { - Organization organization = Organization.from(new OrganizationDto().setUuid("uuid").setKey("key").setName("name")); + Organization organization = Organization.from(new OrganizationDto().setUuid("uuid").setKey("key").setName("name").setDefaultQualityGateUuid("anyuuidr")); 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 f1fb6e2a42c..1be5bbb5db0 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 @@ -62,9 +62,9 @@ public class AnalysisMetadataHolderRule extends ExternalResource implements Muta return this; } - public AnalysisMetadataHolderRule setOrganizationUuid(String uuid) { + 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))); + this.organization.setProperty(Organization.from(new OrganizationDto().setUuid(uuid).setKey("key_" + uuid).setName("name_" + uuid).setDefaultQualityGateUuid(defaultQualityGateUuid))); 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 aedf90d7266..75c251936a8 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 @@ -57,6 +57,16 @@ public class OrganizationTest { Organization.from(underTest); } + @Test + public void build_throws_NPE_if_defaultQualityGateUuid_is_null() { + underTest.setUuid("uuid").setKey("key").setName("name"); + + expectedException.expect(NullPointerException.class); + expectedException.expectMessage("defaultQualityGateUuid can't be null"); + + Organization.from(underTest); + } + @Test public void build_throws_NPE_if_name_is_null() { underTest.setUuid("uuid").setKey("key"); @@ -69,35 +79,37 @@ public class OrganizationTest { @Test public void verify_getters() { - Organization organization = Organization.from(underTest.setUuid("uuid").setKey("key").setName("name")); + Organization organization = Organization.from(underTest.setUuid("uuid").setKey("key").setName("name").setDefaultQualityGateUuid("uuid1")); assertThat(organization.getUuid()).isEqualTo("uuid"); assertThat(organization.getKey()).isEqualTo("key"); assertThat(organization.getName()).isEqualTo("name"); + assertThat(organization.getDefaultQualityGateUuid()).isEqualTo("uuid1"); } @Test public void verify_toString() { - Organization organization = Organization.from(underTest.setUuid("uuid").setKey("key").setName("name")); + Organization organization = Organization.from(underTest.setUuid("uuid").setKey("key").setName("name").setDefaultQualityGateUuid("uuid1")); - assertThat(organization.toString()).isEqualTo("Organization{uuid='uuid', key='key', name='name'}"); + assertThat(organization.toString()).isEqualTo("Organization{uuid='uuid', key='key', name='name', defaultQualityGateUuid='uuid1'}"); } @Test public void equals_is_based_on_uuid_only() { - Organization organization = Organization.from(underTest.setUuid("uuid").setKey("key").setName("name")); + 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"))); - assertThat(organization).isEqualTo(Organization.from(underTest.setUuid("uuid").setKey("other key").setName("name"))); - assertThat(organization).isEqualTo(Organization.from(underTest.setUuid("uuid").setKey("key").setName("other name"))); - assertThat(organization).isNotEqualTo(Organization.from(underTest.setUuid("other uuid").setKey("key").setName("name"))); + 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"))); 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")); + Organization organization = Organization.from(underTest.setUuid("uuid").setKey("key").setName("name").setDefaultQualityGateUuid("uuid1")); 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 aa29f66174e..39eab252ffe 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 @@ -38,6 +38,7 @@ public class DefaultAssigneeTest { public static final String PROJECT_KEY = "PROJECT_KEY"; public static final String ORGANIZATION_UUID = "ORGANIZATION_UUID"; + public static final String QUALITY_GATE_UUID = "QUALITY_GATE_UUID"; @Rule public DbTester db = DbTester.create(); @@ -52,7 +53,8 @@ public class DefaultAssigneeTest { @Before 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"))); + analysisMetadataHolder.setOrganization(Organization.from( + new OrganizationDto().setUuid(ORGANIZATION_UUID).setKey("Organization key").setName("Organization name").setDefaultQualityGateUuid(QUALITY_GATE_UUID))); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/RuleRepositoryImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/RuleRepositoryImplTest.java index eff1c5397fb..476851e8d3e 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/RuleRepositoryImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/issue/RuleRepositoryImplTest.java @@ -51,12 +51,13 @@ public class RuleRepositoryImplTest { private static final RuleKey AC_RULE_KEY = RuleKey.of("a", "c"); private static final int AC_RULE_ID = 684; private static final String ORGANIZATION_UUID = "org-1"; + public static final String QUALITY_GATE_UUID = "QUALITY_GATE_UUID"; @org.junit.Rule public ExpectedException expectedException = ExpectedException.none(); @org.junit.Rule public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule() - .setOrganizationUuid(ORGANIZATION_UUID); + .setOrganizationUuid(ORGANIZATION_UUID, QUALITY_GATE_UUID); private DbClient dbClient = mock(DbClient.class); private DbSession dbSession = mock(DbSession.class); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/qualitygate/QualityGateServiceImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/qualitygate/QualityGateServiceImplTest.java index 8ca8e9504da..04a14603abf 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/qualitygate/QualityGateServiceImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/qualitygate/QualityGateServiceImplTest.java @@ -19,9 +19,9 @@ */ package org.sonar.server.computation.task.projectanalysis.qualitygate; -import com.google.common.base.Optional; import com.google.common.collect.ImmutableList; import java.util.Collections; +import java.util.Optional; import java.util.Random; import org.apache.commons.lang.RandomStringUtils; import org.junit.Before; @@ -40,7 +40,6 @@ import org.sonar.server.qualitygate.ShortLivingBranchQualityGate; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.tuple; -import static org.assertj.guava.api.Assertions.assertThat; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; @@ -73,7 +72,7 @@ public class QualityGateServiceImplTest { @Test public void findById_returns_absent_when_QualityGateDto_does_not_exist() { - assertThat(underTest.findById(SOME_ID)).isAbsent(); + assertThat(underTest.findById(SOME_ID)).isNotPresent(); } @Test @@ -94,8 +93,8 @@ public class QualityGateServiceImplTest { when(qualityGateDao.selectById(any(DbSession.class), eq(SOME_ID))).thenReturn(QUALITY_GATE_DTO); when(qualityGateConditionDao.selectForQualityGate(any(DbSession.class), eq(SOME_ID))).thenReturn(ImmutableList.of(CONDITION_1, CONDITION_2)); // metrics are always supposed to be there - when(metricRepository.getOptionalById(METRIC_ID_1)).thenReturn(java.util.Optional.of(METRIC_1)); - when(metricRepository.getOptionalById(METRIC_ID_2)).thenReturn(java.util.Optional.of(METRIC_2)); + when(metricRepository.getOptionalById(METRIC_ID_1)).thenReturn(Optional.of(METRIC_1)); + when(metricRepository.getOptionalById(METRIC_ID_2)).thenReturn(Optional.of(METRIC_2)); Optional res = underTest.findById(SOME_ID); @@ -112,8 +111,8 @@ public class QualityGateServiceImplTest { when(qualityGateDao.selectById(any(DbSession.class), eq(SOME_ID))).thenReturn(QUALITY_GATE_DTO); when(qualityGateConditionDao.selectForQualityGate(any(DbSession.class), eq(SOME_ID))).thenReturn(ImmutableList.of(CONDITION_1, CONDITION_2)); // metrics are always supposed to be there - when(metricRepository.getOptionalById(METRIC_ID_1)).thenReturn(java.util.Optional.empty()); - when(metricRepository.getOptionalById(METRIC_ID_2)).thenReturn(java.util.Optional.of(METRIC_2)); + when(metricRepository.getOptionalById(METRIC_ID_1)).thenReturn(Optional.empty()); + when(metricRepository.getOptionalById(METRIC_ID_2)).thenReturn(Optional.of(METRIC_2)); Optional res = underTest.findById(SOME_ID); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadQualityGateStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadQualityGateStepTest.java index 6abfb9f5b44..1b47999f75e 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadQualityGateStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadQualityGateStepTest.java @@ -19,8 +19,8 @@ */ package org.sonar.server.computation.task.projectanalysis.step; -import com.google.common.base.Optional; import java.util.Collections; +import java.util.Optional; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -36,9 +36,8 @@ import org.sonar.server.qualitygate.ShortLivingBranchQualityGate; import static java.lang.String.format; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.guava.api.Assertions.assertThat; +import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; public class LoadQualityGateStepTest { @@ -73,14 +72,12 @@ public class LoadQualityGateStepTest { @Test public void execute_sets_default_QualityGate_when_project_has_no_settings() { when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().asConfig()); + QualityGate qualityGate = mock(QualityGate.class); + when(qualityGateService.findDefaultQualityGate(any())).thenReturn(Optional.of(qualityGate)); underTest.execute(); - verifyNoQualityGate(); - - // verify only project is processed - verify(settingsRepository).getConfiguration(); - verifyNoMoreInteractions(settingsRepository); + assertThat(mutableQualityGateHolder.getQualityGate().get()).isSameAs(qualityGate); } @Test @@ -94,9 +91,9 @@ public class LoadQualityGateStepTest { } @Test - public void execute_sets_default_QualityGate_if_it_can_not_be_found_by_service() { - when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().setProperty("sonar.qualitygate", 10).asConfig()); - when(qualityGateService.findById(10)).thenReturn(Optional.absent()); + public void execute_sets_default_QualityGate_if_no_default_quality_gate_on_organization() { + when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().asConfig()); + when(qualityGateService.findDefaultQualityGate(any())).thenReturn(Optional.empty()); underTest.execute(); @@ -105,7 +102,7 @@ public class LoadQualityGateStepTest { @Test public void execute_sets_QualityGate_if_it_can_be_found_by_service() { - QualityGate qualityGate = new QualityGate(465, "name", Collections.emptyList()); + QualityGate qualityGate = new QualityGate(10, "name", Collections.emptyList()); when(settingsRepository.getConfiguration()).thenReturn(new MapSettings().setProperty("sonar.qualitygate", 10).asConfig()); when(qualityGateService.findById(10)).thenReturn(Optional.of(qualityGate)); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/PersistIssuesStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/PersistIssuesStepTest.java index c970bb0409d..ff021d9710d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/PersistIssuesStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/PersistIssuesStepTest.java @@ -74,7 +74,7 @@ public class PersistIssuesStepTest extends BaseStepTest { public BatchReportReaderRule reportReader = new BatchReportReaderRule(); @Rule public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule() - .setOrganizationUuid("org-1"); + .setOrganizationUuid("org-1","qg-uuid-1"); private DbSession session = db.getSession(); private DbClient dbClient = db.getDbClient(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ReportPersistComponentsStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ReportPersistComponentsStepTest.java index 09c708162a4..cf347a6be5c 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ReportPersistComponentsStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ReportPersistComponentsStepTest.java @@ -72,6 +72,7 @@ public class ReportPersistComponentsStepTest extends BaseStepTest { private static final String PROJECT_KEY = "PROJECT_KEY"; private static final String MODULE_KEY = "MODULE_KEY"; private static final String ORGANIZATION_UUID = "org1"; + private static final String QUALITY_GATE_UUID = "gg1"; @Rule public DbTester db = DbTester.create(System2.INSTANCE); @@ -81,7 +82,7 @@ public class ReportPersistComponentsStepTest extends BaseStepTest { public MutableDbIdsRepositoryRule dbIdsRepository = MutableDbIdsRepositoryRule.create(treeRootHolder); @Rule public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule() - .setOrganizationUuid(ORGANIZATION_UUID); + .setOrganizationUuid(ORGANIZATION_UUID, QUALITY_GATE_UUID); private System2 system2 = mock(System2.class); private DbClient dbClient = db.getDbClient(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ViewsPersistComponentsStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ViewsPersistComponentsStepTest.java index 62dd7f293cb..e352478534f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ViewsPersistComponentsStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ViewsPersistComponentsStepTest.java @@ -78,6 +78,7 @@ public class ViewsPersistComponentsStepTest extends BaseStepTest { private static final String PROJECT_VIEW_1_NAME = "PV1_NAME"; private static final String PROJECT_VIEW_1_UUID = "PV1_UUID"; private static final String ORGANIZATION_UUID = "org1"; + private static final String QUALITY_GATE_UUID = "qg1"; @Rule public DbTester dbTester = DbTester.create(System2.INSTANCE); @@ -87,7 +88,7 @@ public class ViewsPersistComponentsStepTest extends BaseStepTest { public MutableDbIdsRepositoryRule dbIdsRepository = MutableDbIdsRepositoryRule.create(treeRootHolder); @Rule public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule() - .setOrganizationUuid(ORGANIZATION_UUID); + .setOrganizationUuid(ORGANIZATION_UUID, QUALITY_GATE_UUID); private System2 system2 = mock(System2.class); private DbClient dbClient = dbTester.getDbClient(); -- 2.39.5