Search in sources :

Example 6 with IssuesChangesNotification

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

the class IssueUpdaterTest method verify_notification_with_resolution.

@Test
public void verify_notification_with_resolution() {
    UserDto assignee = db.users().insertUser();
    RuleDefinitionDto rule = db.rules().insertIssueRule();
    ComponentDto project = db.components().insertPublicProject();
    ComponentDto file = db.components().insertComponent(newFileDto(project));
    DefaultIssue issue = db.issues().insertIssue(rule, project, file, t -> t.setSeverity(MAJOR).setAssigneeUuid(assignee.getUuid())).toDefaultIssue();
    UserDto changeAuthor = db.users().insertUser();
    IssueChangeContext context = IssueChangeContext.createUser(new Date(), changeAuthor.getUuid());
    issueFieldsSetter.setResolution(issue, RESOLUTION_FIXED, 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()).contains(RESOLUTION_FIXED);
    assertThat(changedIssue.getAssignee()).contains(userOf(assignee));
    assertThat(changedIssue.getRule()).isEqualTo(ruleOf(rule));
    assertThat(changedIssue.getProject()).isEqualTo(projectOf(project));
    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)

Example 7 with IssuesChangesNotification

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

the class NotificationFactoryTest method newIssuesChangesNotification_creates_project_from_TreeRootHolder_and_branch_name_only_on_non_main_branches.

@Test
@UseDataProvider("noBranchNameBranches")
public void newIssuesChangesNotification_creates_project_from_TreeRootHolder_and_branch_name_only_on_non_main_branches(Branch branch) {
    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();
    ruleRepository.add(ruleKey);
    treeRootHolder.setRoot(project);
    analysisMetadata.setAnalysisDate(new Random().nextLong());
    analysisMetadata.setBranch(branch);
    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.getProject().getUuid()).isEqualTo(project.getUuid());
    assertThat(changeIssue.getProject().getKey()).isEqualTo(project.getKey());
    assertThat(changeIssue.getProject().getProjectName()).isEqualTo(project.getName());
    assertThat(changeIssue.getProject().getBranchName()).isEmpty();
}
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) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 8 with IssuesChangesNotification

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

the class NotificationFactoryTest method newIssuesChangesNotification_creates_assignee_from_UserDto.

@Test
public void newIssuesChangesNotification_creates_assignee_from_UserDto() {
    RuleKey ruleKey = RuleKey.of("foo", "bar");
    String assigneeUuid = randomAlphabetic(40);
    DefaultIssue issue = new DefaultIssue().setRuleKey(ruleKey).setKey("issueKey").setStatus(STATUS_OPEN).setAssigneeUuid(assigneeUuid);
    UserDto userDto = UserTesting.newUserDto();
    Map<String, UserDto> assigneesByUuid = ImmutableMap.of(assigneeUuid, userDto);
    ReportComponent project = ReportComponent.builder(PROJECT, 1).build();
    ruleRepository.add(ruleKey);
    treeRootHolder.setRoot(project);
    analysisMetadata.setAnalysisDate(new Random().nextLong());
    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);
    ChangedIssue changeIssue = builder.getIssues().iterator().next();
    assertThat(changeIssue.getAssignee()).isPresent();
    IssuesChangesNotificationBuilder.User assignee = changeIssue.getAssignee().get();
    assertThat(assignee.getUuid()).isEqualTo(userDto.getUuid());
    assertThat(assignee.getName()).contains(userDto.getName());
    assertThat(assignee.getLogin()).isEqualTo(userDto.getLogin());
}
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 9 with IssuesChangesNotification

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

the class NotificationFactoryTest method newIssuesChangesNotification_creates_project_from_TreeRootHolder_and_branch_name_from_branch.

@Test
public void newIssuesChangesNotification_creates_project_from_TreeRootHolder_and_branch_name_from_branch() {
    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.getProject().getUuid()).isEqualTo(project.getUuid());
    assertThat(changeIssue.getProject().getKey()).isEqualTo(project.getKey());
    assertThat(changeIssue.getProject().getProjectName()).isEqualTo(project.getName());
    assertThat(changeIssue.getProject().getBranchName()).contains(branchName);
}
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 10 with IssuesChangesNotification

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

the class SendIssueNotificationsStep method sendIssuesChangesNotification.

private void sendIssuesChangesNotification(Set<DefaultIssue> issues, Map<String, UserDto> assigneesByUuid, NotificationStatistics notificationStatistics) {
    IssuesChangesNotification notification = notificationFactory.newIssuesChangesNotification(issues, assigneesByUuid);
    notificationStatistics.issueChangesDeliveries += service.deliverEmails(singleton(notification));
    notificationStatistics.issueChanges++;
    // compatibility with old API
    notificationStatistics.issueChangesDeliveries += service.deliver(notification);
}
Also used : IssuesChangesNotification(org.sonar.server.issue.notification.IssuesChangesNotification)

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