Search in sources :

Example 1 with IssuesChangesNotification

use of org.sonar.server.issue.notification.IssuesChangesNotification in project sonarqube by SonarSource.

the class NotificationFactoryTest method newIssuesChangesNotification_maps_all_issues.

@Test
public void newIssuesChangesNotification_maps_all_issues() {
    Set<DefaultIssue> issues = IntStream.range(0, 3 + new Random().nextInt(5)).mapToObj(i -> new DefaultIssue().setRuleKey(RuleKey.of("repo_" + i, "rule_" + i)).setKey("issue_key_" + i).setStatus("status_" + i)).collect(Collectors.toSet());
    ReportComponent project = ReportComponent.builder(PROJECT, 1).build();
    long analysisDate = new Random().nextLong();
    issues.stream().map(DefaultIssue::ruleKey).forEach(ruleKey -> ruleRepository.add(ruleKey));
    treeRootHolder.setRoot(project);
    analysisMetadata.setAnalysisDate(analysisDate);
    analysisMetadata.setBranch(newNonMainBranch(BranchType.BRANCH, randomAlphabetic(12)));
    IssuesChangesNotification expected = mock(IssuesChangesNotification.class);
    when(issuesChangesSerializer.serialize(any(IssuesChangesNotificationBuilder.class))).thenReturn(expected);
    IssuesChangesNotification notification = underTest.newIssuesChangesNotification(issues, emptyMap());
    assertThat(notification).isSameAs(expected);
    IssuesChangesNotificationBuilder builder = verifyAndCaptureIssueChangeNotificationBuilder();
    assertThat(builder.getIssues()).hasSize(issues.size());
    Map<String, ChangedIssue> changedIssuesByKey = builder.getIssues().stream().collect(uniqueIndex(ChangedIssue::getKey));
    issues.forEach(issue -> {
        ChangedIssue changedIssue = changedIssuesByKey.get(issue.key());
        assertThat(changedIssue.getNewStatus()).isEqualTo(issue.status());
        assertThat(changedIssue.getNewResolution()).isEmpty();
        assertThat(changedIssue.getAssignee()).isEmpty();
        assertThat(changedIssue.getRule().getKey()).isEqualTo(issue.ruleKey());
        assertThat(changedIssue.getRule().getName()).isEqualTo(ruleRepository.getByKey(issue.ruleKey()).getName());
    });
}
Also used : Branch(org.sonar.ce.task.projectanalysis.analysis.Branch) NewIssuesNotification(org.sonar.server.issue.notification.NewIssuesNotification) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) DetailsSupplier(org.sonar.server.issue.notification.NewIssuesNotification.DetailsSupplier) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Map(java.util.Map) AnalysisMetadataHolderRule(org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule) MyNewIssuesNotification(org.sonar.server.issue.notification.MyNewIssuesNotification) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) DIRECTORY(org.sonar.ce.task.projectanalysis.component.Component.Type.DIRECTORY) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) IssuesChangesNotification(org.sonar.server.issue.notification.IssuesChangesNotification) Collectors(java.util.stream.Collectors) PROJECT(org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT) FILE(org.sonar.ce.task.projectanalysis.component.Component.Type.FILE) Stream(java.util.stream.Stream) RuleKey(org.sonar.api.rule.RuleKey) MoreCollectors.uniqueIndex(org.sonar.core.util.stream.MoreCollectors.uniqueIndex) TreeRootHolderRule(org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule) UserTesting(org.sonar.db.user.UserTesting) IssuesChangesNotificationSerializer(org.sonar.server.issue.notification.IssuesChangesNotificationSerializer) Mockito.mock(org.mockito.Mockito.mock) IntStream(java.util.stream.IntStream) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) UserDto(org.sonar.db.user.UserDto) STATUS_OPEN(org.sonar.api.issue.Issue.STATUS_OPEN) RunWith(org.junit.runner.RunWith) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) DumbRule(org.sonar.ce.task.projectanalysis.issue.DumbRule) ArgumentCaptor(org.mockito.ArgumentCaptor) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) AnalysisChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.AnalysisChange) Collections.emptyMap(java.util.Collections.emptyMap) BranchType(org.sonar.db.component.BranchType) DefaultIssue(org.sonar.core.issue.DefaultIssue) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) Durations(org.sonar.api.utils.Durations) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Field(java.lang.reflect.Field) Mockito.verify(org.mockito.Mockito.verify) Rule(org.junit.Rule) RuleDefinition(org.sonar.server.issue.notification.NewIssuesNotification.RuleDefinition) ReportComponent(org.sonar.ce.task.projectanalysis.component.ReportComponent) RuleRepositoryRule(org.sonar.ce.task.projectanalysis.issue.RuleRepositoryRule) Collections(java.util.Collections) IssuesChangesNotificationBuilder(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder) Random(java.util.Random) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) DefaultIssue(org.sonar.core.issue.DefaultIssue) IssuesChangesNotificationBuilder(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder) ReportComponent(org.sonar.ce.task.projectanalysis.component.ReportComponent) IssuesChangesNotification(org.sonar.server.issue.notification.IssuesChangesNotification) Test(org.junit.Test)

