refactor: 使用StringUtils对比字符串

This commit is contained in:
Captain.B 2020-09-27 18:14:19 +08:00
parent 977182a193
commit d44cc8d16e
1 changed files with 24 additions and 18 deletions

View File

@ -4,8 +4,10 @@ import io.metersphere.base.domain.ApiTestReport;
import io.metersphere.base.domain.LoadTestWithBLOBs; import io.metersphere.base.domain.LoadTestWithBLOBs;
import io.metersphere.base.domain.SystemParameter; import io.metersphere.base.domain.SystemParameter;
import io.metersphere.base.domain.TestCaseWithBLOBs; import io.metersphere.base.domain.TestCaseWithBLOBs;
import io.metersphere.commons.constants.APITestStatus;
import io.metersphere.commons.constants.NoticeConstants; import io.metersphere.commons.constants.NoticeConstants;
import io.metersphere.commons.constants.ParamConstants; import io.metersphere.commons.constants.ParamConstants;
import io.metersphere.commons.constants.PerformanceTestStatus;
import io.metersphere.commons.utils.EncryptUtils; import io.metersphere.commons.utils.EncryptUtils;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
import io.metersphere.dto.BaseSystemConfigDTO; import io.metersphere.dto.BaseSystemConfigDTO;
@ -50,9 +52,9 @@ public class MailService {
context.put("url", baseSystemConfigDTO.getUrl()); context.put("url", baseSystemConfigDTO.getUrl());
String performanceTemplate = ""; String performanceTemplate = "";
try { try {
if (status.equals("Completed")) { if (StringUtils.equals(status, PerformanceTestStatus.Completed.name())) {
performanceTemplate = IOUtils.toString(this.getClass().getResource("/mail/successPerformance.html"), StandardCharsets.UTF_8); performanceTemplate = IOUtils.toString(this.getClass().getResource("/mail/successPerformance.html"), StandardCharsets.UTF_8);
} else if (status.equals("Error")) { } else if (StringUtils.equals(status, PerformanceTestStatus.Error.name())) {
performanceTemplate = IOUtils.toString(this.getClass().getResource("/mail/failPerformance.html"), StandardCharsets.UTF_8); performanceTemplate = IOUtils.toString(this.getClass().getResource("/mail/failPerformance.html"), StandardCharsets.UTF_8);
} }
sendHtmlTimeTasks(noticeList, status, context, performanceTemplate); sendHtmlTimeTasks(noticeList, status, context, performanceTemplate);
@ -71,9 +73,9 @@ public class MailService {
context.put("id", apiTestReport.getId()); context.put("id", apiTestReport.getId());
String apiTemplate = ""; String apiTemplate = "";
try { try {
if (apiTestReport.getStatus().equals("Success")) { if (StringUtils.equals(APITestStatus.Success.name(), apiTestReport.getStatus())) {
apiTemplate = IOUtils.toString(this.getClass().getResource("/mail/success.html"), StandardCharsets.UTF_8); apiTemplate = IOUtils.toString(this.getClass().getResource("/mail/success.html"), StandardCharsets.UTF_8);
} else if (apiTestReport.getStatus().equals("Error")) { } else if (StringUtils.equals(APITestStatus.Error.name(), apiTestReport.getStatus())) {
apiTemplate = IOUtils.toString(this.getClass().getResource("/mail/fail.html"), StandardCharsets.UTF_8); apiTemplate = IOUtils.toString(this.getClass().getResource("/mail/fail.html"), StandardCharsets.UTF_8);
} }
sendHtmlTimeTasks(noticeList, apiTestReport.getStatus(), context, apiTemplate); sendHtmlTimeTasks(noticeList, apiTestReport.getStatus(), context, apiTemplate);
@ -184,17 +186,21 @@ public class MailService {
javaMailSender.setDefaultEncoding("UTF-8"); javaMailSender.setDefaultEncoding("UTF-8");
javaMailSender.setProtocol("smtps"); javaMailSender.setProtocol("smtps");
for (SystemParameter p : paramList) { for (SystemParameter p : paramList) {
if (p.getParamKey().equals("smtp.host")) { switch (p.getParamKey()) {
javaMailSender.setHost(p.getParamValue()); case "smtp.host":
} javaMailSender.setHost(p.getParamValue());
if (p.getParamKey().equals("smtp.port")) { break;
javaMailSender.setPort(Integer.parseInt(p.getParamValue())); case "smtp.port":
} javaMailSender.setPort(Integer.parseInt(p.getParamValue()));
if (p.getParamKey().equals("smtp.account")) { break;
javaMailSender.setUsername(p.getParamValue()); case "smtp.account":
} javaMailSender.setUsername(p.getParamValue());
if (p.getParamKey().equals("smtp.password")) { break;
javaMailSender.setPassword(EncryptUtils.aesDecrypt(p.getParamValue()).toString()); case "smtp.password":
javaMailSender.setPassword(EncryptUtils.aesDecrypt(p.getParamValue()).toString());
break;
default:
break;
} }
} }
Properties props = new Properties(); Properties props = new Properties();
@ -224,10 +230,10 @@ public class MailService {
List<String> failEmailList = new ArrayList<>(); List<String> failEmailList = new ArrayList<>();
if (noticeList.size() > 0) { if (noticeList.size() > 0) {
for (NoticeDetail n : noticeList) { for (NoticeDetail n : noticeList) {
if (n.getEnable().equals("true") && n.getEvent().equals(NoticeConstants.EXECUTE_SUCCESSFUL)) { if (StringUtils.equals(n.getEnable(), "true") && StringUtils.equals(n.getEvent(), NoticeConstants.EXECUTE_SUCCESSFUL)) {
successEmailList = userService.queryEmail(n.getNames()); successEmailList = userService.queryEmail(n.getNames());
} }
if (n.getEnable().equals("true") && n.getEvent().equals(NoticeConstants.EXECUTE_FAILED)) { if (StringUtils.equals(n.getEnable(), "true") && StringUtils.equals(n.getEvent(), NoticeConstants.EXECUTE_FAILED)) {
failEmailList = userService.queryEmail(n.getNames()); failEmailList = userService.queryEmail(n.getNames());
} }
} }
@ -235,7 +241,7 @@ public class MailService {
LogUtil.error("Recipient information is empty"); LogUtil.error("Recipient information is empty");
} }
if (status.equals("Success") || status.equals("Completed")) { if (StringUtils.equalsAny(status, PerformanceTestStatus.Completed.name(), APITestStatus.Success.name())) {
recipientEmails = successEmailList.toArray(new String[0]); recipientEmails = successEmailList.toArray(new String[0]);
} else { } else {
recipientEmails = failEmailList.toArray(new String[0]); recipientEmails = failEmailList.toArray(new String[0]);