Skip to content

Commit

Permalink
SONAR-6570 ws api/metrics/search change endpoint to search
Browse files Browse the repository at this point in the history
- change endpoint from 'list' to 'search'
- add pagination in response
- display only optional fields in WS description
  • Loading branch information
teryk committed Jun 1, 2015
1 parent ccb1bc6 commit b99b802
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 21 deletions.
Expand Up @@ -96,4 +96,8 @@ public Void apply(@Nonnull List<Integer> input) {
} }
}); });
} }

public int countCustom(DbSession session) {
return mapper(session).countCustom();
}
} }
Expand Up @@ -20,6 +20,9 @@


package org.sonar.server.metric.ws; 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.measures.Metric;
import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.Response;
Expand All @@ -32,14 +35,11 @@
import org.sonar.server.db.DbClient; import org.sonar.server.db.DbClient;
import org.sonar.server.es.SearchOptions; 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; import static com.google.common.collect.Sets.newHashSet;


public class ListAction implements MetricsWsAction { public class SearchAction implements MetricsWsAction {

private static final String ACTION = "search";


public static final String PARAM_IS_CUSTOM = "is_custom"; public static final String PARAM_IS_CUSTOM = "is_custom";


Expand All @@ -49,21 +49,25 @@ public class ListAction implements MetricsWsAction {
public static final String FIELD_DESCRIPTION = "description"; public static final String FIELD_DESCRIPTION = "description";
public static final String FIELD_DOMAIN = "domain"; public static final String FIELD_DOMAIN = "domain";
public static final String FIELD_TYPE = "type"; 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 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; private final DbClient dbClient;


public ListAction(DbClient dbClient) { public SearchAction(DbClient dbClient) {
this.dbClient = dbClient; this.dbClient = dbClient;
Set<String> possibleFields = newHashSet(FIELD_ID, FIELD_KEY);
possibleFields.addAll(OPTIONAL_FIELDS);
ALL_POSSIBLE_FIELDS = possibleFields;
} }


@Override @Override
public void define(WebService.NewController context) { public void define(WebService.NewController context) {
WebService.NewAction action = context.createAction("list") WebService.NewAction action = context.createAction(ACTION)
.setSince("5.2") .setSince("5.2")
.setResponseExample(getClass().getResource("example-list.json")) .setResponseExample(getClass().getResource("example-list.json"))
.addPagingParams(100) .addPagingParams(100)
.addFieldsParam(POSSIBLE_FIELDS) .addFieldsParam(OPTIONAL_FIELDS)
.setHandler(this); .setHandler(this);


action.createParam(PARAM_IS_CUSTOM) action.createParam(PARAM_IS_CUSTOM)
Expand All @@ -85,10 +89,12 @@ public void handle(Request request, Response response) throws Exception {
DbSession dbSession = dbClient.openSession(false); DbSession dbSession = dbClient.openSession(false);
try { try {
List<MetricDto> metrics = dbClient.metricDao().selectEnabled(dbSession, isCustom, searchOptions); List<MetricDto> metrics = dbClient.metricDao().selectEnabled(dbSession, isCustom, searchOptions);
int nbMetrics = dbClient.metricDao().countCustom(dbSession);
JsonWriter json = response.newJsonWriter(); JsonWriter json = response.newJsonWriter();
json.beginObject(); json.beginObject();
Set<String> desiredFields = desiredFields(request.paramAsStrings(Param.FIELDS)); Set<String> desiredFields = desiredFields(request.paramAsStrings(Param.FIELDS));
writeMetrics(json, metrics, desiredFields); writeMetrics(json, metrics, desiredFields);
searchOptions.writeJson(json, nbMetrics);
json.endObject(); json.endObject();
json.close(); json.close();
} finally { } finally {
Expand All @@ -98,7 +104,7 @@ public void handle(Request request, Response response) throws Exception {


private Set<String> desiredFields(@Nullable List<String> fields) { private Set<String> desiredFields(@Nullable List<String> fields) {
if (fields == null || fields.isEmpty()) { if (fields == null || fields.isEmpty()) {
return POSSIBLE_FIELDS; return ALL_POSSIBLE_FIELDS;
} }


return newHashSet(fields); return newHashSet(fields);
Expand Down
Expand Up @@ -170,6 +170,7 @@
import org.sonar.server.measure.ws.TimeMachineWs; import org.sonar.server.measure.ws.TimeMachineWs;
import org.sonar.server.metric.CoreCustomMetrics; import org.sonar.server.metric.CoreCustomMetrics;
import org.sonar.server.metric.ws.MetricsWs; 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.NotificationCenter;
import org.sonar.server.notifications.NotificationService; import org.sonar.server.notifications.NotificationService;
import org.sonar.server.permission.InternalPermissionService; import org.sonar.server.permission.InternalPermissionService;
Expand Down Expand Up @@ -496,7 +497,7 @@ protected void configureLevel() {
TimeMachineWs.class, TimeMachineWs.class,
ManualMeasuresWs.class, ManualMeasuresWs.class,
MetricsWs.class, MetricsWs.class,
org.sonar.server.metric.ws.ListAction.class, SearchAction.class,
org.sonar.server.metric.ws.TypesAction.class, org.sonar.server.metric.ws.TypesAction.class,
org.sonar.server.metric.ws.DomainsAction.class, org.sonar.server.metric.ws.DomainsAction.class,
org.sonar.server.metric.ws.DeleteAction.class, org.sonar.server.metric.ws.DeleteAction.class,
Expand Down
Expand Up @@ -34,10 +34,10 @@
import org.sonar.server.ws.WsTester; import org.sonar.server.ws.WsTester;


import static org.assertj.core.api.Assertions.assertThat; 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.SearchAction.PARAM_IS_CUSTOM;
import static org.sonar.server.metric.ws.MetricTesting.newMetricDto; import static org.sonar.server.metric.ws.MetricTesting.newMetricDto;


public class ListActionTest { public class SearchActionTest {


@ClassRule @ClassRule
public static DbTester db = new DbTester(); public static DbTester db = new DbTester();
Expand All @@ -49,7 +49,7 @@ public class ListActionTest {
public void setUp() { public void setUp() {
dbClient = new DbClient(db.database(), db.myBatis(), new MetricDao()); dbClient = new DbClient(db.database(), db.myBatis(), new MetricDao());
dbSession = dbClient.openSession(false); dbSession = dbClient.openSession(false);
ws = new WsTester(new MetricsWs(new ListAction(dbClient))); ws = new WsTester(new MetricsWs(new SearchAction(dbClient)));
db.truncateTables(); db.truncateTables();
} }


Expand All @@ -59,16 +59,16 @@ public void tearDown() {
} }


@Test @Test
public void list_metrics_in_database() throws Exception { public void search_metrics_in_database() throws Exception {
insertNewCustomMetric("1", "2", "3"); insertNewCustomMetric("1", "2", "3");


WsTester.Result result = newRequest().execute(); WsTester.Result result = newRequest().execute();


result.assertJson(getClass(), "list_metrics.json"); result.assertJson(getClass(), "search_metrics.json");
} }


@Test @Test
public void list_metrics_ordered_by_name_case_insensitive() throws Exception { public void search_metrics_ordered_by_name_case_insensitive() throws Exception {
insertNewCustomMetric("3", "1", "2"); insertNewCustomMetric("3", "1", "2");


String firstResult = newRequest().setParam(Param.PAGE, "1").setParam(Param.PAGE_SIZE, "1").execute().outputAsString(); String firstResult = newRequest().setParam(Param.PAGE, "1").setParam(Param.PAGE_SIZE, "1").execute().outputAsString();
Expand All @@ -81,7 +81,7 @@ public void list_metrics_ordered_by_name_case_insensitive() throws Exception {
} }


@Test @Test
public void list_metrics_with_pagination() throws Exception { public void search_metrics_with_pagination() throws Exception {
insertNewCustomMetric("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"); insertNewCustomMetric("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");


WsTester.Result result = newRequest() WsTester.Result result = newRequest()
Expand Down Expand Up @@ -158,6 +158,6 @@ private MetricDto newCustomMetric(String id) {
} }


private WsTester.TestRequest newRequest() { private WsTester.TestRequest newRequest() {
return ws.newGetRequest("api/metrics", "list"); return ws.newGetRequest("api/metrics", "search");
} }
} }
Expand Up @@ -21,5 +21,8 @@
"domain":"custom-domain-3", "domain":"custom-domain-3",
"description":"custom-description-3" "description":"custom-description-3"
} }
] ],
"total":3,
"ps":100,
"p":1
} }
Expand Up @@ -40,4 +40,6 @@ public interface MetricMapper {
List<MetricDto> selectByKeys(@Param("keys") List<String> keys); List<MetricDto> selectByKeys(@Param("keys") List<String> keys);


void disable(@Param("ids") List<Integer> ids); void disable(@Param("ids") List<Integer> ids);

int countCustom();
} }
Expand Up @@ -48,6 +48,14 @@
</where> </where>
ORDER BY UPPER(m.short_name) ORDER BY UPPER(m.short_name)
</select> </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" <insert id="insert" parameterType="org.sonar.core.metric.db.MetricDto" useGeneratedKeys="true" keyColumn="id"
keyProperty="id"> keyProperty="id">
Expand Down

0 comments on commit b99b802

Please sign in to comment.