Search in sources :

Example 1 with IssueIndexSyncProgressChecker

use of org.sonar.server.issue.index.IssueIndexSyncProgressChecker in project sonarqube by SonarSource.

the class SearchEventsActionTest method events.

@Test
public void events() {
    userSession.logIn().setRoot();
    when(server.getPublicRootUrl()).thenReturn("https://sonarcloud.io");
    ComponentDto project = db.components().insertPrivateProject();
    ComponentDto branch = db.components().insertProjectBranch(project);
    SnapshotDto projectAnalysis = insertAnalysis(project, 1_500_000_000_000L);
    db.events().insertEvent(newQualityGateEvent(projectAnalysis).setDate(projectAnalysis.getCreatedAt()).setName("Passed"));
    insertIssue(project, projectAnalysis);
    insertIssue(project, projectAnalysis);
    SnapshotDto branchAnalysis = insertAnalysis(branch, 1_501_000_000_000L);
    db.events().insertEvent(newQualityGateEvent(branchAnalysis).setDate(branchAnalysis.getCreatedAt()).setName("Failed"));
    insertIssue(branch, branchAnalysis);
    issueIndexer.indexAllIssues();
    SearchEventsWsResponse result = ws.newRequest().setParam(PARAM_PROJECTS, project.getKey()).setParam(PARAM_FROM, formatDateTime(1_499_000_000_000L)).executeProtobuf(SearchEventsWsResponse.class);
    assertThat(result.getEventsList()).extracting(Event::getCategory, Event::getProject, Event::getMessage).containsOnly(tuple("QUALITY_GATE", project.getKey(), format("Quality Gate status of project '%s' changed to 'Passed'", project.name())), tuple("QUALITY_GATE", project.getKey(), format("Quality Gate status of project '%s' on branch '%s' changed to 'Failed'", project.name(), branch.getBranch())), tuple("NEW_ISSUES", project.getKey(), format("You have 2 new issues on project '%s'", project.name())), tuple("NEW_ISSUES", project.getKey(), format("You have 1 new issue on project '%s' on branch '%s'", project.name(), branch.getBranch())));
    verify(issueIndexSyncProgressChecker).checkIfAnyComponentsNeedIssueSync(any(), argThat(arg -> arg.contains(project.getKey())));
}
Also used : IntStream(java.util.stream.IntStream) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) CeTaskTypes(org.sonar.db.ce.CeTaskTypes) EventCategory(org.sonar.server.projectanalysis.ws.EventCategory) RandomStringUtils.randomAlphanumeric(org.apache.commons.lang.RandomStringUtils.randomAlphanumeric) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Date(java.util.Date) EsTester(org.sonar.server.es.EsTester) EventTesting.newEvent(org.sonar.db.event.EventTesting.newEvent) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Server(org.sonar.api.platform.Server) RuleType(org.sonar.api.rules.RuleType) CeActivityDto(org.sonar.db.ce.CeActivityDto) Param(org.sonar.api.server.ws.WebService.Param) WebService(org.sonar.api.server.ws.WebService) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) IssueIndex(org.sonar.server.issue.index.IssueIndex) IssueIteratorFactory(org.sonar.server.issue.index.IssueIteratorFactory) DateUtils.formatDateTime(org.sonar.api.utils.DateUtils.formatDateTime) RandomUtils.nextLong(org.apache.commons.lang.math.RandomUtils.nextLong) UserSessionRule(org.sonar.server.tester.UserSessionRule) PARAM_PROJECTS(org.sonar.server.developers.ws.SearchEventsAction.PARAM_PROJECTS) DbTester(org.sonar.db.DbTester) RandomUtils.nextInt(org.apache.commons.lang.math.RandomUtils.nextInt) Assertions.tuple(org.assertj.core.api.Assertions.tuple) JsonAssert.assertJson(org.sonar.test.JsonAssert.assertJson) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) WsActionTester(org.sonar.server.ws.WsActionTester) KeyExamples(org.sonar.server.ws.KeyExamples) String.format(java.lang.String.format) Mockito.verify(org.mockito.Mockito.verify) ComponentDto(org.sonar.db.component.ComponentDto) Stream(java.util.stream.Stream) Rule(org.junit.Rule) UserRole(org.sonar.api.web.UserRole) UnauthorizedException(org.sonar.server.exceptions.UnauthorizedException) IssueIndexSyncProgressChecker(org.sonar.server.issue.index.IssueIndexSyncProgressChecker) CeQueueDto(org.sonar.db.ce.CeQueueDto) PARAM_FROM(org.sonar.server.developers.ws.SearchEventsAction.PARAM_FROM) SnapshotDto(org.sonar.db.component.SnapshotDto) SearchEventsWsResponse(org.sonarqube.ws.Developers.SearchEventsWsResponse) EventDto(org.sonar.db.event.EventDto) IssueIndexer(org.sonar.server.issue.index.IssueIndexer) Event(org.sonarqube.ws.Developers.SearchEventsWsResponse.Event) Mockito.mock(org.mockito.Mockito.mock) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) SearchEventsWsResponse(org.sonarqube.ws.Developers.SearchEventsWsResponse) Test(org.junit.Test)

