Search in sources :

Example 26 with Notification

use of org.sonar.api.notifications.Notification in project sonarqube by SonarSource.

the class NewIssuesEmailTemplateTest method format_email_with_issue_on_branch_with_version.

@Test
public void format_email_with_issue_on_branch_with_version() {
    Notification notification = newNotification(32).setFieldValue("branch", "feature1").setFieldValue("projectVersion", "42.1.1");
    EmailMessage message = template.format(notification);
    // TODO datetime to be completed when test is isolated from JVM timezone
    assertThat(message.getMessage()).startsWith("Project: Struts\n" + "Branch: feature1\n" + "Version: 42.1.1\n" + "\n" + "32 new issues (new debt: 1d3h)\n" + "\n" + "    Type\n" + "        Bug: 1    Vulnerability: 10    Code Smell: 3\n" + "\n" + "More details at: http://nemo.sonarsource.org/project/issues?id=org.apache%3Astruts&branch=feature1&createdAt=2010-05-1");
}
Also used : Notification(org.sonar.api.notifications.Notification) Test(org.junit.Test)

Example 27 with Notification

use of org.sonar.api.notifications.Notification in project sonarqube by SonarSource.

the class DefaultAdminCredentialsVerifierNotificationTemplateTest method format_notification.

@Test
public void format_notification() {
    Notification notification = new Notification(DefaultAdminCredentialsVerifierNotification.TYPE);
    EmailMessage emailMessage = underTest.format(notification);
    assertThat(emailMessage.getSubject()).isEqualTo("Default Administrator credentials are still used");
    assertThat(emailMessage.getMessage()).isEqualTo("Hello,\n\n" + "Your SonarQube instance is still using default administrator credentials.\n" + "Make sure to change the password for the 'admin' account or deactivate this account.");
}
Also used : EmailMessage(org.sonar.server.issue.notification.EmailMessage) Notification(org.sonar.api.notifications.Notification) Test(org.junit.Test)

Example 28 with Notification

use of org.sonar.api.notifications.Notification in project sonarqube by SonarSource.

the class NotificationDaemon method processQueue.

private synchronized void processQueue() {
    long start = now();
    long lastLog = start;
    long notifSentCount = 0;
    Notification notifToSend = manager.getFromQueue();
    while (notifToSend != null) {
        notifSentCount += service.deliverEmails(singleton(notifToSend));
        // compatibility with old API
        notifSentCount += service.deliver(notifToSend);
        if (stopping) {
            break;
        }
        long now = now();
        if (now - lastLog > delayBeforeReportingStatusInSeconds * 1000) {
            long remainingNotifCount = manager.count();
            lastLog = now;
            long spentTimeInMinutes = (now - start) / (60 * 1000);
            log(notifSentCount, remainingNotifCount, spentTimeInMinutes);
        }
        notifToSend = manager.getFromQueue();
    }
}
Also used : Notification(org.sonar.api.notifications.Notification)

Example 29 with Notification

use of org.sonar.api.notifications.Notification in project sonarqube by SonarSource.

the class QualityGateEventsStepTest method verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed.

private void verify_event_created_if_base_ALERT_STATUS_measure_exists_and_status_has_changed(Measure.Level previousAlertStatus, QualityGateStatus newQualityGateStatus, String expectedLabel) {
    when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().setQualityGateStatus(newQualityGateStatus).createNoValue()));
    when(measureRepository.getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().setQualityGateStatus(new QualityGateStatus(previousAlertStatus)).createNoValue()));
    underTest.execute(new TestComputationStepContext());
    verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
    verify(measureRepository).getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric);
    verify(eventRepository).add(eq(PROJECT_COMPONENT), eventArgumentCaptor.capture());
    verifyNoMoreInteractions(measureRepository, eventRepository);
    Event event = eventArgumentCaptor.getValue();
    assertThat(event.getCategory()).isEqualTo(Event.Category.ALERT);
    assertThat(event.getName()).isEqualTo(expectedLabel);
    assertThat(event.getDescription()).isEqualTo(ALERT_TEXT);
    assertThat(event.getData()).isNull();
    verify(notificationService).deliver(notificationArgumentCaptor.capture());
    Notification notification = notificationArgumentCaptor.getValue();
    assertThat(notification.getType()).isEqualTo("alerts");
    assertThat(notification.getFieldValue("projectKey")).isEqualTo(PROJECT_COMPONENT.getKey());
    assertThat(notification.getFieldValue("projectName")).isEqualTo(PROJECT_COMPONENT.getName());
    assertThat(notification.getFieldValue("projectVersion")).isEqualTo(PROJECT_COMPONENT.getProjectAttributes().getProjectVersion());
    assertThat(notification.getFieldValue("branch")).isNull();
    assertThat(notification.getFieldValue("alertLevel")).isEqualTo(newQualityGateStatus.getStatus().name());
    assertThat(notification.getFieldValue("alertName")).isEqualTo(expectedLabel);
    reset(measureRepository, eventRepository, notificationService);
}
Also used : QualityGateStatus(org.sonar.ce.task.projectanalysis.measure.QualityGateStatus) Event(org.sonar.ce.task.projectanalysis.event.Event) TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) Notification(org.sonar.api.notifications.Notification) QGChangeNotification(org.sonar.server.qualitygate.notification.QGChangeNotification)

