Search in sources :

Example 6 with EmailSubscriberDto

use of org.sonar.db.EmailSubscriberDto in project sonarqube by SonarSource.

the class AuthorizationDaoTest method selectQualityProfileAdministratorLogins_return_users_with_quality_profile_administrator_permission.

@Test
public void selectQualityProfileAdministratorLogins_return_users_with_quality_profile_administrator_permission() {
    UserDto user1 = db.users().insertUser(withEmail("user1"));
    db.users().insertPermissionOnUser(user1, ADMINISTER_QUALITY_PROFILES);
    UserDto user2 = db.users().insertUser(withEmail("user2"));
    db.users().insertPermissionOnUser(user2, ADMINISTER_QUALITY_PROFILES);
    Set<EmailSubscriberDto> subscribers = underTest.selectQualityProfileAdministratorLogins(dbSession);
    assertThat(subscribers).containsOnly(globalEmailSubscriberOf(user1), globalEmailSubscriberOf(user2));
}
Also used : EmailSubscriberDto(org.sonar.db.EmailSubscriberDto) UserDto(org.sonar.db.user.UserDto) Test(org.junit.Test)

Example 7 with EmailSubscriberDto

use of org.sonar.db.EmailSubscriberDto 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)

Example 8 with EmailSubscriberDto

use of org.sonar.db.EmailSubscriberDto 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 9 with EmailSubscriberDto

use of org.sonar.db.EmailSubscriberDto in project sonarqube by SonarSource.

the class BuiltInQPChangeNotificationHandlerTest method deliver_create_emailRequest_for_each_notification_and_for_each_global_administer_email_subscriber.

@Test
public void deliver_create_emailRequest_for_each_notification_and_for_each_global_administer_email_subscriber() {
    when(emailNotificationChannel.isActivated()).thenReturn(true);
    Set<BuiltInQPChangeNotification> notifications = IntStream.range(0, 1 + new Random().nextInt(10)).mapToObj(i -> mock(BuiltInQPChangeNotification.class)).collect(toSet());
    Set<EmailSubscriberDto> emailSubscribers = IntStream.range(0, 1 + new Random().nextInt(10)).mapToObj(i -> EmailSubscriberDto.create("login_" + i, true, "login_" + i + "@foo")).collect(toSet());
    when(authorizationDao.selectQualityProfileAdministratorLogins(dbSession)).thenReturn(emailSubscribers);
    Set<EmailNotificationChannel.EmailDeliveryRequest> expectedRequests = notifications.stream().flatMap(notification -> emailSubscribers.stream().map(subscriber -> new EmailNotificationChannel.EmailDeliveryRequest(subscriber.getEmail(), notification))).collect(toSet());
    int deliveries = new Random().nextInt(expectedRequests.size());
    when(emailNotificationChannel.deliverAll(expectedRequests)).thenReturn(deliveries);
    int deliver = underTest.deliver(notifications);
    assertThat(deliver).isEqualTo(deliveries);
    verify(emailNotificationChannel).isActivated();
    verify(emailNotificationChannel).deliverAll(expectedRequests);
    verifyNoMoreInteractions(emailNotificationChannel);
    verify(dbClient).openSession(false);
    verify(dbClient).authorizationDao();
    verifyNoMoreInteractions(dbClient);
    verify(authorizationDao).selectQualityProfileAdministratorLogins(dbSession);
    verifyNoMoreInteractions(authorizationDao);
    notifications.forEach(Mockito::verifyZeroInteractions);
}
Also used : IntStream(java.util.stream.IntStream) Collections.emptySet(java.util.Collections.emptySet) AuthorizationDao(org.sonar.db.permission.AuthorizationDao) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Set(java.util.Set) Random(java.util.Random) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) DbSession(org.sonar.db.DbSession) Mockito.verify(org.mockito.Mockito.verify) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) Mockito(org.mockito.Mockito) DbClient(org.sonar.db.DbClient) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) EmailSubscriberDto(org.sonar.db.EmailSubscriberDto) EmailNotificationChannel(org.sonar.server.notification.email.EmailNotificationChannel) Collectors.toSet(java.util.stream.Collectors.toSet) Before(org.junit.Before) Mockito.mock(org.mockito.Mockito.mock) EmailSubscriberDto(org.sonar.db.EmailSubscriberDto) Random(java.util.Random) Mockito(org.mockito.Mockito) EmailNotificationChannel(org.sonar.server.notification.email.EmailNotificationChannel) Test(org.junit.Test)

Example 10 with EmailSubscriberDto

use of org.sonar.db.EmailSubscriberDto in project sonarqube by SonarSource.

the class PropertiesDaoTest method findEmailRecipientsForNotification_with_logins_returns_empty_on_empty_properties_table.

@Test
public void findEmailRecipientsForNotification_with_logins_returns_empty_on_empty_properties_table() {
    db.users().insertUser();
    String dispatcherKey = randomAlphabetic(5);
    String channelKey = randomAlphabetic(6);
    String projectKey = randomAlphabetic(7);
    Set<String> logins = of("user1", "user2");
    Set<EmailSubscriberDto> subscribers = underTest.findEmailSubscribersForNotification(db.getSession(), dispatcherKey, channelKey, projectKey, logins);
    assertThat(subscribers).isEmpty();
}
Also used : EmailSubscriberDto(org.sonar.db.EmailSubscriberDto) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Aggregations

EmailSubscriberDto (org.sonar.db.EmailSubscriberDto)14 Test (org.junit.Test)13 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 DbSession (org.sonar.db.DbSession)6 Random (java.util.Random)5 Set (java.util.Set)5 IntStream (java.util.stream.IntStream)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 Before (org.junit.Before)5 Mockito.mock (org.mockito.Mockito.mock)5 Mockito.verify (org.mockito.Mockito.verify)5 Mockito.when (org.mockito.Mockito.when)5 DbClient (org.sonar.db.DbClient)5 ImmutableSet (com.google.common.collect.ImmutableSet)4 Multimap (com.google.common.collect.Multimap)4 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)4 InvalidClassException (java.io.InvalidClassException)4 Arrays (java.util.Arrays)4 Collection (java.util.Collection)4 Collections (java.util.Collections)4