Search in sources :

Example 1 with GoMailSender

use of com.thoughtworks.go.config.GoMailSender in project gocd by gocd.

the class EmailNotificationListener method onMessage.

public void onMessage(SendEmailMessage message) {
    CruiseConfig cruiseConfig = goConfigService.currentCruiseConfig();
    if (!cruiseConfig.isMailHostConfigured()) {
        return;
    }
    GoMailSender mailSender = factory.createSender();
    ValidationBean validationBean = mailSender.send(message.getSubject(), message.getBody(), message.getTo());
    if (!validationBean.isValid()) {
        LOGGER.error(validationBean.getError());
    }
}
Also used : GoMailSender(com.thoughtworks.go.config.GoMailSender) ValidationBean(com.thoughtworks.go.domain.materials.ValidationBean) CruiseConfig(com.thoughtworks.go.config.CruiseConfig)

Example 2 with GoMailSender

use of com.thoughtworks.go.config.GoMailSender in project gocd by gocd.

the class BackupServiceIntegrationTest method shouldSendEmailToAdminWhenTheBackupFails.

@Test
public void shouldSendEmailToAdminWhenTheBackupFails() throws Exception {
    GoConfigService configService = mock(GoConfigService.class);
    when(configService.adminEmail()).thenReturn("mail@admin.com");
    GoMailSender goMailSender = mock(GoMailSender.class);
    when(configService.getMailSender()).thenReturn(goMailSender);
    when(configService.isUserAdmin(admin)).thenReturn(true);
    DateTime now = new DateTime();
    TimeProvider timeProvider = mock(TimeProvider.class);
    when(timeProvider.currentDateTime()).thenReturn(now);
    HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
    Database databaseStrategyMock = mock(Database.class);
    doThrow(new RuntimeException("Oh no!")).when(databaseStrategyMock).backup(any(File.class));
    BackupService service = new BackupService(dataSource, artifactsDirHolder, configService, timeProvider, backupInfoRepository, systemEnvironment, serverVersion, configRepository, databaseStrategyMock);
    service.initialize();
    service.startBackup(admin, result);
    String ipAddress = SystemUtil.getFirstLocalNonLoopbackIpAddress();
    String body = String.format("Backup of the Go server at '%s' has failed. The reason is: %s", ipAddress, "Oh no!");
    assertThat(result.isSuccessful(), is(false));
    assertThat(result.message(localizer), is("Failed to perform backup. Reason: Oh no!"));
    verify(goMailSender).send(new SendEmailMessage("Server Backup Failed", body, "mail@admin.com"));
    verifyNoMoreInteractions(goMailSender);
    assertThat(FileUtils.listFiles(backupsDirectory, TrueFileFilter.TRUE, TrueFileFilter.TRUE).isEmpty(), is(true));
}
Also used : GoMailSender(com.thoughtworks.go.config.GoMailSender) HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) SendEmailMessage(com.thoughtworks.go.server.messaging.SendEmailMessage) Database(com.thoughtworks.go.database.Database) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) File(java.io.File) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 3 with GoMailSender

use of com.thoughtworks.go.config.GoMailSender in project gocd by gocd.

the class BackupServiceIntegrationTest method shouldSendEmailToAdminAfterTakingBackup.

@Test
public void shouldSendEmailToAdminAfterTakingBackup() throws InvalidCipherTextException {
    GoConfigService configService = mock(GoConfigService.class);
    GoMailSender goMailSender = mock(GoMailSender.class);
    when(configService.getMailSender()).thenReturn(goMailSender);
    when(configService.adminEmail()).thenReturn("mail@admin.com");
    when(configService.isUserAdmin(admin)).thenReturn(true);
    TimeProvider timeProvider = mock(TimeProvider.class);
    DateTime now = new DateTime();
    when(timeProvider.currentDateTime()).thenReturn(now);
    BackupService service = new BackupService(dataSource, artifactsDirHolder, configService, timeProvider, backupInfoRepository, systemEnvironment, serverVersion, configRepository, databaseStrategy);
    service.initialize();
    service.startBackup(admin, new HttpLocalizedOperationResult());
    String ipAddress = SystemUtil.getFirstLocalNonLoopbackIpAddress();
    String body = String.format("Backup of the Go server at '%s' was successfully completed. The backup is stored at location: %s. This backup was triggered by 'admin'.", ipAddress, backupDir(now).getAbsolutePath());
    verify(goMailSender).send(new SendEmailMessage("Server Backup Completed Successfully", body, "mail@admin.com"));
    verifyNoMoreInteractions(goMailSender);
}
Also used : GoMailSender(com.thoughtworks.go.config.GoMailSender) HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) SendEmailMessage(com.thoughtworks.go.server.messaging.SendEmailMessage) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Aggregations

GoMailSender (com.thoughtworks.go.config.GoMailSender)3 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)2 SendEmailMessage (com.thoughtworks.go.server.messaging.SendEmailMessage)2 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)2 DateTime (org.joda.time.DateTime)2 Test (org.junit.Test)2 CruiseConfig (com.thoughtworks.go.config.CruiseConfig)1 Database (com.thoughtworks.go.database.Database)1 ValidationBean (com.thoughtworks.go.domain.materials.ValidationBean)1 File (java.io.File)1