Skip to content

Commit 40d4946

Browse files
committed
add more docs and add test for config
1 parent 5b9979f commit 40d4946

4 files changed

Lines changed: 98 additions & 5 deletions

File tree

pom.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
43
<modelVersion>4.0.0</modelVersion>
54

65
<parent>
@@ -12,7 +11,7 @@
1211

1312
<groupId>it.aboutbits.springboot</groupId>
1413
<artifactId>emailservice</artifactId>
15-
<version>BUILD-SNAPSHOT</version>
14+
<version>1.2.0</version>
1615
<description>Spring Boot Email Service</description>
1716

1817
<properties>

readme.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ public AttachmentDataSource attachmentDataSource(JdbcTemplate jdbcTemplate) {
3030
}
3131
```
3232

33-
The `JdbcAttachmentDataSource` creates its table itself on construction, so no further setup is needed.
34-
3533
#### Inline (CID) attachments
3634

3735
To embed an attachment inline, set a `contentId` on the attachment and reference it in the `htmlBody` via the `cid:` scheme.

src/main/java/it/aboutbits/springboot/emailservice/lib/AttachmentDataSource.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
public interface AttachmentDataSource {
1010
InputStream getAttachmentPayload(long fileReference) throws AttachmentException;
1111

12+
/**
13+
* Stores the payload and returns the reference used to read it back later.
14+
* The implementation takes ownership of the stream and must close it.
15+
*
16+
* @param payload the attachment payload, closed by the implementation
17+
* @return the reference to pass to {@link #getAttachmentPayload(long)}
18+
*/
1219
long storeAttachmentPayload(InputStream payload) throws AttachmentException;
1320

1421
void releaseAttachment(long fileReference) throws AttachmentException;
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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

Comments
 (0)