Search in sources :

Example 1 with Suggestion

use of org.sonarqube.ws.Components.SuggestionsWsResponse.Suggestion in project sonarqube by SonarSource.

the class SuggestionsActionTest method should_only_provide_project_for_certain_qualifiers.

@Test
public void should_only_provide_project_for_certain_qualifiers() {
    String query = randomAlphabetic(10);
    ComponentDto app = db.components().insertPublicApplication(v -> v.setName(query));
    ComponentDto view = db.components().insertPublicPortfolio(v -> v.setName(query));
    ComponentDto subView = db.components().insertComponent(ComponentTesting.newSubPortfolio(view).setName(query));
    ComponentDto project = db.components().insertPrivateProject(p -> p.setName(query));
    ComponentDto module = db.components().insertComponent(ComponentTesting.newModuleDto(project).setName(query));
    ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(module).setName(query));
    ComponentDto test = db.components().insertComponent(ComponentTesting.newFileDto(module).setName(query).setQualifier(UNIT_TEST_FILE));
    componentIndexer.indexAll();
    authorizationIndexerTester.allowOnlyAnyone(project);
    authorizationIndexerTester.allowOnlyAnyone(view);
    authorizationIndexerTester.allowOnlyAnyone(app);
    SuggestionsWsResponse response = ws.newRequest().setMethod("POST").setParam(PARAM_QUERY, project.name()).executeProtobuf(SuggestionsWsResponse.class);
    assertThat(response.getResultsList()).extracting(Category::getQ, c -> c.getItemsList().stream().map(Suggestion::hasProject).findFirst().orElse(null)).containsExactlyInAnyOrder(tuple(SuggestionCategory.APP.getName(), false), tuple(SuggestionCategory.VIEW.getName(), false), tuple(SuggestionCategory.SUBVIEW.getName(), false), tuple(SuggestionCategory.PROJECT.getName(), false));
}
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) Suggestion(org.sonarqube.ws.Components.SuggestionsWsResponse.Suggestion) ComponentDto(org.sonar.db.component.ComponentDto) SuggestionsWsResponse(org.sonarqube.ws.Components.SuggestionsWsResponse) Test(org.junit.Test)

Example 2 with Suggestion

use of org.sonarqube.ws.Components.SuggestionsWsResponse.Suggestion in project sonarqube by SonarSource.

the class SuggestionsAction method toSuggestion.

/**
 * @return null when the component exists in Elasticsearch but not in database. That
 * occurs when failed indexing requests are been recovering.
 */
@CheckForNull
private static Suggestion toSuggestion(ComponentHit hit, Set<String> recentlyBrowsedKeys, Set<String> favoriteUuids, Map<String, ComponentDto> componentsByUuids, Map<String, ComponentDto> projectsByUuids) {
    ComponentDto result = componentsByUuids.get(hit.getUuid());
    if (result == null || // SONAR-11419 this has happened in production while code does not really allow it. An inconsistency in DB may be the cause.
    (QUALIFIERS_FOR_WHICH_TO_RETURN_PROJECT.contains(result.qualifier()) && projectsByUuids.get(result.projectUuid()) == null)) {
        return null;
    }
    Suggestion.Builder builder = Suggestion.newBuilder().setKey(result.getDbKey()).setName(result.name()).setMatch(hit.getHighlightedText().orElse(HtmlEscapers.htmlEscaper().escape(result.name()))).setIsRecentlyBrowsed(recentlyBrowsedKeys.contains(result.getDbKey())).setIsFavorite(favoriteUuids.contains(result.uuid()));
    if (QUALIFIERS_FOR_WHICH_TO_RETURN_PROJECT.contains(result.qualifier())) {
        builder.setProject(projectsByUuids.get(result.projectUuid()).getDbKey());
    }
    return builder.build();
}
Also used : Suggestion(org.sonarqube.ws.Components.SuggestionsWsResponse.Suggestion) ComponentDto(org.sonar.db.component.ComponentDto) CheckForNull(javax.annotation.CheckForNull)

Aggregations

ComponentDto (org.sonar.db.component.ComponentDto)2 Arrays.asList (java.util.Arrays.asList)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 Optional.ofNullable (java.util.Optional.ofNullable)1 Collectors (java.util.stream.Collectors)1 Collectors.joining (java.util.stream.Collectors.joining)1 IntStream.range (java.util.stream.IntStream.range)1 Stream (java.util.stream.Stream)1 CheckForNull (javax.annotation.CheckForNull)1 Nullable (javax.annotation.Nullable)1 RandomStringUtils.randomAlphabetic (org.apache.commons.lang.RandomStringUtils.randomAlphabetic)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Tuple.tuple (org.assertj.core.groups.Tuple.tuple)1 Before (org.junit.Before)1 Rule (org.junit.Rule)1 Test (org.junit.Test)1 Mockito.doReturn (org.mockito.Mockito.doReturn)1 Mockito.mock (org.mockito.Mockito.mock)1 Qualifiers (org.sonar.api.resources.Qualifiers)1