Search in sources :

Example 1 with EmailRecipient

use of org.sonar.server.notification.NotificationManager.EmailRecipient in project sonarqube by SonarSource.

the class MyNewIssuesNotificationHandlerTest method deliver_ignores_notification_without_assignee.

@Test
public void deliver_ignores_notification_without_assignee() {
    String projectKey = randomAlphabetic(10);
    Set<MyNewIssuesNotification> withAssignee = IntStream.range(0, 1 + new Random().nextInt(5)).mapToObj(i -> newNotification(projectKey, randomAlphabetic(11 + i))).collect(toSet());
    Set<MyNewIssuesNotification> noAssignee = randomSetOfNotifications(projectKey, null);
    Set<MyNewIssuesNotification> noProjectKeyNoAssignee = randomSetOfNotifications(null, null);
    Set<EmailRecipient> authorizedRecipients = withAssignee.stream().map(n -> new EmailRecipient(n.getAssignee(), n.getAssignee() + "@foo")).collect(toSet());
    Set<EmailDeliveryRequest> expectedRequests = withAssignee.stream().map(n -> new EmailDeliveryRequest(n.getAssignee() + "@foo", n)).collect(toSet());
    when(emailNotificationChannel.isActivated()).thenReturn(true);
    Set<String> assignees = withAssignee.stream().map(MyNewIssuesNotification::getAssignee).collect(toSet());
    when(notificationManager.findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey, assignees, ALL_MUST_HAVE_ROLE_USER)).thenReturn(authorizedRecipients);
    Set<MyNewIssuesNotification> notifications = Stream.of(withAssignee.stream(), noAssignee.stream(), noProjectKeyNoAssignee.stream()).flatMap(t -> t).collect(toSet());
    int deliver = underTest.deliver(notifications);
    assertThat(deliver).isZero();
    verify(notificationManager).findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey, assignees, ALL_MUST_HAVE_ROLE_USER);
    verifyNoMoreInteractions(notificationManager);
    verify(emailNotificationChannel).isActivated();
    verify(emailNotificationChannel).deliverAll(expectedRequests);
    verifyNoMoreInteractions(emailNotificationChannel);
}
Also used : IntStream(java.util.stream.IntStream) ALL_MUST_HAVE_ROLE_USER(org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject.ALL_MUST_HAVE_ROLE_USER) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) EmailDeliveryRequest(org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) EmailRecipient(org.sonar.server.notification.NotificationManager.EmailRecipient) NotificationManager(org.sonar.server.notification.NotificationManager) Nullable(javax.annotation.Nullable) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSet.of(com.google.common.collect.ImmutableSet.of) Collections.emptySet(java.util.Collections.emptySet) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) Set(java.util.Set) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) PER_PROJECT_NOTIFICATION(org.sonar.server.notification.NotificationDispatcherMetadata.PER_PROJECT_NOTIFICATION) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) Stream(java.util.stream.Stream) GLOBAL_NOTIFICATION(org.sonar.server.notification.NotificationDispatcherMetadata.GLOBAL_NOTIFICATION) NotificationDispatcherMetadata(org.sonar.server.notification.NotificationDispatcherMetadata) EmailNotificationChannel(org.sonar.server.notification.email.EmailNotificationChannel) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) EmailRecipient(org.sonar.server.notification.NotificationManager.EmailRecipient) Random(java.util.Random) EmailDeliveryRequest(org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest) Test(org.junit.Test)

Example 2 with EmailRecipient

use of org.sonar.server.notification.NotificationManager.EmailRecipient in project sonarqube by SonarSource.

the class MyNewIssuesNotificationHandlerTest method deliver_ignores_notification_without_projectKey.

