|
| 1 | +package it.aboutbits.springboot.emailservice; |
| 2 | + |
| 3 | +import it.aboutbits.springboot.emailservice.lib.AttachmentDataSource; |
| 4 | +import it.aboutbits.springboot.emailservice.lib.EmailState; |
| 5 | +import it.aboutbits.springboot.emailservice.lib.application.EmailParameter; |
| 6 | +import it.aboutbits.springboot.emailservice.lib.application.ManageEmail; |
| 7 | +import it.aboutbits.springboot.emailservice.lib.exception.AttachmentException; |
| 8 | +import it.aboutbits.springboot.emailservice.lib.exception.EmailException; |
| 9 | +import it.aboutbits.springboot.emailservice.support.database.WithPostgres; |
| 10 | +import org.jspecify.annotations.NullMarked; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
| 13 | +import org.springframework.boot.test.context.SpringBootTest; |
| 14 | +import org.springframework.context.ApplicationContext; |
| 15 | + |
| 16 | +import java.io.ByteArrayInputStream; |
| 17 | +import java.time.OffsetDateTime; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 21 | + |
| 22 | +@SpringBootTest |
| 23 | +@WithPostgres |
| 24 | +@NullMarked |
| 25 | +class EmailServiceConfigurationTest { |
| 26 | + @Autowired |
| 27 | + ApplicationContext applicationContext; |
| 28 | + |
| 29 | + @Autowired |
| 30 | + ManageEmail manageEmail; |
| 31 | + |
| 32 | + @Test |
| 33 | + void givenNoAttachmentDataSourceBean_context_shouldStart() { |
| 34 | + assertThat(applicationContext.getBeanNamesForType(AttachmentDataSource.class)).isEmpty(); |
| 35 | + assertThat(manageEmail).isNotNull(); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void givenNoAttachmentDataSourceBean_scheduleWithoutAttachment_shouldSucceed() throws EmailException { |
| 40 | + var result = manageEmail.schedule(getValidParameterWithoutAttachment()); |
| 41 | + |
| 42 | + assertThat(result.id()).isPositive(); |
| 43 | + assertThat(result.state()).isEqualTo(EmailState.PENDING); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void givenNoAttachmentDataSourceBean_scheduleWithAttachment_shouldFail() { |
| 48 | + var parameter = getValidParameterWithAttachment(); |
| 49 | + |
| 50 | + assertThatExceptionOfType(EmailException.class).isThrownBy( |
| 51 | + () -> manageEmail.schedule(parameter) |
| 52 | + ).withCauseInstanceOf(AttachmentException.class); |
| 53 | + } |
| 54 | + |
| 55 | + private static EmailParameter getValidParameterWithoutAttachment() { |
| 56 | + return EmailParameter.builder() |
| 57 | + .scheduledAt(OffsetDateTime.now()) |
| 58 | + .email(EmailParameter.Email.builder() |
| 59 | + .subject("Example email subject") |
| 60 | + .textBody("Email body") |
| 61 | + .htmlBody("<h1>Html email body</h1>") |
| 62 | + .recipient("person1@example.com") |
| 63 | + .fromAddress("somebody@aboutbits.it") |
| 64 | + .fromName("somebody") |
| 65 | + .build() |
| 66 | + ).build(); |
| 67 | + } |
| 68 | + |
| 69 | + private static EmailParameter getValidParameterWithAttachment() { |
| 70 | + return EmailParameter.builder() |
| 71 | + .scheduledAt(OffsetDateTime.now()) |
| 72 | + .email(EmailParameter.Email.builder() |
| 73 | + .subject("Example email subject") |
| 74 | + .textBody("Email body") |
| 75 | + .htmlBody("<h1>Html email body</h1>") |
| 76 | + .recipient("person1@example.com") |
| 77 | + .attachment( |
| 78 | + EmailParameter.Email.Attachment.builder() |
| 79 | + .contentType("image/png") |
| 80 | + .fileName("x.png") |
| 81 | + .payload(new ByteArrayInputStream(new byte[0])) |
| 82 | + .build() |
| 83 | + ) |
| 84 | + .fromAddress("somebody@aboutbits.it") |
| 85 | + .fromName("somebody") |
| 86 | + .build() |
| 87 | + ).build(); |
| 88 | + } |
| 89 | +} |
0 commit comments