public class MetricsWs implements WebService {
+ public static final String ENDPOINT = "api/metrics";
+
private final MetricsWsAction[] actions;
public MetricsWs(MetricsWsAction... actions) {
@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");
--- /dev/null
+/*
+ * 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();
+ }
+}
ManualMeasuresWs.class,
MetricsWs.class,
org.sonar.server.metric.ws.ListAction.class,
+ org.sonar.server.metric.ws.TypesAction.class,
// quality gates
QualityGateDao.class,
--- /dev/null
+{
+ "types": [
+ {
+ "key": "INT",
+ "name": "Integer"
+ },
+ {
+ "key": "BOOL",
+ "name": "Yes/No"
+ },
+ {
+ "key": "FLOAT",
+ "name": "Float"
+ },
+ {
+ "key": "PERCENT",
+ "name": "Percent"
+ }
+ ]
+}
--- /dev/null
+/*
+ * 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()
+ );
+ }
+}