use of org.sonarqube.ws.Components.SuggestionsWsResponse in project sonarqube by SonarSource.
the class SuggestionsAction method loadSuggestionsWithSearch.
private SuggestionsWsResponse loadSuggestionsWithSearch(String query, int skip, int limit, Set<String> recentlyBrowsedKeys, List<String> qualifiers) {
if (split(query).noneMatch(token -> token.length() >= MINIMUM_NGRAM_LENGTH)) {
SuggestionsWsResponse.Builder queryBuilder = newBuilder();
getWarning(query).ifPresent(queryBuilder::setWarning);
return queryBuilder.build();
}
List<ComponentDto> favorites = favoriteFinder.list();
Set<String> favoriteKeys = favorites.stream().map(ComponentDto::getDbKey).collect(MoreCollectors.toSet(favorites.size()));
SuggestionQuery.Builder queryBuilder = SuggestionQuery.builder().setQuery(query).setRecentlyBrowsedKeys(recentlyBrowsedKeys).setFavoriteKeys(favoriteKeys).setQualifiers(qualifiers).setSkip(skip).setLimit(limit);
ComponentIndexResults componentsPerQualifiers = searchInIndex(queryBuilder.build());
if (componentsPerQualifiers.isEmpty()) {
return newBuilder().build();
}
try (DbSession dbSession = dbClient.openSession(false)) {
Set<String> componentUuids = componentsPerQualifiers.getQualifiers().map(ComponentHitsPerQualifier::getHits).flatMap(Collection::stream).map(ComponentHit::getUuid).collect(toSet());
List<ComponentDto> componentDtos = dbClient.componentDao().selectByUuids(dbSession, componentUuids);
Set<String> favoriteUuids = favorites.stream().map(ComponentDto::uuid).collect(MoreCollectors.toSet(favorites.size()));
SuggestionsWsResponse.Builder searchWsResponse = buildResponse(recentlyBrowsedKeys, favoriteUuids, componentsPerQualifiers, dbSession, componentDtos, skip + limit);
getWarning(query).ifPresent(searchWsResponse::setWarning);
return searchWsResponse.build();
}
}
use of org.sonarqube.ws.Components.SuggestionsWsResponse in project sonarqube by SonarSource.
the class SuggestionsActionTest method suggestions_without_query_should_not_contain_matches_that_are_neither_favorites_nor_recently_browsed.
@Test
public void suggestions_without_query_should_not_contain_matches_that_are_neither_favorites_nor_recently_browsed() {
ComponentDto project = db.components().insertComponent(newPrivateProjectDto());
componentIndexer.indexAll();
userSessionRule.addProjectPermission(USER, project);
SuggestionsWsResponse response = ws.newRequest().setMethod("POST").executeProtobuf(SuggestionsWsResponse.class);
// assert match in qualifier "TRK"
assertThat(response.getResultsList()).filteredOn(q -> q.getItemsCount() > 0).extracting(Category::getQ).isEmpty();
}
use of org.sonarqube.ws.Components.SuggestionsWsResponse in project sonarqube by SonarSource.
the class SuggestionsActionTest method suggestions_without_query_should_contain_recently_browsed.
@Test
public void suggestions_without_query_should_contain_recently_browsed() {
ComponentDto project = db.components().insertComponent(newPrivateProjectDto());
componentIndexer.indexAll();
userSessionRule.addProjectPermission(USER, project);
SuggestionsWsResponse response = ws.newRequest().setMethod("POST").setParam(PARAM_RECENTLY_BROWSED, project.getDbKey()).executeProtobuf(SuggestionsWsResponse.class);
// assert match in qualifier "TRK"
assertThat(response.getResultsList()).filteredOn(q -> q.getItemsCount() > 0).extracting(Category::getQ).containsExactly(PROJECT);
// assert correct id to be found
assertThat(response.getResultsList()).flatExtracting(Category::getItemsList).extracting(Suggestion::getKey, Suggestion::getIsRecentlyBrowsed).containsExactly(tuple(project.getDbKey(), true));
}
use of org.sonarqube.ws.Components.SuggestionsWsResponse in project sonarqube by SonarSource.
the class SuggestionsActionTest method should_not_return_suggestion_on_non_existing_project.
@Test
public void should_not_return_suggestion_on_non_existing_project() {
ComponentDto project = db.components().insertComponent(newPrivateProjectDto());
componentIndexer.indexAll();
authorizationIndexerTester.allowOnlyAnyone(project);
db.getDbClient().purgeDao().deleteProject(db.getSession(), project.uuid(), PROJECT, project.name(), project.getKey());
db.commit();
SuggestionsWsResponse response = ws.newRequest().setMethod("POST").setParam(PARAM_QUERY, project.getDbKey()).executeProtobuf(SuggestionsWsResponse.class);
// assert match in qualifier "TRK"
assertThat(response.getResultsList()).filteredOn(q -> q.getItemsCount() > 0).isEmpty();
}
use of org.sonarqube.ws.Components.SuggestionsWsResponse in project sonarqube by SonarSource.
the class SuggestionsActionTest method should_contain_component_names.
@Test
public void should_contain_component_names() {
ComponentDto project1 = db.components().insertComponent(newPrivateProjectDto().setName("Project1"));
componentIndexer.indexOnAnalysis(project1.projectUuid());
authorizationIndexerTester.allowOnlyAnyone(project1);
SuggestionsWsResponse response = ws.newRequest().setMethod("POST").setParam(PARAM_QUERY, "Project").executeProtobuf(SuggestionsWsResponse.class);
assertThat(response.getResultsList()).flatExtracting(Category::getItemsList).extracting(Suggestion::getKey, Suggestion::getName).containsExactlyInAnyOrder(tuple(project1.getDbKey(), project1.name()));
}
Aggregations