Search in sources :

Example 11 with UserChange

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

the class FpOrWontFixEmailTemplateTest method format_sets_from_to_login_of_author_change_when_name_is_not_available.

@Test
public void format_sets_from_to_login_of_author_change_when_name_is_not_available() {
    UserChange change = new UserChange(new Random().nextLong(), new User(randomAlphabetic(5), randomAlphabetic(6), null));
    EmailMessage emailMessage = underTest.format(new FPOrWontFixNotification(change, Collections.emptySet(), WONT_FIX));
    assertThat(emailMessage.getFrom()).isEqualTo(change.getUser().getLogin());
}
Also used : UserChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange) User(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User) Random(java.util.Random) Test(org.junit.Test)

Example 12 with UserChange

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

the class ChangesOnMyIssueNotificationHandlerTest method deliver_checks_by_projectKey_if_notifications_have_subscribed_assignee_to_ChangesOnMyIssues_notifications.

@Test
public void deliver_checks_by_projectKey_if_notifications_have_subscribed_assignee_to_ChangesOnMyIssues_notifications() {
    when(emailNotificationChannel.isActivated()).thenReturn(true);
    Project project = newProject();
    Set<ChangedIssue> issues = IntStream.range(0, 1 + new Random().nextInt(2)).mapToObj(i -> new ChangedIssue.Builder("issue_key_" + i).setNewStatus("foo").setAssignee(newUser("assignee_" + i)).setRule(newRule()).setProject(project).build()).collect(toSet());
    IssuesChangesNotificationBuilder builder = new IssuesChangesNotificationBuilder(issues, new UserChange(new Random().nextLong(), new User("user_uuid", "user_login", null)));
    int deliver = underTest.deliver(ImmutableSet.of(serializer.serialize(builder)));
    assertThat(deliver).isZero();
    Set<String> assigneeLogins = issues.stream().map(i -> i.getAssignee().get().getLogin()).collect(toSet());
    verify(notificationManager).findSubscribedEmailRecipients(CHANGE_ON_MY_ISSUES_DISPATCHER_KEY, project.getKey(), assigneeLogins, ALL_MUST_HAVE_ROLE_USER);
    verifyNoMoreInteractions(notificationManager);
    verify(emailNotificationChannel).isActivated();
    verifyNoMoreInteractions(emailNotificationChannel);
}
Also used : IntStream(java.util.stream.IntStream) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) ALL_MUST_HAVE_ROLE_USER(org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject.ALL_MUST_HAVE_ROLE_USER) ListMultimap(com.google.common.collect.ListMultimap) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Random(java.util.Random) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) ArgumentCaptor(org.mockito.ArgumentCaptor) EmailDeliveryRequest(org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest) MoreCollectors.index(org.sonar.core.util.stream.MoreCollectors.index) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) AnalysisChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.AnalysisChange) NotificationManager(org.sonar.server.notification.NotificationManager) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) Nullable(javax.annotation.Nullable) Collectors.toSet(java.util.stream.Collectors.toSet) MoreCollectors.unorderedIndex(org.sonar.core.util.stream.MoreCollectors.unorderedIndex) User(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User) ImmutableSet(com.google.common.collect.ImmutableSet) Project(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Project) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) Set(java.util.Set) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) UserChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange) PER_PROJECT_NOTIFICATION(org.sonar.server.notification.NotificationDispatcherMetadata.PER_PROJECT_NOTIFICATION) Mockito.verify(org.mockito.Mockito.verify) IssuesChangesNotificationBuilderTesting.newRandomNotAHotspotRule(org.sonar.server.issue.notification.IssuesChangesNotificationBuilderTesting.newRandomNotAHotspotRule) Mockito(org.mockito.Mockito) Rule(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Rule) List(java.util.List) Stream(java.util.stream.Stream) GLOBAL_NOTIFICATION(org.sonar.server.notification.NotificationDispatcherMetadata.GLOBAL_NOTIFICATION) Change(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Change) NotificationDispatcherMetadata(org.sonar.server.notification.NotificationDispatcherMetadata) EmailNotificationChannel(org.sonar.server.notification.email.EmailNotificationChannel) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) Project(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Project) UserChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange) User(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User) Random(java.util.Random) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) Test(org.junit.Test)

