MailClientService.java

1
package org.europa.together.service;
2
3
import java.util.List;
4
import java.util.Map;
5
import java.util.concurrent.TimeUnit;
6
import jakarta.mail.Address;
7
import jakarta.mail.MessagingException;
8
import jakarta.mail.Transport;
9
import org.apiguardian.api.API;
10
import static org.apiguardian.api.API.Status.STABLE;
11
import org.europa.together.application.LogbackLogger;
12
import org.europa.together.business.ConfigurationDAO;
13
import org.europa.together.business.Logger;
14
import org.europa.together.business.MailClient;
15
import org.europa.together.domain.ConfigurationDO;
16
import org.europa.together.domain.HashAlgorithm;
17
import org.europa.together.domain.LogLevel;
18
import org.europa.together.utils.Constraints;
19
import org.europa.together.business.CryptoTools;
20
import org.europa.together.domain.Mail;
21
import org.springframework.beans.factory.annotation.Autowired;
22
import org.springframework.stereotype.Service;
23
24
/**
25
 * Service implementation for the JakartaMailClient.
26
 *
27
 * @author elmar.dott@gmail.com
28
 * @version 1.0
29
 * @since 1.0
30
 */
31
@API(status = STABLE, since = "1.0")
32
@Service
33
public final class MailClientService {
34
35
    private static final long serialVersionUID = 206L;
36
    private static final Logger LOGGER = new LogbackLogger(MailClientService.class);
37
    private String configurationFile = "";
38
39
    @Autowired
40
    private CryptoTools cryptoTools;
41
    @Autowired
42
    private ConfigurationDAO configurationDAO;
43
    @Autowired
44
    private MailClient mailClient;
45
46
    /**
47
     * Constructor.
48
     */
49
    @API(status = STABLE, since = "1.0")
50
    public MailClientService() {
51
        LOGGER.log("instance class", LogLevel.INFO);
52
    }
53
54
    /**
55
     * Allows to update the database configuration for the MailClient by a map
56
     * of configuration entries.<br>
57
     * - mailer.host<br>
58
     * - mailer.port<br>
59
     * - mailer.sender<br>
60
     * - mailer.user<br>
61
     * - mailer.password<br>
62
     * - mailer.ssl<br>
63
     * - mailer.tls<br>
64
     * - mailer.debug<br>
65
     * - mailer.count<br>
66
     * - mailer.wait
67
     *
68
     * @param configurationList as Map
69
     */
70
    @API(status = STABLE, since = "3.0")
71
    public void updateConfiguration(
72
            final Map<String, String> configurationList) {
73
74 1 1. updateConfiguration : removed call to org/europa/together/business/MailClient::clearConfiguration → SURVIVED
        mailClient.clearConfiguration();
75
        try {
76
            List<ConfigurationDO> configurationEntries
77
                    = configurationDAO.getAllConfigurationSetEntries(
78
                            Constraints.MODULE_NAME,
79
                            MailClient.CONFIG_VERSION,
80
                            MailClient.CONFIG_SET);
81
            for (ConfigurationDO configEntry : configurationEntries) {
82
83
                for (Map.Entry<String, String> entry : configurationList.entrySet()) {
84 1 1. updateConfiguration : negated conditional → KILLED
                    if (configEntry.getKey().equals(
85
                            cryptoTools.calculateHash(entry.getKey(), HashAlgorithm.SHA256))) {
86 1 1. updateConfiguration : removed call to org/europa/together/domain/ConfigurationDO::setValue → KILLED
                        configEntry.setValue(entry.getValue());
87 1 1. updateConfiguration : removed call to org/europa/together/business/ConfigurationDAO::update → KILLED
                        configurationDAO.update(configEntry.getUuid(), configEntry);
88
                    }
89
                }
90
            }
91
        } catch (Exception ex) {
92
            LOGGER.catchException(ex);
93
        }
94
    }
95
96
    /**
97
     * Get the active configuration for the e-mail service and return the result
98
     * as Map.
99
     *
100
     * @return mailConfiguration as Map
101
     */
102
    @API(status = STABLE, since = "3.0")
103
    public Map<String, String> loadConfiguration() {
104
        mailClient.loadConfigurationFromDatabase();
105
        Map<String, String> config
106
                = mailClient.getDebugActiveConfiguration();
107
        LOGGER.log("CONFIG: " + config, LogLevel.DEBUG);
108 1 1. loadConfiguration : replaced return value with Collections.emptyMap for org/europa/together/service/MailClientService::loadConfiguration → KILLED
        return config;
109
    }
110
111
    /**
112
     * Send an composed (single) e-mail which is configured in a Mail data
113
     * class. An e-mail can be configured as followed:  <br>
114
     * <code>
115
     *  Mail mail = new Mail();
116
     *  mail.setSubject(subject);
117
     *  mail.setMessage(content);
118
     *  mail.addRecipent(mailAdress);
119
     * </code> <br>
120
     * In the case there are more recipients configured the mail will be only
121
     * send to the first entry.
122
     *
123
     * @param mail as MailClient
124
     * @throws jakarta.mail.MessagingException case of error
125
     */
126
    @API(status = STABLE, since = "1.0")
127
    public void sendEmail(final Mail mail) throws MessagingException {
128
        mailClient.loadConfigurationFromDatabase();
129 1 1. sendEmail : removed call to org/europa/together/business/MailClient::composeMail → SURVIVED
        mailClient.composeMail(mail);
130
131
        Address[] address = new Address[1];
132
        address[0] = (Address) mail.getRecipentList().get(0);
133
134
        Transport postman = mailClient.getSession().getTransport();
135 1 1. sendEmail : removed call to jakarta/mail/Transport::connect → KILLED
        postman.connect();
136 1 1. sendEmail : removed call to jakarta/mail/Transport::sendMessage → NO_COVERAGE
        postman.sendMessage(mailClient.getMimeMessage(), address);
137 1 1. sendEmail : removed call to jakarta/mail/Transport::close → NO_COVERAGE
        postman.close();
138
        LOGGER.log("E-Mail should send.", LogLevel.TRACE);
139
    }
140
141
    /**
142
     * Send a bulk of composed mails to a configured list of recipients. The
143
     * bulk mail supports a special feature, to interrupt the sending after an
144
     * defined amount of mails fo a configured time (milliseconds) until the
145
     * next bulk can be send. After the termination the method return th count
146
     * of the send mails. This two parameters are also part of thw whole
147
     * configuration and will not set seperatly.<br>
148
     * - <b>wait time:</b> default = -1 (unlimited)<br>
149
     * - <b>bulk mail limiter:</b> default = -1 (unlimited)
150
     *
151
     * @param mail as MailClient
152
     * @return sendedEmails as int
153
     * @throws jakarta.mail.MessagingException case of error
154
     */
155
    @API(status = STABLE, since = "1.0")
156
    public int sendBulkMail(final Mail mail) throws MessagingException {
157
        int countSendedMails = 0;
158
        try {
159
            mailClient.loadConfigurationFromDatabase();
160 1 1. sendBulkMail : removed call to org/europa/together/business/MailClient::composeMail → SURVIVED
            mailClient.composeMail(mail);
161
162
            int maximumMailBulk = mailClient.getBulkMailLimiter();
163
            long countWaitTime = mailClient.getWaitTime();
164
            Transport postman = mailClient.getSession().getTransport();
165 1 1. sendBulkMail : removed call to jakarta/mail/Transport::connect → SURVIVED
            postman.connect();
166
167
            for (Object recipient : mail.getRecipentList()) {
168
                Address[] address = new Address[1];
169
                address[0] = (Address) recipient;
170 1 1. sendBulkMail : Changed increment from 1 to -1 → NO_COVERAGE
                countSendedMails++;
171
                //after x mails wait for n seconds
172 2 1. sendBulkMail : Replaced integer modulus with multiplication → NO_COVERAGE
2. sendBulkMail : negated conditional → NO_COVERAGE
                if (countSendedMails % maximumMailBulk == 0) {
173 1 1. sendBulkMail : removed call to java/util/concurrent/TimeUnit::sleep → NO_COVERAGE
                    TimeUnit.SECONDS.sleep(countWaitTime);
174
175
                    LOGGER.log("Timer wait for " + countWaitTime + " seconds.",
176
                            LogLevel.DEBUG);
177
                }
178 1 1. sendBulkMail : removed call to jakarta/mail/Transport::send → NO_COVERAGE
                Transport.send(mailClient.getMimeMessage(), address);
179
            }
180 1 1. sendBulkMail : removed call to jakarta/mail/Transport::close → NO_COVERAGE
            postman.close();
181
182
        } catch (InterruptedException ex) {
183
            LOGGER.catchException(ex);
184
        }
185
        LOGGER.log(countSendedMails + " E-Mails should sended", LogLevel.DEBUG);
186 1 1. sendBulkMail : replaced int return with 0 for org/europa/together/service/MailClientService::sendBulkMail → NO_COVERAGE
        return countSendedMails;
187
    }
188
}

Mutations

74

1.1
Location : updateConfiguration
Killed by : none
removed call to org/europa/together/business/MailClient::clearConfiguration → SURVIVED
Covering tests

84

1.1
Location : updateConfiguration
Killed by : org.europa.together.service.MailClientServiceTest.[engine:junit-jupiter]/[class:org.europa.together.service.MailClientServiceTest]/[method:updateConfiguration()]
negated conditional → KILLED

86

1.1
Location : updateConfiguration
Killed by : org.europa.together.service.MailClientServiceTest.[engine:junit-jupiter]/[class:org.europa.together.service.MailClientServiceTest]/[method:updateConfiguration()]
removed call to org/europa/together/domain/ConfigurationDO::setValue → KILLED

87

1.1
Location : updateConfiguration
Killed by : org.europa.together.service.MailClientServiceTest.[engine:junit-jupiter]/[class:org.europa.together.service.MailClientServiceTest]/[method:updateConfiguration()]
removed call to org/europa/together/business/ConfigurationDAO::update → KILLED

108

1.1
Location : loadConfiguration
Killed by : org.europa.together.service.MailClientServiceTest.[engine:junit-jupiter]/[class:org.europa.together.service.MailClientServiceTest]/[method:loadInitConfiguration()]
replaced return value with Collections.emptyMap for org/europa/together/service/MailClientService::loadConfiguration → KILLED

