]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6570 ws api/metrics/search change endpoint to search
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Mon, 1 Jun 2015 10:26:34 +0000 (12:26 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Mon, 1 Jun 2015 10:29:01 +0000 (12:29 +0200)
- change endpoint from 'list' to 'search'
- add pagination in response
- display only optional fields in WS description

server/sonar-server/src/main/java/org/sonar/server/metric/persistence/MetricDao.java
server/sonar-server/src/main/java/org/sonar/server/metric/ws/ListAction.java [deleted file]
server/sonar-server/src/main/java/org/sonar/server/metric/ws/SearchAction.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java
server/sonar-server/src/test/java/org/sonar/server/metric/ws/ListActionTest.java [deleted file]
server/sonar-server/src/test/java/org/sonar/server/metric/ws/SearchActionTest.java [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/metric/ws/ListActionTest/list_metrics.json [deleted file]
server/sonar-server/src/test/resources/org/sonar/server/metric/ws/SearchActionTest/search_metrics.json [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/core/metric/db/MetricMapper.java
sonar-core/src/main/resources/org/sonar/core/metric/db/MetricMapper.xml

index 8b8a01d3bec983aa21116ce2594e816e2901825b..175592fbff2151e1e2445c6f3516e23d337d58de 100644 (file)
@@ -96,4 +96,8 @@ public class MetricDao implements DaoComponent {
       }
     });
   }
+
+  public int countCustom(DbSession session) {
+    return mapper(session).countCustom();
+  }
 }
