MailConfig.java

1
package de.aikiit.mailversendala;
2
3
/*
4
 * Copyright (c) P. Ottlinger
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *   https://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19
20
21
import de.aikiit.mailversendala.template.MailTemplate;
22
import lombok.Getter;
23
import org.apache.logging.log4j.LogManager;
24
import org.apache.logging.log4j.Logger;
25
import org.apache.tamaya.Configuration;
26
27
import java.io.IOException;
28
import java.io.Reader;
29
import java.nio.charset.StandardCharsets;
30
import java.nio.file.Files;
31
import java.nio.file.Path;
32
import java.util.Properties;
33
34
35
/**
36
 * Encapsulates the configuration of this application.
37
 */
38
@Getter
39
public class MailConfig {
40
41
    private static final Logger LOG =
42
            LogManager.getLogger(MailConfig.class);
43
44
    private final String host;
45
    private final int port;
46
    private final String username;
47
    private final String password;
48
    private final String to;
49
    private final String from;
50
    private final String replyTo;
51
    private final String subject;
52
    private final String csvPath;
53
    private final String templatePath;
54
    private final String htmlTemplate;
55
    private final String plaintextTemplate;
56
    private final String attachments;
57
    private final boolean isDemoMode;
58
59
    /**
60
     * Read an optional UTF-8 properties file specified by {@code -Dconfig=path}.
61
     * JVM properties and environment variables override the file, which overrides
62
     * Tamaya's classpath configuration and the built-in defaults.
63
     */
64
    public MailConfig() {
65
        this(loadExternalConfiguration());
66
    }
67
68
    private MailConfig(Properties external) {
69
        LOG.debug("Reading Tamaya configuration ...");
70
        final ConfigValues configuration = new ConfigValues(external, Configuration.current());
71
72
        this.host = configuration.getOrDefault("host", "smtp.example.com");
73
        this.port = Integer.parseInt(configuration.getOrDefault("port", "465"));
74
        this.username = configuration.getOrDefault("username", "user@example.com");
75
        this.password = configuration.getOrDefault("password", "chooseMeWisely");
76
        this.to = configuration.getOrDefault("to", "");
77
        this.from = configuration.getOrDefault("from", "santa@cruz.com");
78
        this.replyTo = configuration.getOrDefault("replyto", "");
79
        this.subject = configuration.getOrDefault("subject", "Do adapt your configuration - will not work");
80
        this.csvPath = configuration.getOrDefault("csvpath", "mailversendala-example.csv");
81
        this.isDemoMode = Boolean.parseBoolean(configuration.getOrDefault("demomode", "true"));
82
        this.templatePath = configuration.getOrDefault("templatepath", "template");
83
        this.htmlTemplate = configuration.getOrDefault("htmltemplate", MailTemplate.BASE_NAME_HTML);
84
        this.plaintextTemplate = configuration.getOrDefault("plaintexttemplate", MailTemplate.BASE_NAME_PLAINTEXT);
85
        this.attachments = configuration.getOrDefault("attachments", "");
86
        LOG.debug("Configuration: DONE.");
87
    }
88
89
    private static Properties loadExternalConfiguration() {
90
        Properties properties = new Properties();
91
        String filename = System.getProperty("config");
92 1 1. loadExternalConfiguration : negated conditional → KILLED
        if (filename != null) {
93 1 1. loadExternalConfiguration : negated conditional → KILLED
            if (filename.isBlank()) {
94
                throw new IllegalArgumentException("Configuration file path (-Dconfig) must not be blank");
95
            }
96
            Path path = Path.of(filename).toAbsolutePath().normalize();
97
            try (Reader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
98 1 1. loadExternalConfiguration : removed call to java/util/Properties::load → KILLED
                properties.load(reader);
99
            } catch (IOException | IllegalArgumentException e) {
100
                // Do not expose property values (in particular SMTP credentials).
101
                throw new IllegalArgumentException("Cannot read configuration file: " + path);
102
            }
103
            LOG.info("Loaded external configuration file: {}", path);
104
        }
105 1 1. loadExternalConfiguration : replaced return value with null for de/aikiit/mailversendala/MailConfig::loadExternalConfiguration → KILLED
        return properties;
106
    }
107
108
    private record ConfigValues(Properties external, Configuration tamaya) {
109
        private String getOrDefault(String key, String fallback) {
110
            String value = System.getProperty(key);
111 1 1. getOrDefault : negated conditional → KILLED
            if (value == null) {
112
                value = System.getenv(key);
113
            }
114 1 1. getOrDefault : negated conditional → KILLED
            if (value == null) {
115
                value = external.getProperty(key);
116
            }
117 2 1. getOrDefault : replaced return value with "" for de/aikiit/mailversendala/MailConfig$ConfigValues::getOrDefault → KILLED
2. getOrDefault : negated conditional → KILLED
            return value == null ? tamaya.getOrDefault(key, fallback) : value;
118
        }
119
    }
120
121
    /**
122
     * If in demo mode <strong>NO</strong> mails should be sent out!
123
     *
124
     * @return {@code true} if in demo mode, thus no mails will be sent by default.
125
     */
126
    public boolean sendOutMails() {
127 2 1. sendOutMails : negated conditional → KILLED
2. sendOutMails : replaced boolean return with true for de/aikiit/mailversendala/MailConfig::sendOutMails → KILLED
        return !isDemoMode;
128
    }
129
130
}