@Test
public void deliver_ignores_notification_without_projectKey() {
    String projectKey = randomAlphabetic(10);
    Set<MyNewIssuesNotification> withProjectKey = IntStream.range(0, 1 + new Random().nextInt(5)).mapToObj(i -> newNotification(projectKey, randomAlphabetic(11 + i))).collect(toSet());
    Set<MyNewIssuesNotification> noProjectKey = IntStream.range(0, 1 + new Random().nextInt(5)).mapToObj(i -> newNotification(null, randomAlphabetic(11 + i))).collect(toSet());
    Set<MyNewIssuesNotification> noProjectKeyNoAssignee = randomSetOfNotifications(null, null);
    Set<EmailRecipient> authorizedRecipients = withProjectKey.stream().map(n -> new EmailRecipient(n.getAssignee(), n.getAssignee() + "@foo")).collect(toSet());
    Set<EmailDeliveryRequest> expectedRequests = withProjectKey.stream().map(n -> new EmailDeliveryRequest(n.getAssignee() + "@foo", n)).collect(toSet());
    when(emailNotificationChannel.isActivated()).thenReturn(true);
    Set<String> assignees = withProjectKey.stream().map(MyNewIssuesNotification::getAssignee).collect(toSet());
    when(notificationManager.findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey, assignees, ALL_MUST_HAVE_ROLE_USER)).thenReturn(authorizedRecipients);
    Set<MyNewIssuesNotification> notifications = Stream.of(withProjectKey.stream(), noProjectKey.stream(), noProjectKeyNoAssignee.stream()).flatMap(t -> t).collect(toSet());
    int deliver = underTest.deliver(notifications);
    assertThat(deliver).isZero();
    verify(notificationManager).findSubscribedEmailRecipients(MY_NEW_ISSUES_DISPATCHER_KEY, projectKey, assignees, ALL_MUST_HAVE_ROLE_USER);
    verifyNoMoreInteractions(notificationManager);
    verify(emailNotificationChannel).isActivated();
    verify(emailNotificationChannel).deliverAll(expectedRequests);
    verifyNoMoreInteractions(emailNotificationChannel);
}
Also used : IntStream(java.util.stream.IntStream) ALL_MUST_HAVE_ROLE_USER(org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject.ALL_MUST_HAVE_ROLE_USER) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) EmailDeliveryRequest(org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) EmailRecipient(org.sonar.server.notification.NotificationManager.EmailRecipient) NotificationManager(org.sonar.server.notification.NotificationManager) Nullable(javax.annotation.Nullable) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSet.of(com.google.common.collect.ImmutableSet.of) Collections.emptySet(java.util.Collections.emptySet) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) Set(java.util.Set) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) PER_PROJECT_NOTIFICATION(org.sonar.server.notification.NotificationDispatcherMetadata.PER_PROJECT_NOTIFICATION) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) Stream(java.util.stream.Stream) GLOBAL_NOTIFICATION(org.sonar.server.notification.NotificationDispatcherMetadata.GLOBAL_NOTIFICATION) NotificationDispatcherMetadata(org.sonar.server.notification.NotificationDispatcherMetadata) EmailNotificationChannel(org.sonar.server.notification.email.EmailNotificationChannel) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) EmailRecipient(org.sonar.server.notification.NotificationManager.EmailRecipient) Random(java.util.Random) EmailDeliveryRequest(org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest) Test(org.junit.Test)

Example 3 with EmailRecipient

use of org.sonar.server.notification.NotificationManager.EmailRecipient in project sonarqube by SonarSource.

the class NewIssuesNotificationHandlerTest method deliver_ignores_notification_without_projectKey.