Example 2 with IssueIndexSyncProgressChecker

use of org.sonar.server.issue.index.IssueIndexSyncProgressChecker in project sonarqube by SonarSource.

the class SearchActionTest method returns_hotspot_of_branch_or_pullRequest.

@Test
public void returns_hotspot_of_branch_or_pullRequest() {
    ComponentDto project = dbTester.components().insertPublicProject();
    userSessionRule.registerComponents(project);
    indexPermissions();
    ComponentDto branch = dbTester.components().insertProjectBranch(project);
    ComponentDto pullRequest = dbTester.components().insertProjectBranch(project, t -> t.setBranchType(BranchType.PULL_REQUEST));
    ComponentDto fileProject = dbTester.components().insertComponent(newFileDto(project));
    ComponentDto fileBranch = dbTester.components().insertComponent(newFileDto(branch));
    ComponentDto filePR = dbTester.components().insertComponent(newFileDto(pullRequest));
    IssueDto[] hotspotProject = IntStream.range(0, 1 + RANDOM.nextInt(10)).mapToObj(i -> {
        RuleDefinitionDto rule = newRule(SECURITY_HOTSPOT);
        return insertHotspot(project, fileProject, rule);
    }).toArray(IssueDto[]::new);
    IssueDto[] hotspotBranch = IntStream.range(0, 1 + RANDOM.nextInt(10)).mapToObj(i -> {
        RuleDefinitionDto rule = newRule(SECURITY_HOTSPOT);
        return insertHotspot(branch, fileBranch, rule);
    }).toArray(IssueDto[]::new);
    IssueDto[] hotspotPR = IntStream.range(0, 1 + RANDOM.nextInt(10)).mapToObj(i -> {
        RuleDefinitionDto rule = newRule(SECURITY_HOTSPOT);
        return insertHotspot(pullRequest, filePR, rule);
    }).toArray(IssueDto[]::new);
    indexIssues();
    SearchWsResponse responseProject = newRequest(project).executeProtobuf(SearchWsResponse.class);
    SearchWsResponse responseBranch = newRequest(branch).executeProtobuf(SearchWsResponse.class);
    SearchWsResponse responsePR = newRequest(pullRequest).executeProtobuf(SearchWsResponse.class);
    assertThat(responseProject.getHotspotsList()).extracting(SearchWsResponse.Hotspot::getKey).containsExactlyInAnyOrder(Arrays.stream(hotspotProject).map(IssueDto::getKey).toArray(String[]::new));
    assertThat(responseBranch.getHotspotsList()).extracting(SearchWsResponse.Hotspot::getKey).containsExactlyInAnyOrder(Arrays.stream(hotspotBranch).map(IssueDto::getKey).toArray(String[]::new));
    assertThat(responsePR.getHotspotsList()).extracting(SearchWsResponse.Hotspot::getKey).containsExactlyInAnyOrder(Arrays.stream(hotspotPR).map(IssueDto::getKey).toArray(String[]::new));
    verify(issueIndexSyncProgressChecker, times(3)).checkIfComponentNeedIssueSync(any(), eq(project.getDbKey()));
}
Also used : Arrays(java.util.Arrays) AsyncIssueIndexing(org.sonar.server.issue.index.AsyncIssueIndexing) SecurityStandards(org.sonar.server.security.SecurityStandards) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) ViewIndexer(org.sonar.server.view.index.ViewIndexer) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) STATUS_CLOSED(org.sonar.api.issue.Issue.STATUS_CLOSED) DbIssues(org.sonar.db.protobuf.DbIssues) WebService(org.sonar.api.server.ws.WebService) Collections.singleton(java.util.Collections.singleton) IssueIndex(org.sonar.server.issue.index.IssueIndex) IssueIteratorFactory(org.sonar.server.issue.index.IssueIteratorFactory) IssueTesting.newIssue(org.sonar.db.issue.IssueTesting.newIssue) Map(java.util.Map) ComponentTesting(org.sonar.db.component.ComponentTesting) DateUtils.formatDateTime(org.sonar.api.utils.DateUtils.formatDateTime) Collectors.toSet(java.util.stream.Collectors.toSet) DbTester(org.sonar.db.DbTester) RuleTesting(org.sonar.db.rule.RuleTesting) PermissionIndexer(org.sonar.server.permission.index.PermissionIndexer) REFERENCE_BRANCH(org.sonar.db.newcodeperiod.NewCodePeriodType.REFERENCE_BRANCH) System2(org.sonar.api.utils.System2) Hotspots(org.sonarqube.ws.Hotspots) Collection(java.util.Collection) Set(java.util.Set) SearchWsResponse(org.sonarqube.ws.Hotspots.SearchWsResponse) Sets(com.google.common.collect.Sets) NotFoundException(org.sonar.server.exceptions.NotFoundException) Collectors.joining(java.util.stream.Collectors.joining) Common(org.sonarqube.ws.Common) STATUS_TO_REVIEW(org.sonar.api.issue.Issue.STATUS_TO_REVIEW) DbClient(org.sonar.db.DbClient) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) Stream(java.util.stream.Stream) STATUS_REVIEWED(org.sonar.api.issue.Issue.STATUS_REVIEWED) ForbiddenException(org.sonar.server.exceptions.ForbiddenException) ProjectDto(org.sonar.db.project.ProjectDto) MoreCollectors.uniqueIndex(org.sonar.core.util.stream.MoreCollectors.uniqueIndex) STATUSES(org.sonar.api.issue.Issue.STATUSES) IssueTesting.newCodeReferenceIssue(org.sonar.db.issue.IssueTesting.newCodeReferenceIssue) SQCategory(org.sonar.server.security.SecurityStandards.SQCategory) ComponentTesting.newDirectory(org.sonar.db.component.ComponentTesting.newDirectory) Mockito.mock(org.mockito.Mockito.mock) IntStream(java.util.stream.IntStream) BranchDto(org.sonar.db.component.BranchDto) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IssueDto(org.sonar.db.issue.IssueDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) EsTester(org.sonar.server.es.EsTester) ComponentTesting.newFileDto(org.sonar.db.component.ComponentTesting.newFileDto) RunWith(org.junit.runner.RunWith) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) TestSystem2(org.sonar.api.impl.utils.TestSystem2) RuleType(org.sonar.api.rules.RuleType) WebAuthorizationTypeSupport(org.sonar.server.permission.index.WebAuthorizationTypeSupport) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) TextRangeResponseFormatter(org.sonar.server.issue.TextRangeResponseFormatter) SECURITY_HOTSPOT(org.sonar.api.rules.RuleType.SECURITY_HOTSPOT) RESOLUTION_FIXED(org.sonar.api.issue.Issue.RESOLUTION_FIXED) Nullable(javax.annotation.Nullable) ImmutableSet.of(com.google.common.collect.ImmutableSet.of) UserSessionRule(org.sonar.server.tester.UserSessionRule) BranchType(org.sonar.db.component.BranchType) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) USER(org.sonar.api.web.UserRole.USER) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) TestRequest(org.sonar.server.ws.TestRequest) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) RESOLUTION_SAFE(org.sonar.api.issue.Issue.RESOLUTION_SAFE) WsActionTester(org.sonar.server.ws.WsActionTester) DbCommons(org.sonar.db.protobuf.DbCommons) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) Component(org.sonarqube.ws.Hotspots.Component) Collectors.toList(java.util.stream.Collectors.toList) Rule(org.junit.Rule) Ordering(com.google.common.collect.Ordering) Issue(org.sonar.api.issue.Issue) IssueIndexSyncProgressChecker(org.sonar.server.issue.index.IssueIndexSyncProgressChecker) Tuple.tuple(org.assertj.core.groups.Tuple.tuple) Comparator(java.util.Comparator) Collections(java.util.Collections) IssueIndexer(org.sonar.server.issue.index.IssueIndexer) ComponentDto(org.sonar.db.component.ComponentDto) IssueDto(org.sonar.db.issue.IssueDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) SearchWsResponse(org.sonarqube.ws.Hotspots.SearchWsResponse) Test(org.junit.Test)

