From 7e920abf329c269075164a0113e0a849981a4248 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Fri, 29 Jan 2016 12:21:26 +0100 Subject: [PATCH] SONAR-7135 WS api/measures/component_tree fix leaves strategy and qualifiers together --- .../sonar/server/component/ws/TreeAction.java | 4 ++-- .../measure/ws/ComponentTreeAction.java | 1 + .../measure/ws/ComponentTreeDataLoader.java | 6 +++--- .../server/component/ws/TreeActionTest.java | 2 +- .../measure/ws/ComponentTreeActionTest.java | 19 +++++++++++++++++++ 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ws/TreeAction.java b/server/sonar-server/src/main/java/org/sonar/server/component/ws/TreeAction.java index af421eb21b8..83619f1b501 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ws/TreeAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ws/TreeAction.java @@ -275,9 +275,9 @@ public class TreeAction implements ComponentsWsAction { return requestQualifiers; } - Sets.SetView intersectQualifiers = Sets.intersection(newHashSet(childrenQualifiers), newHashSet(requestQualifiers)); + Sets.SetView qualifiersIntersection = Sets.intersection(newHashSet(childrenQualifiers), newHashSet(requestQualifiers)); - return new ArrayList<>(intersectQualifiers); + return new ArrayList<>(qualifiersIntersection); } private static TreeWsRequest toTreeWsRequest(Request request) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeAction.java b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeAction.java index c1e08414784..37fbaec12c1 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeAction.java @@ -242,6 +242,7 @@ public class ComponentTreeAction implements MeasuresWsAction { .setPage(request.mandatoryParamAsInt(Param.PAGE)) .setPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE)) .setQuery(request.param(Param.TEXT_QUERY)); + checkRequest(componentTreeWsRequest.getPageSize() <= MAX_SIZE, "The '%s' parameter must be less than %d", Param.PAGE_SIZE, MAX_SIZE); String metricSortValue = componentTreeWsRequest.getMetricSort(); checkRequest(!componentTreeWsRequest.getMetricKeys().isEmpty(), "The '%s' parameter must contain at least one metric key", PARAM_METRIC_KEYS); checkRequest(metricSortValue == null ^ componentTreeWsRequest.getSort().contains(METRIC_SORT), diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeDataLoader.java b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeDataLoader.java index 79d26ae7666..3bb6254614a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeDataLoader.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeDataLoader.java @@ -31,6 +31,7 @@ import com.google.common.collect.Sets; import com.google.common.collect.Table; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -296,10 +297,9 @@ public class ComponentTreeDataLoader { return requestQualifiers; } - // intersection of request and children qualifiers - childrenQualifiers.retainAll(requestQualifiers); + Sets.SetView qualifiersIntersection = Sets.intersection(new HashSet<>(childrenQualifiers), new HashSet(requestQualifiers)); - return childrenQualifiers; + return new ArrayList<>(qualifiersIntersection); } private ComponentTreeQuery toComponentTreeQuery(ComponentTreeWsRequest wsRequest, SnapshotDto baseSnapshot) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ws/TreeActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ws/TreeActionTest.java index bcb08222ea4..b2616105467 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/ws/TreeActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/ws/TreeActionTest.java @@ -89,7 +89,7 @@ public class TreeActionTest { userSession.setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN); ws = new WsActionTester(new TreeAction(dbClient, new ComponentFinder(dbClient), resourceTypes, userSession, Mockito.mock(I18n.class))); resourceTypes.setChildrenQualifiers(Qualifiers.MODULE, Qualifiers.FILE, Qualifiers.DIRECTORY); - resourceTypes.setLeavesQualifiers(Qualifiers.FILE); + resourceTypes.setLeavesQualifiers(Qualifiers.FILE, Qualifiers.UNIT_TEST_FILE); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java index 2c55552ce10..b466058fdaa 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java @@ -75,6 +75,8 @@ import static org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_ADDITIO import static org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_BASE_COMPONENT_ID; import static org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_METRIC_KEYS; import static org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_METRIC_SORT; +import static org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_QUALIFIERS; +import static org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_STRATEGY; @Category(DbTests.class) public class ComponentTreeActionTest { @@ -99,6 +101,8 @@ public class ComponentTreeActionTest { @Before public void setUp() { userSession.setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN); + resourceTypes.setChildrenQualifiers(Qualifiers.MODULE, Qualifiers.FILE, Qualifiers.DIRECTORY); + resourceTypes.setLeavesQualifiers(Qualifiers.FILE, Qualifiers.UNIT_TEST_FILE); } @Test @@ -237,6 +241,8 @@ public class ComponentTreeActionTest { .setParam(Param.SORT, NAME_SORT + ", " + METRIC_SORT) .setParam(PARAM_METRIC_SORT, "coverage") .setParam(PARAM_METRIC_KEYS, "coverage") + .setParam(PARAM_STRATEGY, "leaves") + .setParam(PARAM_QUALIFIERS, "FIL,UTS") .setParam(Param.PAGE, "2") .setParam(Param.PAGE_SIZE, "3")); @@ -349,6 +355,19 @@ public class ComponentTreeActionTest { .setParam(Param.SORT, METRIC_SORT)); } + @Test + public void fail_when_paging_parameter_is_too_big() { + componentDb.insertProjectAndSnapshot(newProjectDto("project-uuid")); + insertNclocMetric(); + expectedException.expect(BadRequestException.class); + expectedException.expectMessage("The 'ps' parameter must be less than 500"); + + call(ws.newRequest() + .setParam(PARAM_BASE_COMPONENT_ID, "project-uuid") + .setParam(PARAM_METRIC_KEYS, "ncloc") + .setParam(Param.PAGE_SIZE, "2540")); + } + private static ComponentTreeWsResponse call(TestRequest request) { TestResponse testResponse = request .setMediaType(MediaTypes.PROTOBUF) -- 2.39.5