From c157750016023b874a701f573be5ed8de9856253 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 22 Nov 2017 16:13:21 +0100 Subject: [PATCH] SONAR-10087 Move some logic from QualityGates to ListAction --- .../server/qualitygate/QualityGates.java | 19 --- .../server/qualitygate/ws/ListAction.java | 41 ++++-- .../server/qualitygate/ws/example-list.json | 12 -- .../server/qualitygate/ws/list-example.json | 15 ++ .../server/qualitygate/QualityGatesTest.java | 25 ---- .../server/qualitygate/ws/ListActionTest.java | 132 ++++++++++++++++++ .../qualitygate/ws/QualityGatesWsTest.java | 33 +---- 7 files changed, 181 insertions(+), 96 deletions(-) delete mode 100644 server/sonar-server/src/main/resources/org/sonar/server/qualitygate/ws/example-list.json create mode 100644 server/sonar-server/src/main/resources/org/sonar/server/qualitygate/ws/list-example.json create mode 100644 server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ListActionTest.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 6a3e809cc11..7340d2364d4 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 @@ -21,9 +21,7 @@ package org.sonar.server.qualitygate; import com.google.common.base.Strings; import java.util.ArrayList; -import java.util.Collection; import java.util.List; -import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.apache.commons.lang.StringUtils; import org.sonar.api.web.UserRole; @@ -101,12 +99,6 @@ public class QualityGates { } } - public Collection list() { - try (DbSession dbSession = dbClient.openSession(false)) { - return dao.selectAll(dbSession); - } - } - public void delete(long idToDelete) { checkIsQualityGateAdministrator(); QualityGateDto qGate = getNonNullQgate(idToDelete); @@ -137,17 +129,6 @@ public class QualityGates { } } - @CheckForNull - public QualityGateDto getDefault() { - Long defaultId = getDefaultId(); - if (defaultId == null) { - return null; - } - try (DbSession dbSession = dbClient.openSession(false)) { - return dao.selectById(dbSession, defaultId); - } - } - public void dissociateProject(DbSession dbSession, ComponentDto project) { checkProjectAdmin(project); propertiesDao.deleteProjectProperty(SONAR_QUALITYGATE_PROPERTY, project.getId(), dbSession); diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ListAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ListAction.java index 74d8a64c695..0d0d5db3804 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ListAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ListAction.java @@ -20,23 +20,28 @@ package org.sonar.server.qualitygate.ws; import com.google.common.io.Resources; +import javax.annotation.CheckForNull; +import org.apache.commons.lang.StringUtils; import org.sonar.api.server.ws.Change; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.text.JsonWriter; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.db.property.PropertyDto; import org.sonar.db.qualitygate.QualityGateDto; -import org.sonar.server.qualitygate.QualityGates; +import static org.sonar.server.qualitygate.QualityGates.SONAR_QUALITYGATE_PROPERTY; import static org.sonarqube.ws.client.qualitygate.QualityGatesWsParameters.PARAM_ID; import static org.sonarqube.ws.client.qualitygate.QualityGatesWsParameters.PARAM_NAME; public class ListAction implements QualityGatesWsAction { - private final QualityGates qualityGates; + private final DbClient dbClient; - public ListAction(QualityGates qualityGates) { - this.qualityGates = qualityGates; + public ListAction(DbClient dbClient) { + this.dbClient = dbClient; } @Override @@ -44,7 +49,7 @@ public class ListAction implements QualityGatesWsAction { controller.createAction("list") .setDescription("Get a list of quality gates") .setSince("4.3") - .setResponseExample(Resources.getResource(this.getClass(), "example-list.json")) + .setResponseExample(Resources.getResource(this.getClass(), "list-example.json")) .setChangelog( new Change("7.0", "'isDefault' field is added on quality gate level"), new Change("7.0", "'default' field on root level is deprecated")) @@ -53,11 +58,12 @@ public class ListAction implements QualityGatesWsAction { @Override public void handle(Request request, Response response) { - try (JsonWriter writer = response.newJsonWriter()) { - QualityGateDto defaultQgate = qualityGates.getDefault(); + try (DbSession dbSession = dbClient.openSession(false); + JsonWriter writer = response.newJsonWriter()) { + QualityGateDto defaultQgate = getDefault(dbSession); Long defaultQgateId = defaultQgate == null ? null : defaultQgate.getId(); writer.beginObject().name("qualitygates").beginArray(); - for (QualityGateDto qualityGate : qualityGates.list()) { + for (QualityGateDto qualityGate : dbClient.qualityGateDao().selectAll(dbSession)) { writer.beginObject() .prop(PARAM_ID, qualityGate.getId()) .prop(PARAM_NAME, qualityGate.getName()) @@ -70,6 +76,25 @@ public class ListAction implements QualityGatesWsAction { } writer.endObject().close(); } + + } + + @CheckForNull + private QualityGateDto getDefault(DbSession dbSession) { + Long defaultId = getDefaultId(dbSession); + if (defaultId == null) { + return null; + } + return dbClient.qualityGateDao().selectById(dbSession, defaultId); + } + + @CheckForNull + private Long getDefaultId(DbSession dbSession) { + PropertyDto defaultQgate = dbClient.propertiesDao().selectGlobalProperty(dbSession, SONAR_QUALITYGATE_PROPERTY); + if (defaultQgate == null || StringUtils.isBlank(defaultQgate.getValue())) { + return null; + } + return Long.valueOf(defaultQgate.getValue()); } } diff --git a/server/sonar-server/src/main/resources/org/sonar/server/qualitygate/ws/example-list.json b/server/sonar-server/src/main/resources/org/sonar/server/qualitygate/ws/example-list.json deleted file mode 100644 index 0a57b779516..00000000000 --- a/server/sonar-server/src/main/resources/org/sonar/server/qualitygate/ws/example-list.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "qualitygates": [ - { - "id": 2, - "name": "QG1" - }, - { - "id": 4, - "name": "QG2" - } - ] -} diff --git a/server/sonar-server/src/main/resources/org/sonar/server/qualitygate/ws/list-example.json b/server/sonar-server/src/main/resources/org/sonar/server/qualitygate/ws/list-example.json new file mode 100644 index 00000000000..7e5b5f5a625 --- /dev/null +++ b/server/sonar-server/src/main/resources/org/sonar/server/qualitygate/ws/list-example.json @@ -0,0 +1,15 @@ +{ + "qualitygates": [ + { + "id": 2, + "name": "Sonar way", + "isDefault": true + }, + { + "id": 4, + "name": "Sonar way - Without Coverage", + "isDefault": false + } + ], + "default": 2 +} 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 a2946607e9a..f8f6b4729f9 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 @@ -20,9 +20,7 @@ package org.sonar.server.qualitygate; import com.google.common.collect.ImmutableList; -import com.google.common.collect.Lists; import java.util.Collection; -import java.util.List; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -93,13 +91,6 @@ public class QualityGatesTest { userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_GATES, organizationProvider.get().getUuid()); } - @Test - public void should_list_qgates() { - List allQgates = Lists.newArrayList(new QualityGateDto().setName("Gate One"), new QualityGateDto().setName("Gate Two")); - when(dao.selectAll(dbSession)).thenReturn(allQgates); - assertThat(underTest.list()).isEqualTo(allQgates); - } - @Test public void should_rename_qgate() { long id = QUALITY_GATE_ID; @@ -199,22 +190,6 @@ public class QualityGatesTest { verify(dao).delete(toDelete, dbSession); } - @Test - public void should_return_default_qgate_if_set() { - String defaultName = "Sonar way"; - long defaultId = QUALITY_GATE_ID; - when(propertiesDao.selectGlobalProperty("sonar.qualitygate")).thenReturn(new PropertyDto().setValue(Long.toString(defaultId))); - QualityGateDto defaultQgate = new QualityGateDto().setId(defaultId).setName(defaultName); - when(dao.selectById(dbSession, defaultId)).thenReturn(defaultQgate); - assertThat(underTest.getDefault()).isEqualTo(defaultQgate); - } - - @Test - public void should_return_null_default_qgate_if_unset() { - when(propertiesDao.selectGlobalProperty("sonar.qualitygate")).thenReturn(new PropertyDto().setValue("")); - assertThat(underTest.getDefault()).isNull(); - } - @Test public void should_copy_qgate() { String name = "Atlantis"; diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ListActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ListActionTest.java new file mode 100644 index 00000000000..1696f719673 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ListActionTest.java @@ -0,0 +1,132 @@ +/* + * 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.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.server.ws.Change; +import org.sonar.api.server.ws.WebService; +import org.sonar.db.DbTester; +import org.sonar.db.qualitygate.QualityGateDto; +import org.sonar.server.tester.UserSessionRule; +import org.sonar.server.ws.WsActionTester; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.AssertionsForClassTypes.tuple; +import static org.sonar.test.JsonAssert.assertJson; + +public class ListActionTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + @Rule + public UserSessionRule userSession = UserSessionRule.standalone(); + @Rule + public DbTester db = DbTester.create(); + + private WsActionTester ws = new WsActionTester(new ListAction(db.getDbClient())); + + @Test + public void verify_definition() { + WebService.Action action = ws.getDef(); + assertThat(action.since()).isEqualTo("4.3"); + assertThat(action.key()).isEqualTo("list"); + assertThat(action.isPost()).isFalse(); + assertThat(action.isInternal()).isFalse(); + assertThat(action.changelog()).extracting(Change::getVersion, Change::getDescription) + .containsExactlyInAnyOrder( + tuple("7.0", "'isDefault' field is added on quality gate level"), + tuple("7.0", "'default' field on root level is deprecated")); + assertThat(action.params()).isEmpty(); + } + + @Test + public void json_example() { + QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate("Sonar way"); + db.qualityGates().insertQualityGate("Sonar way - Without Coverage"); + db.qualityGates().setDefaultQualityGate(defaultQualityGate); + + String response = ws.newRequest() + .execute() + .getInput(); + + assertJson(response).ignoreFields("id", "default") + .isSimilarTo(getClass().getResource("list-example.json")); + } + + @Test + public void list_quality_gates() { + QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate("Sonar way"); + db.qualityGates().insertQualityGate("Sonar way - Without Coverage"); + db.qualityGates().setDefaultQualityGate(defaultQualityGate); + + String response = ws.newRequest() + .execute() + .getInput(); + + assertJson(response).ignoreFields("id", "default") + .isSimilarTo("{\n" + + " \"qualitygates\": [\n" + + " {\n" + + " \"name\": \"Sonar way\",\n" + + " \"isDefault\": true\n" + + " },\n" + + " {\n" + + " \"id\": 4,\n" + + " \"name\": \"Sonar way - Without Coverage\",\n" + + " \"isDefault\": false\n" + + " }\n" + + " ]\n" + + "}\n"); + } + + @Test + public void no_default_quality_gate() { + db.qualityGates().insertQualityGate("Sonar way"); + + String response = ws.newRequest() + .execute() + .getInput(); + + assertJson(response).ignoreFields("id", "default") + .isSimilarTo("{\n" + + " \"qualitygates\": [\n" + + " {\n" + + " \"name\": \"Sonar way\",\n" + + " \"isDefault\": false\n" + + " }\n" + + " ]\n" + + "}\n"); + } + + @Test + public void empty() { + String response = ws.newRequest() + .execute() + .getInput(); + + assertJson(response).ignoreFields("id", "default") + .isSimilarTo("{\n" + + " \"qualitygates\": []\n" + + "}\n"); + } +} 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 f6779e98aa9..13d2cada045 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 @@ -20,7 +20,6 @@ package org.sonar.server.qualitygate.ws; import com.google.common.collect.ImmutableList; -import com.google.common.collect.Lists; import java.util.List; import org.junit.Before; import org.junit.Test; @@ -68,7 +67,6 @@ public class QualityGatesWsTest { SelectAction selectAction = new SelectAction(mock(DbClient.class), mock(UserSessionRule.class), mock(ComponentFinder.class)); tester = new WsTester(new QualityGatesWs( - new ListAction(qGates), new SearchAction(projectFinder), new CreateAction(null, null, null, null), new CopyAction(qGates), @@ -88,14 +86,7 @@ public class QualityGatesWsTest { assertThat(controller).isNotNull(); assertThat(controller.path()).isEqualTo("api/qualitygates"); assertThat(controller.description()).isNotEmpty(); - assertThat(controller.actions()).hasSize(14); - - Action list = controller.action("list"); - assertThat(list).isNotNull(); - assertThat(list.handler()).isNotNull(); - assertThat(list.since()).isEqualTo("4.3"); - assertThat(list.isPost()).isFalse(); - assertThat(list.isInternal()).isFalse(); + assertThat(controller.actions()).hasSize(13); Action create = controller.action("create"); assertThat(create).isNotNull(); @@ -226,28 +217,6 @@ public class QualityGatesWsTest { tester.newPostRequest("api/qualitygates", "destroy").setParam("id", "polop").execute(); } - @Test - public void list_nominal() throws Exception { - when(qGates.list()).thenReturn(Lists.newArrayList( - new QualityGateDto().setId(42L).setName("Golden"), - new QualityGateDto().setId(43L).setName("Star"), - new QualityGateDto().setId(666L).setName("Ninth"))); - tester.newGetRequest("api/qualitygates", "list").execute().assertJson( - "{\"qualitygates\":[{\"id\":42,\"name\":\"Golden\"},{\"id\":43,\"name\":\"Star\"},{\"id\":666,\"name\":\"Ninth\"}]}"); - } - - @Test - public void list_with_default() throws Exception { - QualityGateDto defaultQgate = new QualityGateDto().setId(42L).setName("Golden"); - when(qGates.list()).thenReturn(Lists.newArrayList( - defaultQgate, - new QualityGateDto().setId(43L).setName("Star"), - new QualityGateDto().setId(666L).setName("Ninth"))); - when(qGates.getDefault()).thenReturn(defaultQgate); - tester.newGetRequest("api/qualitygates", "list").execute().assertJson( - "{\"qualitygates\":[{\"id\":42,\"name\":\"Golden\",\"isDefault\":true},{\"id\":43,\"name\":\"Star\",\"isDefault\":false},{\"id\":666,\"name\":\"Ninth\",\"isDefault\":false}],\"default\":42}"); - } - @Test public void search_with_query() throws Exception { long gateId = 12345L; -- 2.39.5