Example 13 with UserChange

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

the class ChangesOnMyIssueNotificationHandlerTest method deliver_checks_by_projectKeys_if_notifications_have_subscribed_assignee_to_ChangesOnMyIssues_notifications.

@Test
public void deliver_checks_by_projectKeys_if_notifications_have_subscribed_assignee_to_ChangesOnMyIssues_notifications() {
    when(emailNotificationChannel.isActivated()).thenReturn(true);
    Set<ChangedIssue> issues = IntStream.range(0, 1 + new Random().nextInt(2)).mapToObj(i -> new ChangedIssue.Builder("issue_key_" + i).setNewStatus("foo").setAssignee(newUser("" + i)).setRule(newRule()).setProject(newProject(i + "")).build()).collect(toSet());
    IssuesChangesNotificationBuilder builder = new IssuesChangesNotificationBuilder(issues, new UserChange(new Random().nextLong(), new User("user_uuid", "user_login", null)));
    int deliver = underTest.deliver(ImmutableSet.of(serializer.serialize(builder)));
    assertThat(deliver).isZero();
    issues.stream().collect(MoreCollectors.index(ChangedIssue::getProject)).asMap().forEach((key, value) -> {
        String projectKey = key.getKey();
        Set<String> assigneeLogins = value.stream().map(i -> i.getAssignee().get().getLogin()).collect(toSet());
        verify(notificationManager).findSubscribedEmailRecipients(CHANGE_ON_MY_ISSUES_DISPATCHER_KEY, projectKey, assigneeLogins, ALL_MUST_HAVE_ROLE_USER);
    });
    verifyNoMoreInteractions(notificationManager);
    verify(emailNotificationChannel).isActivated();
    verifyNoMoreInteractions(emailNotificationChannel);
}
Also used : IntStream(java.util.stream.IntStream) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) ALL_MUST_HAVE_ROLE_USER(org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject.ALL_MUST_HAVE_ROLE_USER) ListMultimap(com.google.common.collect.ListMultimap) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Random(java.util.Random) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) ArgumentCaptor(org.mockito.ArgumentCaptor) EmailDeliveryRequest(org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest) MoreCollectors.index(org.sonar.core.util.stream.MoreCollectors.index) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) AnalysisChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.AnalysisChange) NotificationManager(org.sonar.server.notification.NotificationManager) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) Nullable(javax.annotation.Nullable) Collectors.toSet(java.util.stream.Collectors.toSet) MoreCollectors.unorderedIndex(org.sonar.core.util.stream.MoreCollectors.unorderedIndex) User(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User) ImmutableSet(com.google.common.collect.ImmutableSet) Project(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Project) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) Set(java.util.Set) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) UserChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange) PER_PROJECT_NOTIFICATION(org.sonar.server.notification.NotificationDispatcherMetadata.PER_PROJECT_NOTIFICATION) Mockito.verify(org.mockito.Mockito.verify) IssuesChangesNotificationBuilderTesting.newRandomNotAHotspotRule(org.sonar.server.issue.notification.IssuesChangesNotificationBuilderTesting.newRandomNotAHotspotRule) Mockito(org.mockito.Mockito) Rule(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Rule) List(java.util.List) Stream(java.util.stream.Stream) GLOBAL_NOTIFICATION(org.sonar.server.notification.NotificationDispatcherMetadata.GLOBAL_NOTIFICATION) Change(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Change) NotificationDispatcherMetadata(org.sonar.server.notification.NotificationDispatcherMetadata) EmailNotificationChannel(org.sonar.server.notification.email.EmailNotificationChannel) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) UserChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange) User(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User) Random(java.util.Random) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) Test(org.junit.Test)

Example 14 with UserChange

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

the class ChangesOnMyIssuesNotificationTest method key_is_ChangesOnMyIssues.