Example 3 with IssueIndexSyncProgressChecker

use of org.sonar.server.issue.index.IssueIndexSyncProgressChecker in project sonarqube by SonarSource.

the class SearchActionTest method returns_empty_if_none_of_hotspot_keys_exist.

@Test
public void returns_empty_if_none_of_hotspot_keys_exist() {
    ComponentDto project = dbTester.components().insertPublicProject();
    userSessionRule.registerComponents(project);
    indexPermissions();
    ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
    RuleDefinitionDto rule = newRule(SECURITY_HOTSPOT);
    List<IssueDto> hotspots = IntStream.range(0, 1 + RANDOM.nextInt(15)).mapToObj(i -> dbTester.issues().insertHotspot(rule, project, file, t -> t.setLine(i))).collect(toList());
    indexIssues();
    SearchWsResponse response = newRequest(IntStream.range(0, 1 + RANDOM.nextInt(30)).mapToObj(i -> "key_" + i).collect(toList())).executeProtobuf(SearchWsResponse.class);
    verify(issueIndexSyncProgressChecker).checkIfIssueSyncInProgress(any());
    assertThat(response.getHotspotsList()).isEmpty();
}
Also used : Arrays(java.util.Arrays) AsyncIssueIndexing(org.sonar.server.issue.index.AsyncIssueIndexing) SecurityStandards(org.sonar.server.security.SecurityStandards) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) ViewIndexer(org.sonar.server.view.index.ViewIndexer) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) STATUS_CLOSED(org.sonar.api.issue.Issue.STATUS_CLOSED) DbIssues(org.sonar.db.protobuf.DbIssues) WebService(org.sonar.api.server.ws.WebService) Collections.singleton(java.util.Collections.singleton) IssueIndex(org.sonar.server.issue.index.IssueIndex) IssueIteratorFactory(org.sonar.server.issue.index.IssueIteratorFactory) IssueTesting.newIssue(org.sonar.db.issue.IssueTesting.newIssue) Map(java.util.Map) ComponentTesting(org.sonar.db.component.ComponentTesting) DateUtils.formatDateTime(org.sonar.api.utils.DateUtils.formatDateTime) Collectors.toSet(java.util.stream.Collectors.toSet) DbTester(org.sonar.db.DbTester) RuleTesting(org.sonar.db.rule.RuleTesting) PermissionIndexer(org.sonar.server.permission.index.PermissionIndexer) REFERENCE_BRANCH(org.sonar.db.newcodeperiod.NewCodePeriodType.REFERENCE_BRANCH) System2(org.sonar.api.utils.System2) Hotspots(org.sonarqube.ws.Hotspots) Collection(java.util.Collection) Set(java.util.Set) SearchWsResponse(org.sonarqube.ws.Hotspots.SearchWsResponse) Sets(com.google.common.collect.Sets) NotFoundException(org.sonar.server.exceptions.NotFoundException) Collectors.joining(java.util.stream.Collectors.joining) Common(org.sonarqube.ws.Common) STATUS_TO_REVIEW(org.sonar.api.issue.Issue.STATUS_TO_REVIEW) DbClient(org.sonar.db.DbClient) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) Stream(java.util.stream.Stream) STATUS_REVIEWED(org.sonar.api.issue.Issue.STATUS_REVIEWED) ForbiddenException(org.sonar.server.exceptions.ForbiddenException) ProjectDto(org.sonar.db.project.ProjectDto) MoreCollectors.uniqueIndex(org.sonar.core.util.stream.MoreCollectors.uniqueIndex) STATUSES(org.sonar.api.issue.Issue.STATUSES) IssueTesting.newCodeReferenceIssue(org.sonar.db.issue.IssueTesting.newCodeReferenceIssue) SQCategory(org.sonar.server.security.SecurityStandards.SQCategory) ComponentTesting.newDirectory(org.sonar.db.component.ComponentTesting.newDirectory) Mockito.mock(org.mockito.Mockito.mock) IntStream(java.util.stream.IntStream) BranchDto(org.sonar.db.component.BranchDto) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IssueDto(org.sonar.db.issue.IssueDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) EsTester(org.sonar.server.es.EsTester) ComponentTesting.newFileDto(org.sonar.db.component.ComponentTesting.newFileDto) RunWith(org.junit.runner.RunWith) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) TestSystem2(org.sonar.api.impl.utils.TestSystem2) RuleType(org.sonar.api.rules.RuleType) WebAuthorizationTypeSupport(org.sonar.server.permission.index.WebAuthorizationTypeSupport) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) TextRangeResponseFormatter(org.sonar.server.issue.TextRangeResponseFormatter) SECURITY_HOTSPOT(org.sonar.api.rules.RuleType.SECURITY_HOTSPOT) RESOLUTION_FIXED(org.sonar.api.issue.Issue.RESOLUTION_FIXED) Nullable(javax.annotation.Nullable) ImmutableSet.of(com.google.common.collect.ImmutableSet.of) UserSessionRule(org.sonar.server.tester.UserSessionRule) BranchType(org.sonar.db.component.BranchType) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) USER(org.sonar.api.web.UserRole.USER) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) TestRequest(org.sonar.server.ws.TestRequest) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) RESOLUTION_SAFE(org.sonar.api.issue.Issue.RESOLUTION_SAFE) WsActionTester(org.sonar.server.ws.WsActionTester) DbCommons(org.sonar.db.protobuf.DbCommons) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) Component(org.sonarqube.ws.Hotspots.Component) Collectors.toList(java.util.stream.Collectors.toList) Rule(org.junit.Rule) Ordering(com.google.common.collect.Ordering) Issue(org.sonar.api.issue.Issue) IssueIndexSyncProgressChecker(org.sonar.server.issue.index.IssueIndexSyncProgressChecker) Tuple.tuple(org.assertj.core.groups.Tuple.tuple) Comparator(java.util.Comparator) Collections(java.util.Collections) IssueIndexer(org.sonar.server.issue.index.IssueIndexer) ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) IssueDto(org.sonar.db.issue.IssueDto) SearchWsResponse(org.sonarqube.ws.Hotspots.SearchWsResponse) Test(org.junit.Test)

Aggregations

IntStream (java.util.stream.IntStream)3 Stream (java.util.stream.Stream)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)3 Rule (org.junit.Rule)3 Test (org.junit.Test)3 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)3 Mockito.mock (org.mockito.Mockito.mock)3 Mockito.verify (org.mockito.Mockito.verify)3 ImmutableSet.of (com.google.common.collect.ImmutableSet.of)2 Ordering (com.google.common.collect.Ordering)2 Sets (com.google.common.collect.Sets)2 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)2 DataProviderRunner (com.tngtech.java.junit.dataprovider.DataProviderRunner)2 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 Collections.singleton (java.util.Collections.singleton)2 Comparator (java.util.Comparator)2