From: Teryk Bellahsene Date: Tue, 26 May 2015 13:42:27 +0000 (+0200) Subject: SONAR-6578 WS api/metrics/types list all metric types available X-Git-Tag: 5.2-RC1~1832 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=refs%2Fpull%2F330%2Fhead;p=sonarqube.git SONAR-6578 WS api/metrics/types list all metric types available --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/metric/ws/MetricsWs.java b/server/sonar-server/src/main/java/org/sonar/server/metric/ws/MetricsWs.java index 0d6f060fafc..64360c053c6 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/metric/ws/MetricsWs.java +++ b/server/sonar-server/src/main/java/org/sonar/server/metric/ws/MetricsWs.java @@ -25,6 +25,8 @@ import org.sonar.api.server.ws.WebService; public class MetricsWs implements WebService { + public static final String ENDPOINT = "api/metrics"; + private final MetricsWsAction[] actions; public MetricsWs(MetricsWsAction... actions) { @@ -33,7 +35,7 @@ public class MetricsWs implements WebService { @Override public void define(Context context) { - NewController controller = context.createController("api/metrics"); + NewController controller = context.createController(ENDPOINT); controller.setDescription("Metrics management"); controller.setSince("2.6"); diff --git a/server/sonar-server/src/main/java/org/sonar/server/metric/ws/TypesAction.java b/server/sonar-server/src/main/java/org/sonar/server/metric/ws/TypesAction.java new file mode 100644 index 00000000000..7c730f6193c --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/metric/ws/TypesAction.java @@ -0,0 +1,55 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.metric.ws; + +import org.sonar.api.measures.Metric; +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; + +public class TypesAction implements MetricsWsAction { + @Override + public void define(WebService.NewController context) { + context.createAction("types") + .setDescription("List all available metric types.") + .setResponseExample(getClass().getResource("example-types.json")) + .setSince("5.2") + .setHandler(this); + } + + @Override + public void handle(Request request, Response response) throws Exception { + JsonWriter json = response.newJsonWriter(); + json.beginObject(); + json.name("types"); + json.beginArray(); + for (Metric.ValueType metricType : Metric.ValueType.values()) { + json.beginObject(); + json.prop("key", metricType.name()); + json.prop("name", metricType.description()); + json.endObject(); + } + json.endArray(); + json.endObject(); + json.close(); + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java index 0972fecfe08..24463a6fd77 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java @@ -433,6 +433,7 @@ public class PlatformLevel4 extends PlatformLevel { ManualMeasuresWs.class, MetricsWs.class, org.sonar.server.metric.ws.ListAction.class, + org.sonar.server.metric.ws.TypesAction.class, // quality gates QualityGateDao.class, diff --git a/server/sonar-server/src/main/resources/org/sonar/server/metric/ws/example-types.json b/server/sonar-server/src/main/resources/org/sonar/server/metric/ws/example-types.json new file mode 100644 index 00000000000..6d3be5eced6 --- /dev/null +++ b/server/sonar-server/src/main/resources/org/sonar/server/metric/ws/example-types.json @@ -0,0 +1,20 @@ +{ + "types": [ + { + "key": "INT", + "name": "Integer" + }, + { + "key": "BOOL", + "name": "Yes/No" + }, + { + "key": "FLOAT", + "name": "Float" + }, + { + "key": "PERCENT", + "name": "Percent" + } + ] +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/metric/ws/TypesActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/metric/ws/TypesActionTest.java new file mode 100644 index 00000000000..d633d7d6fa3 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/metric/ws/TypesActionTest.java @@ -0,0 +1,48 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.metric.ws; + +import org.junit.Before; +import org.junit.Test; +import org.sonar.api.measures.Metric.ValueType; +import org.sonar.server.ws.WsTester; + +import static org.assertj.core.api.Assertions.assertThat; + +public class TypesActionTest { + + WsTester ws; + + @Before + public void setUp() throws Exception { + ws = new WsTester(new MetricsWs(new TypesAction())); + } + + @Test + public void validate_content() throws Exception { + String result = ws.newGetRequest(MetricsWs.ENDPOINT, "types").execute().outputAsString(); + + assertThat(result).contains( + ValueType.INT.name(), ValueType.INT.description(), + ValueType.PERCENT.name(), ValueType.PERCENT.description() + ); + } +}