129

1.1
Location : sendEmail
Killed by : none
removed call to org/europa/together/business/MailClient::composeMail → SURVIVED
Covering tests

135

1.1
Location : sendEmail
Killed by : org.europa.together.service.MailClientServiceTest.[engine:junit-jupiter]/[class:org.europa.together.service.MailClientServiceTest]/[method:failConnectionSendMail()]
removed call to jakarta/mail/Transport::connect → KILLED

136

1.1
Location : sendEmail
Killed by : none
removed call to jakarta/mail/Transport::sendMessage → NO_COVERAGE

137

1.1
Location : sendEmail
Killed by : none
removed call to jakarta/mail/Transport::close → NO_COVERAGE

160

1.1
Location : sendBulkMail
Killed by : none
removed call to org/europa/together/business/MailClient::composeMail → SURVIVED
Covering tests

165

1.1
Location : sendBulkMail
Killed by : none
removed call to jakarta/mail/Transport::connect → SURVIVED
Covering tests

170

1.1
Location : sendBulkMail
Killed by : none
Changed increment from 1 to -1 → NO_COVERAGE

172

1.1
Location : sendBulkMail
Killed by : none
Replaced integer modulus with multiplication → NO_COVERAGE

2.2
Location : sendBulkMail
Killed by : none
negated conditional → NO_COVERAGE

173

1.1
Location : sendBulkMail
Killed by : none
removed call to java/util/concurrent/TimeUnit::sleep → NO_COVERAGE

178

1.1
Location : sendBulkMail
Killed by : none
removed call to jakarta/mail/Transport::send → NO_COVERAGE

180

1.1
Location : sendBulkMail
Killed by : none
removed call to jakarta/mail/Transport::close → NO_COVERAGE

186

1.1
Location : sendBulkMail
Killed by : none
replaced int return with 0 for org/europa/together/service/MailClientService::sendBulkMail → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.22.0