Example 30 with Notification

use of org.sonar.api.notifications.Notification in project sonarqube by SonarSource.

the class QualityGateEventsStepTest method verify_event_created_if_no_base_ALERT_STATUS_measure.

private void verify_event_created_if_no_base_ALERT_STATUS_measure(Measure.Level rawAlterStatus, String expectedLabel) {
    QualityGateStatus someQGStatus = new QualityGateStatus(rawAlterStatus, ALERT_TEXT);
    when(measureRepository.getRawMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().setQualityGateStatus(someQGStatus).createNoValue()));
    when(measureRepository.getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric)).thenReturn(of(Measure.newMeasureBuilder().createNoValue()));
    underTest.execute(new TestComputationStepContext());
    verify(measureRepository).getRawMeasure(PROJECT_COMPONENT, alertStatusMetric);
    verify(measureRepository).getBaseMeasure(PROJECT_COMPONENT, alertStatusMetric);
    verify(eventRepository).add(eq(PROJECT_COMPONENT), eventArgumentCaptor.capture());
    verifyNoMoreInteractions(measureRepository, eventRepository);
    Event event = eventArgumentCaptor.getValue();
    assertThat(event.getCategory()).isEqualTo(Event.Category.ALERT);
    assertThat(event.getName()).isEqualTo(expectedLabel);
    assertThat(event.getDescription()).isEqualTo(ALERT_TEXT);
    assertThat(event.getData()).isNull();
    ArgumentCaptor<Collection> collectionCaptor = ArgumentCaptor.forClass(Collection.class);
    verify(notificationService).deliverEmails(collectionCaptor.capture());
    verify(notificationService).deliver(notificationArgumentCaptor.capture());
    Notification notification = notificationArgumentCaptor.getValue();
    assertThat(collectionCaptor.getValue()).hasSize(1);
    assertThat(collectionCaptor.getValue().iterator().next()).isSameAs(notification);
    assertThat(notification).isInstanceOf(QGChangeNotification.class);
    assertThat(notification.getType()).isEqualTo("alerts");
    assertThat(notification.getFieldValue("projectKey")).isEqualTo(PROJECT_COMPONENT.getKey());
    assertThat(notification.getFieldValue("projectName")).isEqualTo(PROJECT_COMPONENT.getName());
    assertThat(notification.getFieldValue("projectVersion")).isEqualTo(PROJECT_COMPONENT.getProjectAttributes().getProjectVersion());
    assertThat(notification.getFieldValue("branch")).isNull();
    assertThat(notification.getFieldValue("alertLevel")).isEqualTo(rawAlterStatus.name());
    assertThat(notification.getFieldValue("alertName")).isEqualTo(expectedLabel);
}
Also used : QualityGateStatus(org.sonar.ce.task.projectanalysis.measure.QualityGateStatus) Event(org.sonar.ce.task.projectanalysis.event.Event) Collection(java.util.Collection) TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) Notification(org.sonar.api.notifications.Notification) QGChangeNotification(org.sonar.server.qualitygate.notification.QGChangeNotification)

Aggregations

Notification (org.sonar.api.notifications.Notification)90 Test (org.junit.Test)83 EmailMessage (org.sonar.plugins.emailnotifications.api.EmailMessage)14 EmailMessage (org.sonar.server.issue.notification.EmailMessage)13 NotificationChannel (org.sonar.api.notifications.NotificationChannel)12 List (java.util.List)7 IntStream (java.util.stream.IntStream)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 Mockito.mock (org.mockito.Mockito.mock)5 Mockito.when (org.mockito.Mockito.when)5 ReportAnalysisFailureNotification (org.sonar.ce.task.projectanalysis.notification.ReportAnalysisFailureNotification)5 Tuple (org.assertj.core.groups.Tuple)4 Languages (org.sonar.api.resources.Languages)4 ActiveRuleChange (org.sonar.server.qualityprofile.ActiveRuleChange)4 Collections (java.util.Collections)3 Random (java.util.Random)3 Before (org.junit.Before)3 Mockito.verify (org.mockito.Mockito.verify)3 Mockito.verifyNoMoreInteractions (org.mockito.Mockito.verifyNoMoreInteractions)3 Mockito.verifyZeroInteractions (org.mockito.Mockito.verifyZeroInteractions)3