1
+ package edu .harvard .iq .dataverse .util ;
2
+
3
+ import com .jayway .restassured .RestAssured ;
4
+ import com .jayway .restassured .parsing .Parser ;
5
+ import org .junit .jupiter .api .BeforeEach ;
6
+ import org .junit .jupiter .api .Tag ;
7
+ import org .junit .jupiter .api .Test ;
8
+ import org .testcontainers .containers .GenericContainer ;
9
+ import org .testcontainers .containers .wait .strategy .Wait ;
10
+ import org .testcontainers .junit .jupiter .Container ;
11
+ import org .testcontainers .junit .jupiter .Testcontainers ;
12
+
13
+ import javax .mail .Message ;
14
+ import javax .mail .Session ;
15
+ import javax .mail .Transport ;
16
+ import javax .mail .internet .InternetAddress ;
17
+ import javax .mail .internet .MimeMessage ;
18
+
19
+ import java .io .PrintWriter ;
20
+ import java .util .Date ;
21
+
22
+ import static com .jayway .restassured .RestAssured .given ;
23
+ import static org .hamcrest .CoreMatchers .equalTo ;
24
+
25
+ @ Tag ("testcontainers" )
26
+ @ Testcontainers
27
+ class MailSessionProducerIT {
28
+
29
+ private static final Integer PORT_SMTP = 1025 ;
30
+ private static final Integer PORT_HTTP = 8025 ;
31
+
32
+ Integer smtpPort ;
33
+ String smtpHost ;
34
+
35
+ MailSessionProducer mailSessionProducer = new MailSessionProducer ();
36
+
37
+ @ Container
38
+ public static GenericContainer <?> mailhog = new GenericContainer <>("mailhog/mailhog" )
39
+ .withExposedPorts (PORT_HTTP , PORT_SMTP )
40
+ .waitingFor (Wait .forHttp ("/" ));
41
+
42
+ @ BeforeEach
43
+ public void setUp () {
44
+ smtpPort = mailhog .getMappedPort (PORT_SMTP );
45
+ smtpHost = mailhog .getContainerIpAddress ();
46
+ Integer httpPort = mailhog .getMappedPort (PORT_HTTP );
47
+
48
+ RestAssured .baseURI = "http://" + smtpHost ;
49
+ RestAssured .port = httpPort ;
50
+ RestAssured .basePath = "/api/v2" ;
51
+ RestAssured .registerParser ("text/json" , Parser .JSON );
52
+
53
+ System .setProperty ("dataverse.mail.system.host" , smtpHost );
54
+ System .setProperty ("dataverse.mail.system.port" , smtpPort .toString ());
55
+ }
56
+
57
+ @ Test
58
+ void testMailSession () {
59
+ // given
60
+ Session mailSession = mailSessionProducer .getSystemMailSession ();
61
+
62
+ // when
63
+ sendEmail (
mailSession ,
"[email protected] " ,
"foobar" ,
"foobar" );
64
+
65
+ // then
66
+ given ().get ("/messages" ).then ()
67
+ .statusCode (200 )
68
+ .body ("total" , equalTo (1 ));
69
+ }
70
+
71
+ public static void sendEmail (Session session , String toEmail , String subject , String body ){
72
+ try
73
+ {
74
+ MimeMessage msg = new MimeMessage (session );
75
+ //set message headers
76
+ msg .addHeader ("Content-type" , "text/HTML; charset=UTF-8" );
77
+ msg .addHeader ("format" , "flowed" );
78
+ msg .addHeader ("Content-Transfer-Encoding" , "8bit" );
79
+ msg .
setReplyTo (
InternetAddress .
parse (
"[email protected] " ,
false ));
80
+ msg .setSubject (subject , "UTF-8" );
81
+ msg .setText (body , "UTF-8" );
82
+ msg .setSentDate (new Date ());
83
+ msg .setRecipients (Message .RecipientType .TO , InternetAddress .parse (toEmail , false ));
84
+
85
+ Transport .send (msg );
86
+ }
87
+ catch (Exception e ) {
88
+ e .printStackTrace ();
89
+ }
90
+ }
91
+
92
+ }
0 commit comments