Example 2 with IssuesChangesNotification

use of org.sonar.server.issue.notification.IssuesChangesNotification in project sonarqube by SonarSource.

the class NotificationFactoryTest method newIssuesChangesNotification_creates_rule_from_RuleRepository.

@Test
public void newIssuesChangesNotification_creates_rule_from_RuleRepository() {
    RuleKey ruleKey = RuleKey.of("foo", "bar");
    DefaultIssue issue = new DefaultIssue().setRuleKey(ruleKey).setKey("issueKey").setStatus(STATUS_OPEN);
    Map<String, UserDto> assigneesByUuid = nonEmptyAssigneesByUuid();
    ReportComponent project = ReportComponent.builder(PROJECT, 1).build();
    String branchName = randomAlphabetic(12);
    ruleRepository.add(ruleKey);
    treeRootHolder.setRoot(project);
    analysisMetadata.setAnalysisDate(new Random().nextLong());
    analysisMetadata.setBranch(newNonMainBranch(BranchType.BRANCH, branchName));
    IssuesChangesNotification expected = mock(IssuesChangesNotification.class);
    when(issuesChangesSerializer.serialize(any(IssuesChangesNotificationBuilder.class))).thenReturn(expected);
    IssuesChangesNotification notification = underTest.newIssuesChangesNotification(ImmutableSet.of(issue), assigneesByUuid);
    assertThat(notification).isSameAs(expected);
    IssuesChangesNotificationBuilder builder = verifyAndCaptureIssueChangeNotificationBuilder();
    assertThat(builder.getIssues()).hasSize(1);
    ChangedIssue changeIssue = builder.getIssues().iterator().next();
    assertThat(changeIssue.getRule().getKey()).isEqualTo(ruleKey);
    assertThat(changeIssue.getRule().getName()).isEqualTo(ruleRepository.getByKey(ruleKey).getName());
}
Also used : Random(java.util.Random) RuleKey(org.sonar.api.rule.RuleKey) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) UserDto(org.sonar.db.user.UserDto) DefaultIssue(org.sonar.core.issue.DefaultIssue) IssuesChangesNotificationBuilder(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder) ReportComponent(org.sonar.ce.task.projectanalysis.component.ReportComponent) IssuesChangesNotification(org.sonar.server.issue.notification.IssuesChangesNotification) Test(org.junit.Test)

Example 3 with IssuesChangesNotification

use of org.sonar.server.issue.notification.IssuesChangesNotification in project sonarqube by SonarSource.

the class NotificationFactoryTest method newIssuesChangesNotification_creates_AnalysisChange_with_analysis_date.

@Test
public void newIssuesChangesNotification_creates_AnalysisChange_with_analysis_date() {
    RuleKey ruleKey = RuleKey.of("foo", "bar");
    DefaultIssue issue = new DefaultIssue().setRuleKey(ruleKey).setKey("issueKey").setStatus(STATUS_OPEN);
    Map<String, UserDto> assigneesByUuid = nonEmptyAssigneesByUuid();
    ReportComponent project = ReportComponent.builder(PROJECT, 1).build();
    long analysisDate = new Random().nextLong();
    ruleRepository.add(ruleKey);
    treeRootHolder.setRoot(project);
    analysisMetadata.setAnalysisDate(analysisDate);
    analysisMetadata.setBranch(newNonMainBranch(BranchType.BRANCH, randomAlphabetic(12)));
    IssuesChangesNotification expected = mock(IssuesChangesNotification.class);
    when(issuesChangesSerializer.serialize(any(IssuesChangesNotificationBuilder.class))).thenReturn(expected);
    IssuesChangesNotification notification = underTest.newIssuesChangesNotification(ImmutableSet.of(issue), assigneesByUuid);
    assertThat(notification).isSameAs(expected);
    IssuesChangesNotificationBuilder builder = verifyAndCaptureIssueChangeNotificationBuilder();
    assertThat(builder.getIssues()).hasSize(1);
    assertThat(builder.getChange()).isInstanceOf(AnalysisChange.class).extracting(IssuesChangesNotificationBuilder.Change::getDate).isEqualTo(analysisDate);
}
Also used : Random(java.util.Random) RuleKey(org.sonar.api.rule.RuleKey) UserDto(org.sonar.db.user.UserDto) DefaultIssue(org.sonar.core.issue.DefaultIssue) IssuesChangesNotificationBuilder(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder) ReportComponent(org.sonar.ce.task.projectanalysis.component.ReportComponent) IssuesChangesNotification(org.sonar.server.issue.notification.IssuesChangesNotification) Test(org.junit.Test)

