Search in sources :

Example 6 with SendEmailMessage

use of com.thoughtworks.go.server.messaging.SendEmailMessage 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) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 7 with SendEmailMessage

use of com.thoughtworks.go.server.messaging.SendEmailMessage 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)

Example 8 with SendEmailMessage

use of com.thoughtworks.go.server.messaging.SendEmailMessage in project gocd by gocd.

the class UniqueEmailSenderTest method shouldBeAbleToSend2Emails.

@Test
public void shouldBeAbleToSend2Emails() {
    ClassMockery mockery = new ClassMockery();
    final EmailNotificationTopic topic = mockery.mock(EmailNotificationTopic.class);
    final SendEmailMessage message = new SendEmailMessage("pavan", "hu kai", "someone");
    mockery.checking(new Expectations() {

        {
            exactly(2).of(topic).post(message);
        }
    });
    EmailSender sender = new AsynchronousEmailSender(topic);
    sender.sendEmail(message);
    sender.sendEmail(message);
}
Also used : Expectations(org.jmock.Expectations) SendEmailMessage(com.thoughtworks.go.server.messaging.SendEmailMessage) EmailNotificationTopic(com.thoughtworks.go.server.messaging.EmailNotificationTopic) ClassMockery(com.thoughtworks.go.util.ClassMockery) Test(org.junit.Test)

Aggregations

SendEmailMessage (com.thoughtworks.go.server.messaging.SendEmailMessage)8 Test (org.junit.Test)7 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)3 GoMailSender (com.thoughtworks.go.config.GoMailSender)2 EmailNotificationTopic (com.thoughtworks.go.server.messaging.EmailNotificationTopic)2 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)2 ClassMockery (com.thoughtworks.go.util.ClassMockery)2 SystemEnvironment (com.thoughtworks.go.util.SystemEnvironment)2 Expectations (org.jmock.Expectations)2 DateTime (org.joda.time.DateTime)2 BasicCruiseConfig (com.thoughtworks.go.config.BasicCruiseConfig)1 CruiseConfig (com.thoughtworks.go.config.CruiseConfig)1 ServerConfig (com.thoughtworks.go.config.ServerConfig)1 Database (com.thoughtworks.go.database.Database)1 AgentInstance (com.thoughtworks.go.domain.AgentInstance)1 MaterialRevisions (com.thoughtworks.go.domain.MaterialRevisions)1 Pipeline (com.thoughtworks.go.domain.Pipeline)1 ServerSiteUrlConfig (com.thoughtworks.go.domain.ServerSiteUrlConfig)1 Stage (com.thoughtworks.go.domain.Stage)1 User (com.thoughtworks.go.domain.User)1