]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10096 rewrite unit test not using old framework
authorGuillaume Jambet <guillaume.jambet@sonarsource.com>
Mon, 27 Nov 2017 13:48:22 +0000 (14:48 +0100)
committerEric Hartmann <hartmann.eric@gmail.Com>
Mon, 4 Dec 2017 12:44:55 +0000 (13:44 +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/DestroyActionTest.java [new file with mode: 0644]

index 2fc1da81afc4d4c38b7553034af396c816b4484c..983350a7e8c3d22eb89e6bae52f49b48656fdb5b 100644 (file)
@@ -87,6 +87,7 @@ public class QualityGates {
     }
   }
 
+  @Deprecated // GJT
   public void delete(long idToDelete) {
     checkIsQualityGateAdministrator();
     QualityGateDto qGate = getNonNullQgate(idToDelete);
index dc1939f4751a75538946955b7f11e28530a37b25..52e6027c2337420cb7864990e342ae32816e3fec 100644 (file)
@@ -105,48 +105,6 @@ public class QualityGatesTest {
     assertThat(propertyCaptor.getValue().getValue()).isEqualTo("42");
   }
 
-  @Test
-  public void should_delete_qgate() {
-    long idToDelete = QUALITY_GATE_ID;
-    String name = "To Delete";
-    QualityGateDto toDelete = new QualityGateDto().setId(idToDelete).setName(name);
-    when(dao.selectById(dbSession, idToDelete)).thenReturn(toDelete);
-    when(dbClient.openSession(false)).thenReturn(dbSession);
-    underTest.delete(idToDelete);
-    verify(dao).selectById(dbSession, idToDelete);
-    verify(propertiesDao).deleteProjectProperties("sonar.qualitygate", "42", dbSession);
-    verify(dao).delete(toDelete, dbSession);
-  }
-
-  @Test
-  public void should_delete_qgate_if_non_default() {
-    long idToDelete = QUALITY_GATE_ID;
-    String name = "To Delete";
-    QualityGateDto toDelete = new QualityGateDto().setId(idToDelete).setName(name);
-    when(dao.selectById(dbSession, idToDelete)).thenReturn(toDelete);
-    when(propertiesDao.selectGlobalProperty("sonar.qualitygate")).thenReturn(new PropertyDto().setValue("666"));
-    when(dbClient.openSession(false)).thenReturn(dbSession);
-    underTest.delete(idToDelete);
-    verify(dao).selectById(dbSession, idToDelete);
-    verify(propertiesDao).deleteProjectProperties("sonar.qualitygate", "42", dbSession);
-    verify(dao).delete(toDelete, dbSession);
-  }
-
-  @Test
-  public void should_delete_qgate_even_if_default() {
-    long idToDelete = QUALITY_GATE_ID;
-    String name = "To Delete";
-    QualityGateDto toDelete = new QualityGateDto().setId(idToDelete).setName(name);
-    when(dao.selectById(dbSession, idToDelete)).thenReturn(toDelete);
-    when(propertiesDao.selectGlobalProperty("sonar.qualitygate")).thenReturn(new PropertyDto().setValue("42"));
-    when(dbClient.openSession(false)).thenReturn(dbSession);
-    underTest.delete(idToDelete);
-    verify(dao).selectById(dbSession, idToDelete);
-    verify(propertiesDao).deleteGlobalProperty("sonar.qualitygate", dbSession);
-    verify(propertiesDao).deleteProjectProperties("sonar.qualitygate", "42", dbSession);
-    verify(dao).delete(toDelete, dbSession);
-  }
-
   @Test
   public void should_copy_qgate() {
     String name = "Atlantis";
diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DestroyActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DestroyActionTest.java
new file mode 100644 (file)
index 0000000..8500f66
--- /dev/null
@@ -0,0 +1,127 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.qualitygate.ws;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.api.utils.System2;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.DbTester;
+import org.sonar.db.property.PropertyDto;
+import org.sonar.db.qualitygate.QualityGateDto;
+import org.sonar.server.organization.TestDefaultOrganizationProvider;
+import org.sonar.server.qualitygate.QualityGates;
+import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.WsActionTester;
+
+import static java.lang.String.valueOf;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES;
+import static org.sonarqube.ws.client.qualitygate.QualityGatesWsParameters.PARAM_ID;
+
+public class DestroyActionTest {
+
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+  @Rule
+  public UserSessionRule userSession = UserSessionRule.standalone();
+  @Rule
+  public DbTester db = DbTester.create(System2.INSTANCE);
+
+  private DbClient dbClient = db.getDbClient();
+  private DbSession dbSession = db.getSession();
+  private TestDefaultOrganizationProvider organizationProvider = TestDefaultOrganizationProvider.from(db);
+  private QualityGates qualityGates = new QualityGates(dbClient, userSession, organizationProvider);
+  private WsActionTester ws;
+  private DestroyAction underTest;
+
+  @Before
+  public void setUp() {
+    underTest = new DestroyAction(qualityGates);
+    ws = new WsActionTester(underTest);
+
+  }
+
+  @Test
+  public void definition() {
+    WebService.Action definition = ws.getDef();
+
+    assertThat(definition.since()).isEqualTo("4.3");
+    assertThat(definition.isPost()).isTrue();
+    assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("id");
+
+    WebService.Param id = definition.param("id");
+    assertThat(id.isRequired()).isTrue();
+  }
+
+  @Test
+  public void should_delete_quality_gate() {
+    userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
+    QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+    Long qualityGateId = qualityGate.getId();
+    assertThat(db.getDbClient().qualityGateDao().selectById(dbSession, qualityGateId)).isNotNull();
+
+    ws.newRequest()
+      .setParam(PARAM_ID, valueOf(qualityGateId))
+      .execute();
+
+    assertThat(db.getDbClient().qualityGateDao().selectById(dbSession, qualityGateId)).isNull();
+  }
+
+  @Test
+  public void should_delete_quality_gate_even_if_default() {
+    userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
+    QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("To Delete"));
+    Long qualityGateId = qualityGate.getId();
+    db.properties().insertProperty(new PropertyDto().setKey("sonar.qualitygate").setValue(Long.toString(qualityGateId)));
+    assertThat(db.getDbClient().qualityGateDao().selectById(dbSession, qualityGateId)).isNotNull();
+
+    ws.newRequest()
+      .setParam(PARAM_ID, valueOf(qualityGateId))
+      .execute();
+
+    assertThat(db.getDbClient().qualityGateDao().selectById(dbSession, qualityGateId)).isNull();
+  }
+
+  @Test
+  public void should_delete_quality_gate_if_non_default_when_a_default_exist() {
+    userSession.addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
+    QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("To Delete"));
+    Long toDeleteQualityGateId = qualityGate.getId();
+    assertThat(db.getDbClient().qualityGateDao().selectById(dbSession, toDeleteQualityGateId)).isNotNull();
+
+    QualityGateDto defaultqualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("Default"));
+    Long defaultQualityGateId = defaultqualityGate.getId();
+    db.properties().insertProperty(new PropertyDto().setKey("sonar.qualitygate").setValue(Long.toString(defaultQualityGateId)));
+    assertThat(db.getDbClient().qualityGateDao().selectById(dbSession, defaultQualityGateId)).isNotNull();
+
+    ws.newRequest()
+      .setParam(PARAM_ID, valueOf(toDeleteQualityGateId))
+      .execute();
+
+    assertThat(db.getDbClient().qualityGateDao().selectById(dbSession, toDeleteQualityGateId)).isNull();
+    assertThat(db.getDbClient().qualityGateDao().selectById(dbSession, defaultQualityGateId)).isNotNull();
+  }
+
+}