From 907da606e2e218c772a95edcaaf36ca1136010e1 Mon Sep 17 00:00:00 2001 From: Guillaume Jambet Date: Mon, 27 Nov 2017 14:48:22 +0100 Subject: [PATCH] SONAR-10096 rewrite unit test not using old framework --- .../server/qualitygate/QualityGates.java | 1 + .../server/qualitygate/QualityGatesTest.java | 42 ------ .../qualitygate/ws/DestroyActionTest.java | 127 ++++++++++++++++++ 3 files changed, 128 insertions(+), 42 deletions(-) create mode 100644 server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DestroyActionTest.java 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 2fc1da81afc..983350a7e8c 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 @@ -87,6 +87,7 @@ public class QualityGates { } } + @Deprecated // GJT public void delete(long idToDelete) { checkIsQualityGateAdministrator(); QualityGateDto qGate = getNonNullQgate(idToDelete); 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 dc1939f4751..52e6027c233 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 @@ -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 index 00000000000..8500f667081 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/DestroyActionTest.java @@ -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(); + } + +} -- 2.39.5