1. Project Clover database Wed Jan 17 2024 23:40:18 CST
  2. Package org.europa.together.application

File JakartaMailClientTest.java

 

Code metrics

0
107
19
1
273
216
20
0.19
5.63
19
1.05

Classes

Class Line # Actions
JakartaMailClientTest 37 107 0% 20 1
0.9920634699.2%
 

Contributing tests

This file is covered by 15 tests. .

Source view

1    package org.europa.together.application;
2   
3    import static com.google.code.beanmatchers.BeanMatchers.*;
4    import com.icegreen.greenmail.util.DummySSLSocketFactory;
5    import com.icegreen.greenmail.util.GreenMail;
6    import com.icegreen.greenmail.util.ServerSetupTest;
7    import java.security.Security;
8    import java.util.Map;
9    import jakarta.mail.Session;
10    import jakarta.mail.internet.InternetAddress;
11    import jakarta.mail.internet.MimeMessage;
12    import org.europa.together.JUnit5Preperator;
13    import org.europa.together.business.DatabaseActions;
14    import org.europa.together.business.Logger;
15    import org.europa.together.business.MailClient;
16    import org.europa.together.domain.ConfigurationDO;
17    import org.europa.together.domain.LogLevel;
18    import org.europa.together.domain.Mail;
19    import org.europa.together.utils.Constraints;
20    import static org.hamcrest.MatcherAssert.assertThat;
21    import static org.junit.jupiter.api.Assertions.*;
22    import org.junit.jupiter.api.AfterAll;
23    import org.junit.jupiter.api.AfterEach;
24    import org.junit.jupiter.api.Assumptions;
25    import org.junit.jupiter.api.BeforeAll;
26    import org.junit.jupiter.api.BeforeEach;
27    import org.junit.jupiter.api.Test;
28    import org.junit.jupiter.api.extension.ExtendWith;
29    import org.springframework.beans.factory.annotation.Autowired;
30    import org.springframework.test.context.ContextConfiguration;
31    import org.springframework.test.context.junit.jupiter.SpringExtension;
32   
33    @SuppressWarnings("unchecked")
34    @ExtendWith({JUnit5Preperator.class})
35    @ExtendWith(SpringExtension.class)
36    @ContextConfiguration(locations = {"/applicationContext.xml"})
 
