Search in sources :

Example 6 with EmailRecipient

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

the class DefaultNotificationManagerTest method findSubscribedEmailRecipients_returns_empty_if_no_email_recipients_in_project_for_dispatcher_key.

@Test
public void findSubscribedEmailRecipients_returns_empty_if_no_email_recipients_in_project_for_dispatcher_key() {
    String dispatcherKey = randomAlphabetic(12);
    String globalPermission = randomAlphanumeric(4);
    String projectPermission = randomAlphanumeric(5);
    String projectKey = randomAlphabetic(6);
    when(propertiesDao.findEmailSubscribersForNotification(dbSession, dispatcherKey, "EmailNotificationChannel", projectKey)).thenReturn(Collections.emptySet());
    Set<EmailRecipient> emailRecipients = underTest.findSubscribedEmailRecipients(dispatcherKey, projectKey, new SubscriberPermissionsOnProject(globalPermission, projectPermission));
    assertThat(emailRecipients).isEmpty();
    verify(authorizationDao, times(0)).keepAuthorizedLoginsOnProject(any(DbSession.class), anySet(), anyString(), anyString());
}
Also used : EmailRecipient(org.sonar.server.notification.NotificationManager.EmailRecipient) DbSession(org.sonar.db.DbSession) SubscriberPermissionsOnProject(org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 7 with EmailRecipient

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

the class DefaultNotificationManagerTest method findSubscribedEmailRecipients_with_logins_does_not_call_DB_for_project_permission_filtering_if_there_is_no_global_subscriber.

@Test
public void findSubscribedEmailRecipients_with_logins_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, logins)).thenReturn(subscribers);
    when(authorizationDao.keepAuthorizedLoginsOnProject(dbSession, logins, projectKey, projectPermission)).thenReturn(logins);
    Set<EmailRecipient> emailRecipients = underTest.findSubscribedEmailRecipients(dispatcherKey, projectKey, logins, 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)

Example 8 with EmailRecipient

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

the class EmailRecipientTest method constructor_fails_with_NPE_if_email_is_null.

@Test
public void constructor_fails_with_NPE_if_email_is_null() {
    String login = randomAlphabetic(12);
    assertThatThrownBy(() -> new EmailRecipient(login, null)).isInstanceOf(NullPointerException.class).hasMessage("email can't be null");
}
Also used : EmailRecipient(org.sonar.server.notification.NotificationManager.EmailRecipient) Test(org.junit.Test)

Example 9 with EmailRecipient

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

the class EmailRecipientTest method equals_is_based_on_login_and_email.

@Test
public void equals_is_based_on_login_and_email() {
    String login = randomAlphabetic(11);
    String email = randomAlphabetic(12);
    EmailRecipient underTest = new EmailRecipient(login, email);
    assertThat(underTest).isEqualTo(new EmailRecipient(login, email)).isNotNull().isNotEqualTo(new Object()).isNotEqualTo(new EmailRecipient(email, login)).isNotEqualTo(new EmailRecipient(randomAlphabetic(5), email)).isNotEqualTo(new EmailRecipient(login, randomAlphabetic(5))).isNotEqualTo(new EmailRecipient(randomAlphabetic(5), randomAlphabetic(6)));
}
Also used : EmailRecipient(org.sonar.server.notification.NotificationManager.EmailRecipient) Test(org.junit.Test)

Example 10 with EmailRecipient

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

the class ReportAnalysisFailureNotificationHandlerTest method deliver_ignores_notification_without_projectKey.

@Test
public void deliver_ignores_notification_without_projectKey() {
    String projectKey = randomAlphabetic(10);
    Set<ReportAnalysisFailureNotification> withProjectKey = IntStream.range(0, 1 + new Random().nextInt(5)).mapToObj(i -> newNotification(projectKey)).collect(toSet());
    Set<ReportAnalysisFailureNotification> 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(REPORT_FAILURE_DISPATCHER_KEY, projectKey, REQUIRED_SUBSCRIBER_PERMISSIONS)).thenReturn(emailRecipients);
    Set<ReportAnalysisFailureNotification> notifications = Stream.of(withProjectKey.stream(), noProjectKey.stream()).flatMap(t -> t).collect(toSet());
    int deliver = underTest.deliver(notifications);
    assertThat(deliver).isZero();
    verify(notificationManager).findSubscribedEmailRecipients(REPORT_FAILURE_DISPATCHER_KEY, projectKey, REQUIRED_SUBSCRIBER_PERMISSIONS);
    verifyNoMoreInteractions(notificationManager);
    verify(emailNotificationChannel).isActivated();
    verify(emailNotificationChannel).deliverAll(expectedRequests);
    verifyNoMoreInteractions(emailNotificationChannel);
}
Also used : IntStream(java.util.stream.IntStream) 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) UserRole(org.sonar.api.web.UserRole) 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) SubscriberPermissionsOnProject(org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject) 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)

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