1. Project Clover database Mon Dec 22 2025 14:59:38 CST
  2. Package org.europa.together.service

File MailClientServiceTest.java

 

Code metrics

0
41
9
1
141
112
11
0.27
4.56
9
1.22

Classes

Class Line # Actions
MailClientServiceTest 33 41 0% 11 2
0.9696%
 

Contributing tests

This file is covered by 5 tests. .

Source view

1    package org.europa.together.service;
2   
3    import static com.google.code.beanmatchers.BeanMatchers.*;
4    //import com.sun.mail.util.MailConnectException;
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    //@ExtendWith({MockitoExtension.class, SpringExtension.class, JUnit5Preperator.class})
32    @ContextConfiguration(locations = {"/applicationContext.xml"})
 
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    //<editor-fold defaultstate="collapsed" desc="Test Preparation">
 
46  1 toggle @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   
 
53  1 toggle @AfterAll
54    static void tearDown() {
55    }
56   
 
57  5 toggle @BeforeEach
58    void testCaseInitialization() {
59  5 CONNECTION.executeSqlFromClasspath(SQL_FILE);
60    }
61   
 
62  5 toggle @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    //</editor-fold>
72   
 
73  1 toggle @Test
74    void constructor() {
75  1 LOGGER.log("TEST CASE: constructor", LogLevel.DEBUG);
76  1 assertThat(MailClientService.class, hasValidBeanConstructor());
77    }
78   
 
79  1 toggle @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   
 
91  1 toggle @Test
92    void updateConfiguration() {
93  1 LOGGER.log("TEST CASE: updateConfiguration", LogLevel.DEBUG);
94   
95    //Arrage
96  1 Map<String, String> config = new HashMap<>();
97  1 config.put("mailer.port", "9999");
98    //Act
99  1 try {
100  1 mailClientService.updateConfiguration(config);
101    } catch (Exception ex) {
102  0 LOGGER.catchException(ex);
103    }
104    //Assume
105  1 Map<String, String> newConfig
106    = mailClientService.loadConfiguration();
107  1 assertEquals("9999", newConfig.get("mailer.port"));
108    }
109   
 
110  1 toggle @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   
 
124  1 toggle @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    }