@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); }
private SearchWsResponse call(SearchRequest wsRequest) { TestRequest request = ws.newRequest(); ofNullable(wsRequest.getOrganization()).ifPresent(p3 -> request.setParam(PARAM_ORGANIZATION, p3)); ofNullable(wsRequest.getLanguage()).ifPresent(p2 -> request.setParam(PARAM_LANGUAGE, p2)); ofNullable(wsRequest.getQualifiers()).ifPresent(p1 -> request.setParam(PARAM_QUALIFIERS, Joiner.on(",").join(p1))); ofNullable(wsRequest.getQuery()).ifPresent(p -> request.setParam(TEXT_QUERY, p)); ofNullable(wsRequest.getPage()).ifPresent(page -> request.setParam(PAGE, String.valueOf(page))); ofNullable(wsRequest.getPageSize()).ifPresent(pageSize -> request.setParam(PAGE_SIZE, String.valueOf(pageSize))); return request.executeProtobuf(SearchWsResponse.class); }
@Test public void search_with_pagination() { OrganizationDto organizationDto = db.organizations().insert(); List<ComponentDto> componentDtoList = new ArrayList<>(); for (int i = 1; i <= 9; i++) { componentDtoList.add(newPrivateProjectDto(organizationDto, "project-uuid-" + i).setDbKey("project-key-" + i).setName("Project Name " + i)); } insertProjectsAuthorizedForUser(componentDtoList.toArray(new ComponentDto[] {})); SearchWsResponse response = call(new SearchRequest().setOrganization(organizationDto.getKey()).setPage(2).setPageSize(3).setQualifiers(singletonList(PROJECT))); assertThat(response.getComponentsList()).extracting(Component::getKey).containsExactly("project-key-4", "project-key-5", "project-key-6"); }
private static ComponentQuery buildEsQuery(OrganizationDto organization, SearchRequest request) { return ComponentQuery.builder() .setQuery(request.getQuery()) .setOrganization(organization.getUuid()) .setLanguage(request.getLanguage()) .setQualifiers(request.getQualifiers()) .build(); }
private OrganizationDto getOrganization(DbSession dbSession, SearchRequest request) { String organizationKey = Optional.ofNullable(request.getOrganization()) .orElseGet(defaultOrganizationProvider.get()::getKey); return WsUtils.checkFoundWithOptional( dbClient.organizationDao().selectByKey(dbSession, organizationKey), "No organizationDto with key '%s'", organizationKey); }
private SearchWsResponse doHandle(SearchRequest request) { try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = getOrganization(dbSession, request); ComponentQuery esQuery = buildEsQuery(organization, request); SearchIdResult<String> results = componentIndex.search(esQuery, new SearchOptions().setPage(request.getPage(), request.getPageSize())); List<ComponentDto> components = dbClient.componentDao().selectByUuids(dbSession, results.getIds()); Map<String, String> projectKeysByUuids = searchProjectsKeysByUuids(dbSession, components); return buildResponse(components, organization, projectKeysByUuids, Paging.forPageIndex(request.getPage()).withPageSize(request.getPageSize()).andTotal((int) results.getTotal())); } }
private static SearchRequest toSearchWsRequest(org.sonar.api.server.ws.Request request) { return new SearchRequest() .setOrganization(request.param(PARAM_ORGANIZATION)) .setQualifiers(request.mandatoryParamAsStrings(PARAM_QUALIFIERS)) .setLanguage(request.param(PARAM_LANGUAGE)) .setQuery(request.param(Param.TEXT_QUERY)) .setPage(request.mandatoryParamAsInt(Param.PAGE)) .setPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE)); }
@Test public void return_project_key() { ComponentDto project = ComponentTesting.newPublicProjectDto(db.getDefaultOrganization()); ComponentDto module = ComponentTesting.newModuleDto(project); ComponentDto file1 = newFileDto(module).setDbKey("file1"); ComponentDto file2 = newFileDto(module).setDbKey("file2"); ComponentDto file3 = newFileDto(project).setDbKey("file3"); db.components().insertComponents(project, module, file1, file2, file3); setBrowsePermissionOnUserAndIndex(project); SearchWsResponse response = call(new SearchRequest().setQualifiers(asList(PROJECT, MODULE, FILE))); assertThat(response.getComponentsList()).extracting(Component::getKey, Component::getProject) .containsOnly(tuple(project.getDbKey(), project.getDbKey()), tuple(module.getDbKey(), project.getDbKey()), tuple(file1.getDbKey(), project.getDbKey()), tuple(file2.getDbKey(), project.getDbKey()), tuple(file3.getDbKey(), project.getDbKey())); }
@Test public void fail_if_unknown_qualifier_provided() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Value of parameter 'qualifiers' (Unknown-Qualifier) must be one of: [BRC, DIR, FIL, TRK]"); call(new SearchRequest().setQualifiers(singletonList("Unknown-Qualifier"))); }
@Test public void does_not_return_branches() { ComponentDto project = db.components().insertMainBranch(); ComponentDto branch = db.components().insertProjectBranch(project); setBrowsePermissionOnUserAndIndex(project, branch); SearchWsResponse response = call(new SearchRequest().setQualifiers(asList(PROJECT, MODULE, FILE))); assertThat(response.getComponentsList()).extracting(Component::getKey) .containsOnly(project.getDbKey()); }
@Test public void search_for_files() { ComponentDto project = ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization()); ComponentDto file1 = newFileDto(project).setDbKey("file1"); ComponentDto file2 = newFileDto(project).setDbKey("file2"); db.components().insertComponents(project, file1, file2); setBrowsePermissionOnUserAndIndex(project); SearchWsResponse response = call(new SearchRequest().setQuery(file1.getDbKey()).setQualifiers(singletonList(FILE))); assertThat(response.getComponentsList()).extracting(Component::getKey).containsOnly(file1.getDbKey()); }
private SearchWsResponse doHandle(SearchRequest request) { try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = getOrganization(dbSession, request); ComponentQuery esQuery = buildEsQuery(organization, request); SearchIdResult<String> results = componentIndex.search(esQuery, new SearchOptions().setPage(request.getPage(), request.getPageSize())); List<ComponentDto> components = dbClient.componentDao().selectByUuids(dbSession, results.getIds()); Map<String, String> projectKeysByUuids = searchProjectsKeysByUuids(dbSession, components); return buildResponse(components, organization, projectKeysByUuids, Paging.forPageIndex(request.getPage()).withPageSize(request.getPageSize()).andTotal((int) results.getTotal())); } }
private OrganizationDto getOrganization(DbSession dbSession, SearchRequest request) { String organizationKey = Optional.ofNullable(request.getOrganization()) .orElseGet(defaultOrganizationProvider.get()::getKey); return WsUtils.checkFoundWithOptional( dbClient.organizationDao().selectByKey(dbSession, organizationKey), "No organizationDto with key '%s'", organizationKey); }
private static ComponentQuery buildEsQuery(OrganizationDto organization, SearchRequest request) { return ComponentQuery.builder() .setQuery(request.getQuery()) .setOrganization(organization.getUuid()) .setLanguage(request.getLanguage()) .setQualifiers(request.getQualifiers()) .build(); }
@Test public void search_with_language() { OrganizationDto organizationDto = db.organizations().insert(); insertProjectsAuthorizedForUser( ComponentTesting.newPrivateProjectDto(organizationDto).setDbKey("java-project").setLanguage("java"), ComponentTesting.newPrivateProjectDto(organizationDto).setDbKey("cpp-project").setLanguage("cpp")); SearchWsResponse response = call(new SearchRequest().setOrganization(organizationDto.getKey()).setLanguage("java").setQualifiers(singletonList(PROJECT))); assertThat(response.getComponentsList()).extracting(Component::getKey).containsOnly("java-project"); }
@Test public void fail_when_no_qualifier_provided() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("The 'qualifiers' parameter is missing"); call(new SearchRequest()); }
@Test public void search_by_key_query() { insertProjectsAuthorizedForUser( ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization()).setDbKey("project-_%-key"), ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization()).setDbKey("project-key-without-escaped-characters")); SearchWsResponse response = call(new SearchRequest().setQuery("project-_%-key").setQualifiers(singletonList(PROJECT))); assertThat(response.getComponentsList()).extracting(Component::getKey).containsOnly("project-_%-key"); }
private static SearchRequest toSearchWsRequest(org.sonar.api.server.ws.Request request) { return new SearchRequest() .setOrganization(request.param(PARAM_ORGANIZATION)) .setQualifiers(request.mandatoryParamAsStrings(PARAM_QUALIFIERS)) .setLanguage(request.param(PARAM_LANGUAGE)) .setQuery(request.param(Param.TEXT_QUERY)) .setPage(request.mandatoryParamAsInt(Param.PAGE)) .setPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE)); }