Search in sources :

Example 1 with SuggestionsWsResponse

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();
    }
}
Also used : ComponentDto(org.sonar.db.component.ComponentDto) SuggestionsWsResponse(org.sonarqube.ws.Components.SuggestionsWsResponse) ComponentIndexResults(org.sonar.server.component.index.ComponentIndexResults) DbSession(org.sonar.db.DbSession) SuggestionQuery(org.sonar.server.component.index.SuggestionQuery) Collection(java.util.Collection)

Example 2 with SuggestionsWsResponse

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();
}
Also used : ComponentDto(org.sonar.db.component.ComponentDto) SuggestionsWsResponse(org.sonarqube.ws.Components.SuggestionsWsResponse) Test(org.junit.Test)

Example 3 with SuggestionsWsResponse

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));
}
Also used : Category(org.sonarqube.ws.Components.SuggestionsWsResponse.Category) ComponentDto(org.sonar.db.component.ComponentDto) SuggestionsWsResponse(org.sonarqube.ws.Components.SuggestionsWsResponse) Test(org.junit.Test)

Example 4 with SuggestionsWsResponse

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();
}
Also used : PermissionIndexerTester(org.sonar.server.permission.index.PermissionIndexerTester) IntStream.range(java.util.stream.IntStream.range) ComponentIndexer(org.sonar.server.component.index.ComponentIndexer) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FILE(org.sonar.api.resources.Qualifiers.FILE) PARAM_RECENTLY_BROWSED(org.sonar.server.component.ws.SuggestionsAction.PARAM_RECENTLY_BROWSED) Collections.singletonList(java.util.Collections.singletonList) WebService(org.sonar.api.server.ws.WebService) Arrays.asList(java.util.Arrays.asList) PARAM_MORE(org.sonar.server.component.ws.SuggestionsAction.PARAM_MORE) ComponentTesting.newPublicProjectDto(org.sonar.db.component.ComponentTesting.newPublicProjectDto) ComponentTesting(org.sonar.db.component.ComponentTesting) ResourceTypesRule(org.sonar.db.component.ResourceTypesRule) Mockito.doReturn(org.mockito.Mockito.doReturn) FavoriteFinder(org.sonar.server.favorite.FavoriteFinder) DbTester(org.sonar.db.DbTester) APP(org.sonar.api.resources.Qualifiers.APP) System2(org.sonar.api.utils.System2) SuggestionsWsResponse(org.sonarqube.ws.Components.SuggestionsWsResponse) Collectors(java.util.stream.Collectors) Suggestion(org.sonarqube.ws.Components.SuggestionsWsResponse.Suggestion) Collectors.joining(java.util.stream.Collectors.joining) ComponentDto(org.sonar.db.component.ComponentDto) List(java.util.List) Stream(java.util.stream.Stream) VIEW(org.sonar.api.resources.Qualifiers.VIEW) SUBVIEW(org.sonar.api.resources.Qualifiers.SUBVIEW) Mockito.mock(org.mockito.Mockito.mock) EsTester(org.sonar.server.es.EsTester) SHORT_INPUT_WARNING(org.sonar.server.component.ws.SuggestionsAction.SHORT_INPUT_WARNING) PROJECT(org.sonar.api.resources.Qualifiers.PROJECT) WebAuthorizationTypeSupport(org.sonar.server.permission.index.WebAuthorizationTypeSupport) MediaTypes(org.sonarqube.ws.MediaTypes) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) Nullable(javax.annotation.Nullable) Before(org.junit.Before) UserSessionRule(org.sonar.server.tester.UserSessionRule) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) USER(org.sonar.api.web.UserRole.USER) Optional.ofNullable(java.util.Optional.ofNullable) TestRequest(org.sonar.server.ws.TestRequest) PARAM_QUERY(org.sonar.server.component.ws.SuggestionsAction.PARAM_QUERY) JsonAssert.assertJson(org.sonar.test.JsonAssert.assertJson) Test(org.junit.Test) WsActionTester(org.sonar.server.ws.WsActionTester) ComponentTesting.newPrivateProjectDto(org.sonar.db.component.ComponentTesting.newPrivateProjectDto) Rule(org.junit.Rule) Qualifiers(org.sonar.api.resources.Qualifiers) UNIT_TEST_FILE(org.sonar.api.resources.Qualifiers.UNIT_TEST_FILE) Change(org.sonar.api.server.ws.Change) ComponentIndex(org.sonar.server.component.index.ComponentIndex) Category(org.sonarqube.ws.Components.SuggestionsWsResponse.Category) MODULE(org.sonar.api.resources.Qualifiers.MODULE) Tuple.tuple(org.assertj.core.groups.Tuple.tuple) TestResponse(org.sonar.server.ws.TestResponse) ComponentTesting.newModuleDto(org.sonar.db.component.ComponentTesting.newModuleDto) ComponentDto(org.sonar.db.component.ComponentDto) SuggestionsWsResponse(org.sonarqube.ws.Components.SuggestionsWsResponse) Test(org.junit.Test)

Example 5 with SuggestionsWsResponse

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()));
}
Also used : Category(org.sonarqube.ws.Components.SuggestionsWsResponse.Category) ComponentDto(org.sonar.db.component.ComponentDto) SuggestionsWsResponse(org.sonarqube.ws.Components.SuggestionsWsResponse) Test(org.junit.Test)

Aggregations

SuggestionsWsResponse (org.sonarqube.ws.Components.SuggestionsWsResponse)25 Test (org.junit.Test)23 ComponentDto (org.sonar.db.component.ComponentDto)23 Category (org.sonarqube.ws.Components.SuggestionsWsResponse.Category)17 Arrays.asList (java.util.Arrays.asList)4 Collections.singletonList (java.util.Collections.singletonList)4 List (java.util.List)4 Optional.ofNullable (java.util.Optional.ofNullable)4 Collectors (java.util.stream.Collectors)4 Collectors.joining (java.util.stream.Collectors.joining)4 IntStream.range (java.util.stream.IntStream.range)4 Stream (java.util.stream.Stream)4 Nullable (javax.annotation.Nullable)4 RandomStringUtils.randomAlphabetic (org.apache.commons.lang.RandomStringUtils.randomAlphabetic)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 Tuple.tuple (org.assertj.core.groups.Tuple.tuple)4 Before (org.junit.Before)4 Rule (org.junit.Rule)4 Mockito.doReturn (org.mockito.Mockito.doReturn)4 Mockito.mock (org.mockito.Mockito.mock)4