com.ch.email.EmailUtility Maven / Gradle / Ivy
package com.ch.email;
import java.text.MessageFormat;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang3.StringUtils;
import com.ch.config.ConfigProperties;
import com.ch.exception.PropertyNotFoundException;
import com.ch.model.EnvironmentInfo;
import com.ch.model.ExecutionReport;
public class EmailUtility {
private static final Logger LOGGER = Logger.getLogger(EmailUtility.class.getName());
public static String emailSubject(ExecutionReport report, String suffix) {
if (report != null && report.getEnvironmentInfo() != null) {
EnvironmentInfo envInfo = report.getEnvironmentInfo();
return emailSubject(report.getApplicationName(), report.getProject(), envInfo.getSuite(), report.getSite(),
envInfo.getEnvironment(), envInfo.getOs(), envInfo.getBrowserName(), suffix);
}
return null;
}
public static String emailSubject(String applicationName, String project, String suiteName, String siteName,
String environment, String os, String browserName, String suffix) {
StringBuilder subject = new StringBuilder("Test Execution Report - ");
appendNotNulls(applicationName, subject, "|");
appendNotNulls(environment, subject, "|");
appendNotNulls(suiteName, subject, "|");
appendNotNulls(os, subject, "|");
appendNotNulls(browserName, subject, "|");
appendNotNulls(suffix, subject, null);
return subject.toString();
}
public static String emailSubjectWithCurrentDate(String applicationName, String project, String suiteName,
String siteName, String environment, String os, String browserName, String timezone, String pattern) {
StringBuilder subject = new StringBuilder("Test Execution Report - ");
appendNotNulls(applicationName, subject, "|");
appendNotNulls(environment, subject, "|");
appendNotNulls(suiteName, subject, "|");
appendNotNulls(os, subject, "|");
appendNotNulls(browserName, subject, "|");
String str = formatCurrentDateToTimezone(timezone, pattern);
appendNotNulls(str, subject, null);
return subject.toString();
}
public static String emailBody(ExecutionReport report, String dateTime) {
if (report != null && report.getEnvironmentInfo() != null) {
EnvironmentInfo envInfo = report.getEnvironmentInfo();
Map testStatusMap = testStatusDistribution(report.getPass_count(), report.getFail_count(),
report.getError_count(), report.getSkip_count(), report.getOther_count());
return emailBody(report.getApplicationName(), envInfo.getSuite(), report.getSite(),
envInfo.getEnvironment(), envInfo.getOs(), envInfo.getBrowserName(), testStatusMap, null,
envInfo.getDeviceName(), envInfo.getDeviceOrientation(), dateTime);
}
return null;
}
public static String emailBody(ExecutionReport report, String downLoadURL, String dateTime) {
if (report != null && report.getEnvironmentInfo() != null) {
EnvironmentInfo envInfo = report.getEnvironmentInfo();
Map testStatusMap = testStatusDistribution(report.getPass_count(), report.getFail_count(),
report.getError_count(), report.getSkip_count(), report.getOther_count());
return emailBody(report.getApplicationName(), envInfo.getSuite(), report.getSite(),
envInfo.getEnvironment(), envInfo.getOs(), envInfo.getBrowserName(), testStatusMap, downLoadURL,
envInfo.getDeviceName(), envInfo.getDeviceOrientation(), dateTime);
}
return null;
}
public static String emailBody(String applicationName, String suiteName, String siteName, String environment,
String os, String browserName, int passCount, int failCount, int skippedCount, int errorCount,
String device, String deviceOrientation, String dateTime) {
Map testStatusMap = testStatusDistribution(passCount, failCount, errorCount, skippedCount, 0);
return emailBody(applicationName, suiteName, siteName, environment, os, browserName, testStatusMap, null,
device, deviceOrientation, dateTime);
}
public static String emailBody(String applicationName, String suiteName, String siteName, String environment,
String os, String browserName, int passCount, int failCount, int skippedCount, int errorCount,
String downLoadURL, ConfigProperties configProperties, String device, String deviceOrientation,
String dateTime) {
Map testStatusMap = testStatusDistribution(passCount, failCount, errorCount, skippedCount, 0);
return emailBody(applicationName, suiteName, siteName, environment, os, browserName, testStatusMap, downLoadURL,
device, deviceOrientation, dateTime);
}
private static String emailBody(String applicationName, String suiteName, String siteName, String environment,
String os, String browserName, Map testStatusMap, String downLoadURL, String deviceName,
String deviceOrientation, String dateTime) {
StringBuilder emailBody = new StringBuilder("Test Summary:
");
emailBody.append("");
appendInfo(emailBody, "Application Name", applicationName);
appendInfo(emailBody, "SiteName", siteName);
appendInfo(emailBody, "Environment", environment);
appendInfo(emailBody, "Suite", capitalize(suiteName));
appendInfo(emailBody, "OS", capitalize(os));
appendInfo(emailBody, "Browser", capitalize(browserName));
if (!StringUtils.isAllEmpty(deviceName)) {
appendInfo(emailBody, "device", deviceName);
}
if (!StringUtils.isAllEmpty(deviceOrientation)) {
appendInfo(emailBody, "deviceOrientation", capitalize(deviceOrientation));
}
if (!testStatusMap.isEmpty()) {
testStatusMap.forEach((k, v) -> appendInfo(emailBody, k, v.toString()));
}
if (!StringUtils.isAllEmpty(dateTime)) {
appendInfo(emailBody, "DateTime", dateTime);
}
emailBody.append("
");
if (StringUtils.isNotBlank(downLoadURL)) {
emailBody.append(appendDownLoadUrl(downLoadURL));
}
return emailBody.toString();
}
private static String appendDownLoadUrl(String downLoadURL) {
StringBuilder downLoadLink = new StringBuilder("");
downLoadLink.append(
"
Download Report by clicking on the button below. The report is available for 30 days.
");
downLoadLink.append("");
downLoadLink.append("");
downLoadLink.append("");
return downLoadLink.toString();
}
private static void appendNotNulls(String name, StringBuilder builder, String delimiter) {
if (StringUtils.isNoneBlank(name)) {
builder.append(name);
if (StringUtils.isNoneBlank(delimiter)) {
builder.append(delimiter);
}
}
}
private static void appendInfo(StringBuilder builder, String key, String value) {
if (StringUtils.isNotEmpty(value)) {
builder.append("");
builder.append(key);
builder.append(" ");
builder.append(value);
builder.append(" ");
}
}
private static String capitalize(String str) {
if (str == null || str.isEmpty()) {
return str;
}
return str.substring(0, 1).toUpperCase() + str.substring(1);
}
private static Map testStatusDistribution(int pass, int fail, int error, int skip, int other) {
Map countMap = new LinkedHashMap();
countMap.put("PASS", pass);
countMap.put("FAIL", fail);
countMap.put("ERROR", error);
countMap.put("SKIP", skip);
countMap.put("OTHER", other);
return countMap;
}
public static String formatCurrentDateToTimezone(String timezone, String pattern) {
try {
// Get the current date and time
ZonedDateTime currentDateTime = ZonedDateTime.now();
// Create a ZoneId for the given timezone
if (timezone == null || timezone.isEmpty()) {
timezone = "America/New_York";
}
ZoneId zoneId = ZoneId.of(timezone);
// Adjust the current date and time to the given timezone
ZonedDateTime zonedDateTime = currentDateTime.withZoneSameInstant(zoneId);
// Use the provided pattern or default to a standard one
if (pattern == null || pattern.isEmpty()) {
pattern = "MMM dd yyyy hh:mm:ss a z"; // Default pattern
}
// Format the date and time as a string
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
return zonedDateTime.format(formatter);
} catch (Exception e) {
// Handle invalid timezone or pattern input
LOGGER.log(Level.SEVERE, "invalid timezone or pattern input", e);
return null;
}
}
public static String getProperty(ConfigProperties config, String propertyName, String defaultValue) {
String returnValue = defaultValue;
try {
if (StringUtils.isNotEmpty(config.getProperty(propertyName))) {
returnValue = config.getProperty(propertyName);
}
} catch (PropertyNotFoundException e) {
Object params[] = { propertyName, defaultValue };
String message = MessageFormat.format("Property {0} not found using default value {1}", params);
LOGGER.warning(message);
}
return returnValue;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy