From 426de5143b2680781b815fdb1087b5b00edc0336 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Mon, 27 Nov 2017 14:44:59 +0100 Subject: [PATCH] SONAR-10088 Remove api/qualitygates/app action --- .../server/qualitygate/QualityGateModule.java | 2 - .../server/qualitygate/ws/AppAction.java | 97 -------- .../server/qualitygate/ws/app-example.json | 19 -- .../qualitygate/QualityGateModuleTest.java | 2 +- .../server/qualitygate/ws/AppActionTest.java | 226 ------------------ .../qualitygate/ws/QualityGatesWsTest.java | 11 +- 6 files changed, 3 insertions(+), 354 deletions(-) delete mode 100644 server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/AppAction.java delete mode 100644 server/sonar-server/src/main/resources/org/sonar/server/qualitygate/ws/app-example.json delete mode 100644 server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/AppActionTest.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateModule.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateModule.java index 6e97f2355f1..000a13ab856 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateModule.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateModule.java @@ -20,7 +20,6 @@ package org.sonar.server.qualitygate; import org.sonar.core.platform.Module; -import org.sonar.server.qualitygate.ws.AppAction; import org.sonar.server.qualitygate.ws.CopyAction; import org.sonar.server.qualitygate.ws.CreateAction; import org.sonar.server.qualitygate.ws.CreateConditionAction; @@ -66,7 +65,6 @@ public class QualityGateModule extends Module { CreateConditionAction.class, DeleteConditionAction.class, UpdateConditionAction.class, - AppAction.class, ProjectStatusAction.class, GetByProjectAction.class); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/AppAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/AppAction.java deleted file mode 100644 index e1851a29d4b..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/AppAction.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * 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 java.util.Collection; -import java.util.stream.Collectors; -import org.sonar.api.server.ws.Request; -import org.sonar.api.server.ws.Response; -import org.sonar.api.server.ws.WebService; -import org.sonar.db.DbClient; -import org.sonar.db.DbSession; -import org.sonar.db.metric.MetricDto; -import org.sonar.server.organization.DefaultOrganizationProvider; -import org.sonar.db.permission.OrganizationPermission; -import org.sonar.server.user.UserSession; -import org.sonarqube.ws.Qualitygates.AppResponse.Metric; - -import static org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY; -import static org.sonar.api.measures.Metric.ValueType.RATING; -import static org.sonar.server.qualitygate.ValidRatingMetrics.isCoreRatingMetric; -import static org.sonar.server.ws.WsUtils.writeProtobuf; -import static org.sonarqube.ws.Qualitygates.AppResponse; - -public class AppAction implements QualityGatesWsAction { - - private final UserSession userSession; - private final DbClient dbClient; - private final DefaultOrganizationProvider defaultOrganizationProvider; - - public AppAction(UserSession userSession, DbClient dbClient, DefaultOrganizationProvider defaultOrganizationProvider) { - this.userSession = userSession; - this.dbClient = dbClient; - this.defaultOrganizationProvider = defaultOrganizationProvider; - } - - @Override - public void define(WebService.NewController controller) { - controller.createAction("app") - .setInternal(true) - .setDescription("Get initialization items for the admin UI. For internal use") - .setResponseExample(getClass().getResource("app-example.json")) - .setSince("4.3") - .setHandler(this); - } - - @Override - public void handle(Request request, Response response) { - writeProtobuf(AppResponse.newBuilder() - .setEdit(userSession.hasPermission(OrganizationPermission.ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid())) - .addAllMetrics(loadMetrics() - .stream() - .map(AppAction::toMetric) - .collect(Collectors.toList())) - .build(), - request, response); - } - - private static Metric toMetric(MetricDto metricDto) { - String domain = metricDto.getDomain(); - Metric.Builder metricBuilder = Metric.newBuilder() - .setKey(metricDto.getKey()) - .setName(metricDto.getShortName()) - .setType(metricDto.getValueType()) - .setHidden(metricDto.isHidden()); - if (domain != null) { - metricBuilder.setDomain(domain); - } - return metricBuilder.build(); - } - - private Collection loadMetrics() { - try (DbSession dbSession = dbClient.openSession(false)) { - return dbClient.metricDao().selectEnabled(dbSession).stream() - .filter(metric -> !metric.isDataType() && !ALERT_STATUS_KEY.equals(metric.getKey()) && - (!RATING.name().equals(metric.getValueType()) || isCoreRatingMetric(metric.getKey()))) - .collect(Collectors.toList()); - } - } - -} diff --git a/server/sonar-server/src/main/resources/org/sonar/server/qualitygate/ws/app-example.json b/server/sonar-server/src/main/resources/org/sonar/server/qualitygate/ws/app-example.json deleted file mode 100644 index 96acddbf206..00000000000 --- a/server/sonar-server/src/main/resources/org/sonar/server/qualitygate/ws/app-example.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "edit": false, - "metrics": [ - { - "key": "accessors", - "name": "Accessors", - "type": "INT", - "domain": "Size", - "hidden": true - }, - { - "key": "blocker_remediation_cost", - "name": "Blocker Technical Debt", - "type": "WORK_DUR", - "domain": "SQALE", - "hidden": false - } - ] -} diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGateModuleTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGateModuleTest.java index 622d9fc6236..18a06580b42 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGateModuleTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGateModuleTest.java @@ -29,6 +29,6 @@ public class QualityGateModuleTest { public void verify_count_of_added_components() { ComponentContainer container = new ComponentContainer(); new QualityGateModule().configure(container); - assertThat(container.size()).isEqualTo(24 + 2); + assertThat(container.size()).isEqualTo(23 + 2); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/AppActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/AppActionTest.java deleted file mode 100644 index 2c019103772..00000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/AppActionTest.java +++ /dev/null @@ -1,226 +0,0 @@ -/* - * 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 java.util.List; -import org.junit.Rule; -import org.junit.Test; -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.metric.MetricDto; -import org.sonar.server.organization.DefaultOrganizationProvider; -import org.sonar.server.organization.TestDefaultOrganizationProvider; -import org.sonar.server.tester.UserSessionRule; -import org.sonar.server.ws.WsActionTester; -import org.sonarqube.ws.Qualitygates.AppResponse; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.api.measures.Metric.ValueType.BOOL; -import static org.sonar.api.measures.Metric.ValueType.DATA; -import static org.sonar.api.measures.Metric.ValueType.DISTRIB; -import static org.sonar.api.measures.Metric.ValueType.INT; -import static org.sonar.api.measures.Metric.ValueType.RATING; -import static org.sonar.api.measures.Metric.ValueType.WORK_DUR; -import static org.sonar.db.metric.MetricTesting.newMetricDto; -import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES; -import static org.sonar.test.JsonAssert.assertJson; -import static org.sonarqube.ws.MediaTypes.JSON; - -public class AppActionTest { - - @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 DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); - private AppAction underTest = new AppAction(userSession, dbClient, defaultOrganizationProvider); - private WsActionTester ws = new WsActionTester(underTest); - - @Test - public void return_metrics() throws Exception { - dbClient.metricDao().insert(dbSession, newMetricDto() - .setKey("metric") - .setShortName("Metric") - .setDomain("General") - .setValueType(BOOL.name()) - .setHidden(true)); - dbSession.commit(); - - AppResponse response = executeRequest(); - - List metrics = response.getMetricsList(); - assertThat(metrics).hasSize(1); - AppResponse.Metric metric = metrics.get(0); - assertThat(metric.getKey()).isEqualTo("metric"); - assertThat(metric.getName()).isEqualTo("Metric"); - assertThat(metric.getDomain()).isEqualTo("General"); - assertThat(metric.getType()).isEqualTo(BOOL.name()); - assertThat(metric.getHidden()).isTrue(); - } - - @Test - public void return_metric_without_domain() throws Exception { - dbClient.metricDao().insert(dbSession, newMetricDto() - .setKey("metric") - .setShortName("Metric") - .setDomain(null) - .setValueType(BOOL.name()) - .setHidden(true)); - dbSession.commit(); - - AppResponse response = executeRequest(); - - List metrics = response.getMetricsList(); - assertThat(metrics).hasSize(1); - AppResponse.Metric metric = metrics.get(0); - assertThat(metric.getKey()).isEqualTo("metric"); - assertThat(metric.hasDomain()).isFalse(); - } - - @Test - public void return_rating_metrics_only_from_core_metrics() throws Exception { - insertMetrics( - newMetricDto().setKey("reliability_rating").setValueType(RATING.name()).setHidden(false), - newMetricDto().setKey("new_reliability_rating").setValueType(RATING.name()).setHidden(false), - newMetricDto().setKey("sqale_rating").setValueType(RATING.name()).setHidden(false), - newMetricDto().setKey("none_core_rating").setValueType(RATING.name()).setHidden(false)); - - AppResponse response = executeRequest(); - - assertThat(response.getMetricsList()).extracting(AppResponse.Metric::getKey).containsOnly( - "reliability_rating", "new_reliability_rating", "sqale_rating"); - } - - @Test - public void does_not_return_DISTRIB_metric() throws Exception { - dbClient.metricDao().insert(dbSession, newMetricDto() - .setKey("function_complexity_distribution") - .setShortName("Function Distribution / Complexity") - .setDomain("Complexity") - .setValueType(DISTRIB.name()) - .setHidden(false)); - dbSession.commit(); - - AppResponse response = executeRequest(); - - assertThat(response.getMetricsList()).isEmpty(); - } - - @Test - public void does_not_return_DATA_metric() throws Exception { - dbClient.metricDao().insert(dbSession, newMetricDto() - .setKey("ncloc_language_distribution") - .setShortName("Lines of Code Per Language") - .setDomain("Size") - .setValueType(DATA.name()) - .setHidden(false)); - dbSession.commit(); - - AppResponse response = executeRequest(); - - assertThat(response.getMetricsList()).isEmpty(); - } - - @Test - public void does_not_return_quality_gate_metric() throws Exception { - dbClient.metricDao().insert(dbSession, newMetricDto() - .setKey("alert_status") - .setShortName("Quality Gate Status") - .setDomain("Releasability") - .setValueType(INT.name()) - .setHidden(false)); - dbSession.commit(); - - AppResponse response = executeRequest(); - - assertThat(response.getMetricsList()).isEmpty(); - } - - @Test - public void return_edit_to_false_when_not_quality_gate_permission() throws Exception { - userSession.logIn(); - - AppResponse response = executeRequest(); - - assertThat(response.getEdit()).isFalse(); - } - - @Test - public void return_edit_to_true_when_quality_gate_permission() throws Exception { - userSession.logIn().addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization()); - - AppResponse response = executeRequest(); - - assertThat(response.getEdit()).isTrue(); - } - - @Test - public void test_example_json_response() { - dbClient.metricDao().insert(dbSession, - newMetricDto() - .setKey("accessors") - .setShortName("Accessors") - .setDomain("Size") - .setValueType(INT.name()) - .setHidden(true), - newMetricDto() - .setKey("blocker_remediation_cost") - .setShortName("Blocker Technical Debt") - .setDomain("SQALE") - .setValueType(WORK_DUR.name()) - .setHidden(false)); - dbSession.commit(); - - String result = ws.newRequest() - .setMediaType(JSON) - .execute() - .getInput(); - - assertJson(ws.getDef().responseExampleAsString()).isSimilarTo(result); - } - - @Test - public void test_ws_definition() { - WebService.Action action = ws.getDef(); - assertThat(action).isNotNull(); - assertThat(action.isInternal()).isTrue(); - assertThat(action.isPost()).isFalse(); - assertThat(action.responseExampleAsString()).isNotEmpty(); - assertThat(action.params()).isEmpty(); - } - - private void insertMetrics(MetricDto... metricDtos) { - for (MetricDto metricDto : metricDtos) { - dbClient.metricDao().insert(dbSession, metricDto); - } - dbSession.commit(); - } - - private AppResponse executeRequest() { - return ws.newRequest().executeProtobuf(AppResponse.class); - } -} 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 20284ab9edf..60be5287542 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 @@ -57,9 +57,6 @@ public class QualityGatesWsTest { @Mock private QgateProjectFinder projectFinder; - @Mock - private AppAction appHandler; - WsTester tester; @Before @@ -76,8 +73,7 @@ public class QualityGatesWsTest { new UpdateConditionAction(null, null, null, null), new DeleteConditionAction(null, null, null), selectAction, - new DeselectAction(qGates, mock(DbClient.class), mock(ComponentFinder.class)), - new AppAction(null, null, null))); + new DeselectAction(qGates, mock(DbClient.class), mock(ComponentFinder.class)))); } @Test @@ -86,7 +82,7 @@ public class QualityGatesWsTest { assertThat(controller).isNotNull(); assertThat(controller.path()).isEqualTo("api/qualitygates"); assertThat(controller.description()).isNotEmpty(); - assertThat(controller.actions()).hasSize(12); + assertThat(controller.actions()).hasSize(11); Action create = controller.action("create"); assertThat(create).isNotNull(); @@ -153,9 +149,6 @@ public class QualityGatesWsTest { assertThat(updateCondition.param("error")).isNotNull(); assertThat(updateCondition.param("period")).isNotNull(); assertThat(updateCondition.isInternal()).isFalse(); - - Action appInit = controller.action("app"); - assertThat(appInit.isInternal()).isTrue(); } @Test -- 2.39.5