From: Teryk Bellahsene Date: Mon, 1 Jun 2015 10:26:34 +0000 (+0200) Subject: SONAR-6570 ws api/metrics/search change endpoint to search X-Git-Tag: 5.2-RC1~1754 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b99b802f6857f6384653e84988b77fb88e58f2ef;p=sonarqube.git SONAR-6570 ws api/metrics/search change endpoint to search - change endpoint from 'list' to 'search' - add pagination in response - display only optional fields in WS description --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/metric/persistence/MetricDao.java b/server/sonar-server/src/main/java/org/sonar/server/metric/persistence/MetricDao.java index 8b8a01d3bec..175592fbff2 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/metric/persistence/MetricDao.java +++ b/server/sonar-server/src/main/java/org/sonar/server/metric/persistence/MetricDao.java @@ -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 index d77523a52be..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/metric/ws/ListAction.java +++ /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 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:" + - ""); - } - - @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 metrics = dbClient.metricDao().selectEnabled(dbSession, isCustom, searchOptions); - JsonWriter json = response.newJsonWriter(); - json.beginObject(); - Set desiredFields = desiredFields(request.paramAsStrings(Param.FIELDS)); - writeMetrics(json, metrics, desiredFields); - json.endObject(); - json.close(); - } finally { - MyBatis.closeQuietly(dbSession); - } - } - - private Set desiredFields(@Nullable List fields) { - if (fields == null || fields.isEmpty()) { - return POSSIBLE_FIELDS; - } - - return newHashSet(fields); - } - - private void writeMetrics(JsonWriter json, List metrics, Set 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 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 index 00000000000..a56f1838e8d --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/metric/ws/SearchAction.java @@ -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 OPTIONAL_FIELDS = newHashSet(FIELD_NAME, FIELD_DESCRIPTION, FIELD_DOMAIN, FIELD_TYPE); + private final Set ALL_POSSIBLE_FIELDS; + + private final DbClient dbClient; + + public SearchAction(DbClient dbClient) { + this.dbClient = dbClient; + Set 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:" + + "
    " + + "
  • true: only custom metrics are returned
  • " + + "
  • false: only non custom metrics are returned
  • " + + "
  • not specified: all metrics are returned
  • " + + "
"); + } + + @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 metrics = dbClient.metricDao().selectEnabled(dbSession, isCustom, searchOptions); + int nbMetrics = dbClient.metricDao().countCustom(dbSession); + JsonWriter json = response.newJsonWriter(); + json.beginObject(); + Set desiredFields = desiredFields(request.paramAsStrings(Param.FIELDS)); + writeMetrics(json, metrics, desiredFields); + searchOptions.writeJson(json, nbMetrics); + json.endObject(); + json.close(); + } finally { + MyBatis.closeQuietly(dbSession); + } + } + + private Set desiredFields(@Nullable List fields) { + if (fields == null || fields.isEmpty()) { + return ALL_POSSIBLE_FIELDS; + } + + return newHashSet(fields); + } + + private void writeMetrics(JsonWriter json, List metrics, Set 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 desiredFields) { + if (desiredFields.contains(field)) { + json.prop(field, value); + } + } +} 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 f05626b68c4..d374fe6102f 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 @@ -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 index ec30e61e418..00000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/metric/ws/ListActionTest.java +++ /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 index 00000000000..9a7038705ef --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/metric/ws/SearchActionTest.java @@ -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 index 4f5049c8aeb..00000000000 --- a/server/sonar-server/src/test/resources/org/sonar/server/metric/ws/ListActionTest/list_metrics.json +++ /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 index 00000000000..0bae892fc3d --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/metric/ws/SearchActionTest/search_metrics.json @@ -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 +} diff --git a/sonar-core/src/main/java/org/sonar/core/metric/db/MetricMapper.java b/sonar-core/src/main/java/org/sonar/core/metric/db/MetricMapper.java index 46eaf787e21..dc85301383a 100644 --- a/sonar-core/src/main/java/org/sonar/core/metric/db/MetricMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/metric/db/MetricMapper.java @@ -40,4 +40,6 @@ public interface MetricMapper { List selectByKeys(@Param("keys") List keys); void disable(@Param("ids") List ids); + + int countCustom(); } diff --git a/sonar-core/src/main/resources/org/sonar/core/metric/db/MetricMapper.xml b/sonar-core/src/main/resources/org/sonar/core/metric/db/MetricMapper.xml index 2e638309850..6b427f585ba 100644 --- a/sonar-core/src/main/resources/org/sonar/core/metric/db/MetricMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/metric/db/MetricMapper.xml @@ -48,6 +48,14 @@ ORDER BY UPPER(m.short_name) +