Skip to content

Commit 09ac8ca

Browse files
committed
Add tests
1 parent 99a72c1 commit 09ac8ca

File tree

6 files changed

+253
-1
lines changed

6 files changed

+253
-1
lines changed

.github/workflows/check-build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
${{ runner.os }}-mvn-build-
4949
5050
- name: Build with Maven
51-
run: ./mvnw -B clean package
51+
run: ./mvnw -B clean package -P run-integration-tests
5252

5353
- name: Check for uncommited changes
5454
run: |
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package software.xdev;
2+
3+
public final class Application
4+
{
5+
@SuppressWarnings("PMD.SystemPrintln")
6+
public static void main(final String[] args)
7+
{
8+
System.out.println("Please have a look at the tests");
9+
}
10+
11+
private Application()
12+
{
13+
}
14+
}

mailpit-java-client/pom.xml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4949
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
5050

51+
<slf4j-version>2.0.17</slf4j-version>
52+
<log4j-version>2.25.2</log4j-version>
53+
54+
<skipTests>true</skipTests>
55+
5156
<generatedDirRelative>src/generated/java</generatedDirRelative>
5257

5358
<!-- Ignore generated code -->
@@ -104,6 +109,57 @@
104109
<artifactId>jakarta.annotation-api</artifactId>
105110
<version>3.0.0</version>
106111
</dependency>
112+
113+
<!-- Tests -->
114+
<!-- Logging -->
115+
<dependency>
116+
<groupId>org.slf4j</groupId>
117+
<artifactId>slf4j-api</artifactId>
118+
<version>${slf4j-version}</version>
119+
<scope>test</scope>
120+
</dependency>
121+
<dependency>
122+
<groupId>org.slf4j</groupId>
123+
<artifactId>jul-to-slf4j</artifactId>
124+
<version>${slf4j-version}</version>
125+
<scope>test</scope>
126+
</dependency>
127+
<dependency>
128+
<groupId>org.apache.logging.log4j</groupId>
129+
<artifactId>log4j-core</artifactId>
130+
<version>${log4j-version}</version>
131+
<scope>test</scope>
132+
</dependency>
133+
<dependency>
134+
<groupId>org.apache.logging.log4j</groupId>
135+
<artifactId>log4j-slf4j2-impl</artifactId>
136+
<version>${log4j-version}</version>
137+
<scope>test</scope>
138+
</dependency>
139+
140+
<!-- JUnit -->
141+
<dependency>
142+
<groupId>org.junit.jupiter</groupId>
143+
<artifactId>junit-jupiter</artifactId>
144+
<version>6.0.1</version>
145+
<scope>test</scope>
146+
</dependency>
147+
148+
<!-- Testcontainers -->
149+
<dependency>
150+
<groupId>org.testcontainers</groupId>
151+
<artifactId>testcontainers</artifactId>
152+
<version>2.0.1</version>
153+
<scope>test</scope>
154+
</dependency>
155+
156+
<!-- Sending mails -->
157+
<dependency>
158+
<groupId>org.simplejavamail</groupId>
159+
<artifactId>simple-java-mail</artifactId>
160+
<version>8.12.6</version>
161+
<scope>test</scope>
162+
</dependency>
107163
</dependencies>
108164