diff --git a/server/sonar-server/src/main/java/org/sonar/server/metric/ws/ListAction.java b/server/sonar-server/src/main/java/org/sonar/server/metric/ws/ListAction.java
deleted file mode 100644 (file)
index d77523a..0000000
+++ /dev/null
@@ -1,128 +0,0 @@
-/*
- * 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.server.ws.WebService.Param;
-import org.sonar.api.utils.text.JsonWriter;
-import org.sonar.core.metric.db.MetricDto;
-import org.sonar.core.persistence.DbSession;
-import org.sonar.core.persistence.MyBatis;
-import org.sonar.server.db.DbClient;
-import org.sonar.server.es.SearchOptions;
-
-import javax.annotation.Nullable;
-
-import java.util.List;
-import java.util.Set;
-
-import static com.google.common.collect.Sets.newHashSet;
-
-public class ListAction implements MetricsWsAction {
-
-  public static final String PARAM_IS_CUSTOM = "is_custom";
-
-  public static final String FIELD_ID = "id";
-  public static final String FIELD_KEY = "key";
-  public static final String FIELD_NAME = "name";
-  public static final String FIELD_DESCRIPTION = "description";
-  public static final String FIELD_DOMAIN = "domain";
-  public static final String FIELD_TYPE = "type";
-  private static final Set<String> POSSIBLE_FIELDS = newHashSet(FIELD_ID, FIELD_KEY, FIELD_NAME, FIELD_DESCRIPTION, FIELD_DOMAIN, FIELD_TYPE);
-
-  private final DbClient dbClient;
-
-  public ListAction(DbClient dbClient) {
-    this.dbClient = dbClient;
-  }
-
-  @Override
-  public void define(WebService.NewController context) {
-    WebService.NewAction action = context.createAction("list")
-      .setSince("5.2")
-      .setResponseExample(getClass().getResource("example-list.json"))
-      .addPagingParams(100)
-      .addFieldsParam(POSSIBLE_FIELDS)
-      .setHandler(this);
-
-    action.createParam(PARAM_IS_CUSTOM)
-      .setExampleValue("true")
-      .setDescription("Choose custom metrics following 3 cases:" +
-        "<ul>" +
-        "<li>true: only custom metrics are returned</li>" +
-        "<li>false: only non custom metrics are returned</li>" +
-        "<li>not specified: all metrics are returned</li>" +
-        "</ul>");
-  }
-
-  @Override
-  public void handle(Request request, Response response) throws Exception {
-    SearchOptions searchOptions = new SearchOptions()
-      .setPage(request.mandatoryParamAsInt(Param.PAGE),
-        request.mandatoryParamAsInt(Param.PAGE_SIZE));
-    Boolean isCustom = request.paramAsBoolean(PARAM_IS_CUSTOM);
-    DbSession dbSession = dbClient.openSession(false);
-    try {
-      List<MetricDto> metrics = dbClient.metricDao().selectEnabled(dbSession, isCustom, searchOptions);
-      JsonWriter json = response.newJsonWriter();
-      json.beginObject();
-      Set<String> desiredFields = desiredFields(request.paramAsStrings(Param.FIELDS));
-      writeMetrics(json, metrics, desiredFields);
-      json.endObject();
-      json.close();
-    } finally {
-      MyBatis.closeQuietly(dbSession);
-    }
-  }
-
-  private Set<String> desiredFields(@Nullable List<String> fields) {
-    if (fields == null || fields.isEmpty()) {
-      return POSSIBLE_FIELDS;
-    }
-
-    return newHashSet(fields);
-  }
-
-  private void writeMetrics(JsonWriter json, List<MetricDto> metrics, Set<String> desiredFields) {
-    json.name("metrics");
-    json.beginArray();
-    for (MetricDto metric : metrics) {
-      json.beginObject();
-      json.prop(FIELD_ID, String.valueOf(metric.getId()));
-      json.prop(FIELD_KEY, metric.getKey());
-      writeIfDesired(json, FIELD_NAME, metric.getShortName(), desiredFields);
-      writeIfDesired(json, FIELD_DESCRIPTION, metric.getDescription(), desiredFields);
-      writeIfDesired(json, FIELD_DOMAIN, metric.getDomain(), desiredFields);
-      writeIfDesired(json, FIELD_TYPE, Metric.ValueType.descriptionOf(metric.getValueType()), desiredFields);
-      json.endObject();
-    }
-    json.endArray();
-  }
-
-  private void writeIfDesired(JsonWriter json, String field, String value, Set<String> desiredFields) {
-    if (desiredFields.contains(field)) {
-      json.prop(field, value);
-    }
-  }
-}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/metric/ws/SearchAction.java b/server/sonar-server/src/main/java/org/sonar/server/metric/ws/SearchAction.java
new file mode 100644 (file)
index 0000000..a56f183
--- /dev/null
@@ -0,0 +1,134 @@
+/*
+ * 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 java.util.List;
+import java.util.Set;
+import javax.annotation.Nullable;
+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.server.ws.WebService.Param;
+import org.sonar.api.utils.text.JsonWriter;
+import org.sonar.core.metric.db.MetricDto;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.core.persistence.MyBatis;
+import org.sonar.server.db.DbClient;
+import org.sonar.server.es.SearchOptions;
+
+import static com.google.common.collect.Sets.newHashSet;
+
+public class SearchAction implements MetricsWsAction {
+
+  private static final String ACTION = "search";
+
+  public static final String PARAM_IS_CUSTOM = "is_custom";
+
+  public static final String FIELD_ID = "id";
+  public static final String FIELD_KEY = "key";
+  public static final String FIELD_NAME = "name";
+  public static final String FIELD_DESCRIPTION = "description";
+  public static final String FIELD_DOMAIN = "domain";
+  public static final String FIELD_TYPE = "type";
+  private static final Set<String> OPTIONAL_FIELDS = newHashSet(FIELD_NAME, FIELD_DESCRIPTION, FIELD_DOMAIN, FIELD_TYPE);
+  private final Set<String> ALL_POSSIBLE_FIELDS;
+
+  private final DbClient dbClient;
+
+  public SearchAction(DbClient dbClient) {
+    this.dbClient = dbClient;
+    Set<String> possibleFields = newHashSet(FIELD_ID, FIELD_KEY);
+    possibleFields.addAll(OPTIONAL_FIELDS);
+    ALL_POSSIBLE_FIELDS = possibleFields;
+  }
+
+  @Override
+  public void define(WebService.NewController context) {
+    WebService.NewAction action = context.createAction(ACTION)
+      .setSince("5.2")
+      .setResponseExample(getClass().getResource("example-list.json"))
+      .addPagingParams(100)
+      .addFieldsParam(OPTIONAL_FIELDS)
+      .setHandler(this);
+
+    action.createParam(PARAM_IS_CUSTOM)
+      .setExampleValue("true")
+      .setDescription("Choose custom metrics following 3 cases:" +
+        "<ul>" +
+        "<li>true: only custom metrics are returned</li>" +
+        "<li>false: only non custom metrics are returned</li>" +
+        "<li>not specified: all metrics are returned</li>" +
+        "</ul>");
+  }
+
+  @Override
+  public void handle(Request request, Response response) throws Exception {
+    SearchOptions searchOptions = new SearchOptions()
+      .setPage(request.mandatoryParamAsInt(Param.PAGE),
+        request.mandatoryParamAsInt(Param.PAGE_SIZE));
+    Boolean isCustom = request.paramAsBoolean(PARAM_IS_CUSTOM);
+    DbSession dbSession = dbClient.openSession(false);
+    try {
+      List<MetricDto> metrics = dbClient.metricDao().selectEnabled(dbSession, isCustom, searchOptions);
+      int nbMetrics = dbClient.metricDao().countCustom(dbSession);
+      JsonWriter json = response.newJsonWriter();
+      json.beginObject();
+      Set<String> desiredFields = desiredFields(request.paramAsStrings(Param.FIELDS));
+      writeMetrics(json, metrics, desiredFields);
+      searchOptions.writeJson(json, nbMetrics);
+      json.endObject();
+      json.close();
+    } finally {
+      MyBatis.closeQuietly(dbSession);
+    }
+  }
+
+  private Set<String> desiredFields(@Nullable List<String> fields) {
+    if (fields == null || fields.isEmpty()) {
+      return ALL_POSSIBLE_FIELDS;
+    }
+
+    return newHashSet(fields);
+  }
+
+  private void writeMetrics(JsonWriter json, List<MetricDto> metrics, Set<String> desiredFields) {
+    json.name("metrics");
+    json.beginArray();
+    for (MetricDto metric : metrics) {
+      json.beginObject();
+      json.prop(FIELD_ID, String.valueOf(metric.getId()));
+      json.prop(FIELD_KEY, metric.getKey());
+      writeIfDesired(json, FIELD_NAME, metric.getShortName(), desiredFields);
+      writeIfDesired(json, FIELD_DESCRIPTION, metric.getDescription(), desiredFields);
+      writeIfDesired(json, FIELD_DOMAIN, metric.getDomain(), desiredFields);
+      writeIfDesired(json, FIELD_TYPE, Metric.ValueType.descriptionOf(metric.getValueType()), desiredFields);
+      json.endObject();
+    }
+    json.endArray();
+  }
+
+  private void writeIfDesired(JsonWriter json, String field, String value, Set<String> desiredFields) {
+    if (desiredFields.contains(field)) {
+      json.prop(field, value);
+    }
+  }
+}
index f05626b68c4fa701c60f18831be300cf7f88b0d4..d374fe6102fa9ab8d1fa381320d90fa703d56d95 100644 (file)
@@ -170,6 +170,7 @@ import org.sonar.server.measure.ws.ManualMeasuresWs;
 import org.sonar.server.measure.ws.TimeMachineWs;
 import org.sonar.server.metric.CoreCustomMetrics;
 import org.sonar.server.metric.ws.MetricsWs;
+import org.sonar.server.metric.ws.SearchAction;
 import org.sonar.server.notifications.NotificationCenter;
 import org.sonar.server.notifications.NotificationService;
 import org.sonar.server.permission.InternalPermissionService;
@@ -496,7 +497,7 @@ public class PlatformLevel4 extends PlatformLevel {
       TimeMachineWs.class,
       ManualMeasuresWs.class,
       MetricsWs.class,
-      org.sonar.server.metric.ws.ListAction.class,
+      SearchAction.class,
       org.sonar.server.metric.ws.TypesAction.class,
       org.sonar.server.metric.ws.DomainsAction.class,
       org.sonar.server.metric.ws.DeleteAction.class,
diff --git a/server/sonar-server/src/test/java/org/sonar/server/metric/ws/ListActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/metric/ws/ListActionTest.java
deleted file mode 100644 (file)
index ec30e61..0000000
+++ /dev/null
@@ -1,163 +0,0 @@
-/*
- * 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.apache.commons.lang.StringUtils;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.ClassRule;
-import org.junit.Test;
-import org.sonar.api.server.ws.WebService.Param;
-import org.sonar.core.metric.db.MetricDto;
-import org.sonar.core.persistence.DbSession;
-import org.sonar.core.persistence.DbTester;
-import org.sonar.server.db.DbClient;
-import org.sonar.server.metric.persistence.MetricDao;
-import org.sonar.server.ws.WsTester;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.sonar.server.metric.ws.ListAction.PARAM_IS_CUSTOM;
-import static org.sonar.server.metric.ws.MetricTesting.newMetricDto;
-
-public class ListActionTest {
-
-  @ClassRule
-  public static DbTester db = new DbTester();
-  DbClient dbClient;
-  DbSession dbSession;
-  WsTester ws;
-
-  @Before
-  public void setUp() {
-    dbClient = new DbClient(db.database(), db.myBatis(), new MetricDao());
-    dbSession = dbClient.openSession(false);
-    ws = new WsTester(new MetricsWs(new ListAction(dbClient)));
-    db.truncateTables();
-  }
-
-  @After
-  public void tearDown() {
-    dbSession.close();
-  }
-
-  @Test
-  public void list_metrics_in_database() throws Exception {
-    insertNewCustomMetric("1", "2", "3");
-
-    WsTester.Result result = newRequest().execute();
-
-    result.assertJson(getClass(), "list_metrics.json");
-  }
-
-  @Test
-  public void list_metrics_ordered_by_name_case_insensitive() throws Exception {
-    insertNewCustomMetric("3", "1", "2");
-
-    String firstResult = newRequest().setParam(Param.PAGE, "1").setParam(Param.PAGE_SIZE, "1").execute().outputAsString();
-    String secondResult = newRequest().setParam(Param.PAGE, "2").setParam(Param.PAGE_SIZE, "1").execute().outputAsString();
-    String thirdResult = newRequest().setParam(Param.PAGE, "3").setParam(Param.PAGE_SIZE, "1").execute().outputAsString();
-
-    assertThat(firstResult).contains("custom-key-1").doesNotContain("custom-key-2").doesNotContain("custom-key-3");
-    assertThat(secondResult).contains("custom-key-2").doesNotContain("custom-key-1").doesNotContain("custom-key-3");
-    assertThat(thirdResult).contains("custom-key-3").doesNotContain("custom-key-1").doesNotContain("custom-key-2");
-  }
-
-  @Test
-  public void list_metrics_with_pagination() throws Exception {
-    insertNewCustomMetric("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
-
-    WsTester.Result result = newRequest()
-      .setParam(Param.PAGE, "3")
-      .setParam(Param.PAGE_SIZE, "4")
-      .execute();
-
-    assertThat(StringUtils.countMatches(result.outputAsString(), "custom-key")).isEqualTo(2);
-  }
-
-  @Test
-  public void list_metric_with_is_custom_true() throws Exception {
-    insertNewCustomMetric("1", "2");
-    insertNewNonCustomMetric("3");
-
-    String result = newRequest()
-      .setParam(PARAM_IS_CUSTOM, "true").execute().outputAsString();
-
-    assertThat(result).contains("custom-key-1", "custom-key-2")
-      .doesNotContain("non-custom-key-3");
-  }
-
-  @Test
-  public void list_metric_with_is_custom_false() throws Exception {
-    insertNewCustomMetric("1", "2");
-    insertNewNonCustomMetric("3");
-
-    String result = newRequest()
-      .setParam(PARAM_IS_CUSTOM, "false").execute().outputAsString();
-
-    assertThat(result).doesNotContain("custom-key-1")
-      .doesNotContain("custom-key-2")
-      .contains("non-custom-key-3");
-  }
-
-  @Test
-  public void list_metric_with_chosen_fields() throws Exception {
-    insertNewCustomMetric("1");
-
-    String result = newRequest().setParam(Param.FIELDS, "name").execute().outputAsString();
-
-    assertThat(result).contains("id", "key", "name")
-      .doesNotContain("domain")
-      .doesNotContain("description")
-      .doesNotContain("type");
-  }
-
-  private void insertNewNonCustomMetric(String... ids) {
-    for (String id : ids) {
-      dbClient.metricDao().insert(dbSession, newMetricDto()
-        .setKey("non-custom-key-" + id)
-        .setEnabled(true)
-        .setUserManaged(false));
-    }
-    dbSession.commit();
-  }
-
-  private void insertNewCustomMetric(String... ids) {
-    for (String id : ids) {
-      dbClient.metricDao().insert(dbSession, newCustomMetric(id));
-    }
-    dbSession.commit();
-  }
-
-  private MetricDto newCustomMetric(String id) {
-    return newMetricDto()
-      .setKey("custom-key-" + id)
-      .setShortName("custom-name-" + id)
-      .setDomain("custom-domain-" + id)
-      .setDescription("custom-description-" + id)
-      .setValueType("INT")
-      .setUserManaged(true)
-      .setEnabled(true);
-  }
-
-  private WsTester.TestRequest newRequest() {
-    return ws.newGetRequest("api/metrics", "list");
-  }
-}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/metric/ws/SearchActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/metric/ws/SearchActionTest.java
new file mode 100644 (file)
index 0000000..9a70387
--- /dev/null
@@ -0,0 +1,163 @@
+/*
+ * 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.apache.commons.lang.StringUtils;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.sonar.api.server.ws.WebService.Param;
+import org.sonar.core.metric.db.MetricDto;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.core.persistence.DbTester;
+import org.sonar.server.db.DbClient;
+import org.sonar.server.metric.persistence.MetricDao;
+import org.sonar.server.ws.WsTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.server.metric.ws.SearchAction.PARAM_IS_CUSTOM;
+import static org.sonar.server.metric.ws.MetricTesting.newMetricDto;
+
+public class SearchActionTest {
+
+  @ClassRule
+  public static DbTester db = new DbTester();
+  DbClient dbClient;
+  DbSession dbSession;
+  WsTester ws;
+
+  @Before
+  public void setUp() {
+    dbClient = new DbClient(db.database(), db.myBatis(), new MetricDao());
+    dbSession = dbClient.openSession(false);
+    ws = new WsTester(new MetricsWs(new SearchAction(dbClient)));
+    db.truncateTables();
+  }
+
+  @After
+  public void tearDown() {
+    dbSession.close();
+  }
+
+  @Test
+  public void search_metrics_in_database() throws Exception {
+    insertNewCustomMetric("1", "2", "3");
+
+    WsTester.Result result = newRequest().execute();
+
+    result.assertJson(getClass(), "search_metrics.json");
+  }
+
+  @Test
+  public void search_metrics_ordered_by_name_case_insensitive() throws Exception {
+    insertNewCustomMetric("3", "1", "2");
+
+    String firstResult = newRequest().setParam(Param.PAGE, "1").setParam(Param.PAGE_SIZE, "1").execute().outputAsString();
+    String secondResult = newRequest().setParam(Param.PAGE, "2").setParam(Param.PAGE_SIZE, "1").execute().outputAsString();
+    String thirdResult = newRequest().setParam(Param.PAGE, "3").setParam(Param.PAGE_SIZE, "1").execute().outputAsString();
+
+    assertThat(firstResult).contains("custom-key-1").doesNotContain("custom-key-2").doesNotContain("custom-key-3");
+    assertThat(secondResult).contains("custom-key-2").doesNotContain("custom-key-1").doesNotContain("custom-key-3");
+    assertThat(thirdResult).contains("custom-key-3").doesNotContain("custom-key-1").doesNotContain("custom-key-2");
+  }
+
+  @Test
+  public void search_metrics_with_pagination() throws Exception {
+    insertNewCustomMetric("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
+
+    WsTester.Result result = newRequest()
+      .setParam(Param.PAGE, "3")
+      .setParam(Param.PAGE_SIZE, "4")
+      .execute();
+
+    assertThat(StringUtils.countMatches(result.outputAsString(), "custom-key")).isEqualTo(2);
+  }
+
+  @Test
+  public void list_metric_with_is_custom_true() throws Exception {
+    insertNewCustomMetric("1", "2");
+    insertNewNonCustomMetric("3");
+
+    String result = newRequest()
+      .setParam(PARAM_IS_CUSTOM, "true").execute().outputAsString();
+
+    assertThat(result).contains("custom-key-1", "custom-key-2")
+      .doesNotContain("non-custom-key-3");
+  }
+
+  @Test
+  public void list_metric_with_is_custom_false() throws Exception {
+    insertNewCustomMetric("1", "2");
+    insertNewNonCustomMetric("3");
+
+    String result = newRequest()
+      .setParam(PARAM_IS_CUSTOM, "false").execute().outputAsString();
+
+    assertThat(result).doesNotContain("custom-key-1")
+      .doesNotContain("custom-key-2")
+      .contains("non-custom-key-3");
+  }
+
+  @Test
+  public void list_metric_with_chosen_fields() throws Exception {
+    insertNewCustomMetric("1");
+
+    String result = newRequest().setParam(Param.FIELDS, "name").execute().outputAsString();
+
+    assertThat(result).contains("id", "key", "name")
+      .doesNotContain("domain")
+      .doesNotContain("description")
+      .doesNotContain("type");
+  }
+
+  private void insertNewNonCustomMetric(String... ids) {
+    for (String id : ids) {
+      dbClient.metricDao().insert(dbSession, newMetricDto()
+        .setKey("non-custom-key-" + id)
+        .setEnabled(true)
+        .setUserManaged(false));
+    }
+    dbSession.commit();
+  }
+
+  private void insertNewCustomMetric(String... ids) {
+    for (String id : ids) {
+      dbClient.metricDao().insert(dbSession, newCustomMetric(id));
+    }
+    dbSession.commit();
+  }
+
+  private MetricDto newCustomMetric(String id) {
+    return newMetricDto()
+      .setKey("custom-key-" + id)
+      .setShortName("custom-name-" + id)
+      .setDomain("custom-domain-" + id)
+      .setDescription("custom-description-" + id)
+      .setValueType("INT")
+      .setUserManaged(true)
+      .setEnabled(true);
+  }
+
+  private WsTester.TestRequest newRequest() {
+    return ws.newGetRequest("api/metrics", "search");
+  }
+}
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/metric/ws/ListActionTest/list_metrics.json b/server/sonar-server/src/test/resources/org/sonar/server/metric/ws/ListActionTest/list_metrics.json
deleted file mode 100644 (file)
index 4f5049c..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-{
-  "metrics":[
-    {
-      "key":"custom-key-1",
-      "name":"custom-name-1",
-      "type":"Integer",
-      "domain":"custom-domain-1",
-      "description":"custom-description-1"
-    },
-    {
-      "key":"custom-key-2",
-      "name":"custom-name-2",
-      "type":"Integer",
-      "domain":"custom-domain-2",
-      "description":"custom-description-2"
-    },
-    {
-      "key":"custom-key-3",
-      "name":"custom-name-3",
-      "type":"Integer",
-      "domain":"custom-domain-3",
-      "description":"custom-description-3"
-    }
-  ]
-}
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/metric/ws/SearchActionTest/search_metrics.json b/server/sonar-server/src/test/resources/org/sonar/server/metric/ws/SearchActionTest/search_metrics.json
new file mode 100644 (file)
index 0000000..0bae892
--- /dev/null
@@ -0,0 +1,28 @@
+{
+  "metrics":[
+    {
+      "key":"custom-key-1",
+      "name":"custom-name-1",
+      "type":"Integer",
+      "domain":"custom-domain-1",
+      "description":"custom-description-1"
+    },
+    {
+      "key":"custom-key-2",
+      "name":"custom-name-2",
+      "type":"Integer",
+      "domain":"custom-domain-2",
+      "description":"custom-description-2"
+    },
+    {
+      "key":"custom-key-3",
+      "name":"custom-name-3",
+      "type":"Integer",
+      "domain":"custom-domain-3",
+      "description":"custom-description-3"
+    }
+  ],
+  "total":3,
+  "ps":100,
+  "p":1
+}
index 46eaf787e211fb3b470daf47980b08f756bb56cc..dc85301383aaf01b8a532dd0cfa6e559a35cd1fd 100644 (file)
@@ -40,4 +40,6 @@ public interface MetricMapper {
   List<MetricDto> selectByKeys(@Param("keys") List<String> keys);
 
   void disable(@Param("ids") List<Integer> ids);
+
+  int countCustom();
 }
index 2e6383098501f6beb8f03ae2bff2e833cc30f276..6b427f585ba47a47aa67bc5fb0c9416370eefeee 100644 (file)
     </where>
     ORDER BY UPPER(m.short_name)
   </select>
+  <select id="countCustom" resultType="Integer">
+    SELECT COUNT(*)
+    FROM metrics m
+    <where>
+      AND m.enabled=${_true}
+      AND m.user_managed=${_true}
+    </where>
+  </select>
 
   <insert id="insert" parameterType="org.sonar.core.metric.db.MetricDto" useGeneratedKeys="true" keyColumn="id"
           keyProperty="id">