@Test public void empty_result() { userSession.logIn(); SearchProjectsWsResponse result = call(request); assertThat(result.getComponentsCount()).isEqualTo(0); Common.Paging paging = result.getPaging(); assertThat(paging.getPageIndex()).isEqualTo(1); assertThat(paging.getPageSize()).isEqualTo(100); assertThat(paging.getTotal()).isEqualTo(0); }
@Test public void response_is_empty_on_provisioned_projects() { ComponentDto project = db.components().insertComponent(newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid")); logInWithBrowsePermission(project); TreeWsResponse response = ws.newRequest() .setParam(PARAM_COMPONENT_ID, "project-uuid").executeProtobuf(TreeWsResponse.class); assertThat(response.getBaseComponent().getId()).isEqualTo("project-uuid"); assertThat(response.getComponentsList()).isEmpty(); assertThat(response.getPaging().getTotal()).isEqualTo(0); assertThat(response.getPaging().getPageSize()).isEqualTo(100); assertThat(response.getPaging().getPageIndex()).isEqualTo(1); }
@Test public void paginate_result() { userSession.logIn(); OrganizationDto organization = db.organizations().insert(); IntStream.rangeClosed(1, 9).forEach(i -> insertProject(organization, c -> c.setName("PROJECT-" + i))); SearchProjectsWsResponse result = call(request.setPage(2).setPageSize(3)); assertThat(result.getPaging().getPageIndex()).isEqualTo(2); assertThat(result.getPaging().getPageSize()).isEqualTo(3); assertThat(result.getPaging().getTotal()).isEqualTo(9); assertThat(result.getComponentsCount()).isEqualTo(3); assertThat(result.getComponentsList()) .extracting(Component::getName) .containsExactly("PROJECT-4", "PROJECT-5", "PROJECT-6"); }
public ImmutableList<Issue> getAllIssuesFor(String resourceKey) { final ImmutableList.Builder<Issue> builder = ImmutableList.builder(); SearchWsRequest query = new SearchWsRequest(); query.setComponentRoots(singletonList(resourceKey)) .setResolved(false) .setPageSize(-1); IssuesService issuesService = sonarClient.issues(); SearchWsResponse response = issuesService.search(query); builder.addAll(response.getIssuesList()); Common.Paging paging = response.getPaging(); Integer total = paging.getTotal(); Integer pageSize = paging.getPageSize(); Integer pages = total / pageSize + (total % pageSize > 0 ? 1 : 0); for (int pageIndex = 2; pageIndex <= pages; pageIndex++) { final ProgressIndicator progressIndicator = ProgressManager.getInstance().getProgressIndicator(); if (progressIndicator.isCanceled()) break; final String pagesProgressMessage = String.format("%d / %d pages downloaded", pageIndex, pages); ProgressIndicatorUtil.setText(progressIndicator, pagesProgressMessage); ProgressIndicatorUtil.setFraction(progressIndicator, pageIndex * 1.0 / pages); query.setPage(pageIndex); builder.addAll(issuesService.search(query).getIssuesList()); } return builder.build(); } }
public List<Component> getAllProjects(WsClient sonarClient) { org.sonarqube.ws.client.component.SearchWsRequest query = new org.sonarqube.ws.client.component.SearchWsRequest() .setQualifiers(singletonList(SonarQualifier.PROJECT.getQualifier())) .setPageSize(500); //-1 is not allowed, neither int max. The limit is 500. List<WsComponents.Component> components = new ArrayList<>(); WsComponents.SearchWsResponse response = sonarClient.components().search(query); Common.Paging paging = response.getPaging(); int total = paging.getTotal(); int pageSize = paging.getPageSize(); int pages = total / pageSize + (total % pageSize > 0 ? 1 : 0); if (total > 0) { components.addAll(response.getComponentsList()); for (int pageIndex = 2; pageIndex <= pages; pageIndex++) { query.setPage(pageIndex); response = sonarClient.components().search(query); components.addAll(response.getComponentsList()); } } return Collections.unmodifiableList(components); }
@Override public void handle(Request request, Response response) throws Exception { boolean isMember = request.mandatoryParamAsBoolean(PARAM_MEMBER); if (isMember){ userSession.checkLoggedIn(); } try (DbSession dbSession = dbClient.openSession(false)) { OrganizationQuery dbQuery = buildDbQuery(request); int total = dbClient.organizationDao().countByQuery(dbSession, dbQuery); Paging paging = buildWsPaging(request, total); List<OrganizationDto> organizations = dbClient.organizationDao().selectByQuery(dbSession, dbQuery, forPage(paging.getPageIndex()).andSize(paging.getPageSize())); Set<String> adminOrganizationUuids = searchOrganizationWithAdminPermission(dbSession); writeResponse(request, response, organizations, adminOrganizationUuids, paging); } }
assertThat(response.getPaging().getPageSize()).isEqualTo(3); assertThat(response.getPaging().getTotal()).isEqualTo(9);
@Override public void handle(Request request, Response response) throws Exception { boolean onlyMembershipOrganizations = request.mandatoryParamAsBoolean(PARAM_MEMBER); if (onlyMembershipOrganizations) { userSession.checkLoggedIn(); } try (DbSession dbSession = dbClient.openSession(false)) { OrganizationQuery dbQuery = buildDbQuery(request); int total = dbClient.organizationDao().countByQuery(dbSession, dbQuery); Paging paging = buildWsPaging(request, total); List<OrganizationDto> organizations = dbClient.organizationDao().selectByQuery(dbSession, dbQuery, forPage(paging.getPageIndex()).andSize(paging.getPageSize())); Set<String> adminOrganizationUuids = searchOrganizationWithAdminPermission(dbSession); Set<String> provisionOrganizationUuids = searchOrganizationWithProvisionPermission(dbSession); Map<String, OrganizationAlmBindingDto> organizationAlmBindingByOrgUuid = dbClient.organizationAlmBindingDao().selectByOrganizations(dbSession, organizations) .stream().collect(MoreCollectors.uniqueIndex(OrganizationAlmBindingDto::getOrganizationUuid)); Organizations.SearchWsResponse wsResponse = buildOrganizations(organizations, adminOrganizationUuids, provisionOrganizationUuids, organizationAlmBindingByOrgUuid, onlyMembershipOrganizations, paging); writeProtobuf(wsResponse, request, response); } }
@Test public void search_with_pagination() { UserDto user = insertUser(); insertDefaultGroup("sonar-users", "Sonar Users"); for (int i = 1; i <= 9; i++) { GroupDto groupDto = insertGroup("group-" + i, "group-" + i); addUserToGroup(user, groupDto); } GroupsWsResponse response = call(ws.newRequest().setParam("login", USER_LOGIN) .setParam(Param.PAGE_SIZE, "3") .setParam(Param.PAGE, "2") .setParam(Param.SELECTED, ALL.value())); assertThat(response.getGroupsList()).extracting(GroupsWsResponse.Group::getName).containsOnly("group-4", "group-5", "group-6"); assertThat(response.getPaging().getPageSize()).isEqualTo(3); assertThat(response.getPaging().getPageIndex()).isEqualTo(2); assertThat(response.getPaging().getTotal()).isEqualTo(10); }