@Test
public void key_is_ChangesOnMyIssues() {
    ChangesOnMyIssuesNotification underTest = new ChangesOnMyIssuesNotification(new UserChange(new Random().nextLong(), new User(randomAlphabetic(2), randomAlphabetic(3), randomAlphabetic(4))), ImmutableSet.of());
    assertThat(underTest.getType()).isEqualTo("ChangesOnMyIssues");
}
Also used : UserChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange) User(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User) Random(java.util.Random) Test(org.junit.Test)

Example 15 with UserChange

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

the class BulkChangeAction method sendNotification.

private void sendNotification(Collection<DefaultIssue> issues, BulkChangeData bulkChangeData, Map<String, UserDto> userDtoByUuid, UserDto author) {
    if (!bulkChangeData.sendNotification) {
        return;
    }
    Set<ChangedIssue> changedIssues = issues.stream().filter(issue -> issue.updateDate() != null).map(issue -> toNotification(bulkChangeData, userDtoByUuid, issue)).filter(Objects::nonNull).collect(toSet(issues.size()));
    if (changedIssues.isEmpty()) {
        return;
    }
    IssuesChangesNotificationBuilder builder = new IssuesChangesNotificationBuilder(changedIssues, new UserChange(oldestUpdateDate(issues), new User(author.getUuid(), author.getLogin(), author.getName())));
    notificationService.scheduleForSending(notificationSerializer.serialize(builder));
}
Also used : PARAM_SET_SEVERITY(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_SET_SEVERITY) Date(java.util.Date) ActionContext(org.sonar.server.issue.ActionContext) BUG(org.sonar.api.rules.RuleType.BUG) DbSession(org.sonar.db.DbSession) Request(org.sonar.api.server.ws.Request) Loggers(org.sonar.api.utils.log.Loggers) SET_SEVERITY_KEY(org.sonar.server.issue.SetSeverityAction.SET_SEVERITY_KEY) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) PARAM_ADD_TAGS(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_ADD_TAGS) WebService(org.sonar.api.server.ws.WebService) IssueChangePostProcessor(org.sonar.server.issue.IssueChangePostProcessor) Collectors.toMap(java.util.stream.Collectors.toMap) UUID_EXAMPLE_01(org.sonar.core.util.Uuids.UUID_EXAMPLE_01) BLOCKER(org.sonar.api.rule.Severity.BLOCKER) UUID_EXAMPLE_02(org.sonar.core.util.Uuids.UUID_EXAMPLE_02) SET_TYPE_KEY(org.sonar.server.issue.SetTypeAction.SET_TYPE_KEY) TRANSITION_PARAMETER(org.sonar.server.issue.TransitionAction.TRANSITION_PARAMETER) Map(java.util.Map) PARAM_ASSIGN(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_ASSIGN) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) PARAM_REMOVE_TAGS(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_REMOVE_TAGS) PARAM_SET_TYPE(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_SET_TYPE) NotificationManager(org.sonar.server.notification.NotificationManager) PARAM_COMMENT(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_COMMENT) PARAM_DO_TRANSITION(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_DO_TRANSITION) TAGS_PARAMETER(org.sonar.server.issue.AbstractChangeTagsAction.TAGS_PARAMETER) System2(org.sonar.api.utils.System2) Project(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Project) OPEN_AS_VULNERABILITY(org.sonar.api.issue.DefaultTransitions.OPEN_AS_VULNERABILITY) Predicate(java.util.function.Predicate) ASSIGNEE_PARAMETER(org.sonar.server.issue.AssignAction.ASSIGNEE_PARAMETER) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) ImmutableMap.of(com.google.common.collect.ImmutableMap.of) String.format(java.lang.String.format) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Objects(java.util.Objects) DbClient(org.sonar.db.DbClient) AddTagsAction(org.sonar.server.issue.AddTagsAction) PARAM_SEND_NOTIFICATIONS(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_SEND_NOTIFICATIONS) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) ACTION_BULK_CHANGE(org.sonarqube.ws.client.issue.IssuesWsParameters.ACTION_BULK_CHANGE) RuleKey(org.sonar.api.rule.RuleKey) Function.identity(java.util.function.Function.identity) MoreCollectors.uniqueIndex(org.sonar.core.util.stream.MoreCollectors.uniqueIndex) Optional(java.util.Optional) IssuesChangesNotificationSerializer(org.sonar.server.issue.notification.IssuesChangesNotificationSerializer) WsUtils.writeProtobuf(org.sonar.server.ws.WsUtils.writeProtobuf) BranchDto(org.sonar.db.component.BranchDto) IssueDto(org.sonar.db.issue.IssueDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) UserDto(org.sonar.db.user.UserDto) Issues(org.sonarqube.ws.Issues) MAX_PAGE_SIZE(org.sonar.server.es.SearchOptions.MAX_PAGE_SIZE) SET_AS_IN_REVIEW(org.sonar.api.issue.DefaultTransitions.SET_AS_IN_REVIEW) HashMap(java.util.HashMap) PARAM_ISSUES(org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_ISSUES) RuleType(org.sonar.api.rules.RuleType) COMMENT_PROPERTY(org.sonar.server.issue.CommentAction.COMMENT_PROPERTY) HashSet(java.util.HashSet) Action(org.sonar.server.issue.Action) WebIssueStorage(org.sonar.server.issue.WebIssueStorage) RemoveTagsAction(org.sonar.server.issue.RemoveTagsAction) MoreCollectors.toSet(org.sonar.core.util.stream.MoreCollectors.toSet) Severity(org.sonar.api.rule.Severity) IssueChangeContext(org.sonar.core.issue.IssueChangeContext) Objects.requireNonNull(java.util.Objects.requireNonNull) Response(org.sonar.api.server.ws.Response) SECURITY_HOTSPOT(org.sonar.api.rules.RuleType.SECURITY_HOTSPOT) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) Nullable(javax.annotation.Nullable) Logger(org.sonar.api.utils.log.Logger) REOPEN(org.sonar.api.issue.DefaultTransitions.REOPEN) COMMENT_KEY(org.sonar.server.issue.CommentAction.COMMENT_KEY) User(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User) BranchType(org.sonar.db.component.BranchType) DefaultIssue(org.sonar.core.issue.DefaultIssue) UserChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange) Consumer(java.util.function.Consumer) UserRole(org.sonar.api.web.UserRole) DO_TRANSITION_KEY(org.sonar.server.issue.TransitionAction.DO_TRANSITION_KEY) DefaultTransitions(org.sonar.api.issue.DefaultTransitions) RESOLVE_AS_REVIEWED(org.sonar.api.issue.DefaultTransitions.RESOLVE_AS_REVIEWED) Change(org.sonar.api.server.ws.Change) AssignAction(org.sonar.server.issue.AssignAction) UserSession(org.sonar.server.user.UserSession) SEVERITY_PARAMETER(org.sonar.server.issue.SetSeverityAction.SEVERITY_PARAMETER) CheckForNull(javax.annotation.CheckForNull) TYPE_PARAMETER(org.sonar.server.issue.SetTypeAction.TYPE_PARAMETER) IssuesChangesNotificationBuilder(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder) UserChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange) User(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) IssuesChangesNotificationBuilder(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder)

Aggregations

UserChange (org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange)40 Test (org.junit.Test)35 ChangedIssue (org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue)27 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)21 Random (java.util.Random)21 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)20 Mockito.mock (org.mockito.Mockito.mock)20 Project (org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Project)20 User (org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User)19 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)18 List (java.util.List)17 Set (java.util.Set)17 AnalysisChange (org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.AnalysisChange)17 ImmutableSet (com.google.common.collect.ImmutableSet)16 DataProviderRunner (com.tngtech.java.junit.dataprovider.DataProviderRunner)16 Collectors.toSet (java.util.stream.Collectors.toSet)16 IntStream (java.util.stream.IntStream)16 Stream (java.util.stream.Stream)16 RandomStringUtils.randomAlphabetic (org.apache.commons.lang.RandomStringUtils.randomAlphabetic)16 RunWith (org.junit.runner.RunWith)16