@Test
public void deliver_ignores_notification_without_projectKey() {
    String projectKey = randomAlphabetic(10);
    Set<NewIssuesNotification> withProjectKey = IntStream.range(0, 1 + new Random().nextInt(5)).mapToObj(i -> newNotification(projectKey)).collect(toSet());
    Set<NewIssuesNotification> noProjectKey = IntStream.range(0, 1 + new Random().nextInt(5)).mapToObj(i -> newNotification(null)).collect(toSet());
    Set<EmailRecipient> emailRecipients = IntStream.range(0, 1 + new Random().nextInt(10)).mapToObj(i -> "user_" + i).map(login -> new EmailRecipient(login, emailOf(login))).collect(toSet());
    Set<EmailDeliveryRequest> expectedRequests = emailRecipients.stream().flatMap(emailRecipient -> withProjectKey.stream().map(notif -> new EmailDeliveryRequest(emailRecipient.getEmail(), notif))).collect(toSet());
    when(emailNotificationChannel.isActivated()).thenReturn(true);
    when(notificationManager.findSubscribedEmailRecipients(NEW_ISSUES_DISPATCHER_KEY, projectKey, ALL_MUST_HAVE_ROLE_USER)).thenReturn(emailRecipients);
    Set<NewIssuesNotification> notifications = Stream.of(withProjectKey.stream(), noProjectKey.stream()).flatMap(t -> t).collect(toSet());
    int deliver = underTest.deliver(notifications);
    assertThat(deliver).isZero();
    verify(notificationManager).findSubscribedEmailRecipients(NEW_ISSUES_DISPATCHER_KEY, projectKey, ALL_MUST_HAVE_ROLE_USER);
    verifyNoMoreInteractions(notificationManager);
    verify(emailNotificationChannel).isActivated();
    verify(emailNotificationChannel).deliverAll(expectedRequests);
    verifyNoMoreInteractions(emailNotificationChannel);
}
Also used : IntStream(java.util.stream.IntStream) ALL_MUST_HAVE_ROLE_USER(org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject.ALL_MUST_HAVE_ROLE_USER) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) EmailDeliveryRequest(org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) EmailRecipient(org.sonar.server.notification.NotificationManager.EmailRecipient) NotificationManager(org.sonar.server.notification.NotificationManager) Nullable(javax.annotation.Nullable) Collectors.toSet(java.util.stream.Collectors.toSet) Collections.emptySet(java.util.Collections.emptySet) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) Set(java.util.Set) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) PER_PROJECT_NOTIFICATION(org.sonar.server.notification.NotificationDispatcherMetadata.PER_PROJECT_NOTIFICATION) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) Stream(java.util.stream.Stream) GLOBAL_NOTIFICATION(org.sonar.server.notification.NotificationDispatcherMetadata.GLOBAL_NOTIFICATION) NotificationDispatcherMetadata(org.sonar.server.notification.NotificationDispatcherMetadata) EmailNotificationChannel(org.sonar.server.notification.email.EmailNotificationChannel) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) EmailRecipient(org.sonar.server.notification.NotificationManager.EmailRecipient) Random(java.util.Random) EmailDeliveryRequest(org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest) Test(org.junit.Test)

Example 4 with EmailRecipient

use of org.sonar.server.notification.NotificationManager.EmailRecipient in project sonarqube by SonarSource.

the class DefaultNotificationManagerTest method findSubscribedEmailRecipients_with_logins_returns_empty_if_login_set_is_empty.

@Test
public void findSubscribedEmailRecipients_with_logins_returns_empty_if_login_set_is_empty() {
    String dispatcherKey = randomAlphabetic(12);
    String projectKey = randomAlphabetic(6);
    Set<EmailRecipient> recipients = underTest.findSubscribedEmailRecipients(dispatcherKey, projectKey, ImmutableSet.of(), ALL_MUST_HAVE_ROLE_USER);
    assertThat(recipients).isEmpty();
}
Also used : EmailRecipient(org.sonar.server.notification.NotificationManager.EmailRecipient) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 5 with EmailRecipient

use of org.sonar.server.notification.NotificationManager.EmailRecipient in project sonarqube by SonarSource.

the class DefaultNotificationManagerTest method findSubscribedEmailRecipients_does_not_call_DB_for_project_permission_filtering_if_there_is_no_global_subscriber.

