From 7a52e94407f8bd73bcb839290c5c1aff5e6d578a Mon Sep 17 00:00:00 2001 From: Guillaume Jambet Date: Tue, 5 Dec 2017 13:53:18 +0100 Subject: [PATCH] SONAR-10134 Removing QualityGates dead code --- .../server/qualitygate/QualityGates.java | 58 +------------------ .../server/qualitygate/QualityGatesTest.java | 34 +---------- .../server/qualitygate/ws/CopyActionTest.java | 1 - .../qualitygate/ws/DeselectActionTest.java | 3 +- .../qualitygate/ws/QualityGatesWsTest.java | 9 --- 5 files changed, 3 insertions(+), 102 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java index ecd19b6e946..59cd8971aa5 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java @@ -19,31 +19,21 @@ */ package org.sonar.server.qualitygate; -import com.google.common.base.Strings; -import java.util.ArrayList; -import java.util.List; import javax.annotation.Nullable; import org.sonar.api.web.UserRole; -import org.sonar.core.util.UuidFactory; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.component.ComponentDto; import org.sonar.db.permission.OrganizationPermission; import org.sonar.db.property.PropertiesDao; import org.sonar.db.property.PropertyDto; -import org.sonar.db.qualitygate.QualityGateConditionDao; -import org.sonar.db.qualitygate.QualityGateConditionDto; import org.sonar.db.qualitygate.QualityGateDao; import org.sonar.db.qualitygate.QualityGateDto; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.organization.DefaultOrganizationProvider; import org.sonar.server.user.UserSession; -import static java.lang.String.format; import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException; -import static org.sonar.server.util.Validation.CANT_BE_EMPTY_MESSAGE; -import static org.sonar.server.util.Validation.IS_ALREADY_USED_MESSAGE; -import static org.sonar.server.ws.WsUtils.checkRequest; /** * Methods from this class should be moved to {@link QualityGateUpdater} and to classes QualityGateFinder / QualityGateConditionsUpdater / etc. @@ -55,38 +45,16 @@ public class QualityGates { private final DbClient dbClient; private final QualityGateDao dao; - private final QualityGateConditionDao conditionDao; private final PropertiesDao propertiesDao; private final UserSession userSession; private final DefaultOrganizationProvider organizationProvider; - private final UuidFactory uuidFactory; - public QualityGates(DbClient dbClient, UserSession userSession, DefaultOrganizationProvider organizationProvider, UuidFactory uuidFactory) { + public QualityGates(DbClient dbClient, UserSession userSession, DefaultOrganizationProvider organizationProvider) { this.dbClient = dbClient; this.dao = dbClient.qualityGateDao(); - this.conditionDao = dbClient.gateConditionDao(); this.propertiesDao = dbClient.propertiesDao(); this.userSession = userSession; this.organizationProvider = organizationProvider; - this.uuidFactory = uuidFactory; - } - - public QualityGateDto copy(long sourceId, String destinationName) { - checkIsQualityGateAdministrator(); - getNonNullQgate(sourceId); - try (DbSession dbSession = dbClient.openSession(false)) { - validateQualityGate(dbSession, null, destinationName); - QualityGateDto destinationGate = new QualityGateDto().setName(destinationName).setBuiltIn(false).setUuid(uuidFactory.create()); - dao.insert(dbSession, destinationGate); - for (QualityGateConditionDto sourceCondition : conditionDao.selectForQualityGate(dbSession, sourceId)) { - conditionDao.insert(new QualityGateConditionDto().setQualityGateId(destinationGate.getId()) - .setMetricId(sourceCondition.getMetricId()).setOperator(sourceCondition.getOperator()) - .setWarningThreshold(sourceCondition.getWarningThreshold()).setErrorThreshold(sourceCondition.getErrorThreshold()).setPeriod(sourceCondition.getPeriod()), - dbSession); - } - dbSession.commit(); - return destinationGate; - } } /** @@ -122,12 +90,6 @@ public class QualityGates { dbSession.commit(); } - private QualityGateDto getNonNullQgate(long id) { - try (DbSession dbSession = dbClient.openSession(false)) { - return getNonNullQgate(dbSession, id); - } - } - private QualityGateDto getNonNullQgate(DbSession dbSession, long id) { QualityGateDto qGate = dao.selectById(dbSession, id); if (qGate == null) { @@ -136,24 +98,6 @@ public class QualityGates { return qGate; } - private void validateQualityGate(DbSession dbSession, @Nullable Long updatingQgateId, @Nullable String name) { - List errors = new ArrayList<>(); - if (Strings.isNullOrEmpty(name)) { - errors.add(format(CANT_BE_EMPTY_MESSAGE, "Name")); - } else { - checkQgateNotAlreadyExists(dbSession, updatingQgateId, name, errors); - } - checkRequest(errors.isEmpty(), errors); - } - - private void checkQgateNotAlreadyExists(DbSession dbSession, @Nullable Long updatingQgateId, String name, List errors) { - QualityGateDto existingQgate = dao.selectByName(dbSession, name); - boolean isModifyingCurrentQgate = updatingQgateId != null && existingQgate != null && existingQgate.getId().equals(updatingQgateId); - if (!isModifyingCurrentQgate && existingQgate != null) { - errors.add(format(IS_ALREADY_USED_MESSAGE, "Name")); - } - } - private void checkIsQualityGateAdministrator() { userSession.checkPermission(OrganizationPermission.ADMINISTER_QUALITY_GATES, organizationProvider.get().getUuid()); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGatesTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGatesTest.java index f7759e71504..ce5f18f1061 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGatesTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGatesTest.java @@ -19,15 +19,11 @@ */ package org.sonar.server.qualitygate; -import com.google.common.collect.ImmutableList; -import java.util.Collection; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; -import org.sonar.core.util.UuidFactoryFast; import org.sonar.core.util.Uuids; import org.sonar.db.DbClient; import org.sonar.db.DbSession; @@ -37,7 +33,6 @@ import org.sonar.db.permission.OrganizationPermission; import org.sonar.db.property.PropertiesDao; import org.sonar.db.property.PropertyDto; import org.sonar.db.qualitygate.QualityGateConditionDao; -import org.sonar.db.qualitygate.QualityGateConditionDto; import org.sonar.db.qualitygate.QualityGateDao; import org.sonar.db.qualitygate.QualityGateDto; import org.sonar.server.organization.TestDefaultOrganizationProvider; @@ -48,7 +43,6 @@ import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyLong; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto; @@ -85,7 +79,7 @@ public class QualityGatesTest { when(componentDao.selectOrFailById(eq(dbSession), anyLong())).thenReturn( newPrivateProjectDto(OrganizationTesting.newOrganizationDto(), PROJECT_UUID).setId(1L).setDbKey(PROJECT_KEY)); - underTest = new QualityGates(dbClient, userSession, organizationProvider, UuidFactoryFast.getInstance()); + underTest = new QualityGates(dbClient, userSession, organizationProvider); userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_GATES, organizationProvider.get().getUuid()); } @@ -105,30 +99,4 @@ public class QualityGatesTest { assertThat(propertyCaptor.getValue().getKey()).isEqualTo("sonar.qualitygate"); assertThat(propertyCaptor.getValue().getValue()).isEqualTo("42"); } - - @Test - public void should_copy_qgate() { - String name = "Atlantis"; - long sourceId = QUALITY_GATE_ID; - final long destId = 43L; - long metric1Id = 1L; - long metric2Id = 2L; - QualityGateConditionDto cond1 = new QualityGateConditionDto().setMetricId(metric1Id); - QualityGateConditionDto cond2 = new QualityGateConditionDto().setMetricId(metric2Id); - Collection conditions = ImmutableList.of(cond1, cond2); - - when(dao.selectById(dbSession, sourceId)).thenReturn(new QualityGateDto().setId(sourceId).setName("SG-1")); - Mockito.doAnswer(invocation -> { - ((QualityGateDto) invocation.getArguments()[1]).setId(destId); - return null; - }).when(dao).insert(eq(dbSession), any(QualityGateDto.class)); - when(conditionDao.selectForQualityGate(eq(dbSession), anyLong())).thenReturn(conditions); - QualityGateDto atlantis = underTest.copy(sourceId, name); - assertThat(atlantis.getName()).isEqualTo(name); - verify(dao).selectByName(dbSession, name); - verify(dao).insert(dbSession, atlantis); - verify(conditionDao).selectForQualityGate(eq(dbSession), anyLong()); - verify(conditionDao, times(conditions.size())).insert(any(QualityGateConditionDto.class), eq(dbSession)); - } - } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CopyActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CopyActionTest.java index 5ee63742748..8c6ecdfb068 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CopyActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CopyActionTest.java @@ -61,7 +61,6 @@ public class CopyActionTest { private DbSession dbSession = db.getSession(); private TestDefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); private QualityGateUpdater qualityGateUpdater = new QualityGateUpdater(dbClient, UuidFactoryFast.getInstance()); - private QualityGatesWsSupport wsSupport = new QualityGatesWsSupport(dbClient, userSession, defaultOrganizationProvider); private CopyAction underTest = new CopyAction(dbClient, userSession, defaultOrganizationProvider, qualityGateUpdater); private WsActionTester ws = new WsActionTester(underTest); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DeselectActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DeselectActionTest.java index daf097341aa..01a8d3b6017 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DeselectActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DeselectActionTest.java @@ -27,7 +27,6 @@ import org.sonar.api.server.ws.Change; import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.System2; import org.sonar.api.web.UserRole; -import org.sonar.core.util.UuidFactoryFast; import org.sonar.core.util.Uuids; import org.sonar.db.DbClient; import org.sonar.db.DbSession; @@ -63,7 +62,7 @@ public class DeselectActionTest { private DbClient dbClient = db.getDbClient(); private DbSession dbSession = db.getSession(); private TestDefaultOrganizationProvider organizationProvider = TestDefaultOrganizationProvider.from(db); - private QualityGates qualityGates = new QualityGates(dbClient, userSession, organizationProvider, UuidFactoryFast.getInstance()); + private QualityGates qualityGates = new QualityGates(dbClient, userSession, organizationProvider); private WsActionTester ws; private ComponentDto project; private QualityGateDto gate; diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/QualityGatesWsTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/QualityGatesWsTest.java index 865ddb4f098..937580d8189 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/QualityGatesWsTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/QualityGatesWsTest.java @@ -34,7 +34,6 @@ import org.sonar.api.server.ws.WebService.Controller; import org.sonar.db.DbClient; import org.sonar.db.qualitygate.ProjectQgateAssociation; import org.sonar.db.qualitygate.ProjectQgateAssociationQuery; -import org.sonar.db.qualitygate.QualityGateDto; import org.sonar.server.component.ComponentFinder; import org.sonar.server.qualitygate.QgateProjectFinder; import org.sonar.server.qualitygate.QgateProjectFinder.Association; @@ -107,14 +106,6 @@ public class QualityGatesWsTest { assertThat(unsetDefault.isInternal()).isFalse(); } - @Test - public void copy_nominal() throws Exception { - String name = "Copied QG"; - when(qGates.copy(24L, name)).thenReturn(new QualityGateDto().setId(42L).setName(name)); - tester.newPostRequest("api/qualitygates", "copy").setParam("id", "24").setParam("name", name).execute() - .assertJson("{\"id\":42,\"name\":\"Copied QG\"}"); - } - @Test public void search_with_query() throws Exception { long gateId = 12345L; -- 2.39.5