]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6578 WS api/metrics/types list all metric types available 330/head
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 26 May 2015 13:42:27 +0000 (15:42 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 26 May 2015 14:20:02 +0000 (16:20 +0200)
server/sonar-server/src/main/java/org/sonar/server/metric/ws/MetricsWs.java
server/sonar-server/src/main/java/org/sonar/server/metric/ws/TypesAction.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java
server/sonar-server/src/main/resources/org/sonar/server/metric/ws/example-types.json [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/metric/ws/TypesActionTest.java [new file with mode: 0644]

index 0d6f060fafcb43594038eca797c712bafbb1d553..64360c053c6defa873899ca569b149e591956303 100644 (file)
@@ -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 (file)
index 0000000..7c730f6
--- /dev/null
@@ -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();
+  }
+}
index 0972fecfe08030e55db3bd807d01d98e6f2a88e3..24463a6fd773c741319ce3b98da58d889609fe56 100644 (file)
@@ -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 (file)
index 0000000..6d3be5e
--- /dev/null
@@ -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 (file)
index 0000000..d633d7d
--- /dev/null
@@ -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()
+      );
+  }
+}