@Test
public void findSubscribedEmailRecipients_does_not_call_DB_for_project_permission_filtering_if_there_is_no_global_subscriber() {
    String dispatcherKey = randomAlphabetic(12);
    String globalPermission = randomAlphanumeric(4);
    String projectPermission = randomAlphanumeric(5);
    String projectKey = randomAlphabetic(6);
    Set<EmailSubscriberDto> subscribers = IntStream.range(0, 1 + new Random().nextInt(10)).mapToObj(i -> EmailSubscriberDto.create("user" + i, false, "user" + i + "@sonarsource.com")).collect(Collectors.toSet());
    Set<String> logins = subscribers.stream().map(EmailSubscriberDto::getLogin).collect(Collectors.toSet());
    when(propertiesDao.findEmailSubscribersForNotification(dbSession, dispatcherKey, "EmailNotificationChannel", projectKey)).thenReturn(subscribers);
    when(authorizationDao.keepAuthorizedLoginsOnProject(dbSession, logins, projectKey, projectPermission)).thenReturn(logins);
    Set<EmailRecipient> emailRecipients = underTest.findSubscribedEmailRecipients(dispatcherKey, projectKey, new SubscriberPermissionsOnProject(globalPermission, projectPermission));
    Set<EmailRecipient> expected = subscribers.stream().map(i -> new EmailRecipient(i.getLogin(), i.getEmail())).collect(Collectors.toSet());
    assertThat(emailRecipients).isEqualTo(expected);
    verify(authorizationDao, times(0)).keepAuthorizedLoginsOnProject(eq(dbSession), anySet(), anyString(), eq(globalPermission));
    verify(authorizationDao, times(1)).keepAuthorizedLoginsOnProject(eq(dbSession), anySet(), anyString(), eq(projectPermission));
}
Also used : IntStream(java.util.stream.IntStream) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) Arrays(java.util.Arrays) ALL_MUST_HAVE_ROLE_USER(org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject.ALL_MUST_HAVE_ROLE_USER) RandomStringUtils.randomAlphanumeric(org.apache.commons.lang.RandomStringUtils.randomAlphanumeric) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) PropertiesDao(org.sonar.db.property.PropertiesDao) Multimap(com.google.common.collect.Multimap) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) Mockito.spy(org.mockito.Mockito.spy) DbSession(org.sonar.db.DbSession) InvalidClassException(java.io.InvalidClassException) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Map(java.util.Map) NotificationChannel(org.sonar.api.notifications.NotificationChannel) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) EmailRecipient(org.sonar.server.notification.NotificationManager.EmailRecipient) NotificationQueueDao(org.sonar.db.notification.NotificationQueueDao) Mockito.only(org.mockito.Mockito.only) Before(org.junit.Before) Subscriber(org.sonar.db.property.Subscriber) ImmutableSet(com.google.common.collect.ImmutableSet) InOrder(org.mockito.InOrder) System2(org.sonar.api.utils.System2) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) Notification(org.sonar.api.notifications.Notification) AuthorizationDao(org.sonar.db.permission.AuthorizationDao) Collection(java.util.Collection) Set(java.util.Set) NotificationQueueDto(org.sonar.db.notification.NotificationQueueDto) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) Mockito.verify(org.mockito.Mockito.verify) DbClient(org.sonar.db.DbClient) List(java.util.List) UserRole(org.sonar.api.web.UserRole) VerificationModeFactory.times(org.mockito.internal.verification.VerificationModeFactory.times) Mockito.inOrder(org.mockito.Mockito.inOrder) EmailSubscriberDto(org.sonar.db.EmailSubscriberDto) Collections(java.util.Collections) SubscriberPermissionsOnProject(org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) EmailRecipient(org.sonar.server.notification.NotificationManager.EmailRecipient) EmailSubscriberDto(org.sonar.db.EmailSubscriberDto) Random(java.util.Random) SubscriberPermissionsOnProject(org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)18 EmailRecipient (org.sonar.server.notification.NotificationManager.EmailRecipient)18 Collections (java.util.Collections)9 Random (java.util.Random)9 Set (java.util.Set)9 Collectors (java.util.stream.Collectors)9 IntStream (java.util.stream.IntStream)9 RandomStringUtils.randomAlphabetic (org.apache.commons.lang.RandomStringUtils.randomAlphabetic)9 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)9 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)9 Mockito.mock (org.mockito.Mockito.mock)9 Mockito.verify (org.mockito.Mockito.verify)9 Mockito.when (org.mockito.Mockito.when)9 SubscriberPermissionsOnProject (org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject)9 DbSession (org.sonar.db.DbSession)6 ImmutableSet (com.google.common.collect.ImmutableSet)5 Multimap (com.google.common.collect.Multimap)5 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)5 InvalidClassException (java.io.InvalidClassException)5 Arrays (java.util.Arrays)5