Search in sources :

Example 11 with IssuesChangesNotification

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

the class SendIssueNotificationsStepTest method sendIssueChangeNotificationOnBranch.

private void sendIssueChangeNotificationOnBranch(long issueCreatedAt) {
    ComponentDto project = newPrivateProjectDto();
    ComponentDto branch = newBranchComponent(project, newBranchDto(project).setKey(BRANCH_NAME));
    ComponentDto file = newFileDto(branch);
    treeRootHolder.setRoot(builder(Type.PROJECT, 2).setKey(branch.getDbKey()).setPublicKey(branch.getKey()).setName(branch.longName()).setUuid(branch.uuid()).addChildren(builder(Type.FILE, 11).setKey(file.getDbKey()).setPublicKey(file.getKey()).setName(file.longName()).build()).build());
    analysisMetadataHolder.setProject(Project.from(project));
    RuleDefinitionDto ruleDefinitionDto = newRule();
    RuleType randomTypeExceptHotspot = RuleType.values()[nextInt(RuleType.values().length - 1)];
    DefaultIssue issue = newIssue(ruleDefinitionDto, branch, file).setType(randomTypeExceptHotspot).toDefaultIssue().setNew(false).setChanged(true).setSendNotifications(true).setCreationDate(new Date(issueCreatedAt));
    protoIssueCache.newAppender().append(issue).close();
    when(notificationService.hasProjectSubscribersForTypes(project.uuid(), NOTIF_TYPES)).thenReturn(true);
    IssuesChangesNotification issuesChangesNotification = mock(IssuesChangesNotification.class);
    when(notificationFactory.newIssuesChangesNotification(anySet(), anyMap())).thenReturn(issuesChangesNotification);
    analysisMetadataHolder.setBranch(newBranch(BranchType.BRANCH));
    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()).isEmpty();
    verify(notificationService).hasProjectSubscribersForTypes(project.uuid(), NOTIF_TYPES);
    verify(notificationService).deliverEmails(singleton(issuesChangesNotification));
    verify(notificationService).deliver(issuesChangesNotification);
    verifyNoMoreInteractions(notificationService);
}
Also used : 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) Date(java.util.Date) IssuesChangesNotification(org.sonar.server.issue.notification.IssuesChangesNotification)

Example 12 with IssuesChangesNotification

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

the class NotificationFactory method newIssuesChangesNotification.

public IssuesChangesNotification newIssuesChangesNotification(Set<DefaultIssue> issues, Map<String, UserDto> assigneesByUuid) {
    AnalysisChange change = new AnalysisChange(analysisMetadataHolder.getAnalysisDate());
    Set<ChangedIssue> changedIssues = issues.stream().map(issue -> new ChangedIssue.Builder(issue.key()).setAssignee(getAssignee(issue.assignee(), assigneesByUuid)).setNewResolution(issue.resolution()).setNewStatus(issue.status()).setRule(getRuleByRuleKey(issue.ruleKey())).setProject(getProject()).build()).collect(MoreCollectors.toSet(issues.size()));
    return issuesChangesSerializer.serialize(new IssuesChangesNotificationBuilder(changedIssues, change));
}
Also used : Branch(org.sonar.ce.task.projectanalysis.analysis.Branch) NewIssuesNotification(org.sonar.server.issue.notification.NewIssuesNotification) UserDto(org.sonar.db.user.UserDto) FILE(org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit.FILE) TypeAwareVisitorAdapter(org.sonar.ce.task.projectanalysis.component.TypeAwareVisitorAdapter) PULL_REQUEST(org.sonar.db.component.BranchType.PULL_REQUEST) DetailsSupplier(org.sonar.server.issue.notification.NewIssuesNotification.DetailsSupplier) Map(java.util.Map) MyNewIssuesNotification(org.sonar.server.issue.notification.MyNewIssuesNotification) Objects.requireNonNull(java.util.Objects.requireNonNull) PRE_ORDER(org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) ComputeEngineSide(org.sonar.api.ce.ComputeEngineSide) AnalysisChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.AnalysisChange) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) Nullable(javax.annotation.Nullable) Component(org.sonar.ce.task.projectanalysis.component.Component) RuleRepository(org.sonar.ce.task.projectanalysis.issue.RuleRepository) User(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User) DefaultIssue(org.sonar.core.issue.DefaultIssue) ImmutableMap(com.google.common.collect.ImmutableMap) Project(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Project) Durations(org.sonar.api.utils.Durations) DepthTraversalTypeAwareCrawler(org.sonar.ce.task.projectanalysis.component.DepthTraversalTypeAwareCrawler) Set(java.util.Set) IssuesChangesNotification(org.sonar.server.issue.notification.IssuesChangesNotification) AnalysisMetadataHolder(org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder) Preconditions.checkState(com.google.common.base.Preconditions.checkState) RuleKey(org.sonar.api.rule.RuleKey) TreeRootHolder(org.sonar.ce.task.projectanalysis.component.TreeRootHolder) RuleDefinition(org.sonar.server.issue.notification.NewIssuesNotification.RuleDefinition) Optional(java.util.Optional) CheckForNull(javax.annotation.CheckForNull) IssuesChangesNotificationSerializer(org.sonar.server.issue.notification.IssuesChangesNotificationSerializer) IssuesChangesNotificationBuilder(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) IssuesChangesNotificationBuilder(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder) IssuesChangesNotificationBuilder(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder) AnalysisChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.AnalysisChange)

Example 13 with IssuesChangesNotification

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

the class IssueUpdaterTest method verify_notification_without_resolution.

@Test
public void verify_notification_without_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.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()).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 14 with IssuesChangesNotification

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

the class IssueUpdaterTest method verify_notification_when_assignee_has_changed.

@Test
public void verify_notification_when_assignee_has_changed() {
    UserDto oldAssignee = 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.setAssigneeUuid(oldAssignee.getUuid())).toDefaultIssue();
    UserDto changeAuthor = db.users().insertUser();
    IssueChangeContext context = IssueChangeContext.createUser(new Date(), changeAuthor.getUuid());
    UserDto newAssignee = db.users().insertUser();
    issueFieldsSetter.assign(issue, newAssignee, 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()).contains(userOf(newAssignee));
    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)

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