| 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({SpringExtension.class, JUnit5Preperator.class}) |
| 35 |
|
@ContextConfiguration(locations = {"/applicationContext.xml"}) |
| |
|
| 99.2% |
Uncovered Elements: 1 (126) |
Complexity: 20 |
Complexity Density: 0.19 |
|
| 36 |
|
public class JakartaMailClientTest { |
| 37 |
|
|
| 38 |
|
private static final Logger LOGGER = new LogbackLogger(JakartaMailClientTest.class); |
| 39 |
|
|
| 40 |
|
private static final String DIRECTORY |
| 41 |
|
= Constraints.SYSTEM_APP_DIR + "/target/test-classes/"; |
| 42 |
|
private static final String SQL_FILE |
| 43 |
|
= "org/europa/together/sql/email-config-test.sql"; |
| 44 |
|
private static final String FLUSH_TABLE |
| 45 |
|
= "TRUNCATE TABLE " + ConfigurationDO.TABLE_NAME + ";"; |
| 46 |
|
|
| 47 |
|
@Autowired |
| 48 |
|
private MailClient mailClient; |
| 49 |
|
|
| 50 |
|
private static GreenMail SMTP_SERVER; |
| 51 |
|
private static DatabaseActions jdbcActions |
| 52 |
|
= JUnit5Preperator.JDBC_CONNECTION; |
| 53 |
|
|
| 54 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
| 55 |
1 |
@BeforeAll... |
| 56 |
|
static void setUp() { |
| 57 |
|
|
| 58 |
1 |
Assumptions.assumeTrue(JUnit5Preperator.isConnected(), "JDBC DBMS Connection failed."); |
| 59 |
|
|
| 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 |
|
|
| |
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 69 |
1 |
@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 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 80 |
15 |
@BeforeEach... |
| 81 |
|
void testCaseInitialization() { |
| 82 |
|
} |
| 83 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 84 |
15 |
@AfterEach... |
| 85 |
|
void testCaseTermination() { |
| 86 |
|
} |
| 87 |
|
|
| 88 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 89 |
1 |
@Test... |
| 90 |
|
void constructor() { |
| 91 |
1 |
LOGGER.log("TEST CASE: constructor", LogLevel.DEBUG); |
| 92 |
|
|
| 93 |
1 |
assertThat(JakartaMailClient.class, hasValidBeanConstructor()); |
| 94 |
|
} |
| 95 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
| 96 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 107 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 116 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 124 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
| 133 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 152 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 162 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 170 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
| 179 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 192 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
| 202 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
| 222 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
| 241 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
| 258 |
1 |
@Test... |
| 259 |
|
void getMailObject() 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.toString(), mailClient.getMailObject().toString()); |
| 272 |
|
} |
| 273 |
|
} |