From e32f87cfcc6d09c0e25a1e7628664b750bb22a24 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Fri, 13 May 2016 19:11:15 +0200 Subject: [PATCH] SONAR-7576 WS api/measures/component_tree paging fixed when components with measures only --- .../measure/ws/ComponentTreeDataLoader.java | 30 ++++++++++--------- .../measure/ws/ComponentTreeActionTest.java | 2 ++ 2 files changed, 18 insertions(+), 14 deletions(-) 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 3878a6491ac..196e21d2bf9 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 @@ -43,9 +43,7 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.ResourceTypes; -import org.sonar.api.utils.Paging; import org.sonar.api.web.UserRole; -import org.sonar.core.permission.GlobalPermissions; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.component.ComponentDto; @@ -70,6 +68,7 @@ import static java.lang.String.format; import static java.util.Collections.emptyMap; import static java.util.Collections.singletonList; import static java.util.Objects.requireNonNull; +import static org.sonar.api.utils.Paging.offset; import static org.sonar.server.component.ComponentFinder.ParamNames.BASE_COMPONENT_ID_AND_KEY; import static org.sonar.server.component.ComponentFinder.ParamNames.DEVELOPER_ID_AND_KEY; import static org.sonar.server.measure.ws.ComponentTreeAction.ALL_STRATEGY; @@ -113,7 +112,6 @@ public class ComponentTreeDataLoader { ComponentTreeQuery dbQuery = toComponentTreeQuery(wsRequest, baseSnapshot); ComponentDtosAndTotal componentDtosAndTotal = searchComponents(dbSession, dbQuery, wsRequest); List components = componentDtosAndTotal.componentDtos; - int componentCount = componentDtosAndTotal.total; List metrics = searchMetrics(dbSession, wsRequest); List periods = snapshotToWsPeriods(baseSnapshot); Table measuresByComponentUuidAndMetric = searchMeasuresByComponentUuidAndMetric(dbSession, baseComponent, baseSnapshot, components, metrics, @@ -121,7 +119,8 @@ public class ComponentTreeDataLoader { components = filterComponents(components, measuresByComponentUuidAndMetric, metrics, wsRequest); components = sortComponents(components, wsRequest, metrics, measuresByComponentUuidAndMetric); - components = paginateComponents(components, componentCount, wsRequest); + int componentCount = computeComponentCount(componentDtosAndTotal.total, components, componentWithMeasuresOnly(wsRequest)); + components = paginateComponents(components, wsRequest); Map referenceComponentsById = searchReferenceComponentsById(dbSession, components); return ComponentTreeData.builder() @@ -138,6 +137,10 @@ public class ComponentTreeDataLoader { } } + private int computeComponentCount(int dbComponentCount, List components, boolean returnOnlyComponentsWithMeasures) { + return returnOnlyComponentsWithMeasures ? components.size() : dbComponentCount; + } + @CheckForNull private Long searchDeveloperId(DbSession dbSession, ComponentTreeWsRequest wsRequest) { if (wsRequest.getDeveloperId() == null && wsRequest.getDeveloperKey() == null) { @@ -256,9 +259,8 @@ public class ComponentTreeDataLoader { } private static List filterComponents(List components, - Table measuresByComponentUuidAndMetric, - List metrics, ComponentTreeWsRequest wsRequest) { - if (!WITH_MEASURES_ONLY_METRIC_SORT_FILTER.equals(wsRequest.getMetricSortFilter())) { + Table measuresByComponentUuidAndMetric, List metrics, ComponentTreeWsRequest wsRequest) { + if (!componentWithMeasuresOnly(wsRequest)) { return components; } @@ -271,6 +273,10 @@ public class ComponentTreeDataLoader { .toList(); } + private static boolean componentWithMeasuresOnly(ComponentTreeWsRequest wsRequest) { + return WITH_MEASURES_ONLY_METRIC_SORT_FILTER.equals(wsRequest.getMetricSortFilter()); + } + private static List sortComponents(List components, ComponentTreeWsRequest wsRequest, List metrics, Table measuresByComponentUuidAndMetric) { if (!isSortByMetric(wsRequest)) { @@ -280,18 +286,14 @@ public class ComponentTreeDataLoader { return ComponentTreeSort.sortComponents(components, wsRequest, metrics, measuresByComponentUuidAndMetric); } - private static List paginateComponents(List components, int componentCount, ComponentTreeWsRequest wsRequest) { + private static List paginateComponents(List components, ComponentTreeWsRequest wsRequest) { if (!isSortByMetric(wsRequest)) { return components; } - Paging paging = Paging.forPageIndex(wsRequest.getPage()) - .withPageSize(wsRequest.getPageSize()) - .andTotal(componentCount); - return from(components) - .skip(paging.offset()) - .limit(paging.pageSize()) + .skip(offset(wsRequest.getPage(), wsRequest.getPageSize())) + .limit(wsRequest.getPageSize()) .toList(); } 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 f523642673f..fd6553a4c68 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 @@ -277,6 +277,7 @@ public class ComponentTreeActionTest { .setParam(PARAM_METRIC_KEYS, "ncloc")); assertThat(response.getComponentsList()).extracting("id").containsExactly("file-uuid-1", "file-uuid-2", "file-uuid-3", "file-uuid-4"); + assertThat(response.getPaging().getTotal()).isEqualTo(4); } @Test @@ -305,6 +306,7 @@ public class ComponentTreeActionTest { assertThat(response.getComponentsList()).extracting("id") .containsExactly("file-uuid-1", "file-uuid-2", "file-uuid-3") .doesNotContain("file-uuid-4"); + assertThat(response.getPaging().getTotal()).isEqualTo(3); } @Test -- 2.39.5