Example 4 with IssuesChangesNotification

use of org.sonar.server.issue.notification.IssuesChangesNotification in project sonarqube by SonarSource.

the class SendIssueNotificationsStepTest method sendIssueChangeNotification.

private void sendIssueChangeNotification(long issueCreatedAt) {
    UserDto user = db.users().insertUser();
    ComponentDto project = newPrivateProjectDto().setDbKey(PROJECT.getDbKey()).setLongName(PROJECT.getName());
    analysisMetadataHolder.setProject(Project.from(project));
    ComponentDto file = newFileDto(project).setDbKey(FILE.getDbKey()).setLongName(FILE.getName());
    treeRootHolder.setRoot(builder(Type.PROJECT, 2).setKey(project.getDbKey()).setPublicKey(project.getKey()).setName(project.longName()).setUuid(project.uuid()).addChildren(builder(Type.FILE, 11).setKey(file.getDbKey()).setPublicKey(file.getKey()).setName(file.longName()).build()).build());
    RuleDefinitionDto ruleDefinitionDto = newRule();
    RuleType randomTypeExceptHotspot = RuleType.values()[nextInt(RuleType.values().length - 1)];
    DefaultIssue issue = prepareIssue(issueCreatedAt, user, project, file, ruleDefinitionDto, randomTypeExceptHotspot);
    IssuesChangesNotification issuesChangesNotification = mock(IssuesChangesNotification.class);
    when(notificationService.hasProjectSubscribersForTypes(project.uuid(), NOTIF_TYPES)).thenReturn(true);
    when(notificationFactory.newIssuesChangesNotification(anySet(), anyMap())).thenReturn(issuesChangesNotification);
    underTest.execute(new TestComputationStepContext());
    verify(notificationFactory).newIssuesChangesNotification(issuesSetCaptor.capture(), assigneeByUuidCaptor.capture());
    assertThat(issuesSetCaptor.getValue()).hasSize(1);
    assertThat(issuesSetCaptor.getValue().iterator().next()).isEqualTo(issue);
    assertThat(assigneeByUuidCaptor.getValue()).hasSize(1);
    assertThat(assigneeByUuidCaptor.getValue().get(user.getUuid())).isNotNull();
    verify(notificationService).hasProjectSubscribersForTypes(project.uuid(), NOTIF_TYPES);
    verify(notificationService).deliverEmails(singleton(issuesChangesNotification));
    verify(notificationService).deliver(issuesChangesNotification);
    verifyNoMoreInteractions(notificationService);
}
Also used : UserDto(org.sonar.db.user.UserDto) ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) RuleType(org.sonar.api.rules.RuleType) DefaultIssue(org.sonar.core.issue.DefaultIssue) TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) IssuesChangesNotification(org.sonar.server.issue.notification.IssuesChangesNotification)

Example 5 with IssuesChangesNotification

use of org.sonar.server.issue.notification.IssuesChangesNotification in project sonarqube by SonarSource.

the class IssueUpdaterTest method verify_notification_on_branch.