Mutations

92

1.1
Location : loadExternalConfiguration
Killed by : de.aikiit.mailversendala.MailConfigTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.MailConfigTest]/[method:ensureConfigurationIsValid()]
negated conditional → KILLED

93

1.1
Location : loadExternalConfiguration
Killed by : de.aikiit.mailversendala.MailConfigTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.MailConfigTest]/[method:readsExternalUtf8ConfigurationAndAllowsJvmOverrides()]
negated conditional → KILLED

98

1.1
Location : loadExternalConfiguration
Killed by : de.aikiit.mailversendala.MailConfigTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.MailConfigTest]/[method:readsExternalUtf8ConfigurationAndAllowsJvmOverrides()]
removed call to java/util/Properties::load → KILLED

105

1.1
Location : loadExternalConfiguration
Killed by : de.aikiit.mailversendala.MailConfigTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.MailConfigTest]/[method:ensureConfigurationIsValid()]
replaced return value with null for de/aikiit/mailversendala/MailConfig::loadExternalConfiguration → KILLED

111

1.1
Location : getOrDefault
Killed by : de.aikiit.mailversendala.MailConfigTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.MailConfigTest]/[method:readsExternalUtf8ConfigurationAndAllowsJvmOverrides()]
negated conditional → KILLED

114

1.1
Location : getOrDefault
Killed by : de.aikiit.mailversendala.MailConfigTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.MailConfigTest]/[method:readsExternalUtf8ConfigurationAndAllowsJvmOverrides()]
negated conditional → KILLED

117

1.1
Location : getOrDefault
Killed by : de.aikiit.mailversendala.SendOutTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.SendOutTest]/[method:initializeMailSenderProperly()]
replaced return value with "" for de/aikiit/mailversendala/MailConfig$ConfigValues::getOrDefault → KILLED

2.2
Location : getOrDefault
Killed by : de.aikiit.mailversendala.SendOutTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.SendOutTest]/[method:initializeMailSenderProperly()]
negated conditional → KILLED

127

1.1
Location : sendOutMails
Killed by : de.aikiit.mailversendala.MailConfigTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.MailConfigTest]/[method:readsExternalUtf8ConfigurationAndAllowsJvmOverrides()]
negated conditional → KILLED

2.2
Location : sendOutMails
Killed by : de.aikiit.mailversendala.MailConfigTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.MailConfigTest]/[method:readsExternalUtf8ConfigurationAndAllowsJvmOverrides()]
replaced boolean return with true for de/aikiit/mailversendala/MailConfig::sendOutMails → KILLED

Active mutators

Tests examined


Report generated by PIT 1.30.2 support