37    public class JakartaMailClientTest {
38   
39    private static final Logger LOGGER = new LogbackLogger(JakartaMailClientTest.class);
40   
41    private static final String DIRECTORY
42    = Constraints.SYSTEM_APP_DIR + "/target/test-classes/";
43    private static final String SQL_FILE
44    = "org/europa/together/sql/email-config-test.sql";
45    private static final String FLUSH_TABLE
46    = "TRUNCATE TABLE " + ConfigurationDO.TABLE_NAME + ";";
47   
48    @Autowired
49    private MailClient mailClient;
50   
51    private static GreenMail SMTP_SERVER;
52    private static DatabaseActions jdbcActions = new JdbcActions();
53   
54    //<editor-fold defaultstate="collapsed" desc="Test Preparation">
 
55  1 toggle @BeforeAll
56    static void setUp() {
57    //DBMS
58  1 Assumptions.assumeTrue(jdbcActions.connect("test"), "JDBC DBMS Connection failed.");
59    //SMTP Test Server
60  1 Security.setProperty("ssl.SocketFactory.provider", DummySSLSocketFactory.class.getName());
61  1 SMTP_SERVER = new GreenMail(ServerSetupTest.SMTPS);
62  1 SMTP_SERVER.start();
63  1 SMTP_SERVER.setUser("john.doe@localhost", "JohnDoe", "s3cr3t");
64  1 Assumptions.assumeTrue(SMTP_SERVER.getSmtps().isRunning(), "GreenMail embedded SMTP Server fail to start.");
65   
66  1 LOGGER.log("Assumptions passed ...\n\n", LogLevel.DEBUG);
67    }
68   
 
69  1 toggle @AfterAll
70    static void tearDown() {
71  1 try {
72  1 SMTP_SERVER.stop();
73  1 jdbcActions.executeQuery(FLUSH_TABLE);
74   
75    } catch (Exception ex) {
76  0 LOGGER.catchException(ex);
77    }
78    }
79   
 
80  15 toggle @BeforeEach
81    void testCaseInitialization() {
82    }
83   
 
84  15 toggle @AfterEach
85    void testCaseTermination() {
86    }
87    //</editor-fold>
88   
 
89  1 toggle @Test
90    void constructor() {
91  1 LOGGER.log("TEST CASE: constructor", LogLevel.DEBUG);
92   
93  1 assertThat(JakartaMailClient.class, hasValidBeanConstructor());
94    }
95   
 
96  1 toggle @Test
97    void initialConfiguration() {
98  1 LOGGER.log("TEST CASE: initialConfiguration", LogLevel.DEBUG);
99   
100  1 MailClient client = new JakartaMailClient();
101  1 assertEquals(-1, client.getBulkMailLimiter());
102  1 assertEquals(-1, client.getWaitTime());
103  1 assertEquals(10, client.getDebugActiveConfiguration().size());
104  1 assertEquals("mailer.host", client.getDebugActiveConfiguration().get("mailer.host"));
105    }
106   
 
107  1 toggle @Test
108    void cleanConfiguration() {
109  1 LOGGER.log("TEST CASE: cleanConfiguration", LogLevel.DEBUG);
110   
111  1 MailClient client = new JakartaMailClient();
112  1 client.clearConfiguration();
113  1 assertEquals(0, client.getDebugActiveConfiguration().size());
114    }
115   
 
116  1 toggle @Test
117    void loadConfigurationFromClasspath() throws Exception {
118  1 LOGGER.log("TEST CASE: loadConfigurationFromClasspath", LogLevel.DEBUG);
119   
120  1 mailClient.loadConfigurationFromProperties("org/europa/together/properties/mail-test-classpath.properties");
121  1 assertEquals("localhost", mailClient.getDebugActiveConfiguration().get("mailer.host"));
122    }
123   
 
124  1 toggle @Test
125    void loadConfigurationFromFileSystem() throws Exception {
126  1 LOGGER.log("TEST CASE: loadConfigurationFromFileSystem", LogLevel.DEBUG);
127   
128  1 mailClient.loadConfigurationFromProperties(DIRECTORY
129    + "/org/europa/together/properties/mail-test-filesystem.properties");
130  1 assertEquals("www.sample.com", mailClient.getDebugActiveConfiguration().get("mailer.host"));
131    }
132   
 
133  1 toggle @Test
134    void loadConfigurationFromDatabase() {
135  1 LOGGER.log("TEST CASE: loadConfigurationFromDatabase", LogLevel.DEBUG);
136   
137  1 jdbcActions.executeSqlFromClasspath(SQL_FILE);
138  1 assertTrue(mailClient.loadConfigurationFromDatabase());
139   
140  1 assertEquals("smtp.sample.org", mailClient.getDebugActiveConfiguration().get("mailer.host"));
141  1 assertEquals("465", mailClient.getDebugActiveConfiguration().get("mailer.port"));
142  1 assertEquals("send.from@mail.me", mailClient.getDebugActiveConfiguration().get("mailer.sender"));
143  1 assertEquals("JohnDoe", mailClient.getDebugActiveConfiguration().get("mailer.user"));
144  1 assertEquals("s3cr3t", mailClient.getDebugActiveConfiguration().get("mailer.password"));
145  1 assertEquals("true", mailClient.getDebugActiveConfiguration().get("mailer.ssl"));
146  1 assertEquals("true", mailClient.getDebugActiveConfiguration().get("mailer.tls"));
147  1 assertEquals("false", mailClient.getDebugActiveConfiguration().get("mailer.debug"));
148  1 assertEquals("1", mailClient.getDebugActiveConfiguration().get("mailer.count"));
149  1 assertEquals("0", mailClient.getDebugActiveConfiguration().get("mailer.wait"));
150    }
151   
 
152  1 toggle @Test
153    void tryLoadConfigEntryNotExist() {
154  1 LOGGER.log("TEST CASE: tryLoadConfigEntryNotExist", LogLevel.DEBUG);
155   
156  1 jdbcActions.executeSqlFromClasspath(SQL_FILE);
157  1 assertTrue(mailClient.loadConfigurationFromDatabase());
158   
159  1 assertNull(mailClient.getDebugActiveConfiguration().get("entry.do.not.exist"));
160    }
161   
 
162  1 toggle @Test
163    void failLoadConfigurationFromDatabase() throws Exception {
164  1 LOGGER.log("TEST CASE: failLoadConfigurationFromDatabase", LogLevel.DEBUG);
165   
166  1 jdbcActions.executeQuery(FLUSH_TABLE);
167  1 assertFalse(mailClient.loadConfigurationFromDatabase());
168    }
169   
 
170  1 toggle @Test
171    void failLoadConfigurationFromPropertyFile() throws Exception {
172  1 LOGGER.log("TEST CASE: failLoadConfigurationFromPropertyFile", LogLevel.DEBUG);
173   
174  1 assertThrows(Exception.class, () -> {
175  1 mailClient.loadConfigurationFromProperties(DIRECTORY + "/not-exist.properties");
176    });
177    }
178   
 
179  1 toggle @Test
180    void connectSMTPServer() throws Exception {
181  1 LOGGER.log("TEST CASE: connectSMTPServer", LogLevel.DEBUG);
182   
183  1 mailClient.loadConfigurationFromProperties("org/europa/together/properties/mail-test.properties");
184  1 Map<String, String> conf = mailClient.getDebugActiveConfiguration();
185  1 assertEquals("3465", conf.get("mailer.port"));
186   
187  1 Session session = mailClient.getSession();
188  1 assertNotNull(session);
189  1 assertEquals("127.0.0.1", session.getProperty("mail.smtp.host"));
190    }
191   
 
192  1 toggle @Test
193    void failConnetSMTPServer() throws Exception {
194  1 LOGGER.log("TEST CASE: failConnectSMTPServer", LogLevel.DEBUG);
195   
196  1 mailClient.clearConfiguration();
197  1 assertThrows(Exception.class, () -> {
198  1 mailClient.getSession();
199    });
200    }
201   
 
202  1 toggle @Test
203    void composeHtmlMail() throws Exception {
204  1 LOGGER.log("TEST CASE: composeHtmlMail", LogLevel.DEBUG);
205   
206  1 Mail mail = new Mail();
207  1 mail.setMimeTypeToHTML();
208  1 mail.setSubject("TESTMAIL");
209  1 mail.setMessage("<h1>TEST</h1><br/><p>This ist the testmail content.</p>");
210  1 mail.addRecipent("otto@sample.org");
211   
212  1 mailClient.loadConfigurationFromProperties("org/europa/together/properties/mail-test.properties");
213  1 mailClient.composeMail(mail);
214  1 MimeMessage message = mailClient.getMimeMessage();
215   
216  1 assertEquals("text/plain", message.getContentType());
217  1 assertEquals("TESTMAIL", message.getSubject());
218  1 assertEquals("noreply@sample.org", message.getFrom()[0].toString());
219  1 assertEquals(new InternetAddress("noreply@sample.org"), message.getSender());
220    }
221   
 
222  1 toggle @Test
223    void composeTextMail() throws Exception {
224  1 LOGGER.log("TEST CASE: composeTextMail", LogLevel.DEBUG);
225   
226  1 Mail mail = new Mail();
227  1 mail.setSubject("TESTMAIL");
228  1 mail.setMessage("This ist the testmail content.");
229  1 mail.addRecipent("otto@sample.org");
230   
231  1 mailClient.loadConfigurationFromProperties("org/europa/together/properties/mail-test.properties");
232  1 mailClient.composeMail(mail);
233  1 MimeMessage message = mailClient.getMimeMessage();
234   
235  1 assertEquals("text/plain", message.getContentType());
236  1 assertEquals("TESTMAIL", message.getSubject());
237  1 assertEquals("noreply@sample.org", message.getFrom()[0].toString());
238  1 assertEquals(new InternetAddress("noreply@sample.org"), message.getSender());
239    }
240   
 
241  1 toggle @Test
242    void composeTextMailWithAttachment() throws Exception {
243  1 LOGGER.log("TEST CASE: composeTextMailWithAttachment", LogLevel.DEBUG);
244   
245  1 Mail mail = new Mail();
246  1 mail.setSubject("TESTMAIL");
247  1 mail.setMessage("This ist the testmail content.");
248  1 mail.addRecipent("otto@sample.org");
249  1 mail.addAttachment(DIRECTORY + "/Attachment.pdf");
250   
251  1 mailClient.loadConfigurationFromProperties("org/europa/together/properties/mail-test.properties");
252  1 mailClient.composeMail(mail);
253  1 MimeMessage message = mailClient.getMimeMessage();
254   
255  1 assertEquals("text/plain", message.getContentType());
256    }
257   
 
258  1 toggle @Test
259    void testGetMailObject() throws Exception {
260  1 LOGGER.log("TEST CASE: getMailObject", LogLevel.DEBUG);
261   
262  1 Mail mail = new Mail();
263  1 mail.setSubject("TESTMAIL");
264  1 mail.setMessage("This ist the testmail content.");
265  1 mail.addRecipent("otto@sample.org");
266  1 mail.addAttachment(DIRECTORY + "/Attachment.pdf");
267   
268  1 mailClient.loadConfigurationFromProperties("org/europa/together/properties/mail-test.properties");
269  1 mailClient.composeMail(mail);
270   
271  1 assertEquals(mail, mailClient.getMailObject());
272    }
273    }