]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10134 Removing QualityGates dead code
authorGuillaume Jambet <guillaume.jambet@sonarsource.com>
Tue, 5 Dec 2017 12:53:18 +0000 (13:53 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 14 Dec 2017 16:03:35 +0000 (17:03 +0100)
server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java
server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGatesTest.java
server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CopyActionTest.java
server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DeselectActionTest.java
server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/QualityGatesWsTest.java

index ecd19b6e94670e1190506ba3c0c905e2d391f70e..59cd8971aa5e4254767369131b25c63d4abdc0dd 100644 (file)
  */
 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<String> 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<String> 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());
   }
index f7759e715042d795af70676541708e398a0353f1..ce5f18f106151fa0ba59434e42694854766f0b09 100644 (file)
  */
 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<QualityGateConditionDto> 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));
-  }
-
 }
index 5ee63742748468eb55c1829509d8f84074acf57c..8c6ecdfb068b5f36c7ab8cf081e8e4bdeebe3464 100644 (file)
@@ -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);
index daf097341aa3e2b0cc7a8e0cccaf7fa87171b1de..01a8d3b6017798c542510cc7ba5811e315a38c4c 100644 (file)
@@ -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;
index 865ddb4f098330fc15e41d5242e6ba4c768e0142..937580d81894334e3857838b8b58f63a2e7fcad9 100644 (file)
@@ -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;