@Test
public void verify_notification_on_branch() {
    RuleDefinitionDto rule = db.rules().insertIssueRule();
    ComponentDto project = db.components().insertPublicProject();
    ComponentDto branch = db.components().insertProjectBranch(project, t -> t.setBranchType(BRANCH));
    ComponentDto file = db.components().insertComponent(newFileDto(branch));
    DefaultIssue issue = db.issues().insertIssue(rule, branch, file, t -> t.setSeverity(MAJOR)).toDefaultIssue();
    UserDto changeAuthor = db.users().insertUser();
    IssueChangeContext context = IssueChangeContext.createUser(new Date(), changeAuthor.getUuid());
    issueFieldsSetter.setSeverity(issue, BLOCKER, context);
    underTest.saveIssueAndPreloadSearchResponseData(db.getSession(), issue, context, false);
    verify(notificationManager).scheduleForSending(notificationArgumentCaptor.capture());
    IssuesChangesNotification issueChangeNotification = notificationArgumentCaptor.getValue();
    IssuesChangesNotificationBuilder builder = issuesChangesSerializer.from(issueChangeNotification);
    assertThat(builder.getIssues()).hasSize(1);
    ChangedIssue changedIssue = builder.getIssues().iterator().next();
    assertThat(changedIssue.getKey()).isEqualTo(issue.key());
    assertThat(changedIssue.getNewStatus()).isEqualTo(issue.status());
    assertThat(changedIssue.getNewResolution()).isEmpty();
    assertThat(changedIssue.getAssignee()).isEmpty();
    assertThat(changedIssue.getRule()).isEqualTo(ruleOf(rule));
    assertThat(changedIssue.getProject()).isEqualTo(projectBranchOf(db, branch));
    assertThat(builder.getChange()).isEqualTo(new UserChange(issue.updateDate().getTime(), userOf(changeAuthor)));
}
Also used : MAJOR(org.sonar.api.rule.Severity.MAJOR) IssueDto(org.sonar.db.issue.IssueDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) UserDto(org.sonar.db.user.UserDto) Date(java.util.Date) EsTester(org.sonar.server.es.EsTester) ComponentTesting.newFileDto(org.sonar.db.component.ComponentTesting.newFileDto) RuleStatus(org.sonar.api.rule.RuleStatus) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) IssueFieldsSetter(org.sonar.server.issue.IssueFieldsSetter) WebIssueStorage(org.sonar.server.issue.WebIssueStorage) ArgumentCaptor(org.mockito.ArgumentCaptor) IssueIteratorFactory(org.sonar.server.issue.index.IssueIteratorFactory) BLOCKER(org.sonar.api.rule.Severity.BLOCKER) IssueChangeContext(org.sonar.core.issue.IssueChangeContext) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) NotificationManager(org.sonar.server.notification.NotificationManager) RESOLUTION_FIXED(org.sonar.api.issue.Issue.RESOLUTION_FIXED) DbTester(org.sonar.db.DbTester) BranchType(org.sonar.db.component.BranchType) TestIssueChangePostProcessor(org.sonar.server.issue.TestIssueChangePostProcessor) DefaultIssue(org.sonar.core.issue.DefaultIssue) System2(org.sonar.api.utils.System2) DefaultRuleFinder(org.sonar.server.rule.DefaultRuleFinder) IssuesChangesNotificationBuilderTesting.ruleOf(org.sonar.server.issue.notification.IssuesChangesNotificationBuilderTesting.ruleOf) IssuesChangesNotificationBuilderTesting.userOf(org.sonar.server.issue.notification.IssuesChangesNotificationBuilderTesting.userOf) Test(org.junit.Test) IssuesChangesNotification(org.sonar.server.issue.notification.IssuesChangesNotification) BRANCH(org.sonar.db.component.BranchType.BRANCH) UserChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange) IssuesChangesNotificationBuilderTesting.projectOf(org.sonar.server.issue.notification.IssuesChangesNotificationBuilderTesting.projectOf) SequenceUuidFactory(org.sonar.core.util.SequenceUuidFactory) Mockito.verify(org.mockito.Mockito.verify) DbClient(org.sonar.db.DbClient) ComponentDto(org.sonar.db.component.ComponentDto) Rule(org.junit.Rule) IssuesChangesNotificationBuilderTesting.projectBranchOf(org.sonar.server.issue.notification.IssuesChangesNotificationBuilderTesting.projectBranchOf) IssueIndexer(org.sonar.server.issue.index.IssueIndexer) IssuesChangesNotificationSerializer(org.sonar.server.issue.notification.IssuesChangesNotificationSerializer) IssuesChangesNotificationBuilder(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder) Mockito.mock(org.mockito.Mockito.mock) UserChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange) IssueChangeContext(org.sonar.core.issue.IssueChangeContext) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) UserDto(org.sonar.db.user.UserDto) ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) DefaultIssue(org.sonar.core.issue.DefaultIssue) IssuesChangesNotificationBuilder(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder) Date(java.util.Date) IssuesChangesNotification(org.sonar.server.issue.notification.IssuesChangesNotification) Test(org.junit.Test)

Aggregations

IssuesChangesNotification (org.sonar.server.issue.notification.IssuesChangesNotification)14 DefaultIssue (org.sonar.core.issue.DefaultIssue)13 UserDto (org.sonar.db.user.UserDto)12 IssuesChangesNotificationBuilder (org.sonar.server.issue.notification.IssuesChangesNotificationBuilder)11 Test (org.junit.Test)10 ChangedIssue (org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue)10 RuleKey (org.sonar.api.rule.RuleKey)7 Random (java.util.Random)6 ReportComponent (org.sonar.ce.task.projectanalysis.component.ReportComponent)6 ComponentDto (org.sonar.db.component.ComponentDto)6 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)6 IssuesChangesNotificationSerializer (org.sonar.server.issue.notification.IssuesChangesNotificationSerializer)6 Date (java.util.Date)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 Rule (org.junit.Rule)5 ArgumentCaptor (org.mockito.ArgumentCaptor)5 Mockito.mock (org.mockito.Mockito.mock)5 Mockito.verify (org.mockito.Mockito.verify)5 BranchType (org.sonar.db.component.BranchType)5 Mockito.verifyZeroInteractions (org.mockito.Mockito.verifyZeroInteractions)4