refactor: 发送邮件代码重构
This commit is contained in:
parent
c4f1e23291
commit
fe019babdf
|
@ -3,11 +3,11 @@ package io.metersphere.api.jmeter;
|
|||
import io.metersphere.api.service.APIReportService;
|
||||
import io.metersphere.api.service.APITestService;
|
||||
import io.metersphere.base.domain.ApiTestReport;
|
||||
import io.metersphere.base.domain.Notice;
|
||||
import io.metersphere.commons.constants.APITestStatus;
|
||||
import io.metersphere.commons.constants.ApiRunMode;
|
||||
import io.metersphere.commons.utils.CommonBeanFactory;
|
||||
import io.metersphere.commons.utils.LogUtil;
|
||||
import io.metersphere.notice.domain.NoticeDTO;
|
||||
import io.metersphere.notice.service.MailService;
|
||||
import io.metersphere.notice.service.NoticeService;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
@ -16,7 +16,6 @@ import org.apache.jmeter.samplers.SampleResult;
|
|||
import org.apache.jmeter.visualizers.backend.AbstractBackendListenerClient;
|
||||
import org.apache.jmeter.visualizers.backend.BackendListenerContext;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.Serializable;
|
||||
import java.util.*;
|
||||
|
||||
|
@ -120,17 +119,12 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
|
|||
queue.clear();
|
||||
super.teardownTest(context);
|
||||
NoticeService noticeService = CommonBeanFactory.getBean(NoticeService.class);
|
||||
List<Notice> notice = null;
|
||||
try {
|
||||
notice = noticeService.queryNotice(testResult.getTestId());
|
||||
List<NoticeDTO> noticeDTOS = noticeService.queryNotice(testResult.getTestId());
|
||||
MailService mailService = CommonBeanFactory.getBean(MailService.class);
|
||||
mailService.sendApiTestNotice(report.getId(), noticeDTOS, report.getStatus());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
MailService mailService = CommonBeanFactory.getBean(MailService.class);
|
||||
try {
|
||||
mailService.sendHtml(report.getId(), notice, report.getStatus(), "api");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
LogUtil.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
package io.metersphere.base.domain;
|
||||
|
||||
import java.io.Serializable;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@Data
|
||||
public class Notice implements Serializable {
|
||||
private String event;
|
||||
|
@ -15,9 +16,5 @@ public class Notice implements Serializable {
|
|||
|
||||
private String enable;
|
||||
|
||||
private String[] names;
|
||||
|
||||
private String[] emails;
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
|
@ -1,8 +1,7 @@
|
|||
package io.metersphere.notice.controller;
|
||||
|
||||
import io.metersphere.base.domain.Notice;
|
||||
import io.metersphere.notice.controller.request.NoticeRequest;
|
||||
import io.metersphere.notice.service.MailService;
|
||||
import io.metersphere.notice.domain.NoticeDTO;
|
||||
import io.metersphere.notice.service.NoticeService;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
|
@ -21,7 +20,7 @@ public class NoticeController {
|
|||
}
|
||||
|
||||
@GetMapping("/query/{testId}")
|
||||
public List<Notice> queryNotice(@PathVariable String testId) {
|
||||
public List<NoticeDTO> queryNotice(@PathVariable String testId) {
|
||||
return noticeService.queryNotice(testId);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
package io.metersphere.notice.domain;
|
||||
|
||||
import io.metersphere.base.domain.Notice;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class NoticeDTO extends Notice {
|
||||
private String[] names;
|
||||
}
|
|
@ -1,14 +1,13 @@
|
|||
package io.metersphere.notice.service;
|
||||
|
||||
import io.metersphere.api.dto.APIReportResult;
|
||||
import io.metersphere.base.domain.Notice;
|
||||
import io.metersphere.base.domain.SystemParameter;
|
||||
import io.metersphere.base.domain.TestCaseWithBLOBs;
|
||||
import io.metersphere.commons.constants.ParamConstants;
|
||||
import io.metersphere.commons.utils.EncryptUtils;
|
||||
import io.metersphere.commons.utils.LogUtil;
|
||||
import io.metersphere.dto.LoadTestDTO;
|
||||
import io.metersphere.i18n.Translator;
|
||||
import io.metersphere.notice.domain.NoticeDTO;
|
||||
import io.metersphere.service.SystemParameterService;
|
||||
import io.metersphere.service.UserService;
|
||||
import io.metersphere.track.request.testreview.SaveCommentRequest;
|
||||
|
@ -17,7 +16,6 @@ import org.apache.commons.collections4.MapUtils;
|
|||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang3.RegExUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.mail.MailException;
|
||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
@ -38,27 +36,19 @@ public class MailService {
|
|||
@Resource
|
||||
private SystemParameterService systemParameterService;
|
||||
|
||||
public void sendHtml(String id, List<Notice> notice, String status, String type) {
|
||||
JavaMailSenderImpl javaMailSender = getMailSender();
|
||||
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
|
||||
String testName = "";
|
||||
if (type.equals("api")) {
|
||||
APIReportResult reportResult = apiAndPerformanceHelper.getApi(id);
|
||||
testName = reportResult.getTestName();
|
||||
} else if (type.equals("performance")) {
|
||||
LoadTestDTO performanceResult = apiAndPerformanceHelper.getPerformance(id);
|
||||
testName = performanceResult.getName();
|
||||
status = performanceResult.getStatus();
|
||||
}
|
||||
public void sendApiTestNotice(String id, List<NoticeDTO> notice, String status) {
|
||||
APIReportResult reportResult = apiAndPerformanceHelper.getApi(id);
|
||||
String testName = reportResult.getTestName();
|
||||
|
||||
Map<String, String> context = new HashMap<>();
|
||||
context.put("title", type + Translator.get("timing_task_result_notification"));
|
||||
context.put("title", "API" + Translator.get("timing_task_result_notification"));
|
||||
context.put("testName", testName);
|
||||
|
||||
try {
|
||||
String failTemplate = IOUtils.toString(this.getClass().getResource("/mail/fail.html"), StandardCharsets.UTF_8);
|
||||
String successTemplate = IOUtils.toString(this.getClass().getResource("/mail/success.html"), StandardCharsets.UTF_8);
|
||||
|
||||
JavaMailSenderImpl javaMailSender = getMailSender();
|
||||
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
|
||||
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
|
||||
helper.setFrom(javaMailSender.getUsername());
|
||||
helper.setSubject(Translator.get("timing_task_result_notification"));
|
||||
|
@ -66,7 +56,7 @@ public class MailService {
|
|||
List<String> successEmailList = new ArrayList<>();
|
||||
List<String> failEmailList = new ArrayList<>();
|
||||
if (notice.size() > 0) {
|
||||
for (Notice n : notice) {
|
||||
for (NoticeDTO n : notice) {
|
||||
if (n.getEnable().equals("true") && n.getEvent().equals("执行成功")) {
|
||||
successEmailList = userService.queryEmail(n.getNames());
|
||||
}
|
||||
|
@ -86,12 +76,8 @@ public class MailService {
|
|||
helper.setText(getContent(failTemplate, context), true);
|
||||
}
|
||||
helper.setTo(users);
|
||||
} catch (Exception e) {
|
||||
LogUtil.error(e);
|
||||
}
|
||||
try {
|
||||
javaMailSender.send(mimeMessage);
|
||||
} catch (MailException e) {
|
||||
} catch (Exception e) {
|
||||
LogUtil.error(e);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import io.metersphere.base.domain.Notice;
|
|||
import io.metersphere.base.domain.NoticeExample;
|
||||
import io.metersphere.base.mapper.NoticeMapper;
|
||||
import io.metersphere.notice.controller.request.NoticeRequest;
|
||||
import io.metersphere.notice.domain.NoticeDTO;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
@ -16,49 +17,34 @@ public class NoticeService {
|
|||
private NoticeMapper noticeMapper;
|
||||
|
||||
public void saveNotice(NoticeRequest noticeRequest) {
|
||||
Notice notice = new Notice();
|
||||
NoticeExample example = new NoticeExample();
|
||||
example.createCriteria().andTestIdEqualTo(noticeRequest.getTestId());
|
||||
List<Notice> notices = noticeMapper.selectByExample(example);
|
||||
if (notices.size() > 0) {
|
||||
noticeMapper.deleteByExample(example);
|
||||
}
|
||||
saveNotice(noticeRequest, notice);
|
||||
}
|
||||
|
||||
private void saveNotice(NoticeRequest noticeRequest, Notice notice) {
|
||||
noticeRequest.getNotices().forEach(n -> {
|
||||
if (n.getNames().length > 0) {
|
||||
for (String x : n.getNames()) {
|
||||
notice.setEvent(n.getEvent());
|
||||
notice.setEmail(n.getEmail());
|
||||
notice.setEnable(n.getEnable());
|
||||
notice.setTestId(noticeRequest.getTestId());
|
||||
notice.setName(x);
|
||||
noticeMapper.insert(notice);
|
||||
}
|
||||
} else {
|
||||
notice.setEvent(n.getEvent());
|
||||
notice.setEmail(n.getEmail());
|
||||
notice.setEnable(n.getEnable());
|
||||
notice.setTestId(noticeRequest.getTestId());
|
||||
notice.setName("");
|
||||
noticeMapper.insert(notice);
|
||||
}
|
||||
Notice notice = new Notice();
|
||||
notice.setEvent(n.getEvent());
|
||||
notice.setEmail(n.getEmail());
|
||||
notice.setEnable(n.getEnable());
|
||||
notice.setTestId(noticeRequest.getTestId());
|
||||
notice.setName("");
|
||||
noticeMapper.insert(notice);
|
||||
});
|
||||
}
|
||||
|
||||
public List<Notice> queryNotice(String id) {
|
||||
public List<NoticeDTO> queryNotice(String id) {
|
||||
NoticeExample example = new NoticeExample();
|
||||
example.createCriteria().andTestIdEqualTo(id);
|
||||
List<Notice> notices = noticeMapper.selectByExample(example);
|
||||
List<Notice> notice = new ArrayList<>();
|
||||
List<NoticeDTO> noticeDTOS = new ArrayList<>();
|
||||
List<String> success = new ArrayList<>();
|
||||
List<String> fail = new ArrayList<>();
|
||||
String[] successArray;
|
||||
String[] failArray;
|
||||
Notice notice1 = new Notice();
|
||||
Notice notice2 = new Notice();
|
||||
NoticeDTO notice1 = new NoticeDTO();
|
||||
NoticeDTO notice2 = new NoticeDTO();
|
||||
for (Notice n : notices) {
|
||||
if (n.getEvent().equals("执行成功")) {
|
||||
success.add(n.getName());
|
||||
|
@ -79,9 +65,9 @@ public class NoticeService {
|
|||
failArray = fail.toArray(new String[0]);
|
||||
notice1.setNames(successArray);
|
||||
notice2.setNames(failArray);
|
||||
notice.add(notice1);
|
||||
notice.add(notice2);
|
||||
return notice;
|
||||
noticeDTOS.add(notice1);
|
||||
noticeDTOS.add(notice2);
|
||||
return noticeDTOS;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue