| 1 |
|
package org.europa.together.service; |
| 2 |
|
|
| 3 |
|
import static com.google.code.beanmatchers.BeanMatchers.*; |
| 4 |
|
|
| 5 |
|
import org.eclipse.angus.mail.util.MailConnectException; |
| 6 |
|
import java.util.HashMap; |
| 7 |
|
import java.util.Map; |
| 8 |
|
import org.europa.together.JUnit5Preperator; |
| 9 |
|
import org.europa.together.application.LogbackLogger; |
| 10 |
|
import org.europa.together.business.DatabaseActions; |
| 11 |
|
import org.europa.together.business.Logger; |
| 12 |
|
import org.europa.together.domain.LogLevel; |
| 13 |
|
import org.europa.together.domain.Mail; |
| 14 |
|
import org.europa.together.utils.StringUtils; |
| 15 |
|
import static org.hamcrest.MatcherAssert.*; |
| 16 |
|
import static org.junit.jupiter.api.Assertions.*; |
| 17 |
|
import org.junit.jupiter.api.AfterAll; |
| 18 |
|
import org.junit.jupiter.api.AfterEach; |
| 19 |
|
import org.junit.jupiter.api.Assumptions; |
| 20 |
|
import org.junit.jupiter.api.BeforeAll; |
| 21 |
|
import org.junit.jupiter.api.BeforeEach; |
| 22 |
|
import org.junit.jupiter.api.Test; |
| 23 |
|
import org.junit.jupiter.api.extension.ExtendWith; |
| 24 |
|
import org.springframework.beans.factory.annotation.Autowired; |
| 25 |
|
import org.springframework.test.context.ContextConfiguration; |
| 26 |
|
|
| 27 |
|
import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 28 |
|
|
| 29 |
|
@SuppressWarnings("unchecked") |
| 30 |
|
@ExtendWith({SpringExtension.class, JUnit5Preperator.class}) |
| 31 |
|
|
| 32 |
|
@ContextConfiguration(locations = {"/applicationContext.xml"}) |
| |
|
| 96% |
Uncovered Elements: 2 (50) |
Complexity: 11 |
Complexity Density: 0.27 |
|
| 33 |
|
public class MailClientServiceTest { |
| 34 |
|
|
| 35 |
|
private static final Logger LOGGER |
| 36 |
|
= new LogbackLogger(MailClientServiceTest.class); |
| 37 |
|
private static final String SQL_FILE |
| 38 |
|
= "/org/europa/together/sql/email-config-test.sql"; |
| 39 |
|
public static DatabaseActions CONNECTION |
| 40 |
|
= JUnit5Preperator.JDBC_CONNECTION; |
| 41 |
|
|
| 42 |
|
@Autowired |
| 43 |
|
private MailClientService mailClientService; |
| 44 |
|
|
| 45 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 46 |
1 |
@BeforeAll... |
| 47 |
|
static void setUp() { |
| 48 |
1 |
Assumptions.assumeTrue(JUnit5Preperator.isConnected(), "JDBC DBMS Connection failed."); |
| 49 |
|
|
| 50 |
1 |
LOGGER.log("Assumptions passed ...\n\n", LogLevel.DEBUG); |
| 51 |
|
} |
| 52 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 53 |
1 |
@AfterAll... |
| 54 |
|
static void tearDown() { |
| 55 |
|
} |
| 56 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 57 |
5 |
@BeforeEach... |
| 58 |
|
void testCaseInitialization() { |
| 59 |
5 |
CONNECTION.executeSqlFromClasspath(SQL_FILE); |
| 60 |
|
} |
| 61 |
|
|
| |
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 62 |
5 |
@AfterEach... |
| 63 |
|
void testCaseTermination() throws Exception { |
| 64 |
5 |
LOGGER.log("CleanUp Test Suite.", LogLevel.DEBUG); |
| 65 |
5 |
try { |
| 66 |
5 |
CONNECTION.executeQuery("TRUNCATE TABLE APP_CONFIG;"); |
| 67 |
|
} catch (Exception ex) { |
| 68 |
0 |
LOGGER.catchException(ex); |
| 69 |
|
} |
| 70 |
|
} |
| 71 |
|
|
| 72 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 73 |
1 |
@Test... |
| 74 |
|
void constructor() { |
| 75 |
1 |
LOGGER.log("TEST CASE: constructor", LogLevel.DEBUG); |
| 76 |
1 |
assertThat(MailClientService.class, hasValidBeanConstructor()); |
| 77 |
|
} |
| 78 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
| 79 |
1 |
@Test... |
| 80 |
|
void loadInitConfiguration() { |
| 81 |
1 |
LOGGER.log("TEST CASE: loadConfiguration", LogLevel.DEBUG); |
| 82 |
|
|
| 83 |
1 |
Map<String, String> config = mailClientService.loadConfiguration(); |
| 84 |
1 |
LOGGER.log("Mail Config: " + config.toString(), LogLevel.DEBUG); |
| 85 |
|
|
| 86 |
1 |
assertNotNull(config); |
| 87 |
1 |
assertEquals(10, config.size()); |
| 88 |
1 |
assertEquals("smtp.sample.org", config.get("mailer.host")); |
| 89 |
|
} |
| 90 |
|
|
| |
|
| 87.5% |
Uncovered Elements: 1 (8) |
Complexity: 2 |
Complexity Density: 0.25 |
1PASS
|
|
| 91 |
1 |
@Test... |
| 92 |
|
void updateConfiguration() { |
| 93 |
1 |
LOGGER.log("TEST CASE: updateConfiguration", LogLevel.DEBUG); |
| 94 |
|
|
| 95 |
|
|
| 96 |
1 |
Map<String, String> config = new HashMap<>(); |
| 97 |
1 |
config.put("mailer.port", "9999"); |
| 98 |
|
|
| 99 |
1 |
try { |
| 100 |
1 |
mailClientService.updateConfiguration(config); |
| 101 |
|
} catch (Exception ex) { |
| 102 |
0 |
LOGGER.catchException(ex); |
| 103 |
|
} |
| 104 |
|
|
| 105 |
1 |
Map<String, String> newConfig |
| 106 |
|
= mailClientService.loadConfiguration(); |
| 107 |
1 |
assertEquals("9999", newConfig.get("mailer.port")); |
| 108 |
|
} |
| 109 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
| 110 |
1 |
@Test... |
| 111 |
|
void failConnectionSendMail() throws Exception { |
| 112 |
1 |
LOGGER.log("TEST CASE: failConnectionSendMail", LogLevel.DEBUG); |
| 113 |
|
|
| 114 |
1 |
Mail mail = new Mail(); |
| 115 |
1 |
mail.setSubject("JGiven Test E-Mail"); |
| 116 |
1 |
mail.setMessage(StringUtils.generateLoremIpsum(0)); |
| 117 |
1 |
mail.addRecipent("recipient_01@sample.org"); |
| 118 |
|
|
| 119 |
1 |
assertThrows(MailConnectException.class, () -> { |
| 120 |
1 |
mailClientService.sendEmail(mail); |
| 121 |
|
}); |
| 122 |
|
} |
| 123 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
| 124 |
1 |
@Test... |
| 125 |
|
void failConnectionSendBulkMail() throws Exception { |
| 126 |
1 |
LOGGER.log("TEST CASE: failConnectionSendBulkMail", LogLevel.DEBUG); |
| 127 |
|
|
| 128 |
1 |
Mail mail = new Mail(); |
| 129 |
1 |
mail.setSubject("JGiven Test E-Mail"); |
| 130 |
1 |
mail.setMessage(StringUtils.generateLoremIpsum(0)); |
| 131 |
1 |
mail.addRecipent("recipient_01@sample.org"); |
| 132 |
1 |
mail.addRecipent("recipient_02@sample.org"); |
| 133 |
1 |
mail.addRecipent("recipient_03@sample.org"); |
| 134 |
1 |
mail.addRecipent("recipient_04@sample.org"); |
| 135 |
1 |
mail.addRecipent("recipient_05@sample.org"); |
| 136 |
|
|
| 137 |
1 |
assertThrows(MailConnectException.class, () -> { |
| 138 |
1 |
mailClientService.sendBulkMail(mail); |
| 139 |
|
}); |
| 140 |
|
} |
| 141 |
|
} |