109165
<build>
@@ -213,9 +269,24 @@
213269
</execution>
214270
</executions>
215271
</plugin>
272+
273+
<plugin>
274+
<groupId>org.apache.maven.plugins</groupId>
275+
<artifactId>maven-surefire-plugin</artifactId>
276+
<version>3.5.4</version>
277+
<configuration>
278+
<skipTests>${skipTests}</skipTests>
279+
</configuration>
280+
</plugin>
216281
</plugins>
217282
</build>
218283
<profiles>
284+
<profile>
285+
<id>run-integration-tests</id>
286+
<properties>
287+
<skipTests>false</skipTests>
288+
</properties>
289+
</profile>
219290
<profile>
220291
<id>publish</id>
221292
<build>
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package software.xdev.mailpit;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.time.Duration;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
import org.apache.hc.client5.http.config.ConnectionConfig;
10+
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
11+
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
12+
import org.apache.hc.core5.util.Timeout;
13+
import org.junit.jupiter.api.Assertions;
14+
import org.junit.jupiter.api.Test;
15+
import org.simplejavamail.api.mailer.Mailer;
16+
import org.simplejavamail.api.mailer.config.TransportStrategy;
17+
import org.simplejavamail.email.EmailBuilder;
18+
import org.simplejavamail.mailer.MailerBuilder;
19+
20+
import software.xdev.mailpit.api.MessagesApi;
21+
import software.xdev.mailpit.client.ApiClient;
22+
import software.xdev.mailpit.container.MailpitContainer;
23+
import software.xdev.mailpit.model.Address;
24+
import software.xdev.mailpit.model.MessageSummary;
25+
26+
27+
class SimpleTest
28+
{
29+
@Test
30+
void check()
31+
{
32+
final String user = "[email protected]";
33+
final String pw = "test";
34+
try(final MailpitContainer mailpitContainer = new MailpitContainer()
35+
.allowInsecure()
36+
.withSmtpAuth(Map.of(user, pw)))
37+
{
38+
mailpitContainer.start();
39+
40+
final String to = "[email protected]";
41+
final String subject = "Test Subject";
42+
final String plainText = "Test Plain Text";
43+
44+
try(final Mailer mailer = MailerBuilder.withSMTPServer(
45+
mailpitContainer.getHost(),
46+
mailpitContainer.getMappedPort(MailpitContainer.SMTP_PORT),
47+
user,
48+
pw)
49+
.withTransportStrategy(TransportStrategy.SMTP)
50+
.buildMailer())
51+
{
52+
mailer.sendMail(
53+
EmailBuilder.startingBlank()
54+
.from(user)
55+
.to(to)
56+
.withSubject(subject)
57+
.withPlainText(plainText)
58+
.buildEmail());
59+
}
60+
catch(final Exception ex)
61+
{
62+
Assertions.fail(ex);
63+
}
64+
65+
final ApiClient apiClient = createApiClient(mailpitContainer);
66+
final List<MessageSummary> messages = new MessagesApi(apiClient)
67+
.getMessagesParams(null, null)
68+
.getMessages();
69+
assertEquals(1, messages.size());
70+
71+
final MessageSummary message = messages.stream().findFirst().orElseThrow();
72+
73+
Assertions.assertAll(
74+
() -> assertEquals(user, message.getFrom().getAddress()),
75+
() -> assertEquals(to, message.getTo().stream().findFirst().map(Address::getAddress).orElse(null)),
76+
() -> assertEquals(subject, message.getSubject()),
77+
() -> assertEquals(plainText, message.getSnippet())
78+
);
79+
}
80+
}
81+
82+
private static ApiClient createApiClient(final MailpitContainer container)
83+
{
84+
final Duration defaultTimeout = Duration.ofSeconds(30);
85+
86+
final ApiClient client = new ApiClient();
87+
client.setHttpClient(HttpClientBuilder.create()
88+
.setConnectionManager(PoolingHttpClientConnectionManagerBuilder.create()
89+
.setDefaultConnectionConfig(ConnectionConfig.custom()
90+
.setConnectTimeout(Timeout.of(defaultTimeout))
91+
.setSocketTimeout(Timeout.of(defaultTimeout))
92+
.build())
93+
.build())
94+
.build());
95+
client.setBasePath("http://" + container.getHost() + ":" + container.getMappedPort(MailpitContainer.WEB_PORT));
96+
return client;
97+
}
98+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package software.xdev.mailpit.container;
2+
3+
import java.util.Map;
4+
import java.util.stream.Collectors;
5+
6+
import org.testcontainers.containers.GenericContainer;
7+
import org.testcontainers.utility.DockerImageName;
8+
9+
10+
public class MailpitContainer extends GenericContainer<MailpitContainer>
11+
{
12+
public static final DockerImageName DEFAULT_IMAGE = DockerImageName.parse("axllent/mailpit:v1.27");
13+
14+
public static final int WEB_PORT = 8025;
15+
public static final int SMTP_PORT = 1025;
16+
17+
public MailpitContainer()
18+
{
19+
super(DEFAULT_IMAGE);
20+
21+
// Version check is not needed in tests
22+
this.addEnv("MP_DISABLE_VERSION_CHECK", "true");
23+
// Resolving client reverse dns is not needed
24+
this.addEnv("MP_SMTP_DISABLE_RDNS", "true");
25+
26+
// Enforce CSP (only important when debugging)
27+
this.addEnv("MP_BLOCK_REMOTE_CSS_AND_FONTS", "true");
28+
29+
this.addExposedPort(WEB_PORT);
30+
this.addExposedPort(SMTP_PORT);
31+
}
32+
33+
public MailpitContainer allowInsecure()
34+
{
35+
this.addEnv("MP_SMTP_AUTH_ALLOW_INSECURE", "true");
36+
return this;
37+
}
38+
39+
public MailpitContainer withSmtpAuth(final Map<String, String> usernamePasswords)
40+
{
41+
this.addEnv(
42+
"MP_SMTP_AUTH",
43+
usernamePasswords.entrySet()
44+
.stream()
45+
.map(e -> e.getKey() + ":" + e.getValue())
46+
.collect(Collectors.joining(" ")));
47+
return this;
48+
}
49+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE Configuration>
3+
<Configuration status="ERROR">
4+
5+
<Properties>
6+
<Property name="consolePattern">%d{HH:mm:ss} %-5p [%t] [%-25.25c] %m %n</Property>
7+
</Properties>
8+
9+
<Appenders>
10+
<Console name="stdout" target="SYSTEM_OUT">
11+
<PatternLayout pattern="${consolePattern}" />
12+
</Console>
13+
</Appenders>
14+
15+
<Loggers>
16+
<Root level="INFO">
17+
<AppenderRef ref="stdout"/>
18+
</Root>
19+
</Loggers>
20+
</Configuration>

0 commit comments

Comments
 (0)