@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 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); }
private static <F, G> void processPage(String baseUrl, CheckedFunction<InputStream, G> responseParser, Function<G, Paging> getPaging, Function<G, List<F>> itemExtractor, Consumer<F> itemConsumer, boolean limitToTwentyPages, ProgressWrapper progress, AtomicInteger page, AtomicBoolean stop, AtomicInteger loaded, WsResponse response) throws IOException { G protoBufResponse = responseParser.apply(response.contentStream()); List<F> items = itemExtractor.apply(protoBufResponse); for (F item : items) { itemConsumer.accept(item); loaded.incrementAndGet(); } boolean isEmpty = items.isEmpty(); Paging paging = getPaging.apply(protoBufResponse); // SONAR-9150 Some WS used to miss the paging information, so iterate until response is empty stop.set(isEmpty || (paging.getTotal() > 0 && page.get() * PAGE_SIZE >= paging.getTotal())); if (!stop.get() && limitToTwentyPages && page.get() >= MAX_PAGES) { stop.set(true); LOG.debug("Limiting number of requested pages from '{}' to {}. Some of the data won't be fetched", baseUrl, MAX_PAGES); } progress.setProgressAndCheckCancel("Page " + page, loaded.get() / (float) paging.getTotal()); }
assertThat(response.getPaging().getPageIndex()).isEqualTo(2); assertThat(response.getPaging().getPageSize()).isEqualTo(3); assertThat(response.getPaging().getTotal()).isEqualTo(9);
.containsExactly(file1.uuid(), file2.uuid(), file3.uuid()) .doesNotContain(file4.uuid()); assertThat(response.getPaging().getTotal()).isEqualTo(3);
@Test public void return_children() { ComponentDto project = newPrivateProjectDto(db.organizations().insert(), "project-uuid"); db.components().insertProjectAndSnapshot(project); ComponentDto module = newModuleDto("module-uuid-1", project); db.components().insertComponent(module); db.components().insertComponent(newFileDto(project, 1)); for (int i = 2; i <= 9; i++) { db.components().insertComponent(newFileDto(module, i)); } ComponentDto directory = newDirectory(module, "directory-path-1"); db.components().insertComponent(directory); db.components().insertComponent(newFileDto(module, directory, 10)); db.commit(); logInWithBrowsePermission(project); TreeWsResponse response = ws.newRequest() .setParam(PARAM_STRATEGY, "children") .setParam(PARAM_COMPONENT_ID, "module-uuid-1") .setParam(Param.PAGE, "2") .setParam(Param.PAGE_SIZE, "3") .setParam(Param.TEXT_QUERY, "file-name") .setParam(Param.ASCENDING, "false") .setParam(Param.SORT, "name").executeProtobuf(TreeWsResponse.class); assertThat(response.getComponentsCount()).isEqualTo(3); assertThat(response.getPaging().getTotal()).isEqualTo(8); assertThat(response.getComponentsList()).extracting("id").containsExactly("file-uuid-6", "file-uuid-5", "file-uuid-4"); }
@Test public void return_descendants() { ComponentDto project = newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid"); SnapshotDto projectSnapshot = db.components().insertProjectAndSnapshot(project); ComponentDto module = newModuleDto("module-uuid-1", project); db.components().insertComponent(module); db.components().insertComponent(newFileDto(project, 10)); for (int i = 2; i <= 9; i++) { db.components().insertComponent(newFileDto(module, i)); } ComponentDto directory = newDirectory(module, "directory-path-1"); db.components().insertComponent(directory); db.components().insertComponent(newFileDto(module, directory, 1)); db.commit(); logInWithBrowsePermission(project); TreeWsResponse response = ws.newRequest() .setParam(PARAM_STRATEGY, "all") .setParam(PARAM_COMPONENT_ID, "module-uuid-1") .setParam(Param.PAGE, "2") .setParam(Param.PAGE_SIZE, "3") .setParam(Param.TEXT_QUERY, "file-name") .setParam(Param.ASCENDING, "true") .setParam(Param.SORT, "path").executeProtobuf(TreeWsResponse.class); assertThat(response.getComponentsCount()).isEqualTo(3); assertThat(response.getPaging().getTotal()).isEqualTo(9); assertThat(response.getComponentsList()).extracting("id").containsExactly("file-uuid-4", "file-uuid-5", "file-uuid-6"); }
@Test public void return_leaves() { ComponentDto project = newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid"); db.components().insertProjectAndSnapshot(project); ComponentDto module = newModuleDto("module-uuid-1", project); db.components().insertComponent(module); db.components().insertComponent(newFileDto(project, 1)); db.components().insertComponent(newFileDto(module, 2)); ComponentDto directory = newDirectory(project, "directory-path-1"); db.components().insertComponent(directory); db.components().insertComponent(newFileDto(module, directory, 3)); db.commit(); logInWithBrowsePermission(project); TreeWsResponse response = ws.newRequest() .setParam(PARAM_STRATEGY, "leaves") .setParam(PARAM_COMPONENT_ID, "project-uuid") .setParam(PARAM_QUALIFIERS, FILE).executeProtobuf(TreeWsResponse.class); assertThat(response.getComponentsCount()).isEqualTo(3); assertThat(response.getPaging().getTotal()).isEqualTo(3); assertThat(response.getComponentsList()).extracting("id").containsExactly("file-uuid-1", "file-uuid-2", "file-uuid-3"); }
@Test public void sort_by_metric_value() { ComponentDto project = db.components().insertPrivateProject(); SnapshotDto projectSnapshot = db.components().insertSnapshot(project); ComponentDto file4 = db.components().insertComponent(newFileDto(project, null, "file-uuid-4")); ComponentDto file3 = db.components().insertComponent(newFileDto(project, null, "file-uuid-3")); ComponentDto file1 = db.components().insertComponent(newFileDto(project, null, "file-uuid-1")); ComponentDto file2 = db.components().insertComponent(newFileDto(project, null, "file-uuid-2")); MetricDto ncloc = newMetricDto().setKey("ncloc").setValueType(INT.name()).setDirection(1); dbClient.metricDao().insert(dbSession, ncloc); db.commit(); db.measures().insertLiveMeasure(file1, ncloc, m -> m.setValue(1.0d)); db.measures().insertLiveMeasure(file2, ncloc, m -> m.setValue(2.0d)); db.measures().insertLiveMeasure(file3, ncloc, m -> m.setValue(3.0d)); ComponentTreeWsResponse response = ws.newRequest() .setParam(PARAM_COMPONENT, project.getKey()) .setParam(SORT, METRIC_SORT) .setParam(PARAM_METRIC_SORT, "ncloc") .setParam(PARAM_METRIC_KEYS, "ncloc") .executeProtobuf(ComponentTreeWsResponse.class); 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 public void validate_pagination_first_page() { for (int i = 0; i < 12; i++) { webhookDeliveryDbTester.insert(newDto().setComponentUuid(project.uuid()).setCeTaskUuid("t1").setWebhookUuid("wh-1-uuid")); } userSession.logIn().addProjectPermission(UserRole.ADMIN, project); Webhooks.DeliveriesWsResponse response = ws.newRequest() .setParam("webhook", "wh-1-uuid") .setParam("p", "1") .setParam("ps", "10") .executeProtobuf(Webhooks.DeliveriesWsResponse.class); assertThat(response.getDeliveriesCount()).isEqualTo(10); assertThat(response.getPaging().getTotal()).isEqualTo(12); assertThat(response.getPaging().getPageIndex()).isEqualTo(1); }
@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); }
@Test public void validate_pagination_last_page() { for (int i = 0; i < 12; i++) { webhookDeliveryDbTester.insert(newDto().setComponentUuid(project.uuid()).setCeTaskUuid("t1").setWebhookUuid("wh-1-uuid")); } userSession.logIn().addProjectPermission(UserRole.ADMIN, project); Webhooks.DeliveriesWsResponse response = ws.newRequest() .setParam("webhook", "wh-1-uuid") .setParam("p", "2") .setParam("ps", "10") .executeProtobuf(Webhooks.DeliveriesWsResponse.class); assertThat(response.getDeliveriesCount()).isEqualTo(2); assertThat(response.getPaging().getTotal()).isEqualTo(12); assertThat(response.getPaging().getPageIndex()).isEqualTo(2); }
@Test public void return_only_components_from_projects_on_which_user_has_browse_permission() { ComponentDto project1 = ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization()); ComponentDto file1 = newFileDto(project1).setDbKey("file1"); ComponentDto file2 = newFileDto(project1).setDbKey("file2"); ComponentDto project2 = ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization()); ComponentDto file3 = newFileDto(project2).setDbKey("file3"); db.components().insertComponents(project1, file1, file2, project2, file3); setBrowsePermissionOnUserAndIndex(project1); SearchWsResponse response = call(new SearchRequest().setQualifiers(singletonList(FILE))); assertThat(response.getComponentsList()).extracting(Component::getKey) .containsExactlyInAnyOrder(file1.getDbKey(), file2.getDbKey()); assertThat(response.getPaging().getTotal()).isEqualTo(2); }
@Test public void check_projects_facet_max_size() { RuleDefinitionDto rule = db.rules().insert(); IntStream.rangeClosed(1, 110) .forEach(i -> { ComponentDto project = db.components().insertPublicProject(); db.issues().insert(rule, project, project); }); indexPermissions(); indexIssues(); SearchWsResponse response = ws.newRequest() .setParam(FACETS, "projects") .executeProtobuf(SearchWsResponse.class); assertThat(response.getPaging().getTotal()).isEqualTo(110); assertThat(response.getFacets().getFacets(0).getValuesCount()).isEqualTo(100); }
@Test public void empty_search() { SearchWsResponse response = ws.newRequest().executeProtobuf(SearchWsResponse.class); assertThat(response) .extracting(SearchWsResponse::getIssuesList, r -> r.getPaging().getTotal()) .containsExactlyInAnyOrder(Collections.emptyList(), 0); }
@Test public void do_not_return_unprocessed_analyses() { dbClient.snapshotDao().insert(dbSession, newAnalysis(project).setStatus(STATUS_UNPROCESSED)); db.commit(); SearchHistoryResponse result = call(); // one analysis in setUp method assertThat(result.getPaging().getTotal()).isEqualTo(1); }
@Test public void empty_search_with_unknown_branch() { SearchWsResponse response = ws.newRequest() .setParam("onComponentOnly", "true") .setParam("componentKeys", "foo") .setParam("branch", "bar") .executeProtobuf(SearchWsResponse.class); assertThat(response) .extracting(SearchWsResponse::getIssuesList, r -> r.getPaging().getTotal()) .containsExactlyInAnyOrder(Collections.emptyList(), 0); }
@Test public void return_empty_result_when_no_user() { userSession.logIn(); SearchWsResponse response = ws.newRequest() .executeProtobuf(SearchWsResponse.class); assertThat(response.getUsersList()).isEmpty(); assertThat(response.getPaging().getTotal()).isZero(); }