Search in sources :

Example 16 with SuggestionsWsResponse

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

the class SuggestionsActionTest method does_not_return_branches.

@Test
public void does_not_return_branches() {
    ComponentDto project = db.components().insertPublicProject();
    authorizationIndexerTester.allowOnlyAnyone(project);
    ComponentDto branch = db.components().insertProjectBranch(project);
    componentIndexer.indexAll();
    authorizationIndexerTester.allowOnlyAnyone(project);
    SuggestionsWsResponse response = ws.newRequest().setMethod("POST").setParam(PARAM_QUERY, project.name()).executeProtobuf(SuggestionsWsResponse.class);
    assertThat(response.getResultsList()).filteredOn(c -> "TRK".equals(c.getQ())).extracting(Category::getItemsList).hasSize(1);
}
Also used : ComponentDto(org.sonar.db.component.ComponentDto) SuggestionsWsResponse(org.sonarqube.ws.Components.SuggestionsWsResponse) Test(org.junit.Test)

Example 17 with SuggestionsWsResponse

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

the class SuggestionsActionTest method should_not_return_modules.

@Test
public void should_not_return_modules() {
    ComponentDto project = db.components().insertComponent(newPrivateProjectDto().setName("ProjectWithModules"));
    db.components().insertComponent(newModuleDto(project).setName("Module1"));
    db.components().insertComponent(newModuleDto(project).setName("Module2"));
    componentIndexer.indexOnAnalysis(project.projectUuid());
    authorizationIndexerTester.allowOnlyAnyone(project);
    SuggestionsWsResponse response = ws.newRequest().setMethod("POST").setParam(PARAM_QUERY, "Module").executeProtobuf(SuggestionsWsResponse.class);
    assertThat(response.getResultsList()).flatExtracting(Category::getItemsList).extracting(Suggestion::getKey).containsOnly(project.getDbKey());
}
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 18 with SuggestionsWsResponse

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

the class SuggestionsActionTest method must_not_search_if_no_valid_tokens_are_provided.

@Test
public void must_not_search_if_no_valid_tokens_are_provided() {
    ComponentDto project = db.components().insertComponent(newPrivateProjectDto().setName("SonarQube"));
    componentIndexer.indexAll();
    authorizationIndexerTester.allowOnlyAnyone(project);
    SuggestionsWsResponse response = ws.newRequest().setMethod("POST").setParam(PARAM_QUERY, "S o").executeProtobuf(SuggestionsWsResponse.class);
    assertThat(response.getResultsList()).filteredOn(q -> q.getItemsCount() > 0).isEmpty();
    assertThat(response.getWarning()).contains(SHORT_INPUT_WARNING);
}
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 19 with SuggestionsWsResponse

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

the class SuggestionsActionTest method should_warn_about_short_inputs_but_return_results_based_on_other_terms.

@Test
public void should_warn_about_short_inputs_but_return_results_based_on_other_terms() {
    ComponentDto project = db.components().insertComponent(newPrivateProjectDto().setName("SonarQube"));
    componentIndexer.indexAll();
    authorizationIndexerTester.allowOnlyAnyone(project);
    SuggestionsWsResponse response = ws.newRequest().setMethod("POST").setParam(PARAM_QUERY, "Sonar Q").executeProtobuf(SuggestionsWsResponse.class);
    assertThat(response.getResultsList()).flatExtracting(Category::getItemsList).extracting(Suggestion::getKey).contains(project.getDbKey());
    assertThat(response.getWarning()).contains(SHORT_INPUT_WARNING);
}
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 20 with SuggestionsWsResponse

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

the class SuggestionsActionTest method check_proposal_to_show_more_results.

private void check_proposal_to_show_more_results(int numberOfProjects, int expectedNumberOfResults, long expectedNumberOfMoreResults, @Nullable SuggestionCategory more, boolean useQuery) {
    String namePrefix = "MyProject";
    List<ComponentDto> projects = range(0, numberOfProjects).mapToObj(i -> db.components().insertComponent(newPublicProjectDto().setName(namePrefix + i))).collect(Collectors.toList());
    componentIndexer.indexAll();
    projects.forEach(authorizationIndexerTester::allowOnlyAnyone);
    TestRequest request = ws.newRequest().setMethod("POST");
    if (useQuery) {
        request.setParam(PARAM_QUERY, namePrefix);
    } else {
        doReturn(projects).when(favoriteFinder).list();
    }
    ofNullable(more).ifPresent(c -> request.setParam(PARAM_MORE, c.getName()));
    SuggestionsWsResponse response = request.executeProtobuf(SuggestionsWsResponse.class);
    // include limited number of results in the response
    assertThat(response.getResultsList()).flatExtracting(Category::getItemsList).hasSize(expectedNumberOfResults);
    // indicate, that there are more results
    if (expectedNumberOfResults == 0 && expectedNumberOfMoreResults == 0) {
        assertThat(response.getResultsList()).filteredOn(q -> q.getItemsCount() > 0).isEmpty();
    } else {
        assertThat(response.getResultsList()).filteredOn(c -> "TRK".equals(c.getQ())).extracting(Category::getMore).containsExactly(expectedNumberOfMoreResults);
        response.getResultsList().stream().filter(c -> !"TRK".equals(c.getQ())).map(Category::getMore).forEach(m -> assertThat(m).isEqualTo(0L));
    }
}
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) TestRequest(org.sonar.server.ws.TestRequest)

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