From 5721cc4da71464f4281ca607888ad583691d5409 Mon Sep 17 00:00:00 2001 From: seagull <24163551@qq.com> Date: Thu, 17 Sep 2020 20:13:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8A=8A=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=BB=93=E6=9E=9C=E6=8E=A8=E9=80=81=E5=88=B0=E4=BC=81=E4=B8=9A?= =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E4=BB=A5=E5=8F=8A=E9=98=BF=E9=87=8C=E9=92=89?= =?UTF-8?q?=E9=92=89=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Resources/email_template/task-body.ftl | 2 +- src/main/Resources/sys_config.properties | 5 - .../execution/appium/AppTestControl.java | 6 +- .../execution/httpinterface/TestControl.java | 6 +- .../execution/webdriver/WebTestControl.java | 6 +- .../remote/api/serverOperation.java | 33 +- .../remote/entity/TaskScheduling.java | 519 +++++++++--------- .../tool/mail/FreemarkerEmailTemplate.java | 2 +- .../java/luckyclient/tool/mail/HtmlMail.java | 56 +- .../tool/mail/MailSendInitialization.java | 239 +++++--- .../utils/httputils/HttpClientTools.java | 3 +- 11 files changed, 462 insertions(+), 415 deletions(-) diff --git a/src/main/Resources/email_template/task-body.ftl b/src/main/Resources/email_template/task-body.ftl index 2025d9b..f32d7b6 100644 --- a/src/main/Resources/email_template/task-body.ftl +++ b/src/main/Resources/email_template/task-body.ftl @@ -46,7 +46,7 @@

自动构建状态: ${buildstatus}

自动重启TOMCAT状态: ${restartstatus}


-

本次任务预期执行用例共【${casecount}】条,耗時【${time}】

+

本次任务预期执行用例共【${casecount}】条,耗時【${time}

用例执行成功: ${casesuc}

用例执行失败: ${casefail}

用例有可能由于脚本原因未成功解析被锁定:${caselock}

diff --git a/src/main/Resources/sys_config.properties b/src/main/Resources/sys_config.properties index 9dbcfb1..c9da8d2 100644 --- a/src/main/Resources/sys_config.properties +++ b/src/main/Resources/sys_config.properties @@ -21,11 +21,6 @@ mail.smtp.username=xxxxx@xx.com mail.smtp.password=xxxxx #邮件模板路径 mail.freemarker.template=/email_template/ -#==================向第三方应用推送任务执行结果(JSON)======================== -#第三方平台推送开关 true打开 false关闭 -task.push.switch=false -#第三方平台推送URL -task.push.url=localhost #==================Jenkins API配置======================== #jenkins访问地址,示例: http://192.168.100.100:8080/jenkins/ jenkins.url=http://localhost diff --git a/src/main/java/luckyclient/execution/appium/AppTestControl.java b/src/main/java/luckyclient/execution/appium/AppTestControl.java index 2c39ea0..8f0fd09 100644 --- a/src/main/java/luckyclient/execution/appium/AppTestControl.java +++ b/src/main/java/luckyclient/execution/appium/AppTestControl.java @@ -168,7 +168,7 @@ public class AppTestControl { LogUtil.APP.info("当前项目【{}】测试计划中的用例已经全部执行完成...",projectname); MailSendInitialization.sendMailInitialization(HtmlMail.htmlSubjectFormat(jobname), HtmlMail.htmlContentFormat(tastcount, taskId, buildResult.toString(), restartstatus, testtime, jobname), - taskId, taskScheduling, tastcount); + taskId, taskScheduling, tastcount,testtime,buildResult.toString(),restartstatus); // 关闭APP以及appium会话 if ("Android".equals(properties.getProperty("platformName"))) { assert androiddriver != null; @@ -179,11 +179,11 @@ public class AppTestControl { } } else { LogUtil.APP.warn("项目构建失败,自动化测试自动退出!请前往JENKINS中检查项目构建情况。"); - MailSendInitialization.sendMailInitialization(jobname, "构建项目过程中失败,自动化测试自动退出!请前去JENKINS查看构建情况!", taskId, taskScheduling, null); + MailSendInitialization.sendMailInitialization(jobname, "构建项目过程中失败,自动化测试自动退出!请前去JENKINS查看构建情况!", taskId, taskScheduling, null,"0小时0分0秒",buildResult.toString(),restartstatus); } } else { LogUtil.APP.warn("项目TOMCAT重启失败,自动化测试自动退出!请检查项目TOMCAT运行情况。"); - MailSendInitialization.sendMailInitialization(jobname, "项目TOMCAT重启失败,自动化测试自动退出!请检查项目TOMCAT运行情况!", taskId, taskScheduling, null); + MailSendInitialization.sendMailInitialization(jobname, "项目TOMCAT重启失败,自动化测试自动退出!请检查项目TOMCAT运行情况!", taskId, taskScheduling, null,"0小时0分0秒",buildResult.toString(),restartstatus); } //关闭Appium服务的线程 if(as!=null){ diff --git a/src/main/java/luckyclient/execution/httpinterface/TestControl.java b/src/main/java/luckyclient/execution/httpinterface/TestControl.java index 4aa0fdb..c9bfd2c 100644 --- a/src/main/java/luckyclient/execution/httpinterface/TestControl.java +++ b/src/main/java/luckyclient/execution/httpinterface/TestControl.java @@ -153,18 +153,18 @@ public class TestControl { String testtime = serverOperation.getTestTime(taskid); MailSendInitialization.sendMailInitialization(HtmlMail.htmlSubjectFormat(jobname), HtmlMail.htmlContentFormat(tastcount, taskid, buildResult.toString(), restartstatus, testtime, jobname), - taskid, taskScheduling, tastcount); + taskid, taskScheduling, tastcount,testtime,buildResult.toString(),restartstatus); threadExecute.shutdown(); LogUtil.APP.info("亲,没有下一条啦!我发现你的用例已经全部执行完毕,快去看看有没有失败的用例吧!"); } else { LogUtil.APP.warn("项目构建失败,自动化测试自动退出!请查看构建日志检查项目构建情况..."); MailSendInitialization.sendMailInitialization(jobname, "构建项目过程中失败,自动化测试自动退出!请查看构建日志检查项目构建情况...", taskid, - taskScheduling, null); + taskScheduling, null,"0小时0分0秒",buildResult.toString(),restartstatus); } } else { LogUtil.APP.warn("项目TOMCAT重启失败,自动化测试自动退出!请检查项目TOMCAT运行情况。"); MailSendInitialization.sendMailInitialization(jobname, "项目TOMCAT重启失败,自动化测试自动退出!请检查项目TOMCAT运行情况!", taskid, - taskScheduling, null); + taskScheduling, null,"0小时0分0秒",buildResult.toString(),restartstatus); } } diff --git a/src/main/java/luckyclient/execution/webdriver/WebTestControl.java b/src/main/java/luckyclient/execution/webdriver/WebTestControl.java index d4bb4b8..58e006c 100644 --- a/src/main/java/luckyclient/execution/webdriver/WebTestControl.java +++ b/src/main/java/luckyclient/execution/webdriver/WebTestControl.java @@ -135,19 +135,19 @@ public class WebTestControl { LogUtil.APP.info("当前项目【{}】测试计划中的用例已经全部执行完成...",projectname); MailSendInitialization.sendMailInitialization(HtmlMail.htmlSubjectFormat(jobname), HtmlMail.htmlContentFormat(tastcount, taskid, buildResult.toString(), restartstatus, testtime, jobname), - taskid, taskScheduling, tastcount); + taskid, taskScheduling, tastcount,testtime,buildResult.toString(),restartstatus); // 关闭浏览器 assert wd != null; wd.quit(); } else { LogUtil.APP.warn("项目构建失败,自动化测试自动退出!请前往JENKINS中检查项目构建情况。"); MailSendInitialization.sendMailInitialization(jobname, "构建项目过程中失败,自动化测试自动退出!请前去JENKINS查看构建情况!", taskid, - taskScheduling, null); + taskScheduling, null,"0小时0分0秒",buildResult.toString(),restartstatus); } } else { LogUtil.APP.warn("项目TOMCAT重启失败,自动化测试自动退出!请检查项目TOMCAT运行情况。"); MailSendInitialization.sendMailInitialization(jobname, "项目TOMCAT重启失败,自动化测试自动退出!请检查项目TOMCAT运行情况!", taskid, - taskScheduling, null); + taskScheduling, null,"0小时0分0秒",buildResult.toString(),restartstatus); } } diff --git a/src/main/java/luckyclient/remote/api/serverOperation.java b/src/main/java/luckyclient/remote/api/serverOperation.java index e7196e0..d30dcdf 100644 --- a/src/main/java/luckyclient/remote/api/serverOperation.java +++ b/src/main/java/luckyclient/remote/api/serverOperation.java @@ -116,24 +116,26 @@ public class serverOperation { * 发送 eMailer varchar(100) ; --收件人 */ - public static String[] getEmailAddress(String taskIdStr) { - int taskId = Integer.parseInt(taskIdStr); + public static String[] getEmailAddress(TaskScheduling taskScheduling,String taskIdStr) { String[] address = null; try { - TaskScheduling taskScheduling = GetServerApi.cGetTaskSchedulingByTaskId(taskId); if (!taskScheduling.getEmailSendCondition().equals(-1)) { String temp = taskScheduling.getEmailAddress(); - // 清除最后一个; - if (temp.contains(";") && temp.substring(temp.length() - 1).contains(";")) { - temp = temp.substring(0, temp.length() - 1); - } - // 多个地址 - if (!temp.contains("null") && temp.contains(";")) { - address = temp.split(";", -1); - // 一个地址 - } else if (!temp.contains("null") && !temp.contains(";")) { - address = new String[1]; - address[0] = temp; + if(StrUtil.isNotBlank(temp)){ + // 清除最后一个; + if (temp.contains(";") && temp.substring(temp.length() - 1).contains(";")) { + temp = temp.substring(0, temp.length() - 1); + } + // 多个地址 + if (!temp.contains("null") && temp.contains(";")) { + address = temp.split(";", -1); + // 一个地址 + } else if (!temp.contains("null") && !temp.contains(";")) { + address = new String[1]; + address[0] = temp; + } + }else{ + LogUtil.APP.warn("邮件地址配置为空,取消邮件发送..."); } } } catch (Exception e) { @@ -231,8 +233,7 @@ public class serverOperation { long hour = (l / (60 * 60 * 1000) - day * 24); long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60); long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60); - desTime = "" + hour + "小时" + min - + "" + s + "秒"; + desTime = hour + "小时" + min + "分" + s + "秒"; } } catch (Exception e) { // TODO Auto-generated catch block diff --git a/src/main/java/luckyclient/remote/entity/TaskScheduling.java b/src/main/java/luckyclient/remote/entity/TaskScheduling.java index 3322104..743778f 100644 --- a/src/main/java/luckyclient/remote/entity/TaskScheduling.java +++ b/src/main/java/luckyclient/remote/entity/TaskScheduling.java @@ -1,254 +1,267 @@ -package luckyclient.remote.entity; - -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; - -/** - * 任务调度实体 - * ================================================================= - * 这是一个受限制的自由软件!您不能在任何未经允许的前提下对程序代码进行修改和用于商业用途;也不允许对程序代码修改后以任何形式任何目的的再发布。 - * 为了尊重作者的劳动成果,LuckyFrame关键版权信息严禁篡改 有任何疑问欢迎联系作者讨论。 QQ:1573584944 Seagull - * ================================================================= - * @author Seagull - * @date 2019年4月13日 - */ -public class TaskScheduling extends BaseEntity -{ - private static final long serialVersionUID = 1L; - - /** 预约调度ID */ - private Integer schedulingId; - /** 预约调度名称 */ - private String schedulingName; - /** 任务ID */ - private Integer jobId; - /** 项目ID */ - private Integer projectId; - /** 测试计划ID */ - private Integer planId; - /** 客户端ID */ - private Integer clientId; - /** 邮件通知地址 */ - private String emailAddress; - /** 发送邮件通知时的具体逻辑, 0-全部,1-成功,-1-失败 */ - private Integer emailSendCondition; - /** jenkins构建链接 */ - private String buildingLink; - /** 远程执行Shell脚本 */ - private String remoteShell; - /** 客户端执行线程数 */ - private Integer exThreadCount; - /** 任务类型 0 接口 1 Web UI 2 移动 */ - private Integer taskType; - /** UI自动化浏览器类型 0 IE 1 火狐 2 谷歌 3 Edge */ - private Integer browserType; - /** 任务超时时间(分钟) */ - private Integer taskTimeout; - /** 客户端测试驱动桩路径 */ - private String clientDriverPath; - /** 关联项目实体 */ - private Project project; - /** 关联项目计划 */ - private ProjectPlan projectPlan; - /** 任务名称 */ - private String jobName; - /** cron执行表达式 */ - private String cronExpression; - /** 任务状态(0正常 1暂停) */ - private String status; - - public String getSchedulingName() { - return schedulingName; - } - - public void setSchedulingName(String schedulingName) { - this.schedulingName = schedulingName; - } - - public String getJobName() { - return jobName; - } - - public void setJobName(String jobName) { - this.jobName = jobName; - } - - public String getCronExpression() { - return cronExpression; - } - - public void setCronExpression(String cronExpression) { - this.cronExpression = cronExpression; - } - - public String getStatus() { - return status; - } - - public void setStatus(String status) { - this.status = status; - } - - public ProjectPlan getProjectPlan() { - return projectPlan; - } - - public void setProjectPlan(ProjectPlan projectPlan) { - this.projectPlan = projectPlan; - } - - public Project getProject() { - return project; - } - - public void setProject(Project project) { - this.project = project; - } - - public void setSchedulingId(Integer schedulingId) - { - this.schedulingId = schedulingId; - } - - public Integer getSchedulingId() - { - return schedulingId; - } - public void setJobId(Integer jobId) - { - this.jobId = jobId; - } - - public Integer getJobId() - { - return jobId; - } - public void setProjectId(Integer projectId) - { - this.projectId = projectId; - } - - public Integer getProjectId() - { - return projectId; - } - public void setPlanId(Integer planId) - { - this.planId = planId; - } - - public Integer getPlanId() - { - return planId; - } - public void setClientId(Integer clientId) - { - this.clientId = clientId; - } - - public Integer getClientId() - { - return clientId; - } - public void setEmailAddress(String emailAddress) - { - this.emailAddress = emailAddress; - } - - public String getEmailAddress() - { - return emailAddress; - } - public void setEmailSendCondition(Integer emailSendCondition) - { - this.emailSendCondition = emailSendCondition; - } - - public Integer getEmailSendCondition() - { - return emailSendCondition; - } - public void setBuildingLink(String buildingLink) - { - this.buildingLink = buildingLink; - } - - public String getBuildingLink() - { - return buildingLink; - } - public void setRemoteShell(String remoteShell) - { - this.remoteShell = remoteShell; - } - - public String getRemoteShell() - { - return remoteShell; - } - public void setExThreadCount(Integer exThreadCount) - { - this.exThreadCount = exThreadCount; - } - - public Integer getExThreadCount() - { - return exThreadCount; - } - public void setTaskType(Integer taskType) - { - this.taskType = taskType; - } - - public Integer getTaskType() - { - return taskType; - } - public void setBrowserType(Integer browserType) - { - this.browserType = browserType; - } - - public Integer getBrowserType() - { - return browserType; - } - public void setTaskTimeout(Integer taskTimeout) - { - this.taskTimeout = taskTimeout; - } - - public Integer getTaskTimeout() - { - return taskTimeout; - } - public void setClientDriverPath(String clientDriverPath) - { - this.clientDriverPath = clientDriverPath; - } - - public String getClientDriverPath() - { - return clientDriverPath; - } - - @Override - public String toString() { - return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE) - .append("schedulingId", getSchedulingId()) - .append("jobId", getJobId()) - .append("projectId", getProjectId()) - .append("planId", getPlanId()) - .append("clientId", getClientId()) - .append("emailAddress", getEmailAddress()) - .append("emailSendCondition", getEmailSendCondition()) - .append("buildingLink", getBuildingLink()) - .append("remoteShell", getRemoteShell()) - .append("exThreadCount", getExThreadCount()) - .append("taskType", getTaskType()) - .append("browserType", getBrowserType()) - .append("taskTimeout", getTaskTimeout()) - .append("clientDriverPath", getClientDriverPath()) - .toString(); - } +package luckyclient.remote.entity; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; + +/** + * 任务调度实体 + * ================================================================= + * 这是一个受限制的自由软件!您不能在任何未经允许的前提下对程序代码进行修改和用于商业用途;也不允许对程序代码修改后以任何形式任何目的的再发布。 + * 为了尊重作者的劳动成果,LuckyFrame关键版权信息严禁篡改 有任何疑问欢迎联系作者讨论。 QQ:1573584944 Seagull + * ================================================================= + * @author Seagull + * @date 2019年4月13日 + */ +public class TaskScheduling extends BaseEntity +{ + private static final long serialVersionUID = 1L; + + /** 预约调度ID */ + private Integer schedulingId; + /** 预约调度名称 */ + private String schedulingName; + /** 任务ID */ + private Integer jobId; + /** 项目ID */ + private Integer projectId; + /** 测试计划ID */ + private Integer planId; + /** 客户端ID */ + private Integer clientId; + /** 邮件通知地址 */ + private String emailAddress; + /** 第三方推送地址 */ + private String pushUrl; + /** 发送邮件通知时的具体逻辑, 0-全部,1-成功,-1-失败 */ + private Integer emailSendCondition; + /** jenkins构建链接 */ + private String buildingLink; + /** 远程执行Shell脚本 */ + private String remoteShell; + /** 客户端执行线程数 */ + private Integer exThreadCount; + /** 任务类型 0 接口 1 Web UI 2 移动 */ + private Integer taskType; + /** UI自动化浏览器类型 0 IE 1 火狐 2 谷歌 3 Edge */ + private Integer browserType; + /** 任务超时时间(分钟) */ + private Integer taskTimeout; + /** 客户端测试驱动桩路径 */ + private String clientDriverPath; + /** 关联项目实体 */ + private Project project; + /** 关联项目计划 */ + private ProjectPlan projectPlan; + /** 任务名称 */ + private String jobName; + /** cron执行表达式 */ + private String cronExpression; + /** 任务状态(0正常 1暂停) */ + private String status; + + public String getSchedulingName() { + return schedulingName; + } + + public void setSchedulingName(String schedulingName) { + this.schedulingName = schedulingName; + } + + public String getJobName() { + return jobName; + } + + public void setJobName(String jobName) { + this.jobName = jobName; + } + + public String getCronExpression() { + return cronExpression; + } + + public void setCronExpression(String cronExpression) { + this.cronExpression = cronExpression; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public ProjectPlan getProjectPlan() { + return projectPlan; + } + + public void setProjectPlan(ProjectPlan projectPlan) { + this.projectPlan = projectPlan; + } + + public Project getProject() { + return project; + } + + public void setProject(Project project) { + this.project = project; + } + + public void setSchedulingId(Integer schedulingId) + { + this.schedulingId = schedulingId; + } + + public Integer getSchedulingId() + { + return schedulingId; + } + public void setJobId(Integer jobId) + { + this.jobId = jobId; + } + + public Integer getJobId() + { + return jobId; + } + public void setProjectId(Integer projectId) + { + this.projectId = projectId; + } + + public Integer getProjectId() + { + return projectId; + } + public void setPlanId(Integer planId) + { + this.planId = planId; + } + + public Integer getPlanId() + { + return planId; + } + public void setClientId(Integer clientId) + { + this.clientId = clientId; + } + + public Integer getClientId() + { + return clientId; + } + public void setEmailAddress(String emailAddress) + { + this.emailAddress = emailAddress; + } + + public String getEmailAddress() + { + return emailAddress; + } + + public void setPushUrl(String pushUrl) + { + this.pushUrl = pushUrl; + } + public String getPushUrl() + { + return pushUrl; + } + + public void setEmailSendCondition(Integer emailSendCondition) + { + this.emailSendCondition = emailSendCondition; + } + + public Integer getEmailSendCondition() + { + return emailSendCondition; + } + public void setBuildingLink(String buildingLink) + { + this.buildingLink = buildingLink; + } + + public String getBuildingLink() + { + return buildingLink; + } + public void setRemoteShell(String remoteShell) + { + this.remoteShell = remoteShell; + } + + public String getRemoteShell() + { + return remoteShell; + } + public void setExThreadCount(Integer exThreadCount) + { + this.exThreadCount = exThreadCount; + } + + public Integer getExThreadCount() + { + return exThreadCount; + } + public void setTaskType(Integer taskType) + { + this.taskType = taskType; + } + + public Integer getTaskType() + { + return taskType; + } + public void setBrowserType(Integer browserType) + { + this.browserType = browserType; + } + + public Integer getBrowserType() + { + return browserType; + } + public void setTaskTimeout(Integer taskTimeout) + { + this.taskTimeout = taskTimeout; + } + + public Integer getTaskTimeout() + { + return taskTimeout; + } + public void setClientDriverPath(String clientDriverPath) + { + this.clientDriverPath = clientDriverPath; + } + + public String getClientDriverPath() + { + return clientDriverPath; + } + + @Override + public String toString() { + return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE) + .append("schedulingId", getSchedulingId()) + .append("jobId", getJobId()) + .append("projectId", getProjectId()) + .append("planId", getPlanId()) + .append("clientId", getClientId()) + .append("emailAddress", getEmailAddress()) + .append("pushUrl", getPushUrl()) + .append("emailSendCondition", getEmailSendCondition()) + .append("buildingLink", getBuildingLink()) + .append("remoteShell", getRemoteShell()) + .append("exThreadCount", getExThreadCount()) + .append("taskType", getTaskType()) + .append("browserType", getBrowserType()) + .append("taskTimeout", getTaskTimeout()) + .append("clientDriverPath", getClientDriverPath()) + .toString(); + } } \ No newline at end of file diff --git a/src/main/java/luckyclient/tool/mail/FreemarkerEmailTemplate.java b/src/main/java/luckyclient/tool/mail/FreemarkerEmailTemplate.java index 4608fc0..9bd7716 100644 --- a/src/main/java/luckyclient/tool/mail/FreemarkerEmailTemplate.java +++ b/src/main/java/luckyclient/tool/mail/FreemarkerEmailTemplate.java @@ -39,7 +39,7 @@ public class FreemarkerEmailTemplate { /** * 模板引擎配置 */ - public String getText(String templateId, Map parameters) { + public String getText(String templateId, Map parameters) { @SuppressWarnings("deprecation") Configuration configuration = new Configuration(); configuration.setTemplateLoader(new ClassTemplateLoader(FreemarkerEmailTemplate.class, TEMPLATE_PATH)); diff --git a/src/main/java/luckyclient/tool/mail/HtmlMail.java b/src/main/java/luckyclient/tool/mail/HtmlMail.java index afa6556..7cb34cd 100644 --- a/src/main/java/luckyclient/tool/mail/HtmlMail.java +++ b/src/main/java/luckyclient/tool/mail/HtmlMail.java @@ -1,9 +1,13 @@ package luckyclient.tool.mail; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; import java.util.HashMap; import java.util.Map; import java.util.Properties; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import luckyclient.remote.entity.ProjectProtocolTemplate; import luckyclient.utils.LogUtil; import luckyclient.utils.config.SysConfig; @@ -22,53 +26,25 @@ import luckyclient.utils.httputils.HttpClientTools; public class HtmlMail { static FreemarkerEmailTemplate fet = new FreemarkerEmailTemplate(); - public static String htmlContentFormat(int[] taskcount, String taskid, String buildstatus, String restartstatus, String time, String jobname) { - Map parameters = new HashMap<>(0); - parameters.put("buildstatus", buildstatus); - parameters.put("restartstatus", restartstatus); - parameters.put("casecount", taskcount[0]); - parameters.put("casesuc", taskcount[1]); - parameters.put("casefail", taskcount[2]); - parameters.put("caselock", taskcount[3]); - parameters.put("caseunex", taskcount[4]); + public static String htmlContentFormat(int[] taskCount, String taskId, String buildStatus, String restartStatus, String time, String jobName) { + Map parameters = new HashMap<>(0); + parameters.put("buildstatus", buildStatus); + parameters.put("restartstatus", restartStatus); + parameters.put("casecount", taskCount[0]); + parameters.put("casesuc", taskCount[1]); + parameters.put("casefail", taskCount[2]); + parameters.put("caselock", taskCount[3]); + parameters.put("caseunex", taskCount[4]); parameters.put("time", time); - parameters.put("taskid", taskid); - parameters.put("jobname", jobname); - try { - Map headmsg = new HashMap<>(0); - Properties properties = SysConfig.getConfiguration(); - if ("true".equals(properties.getProperty("task.push.switch").toLowerCase())) { - LogUtil.APP.info("开始向第三方平台推送任务执行情况..."); - Map pushparameters = new HashMap<>(0); - pushparameters.put("buildstatus", buildstatus); - pushparameters.put("restartstatus", restartstatus); - pushparameters.put("casecount", taskcount[0]); - pushparameters.put("casesuc", taskcount[1]); - pushparameters.put("casefail", taskcount[2]); - pushparameters.put("caselock", taskcount[3]); - pushparameters.put("caseunex", taskcount[4]); - pushparameters.put("time", time); - pushparameters.put("taskid", taskid); - pushparameters.put("jobname", jobname); + parameters.put("taskid", taskId); + parameters.put("jobname", jobName); - String pushurl = properties.getProperty("task.push.url"); - ProjectProtocolTemplate ppt=new ProjectProtocolTemplate(); - ppt.setEncoding("utf-8"); - ppt.setTimeout(60); - HttpClientTools hct = new HttpClientTools(); - hct.httpClientPostJson(pushurl, pushparameters, headmsg,ppt); - } - } catch (Exception e) { - LogUtil.APP.error("向第三方平台推送任务执行情况出现异常,请检查!", e); - return fet.getText("task-body", parameters); - } return fet.getText("task-body", parameters); } public static String htmlSubjectFormat(String jobname) { - Map parameters = new HashMap<>(0); + Map parameters = new HashMap<>(0); parameters.put("jobname", jobname); return fet.getText("task-title", parameters); } - } diff --git a/src/main/java/luckyclient/tool/mail/MailSendInitialization.java b/src/main/java/luckyclient/tool/mail/MailSendInitialization.java index bec90ae..e75f965 100644 --- a/src/main/java/luckyclient/tool/mail/MailSendInitialization.java +++ b/src/main/java/luckyclient/tool/mail/MailSendInitialization.java @@ -1,89 +1,150 @@ -package luckyclient.tool.mail; - -import java.util.Properties; - -import luckyclient.remote.api.serverOperation; -import luckyclient.remote.entity.TaskScheduling; -import luckyclient.utils.LogUtil; -import luckyclient.utils.config.SysConfig; - -/** - * ================================================================= - * 这是一个受限制的自由软件!您不能在任何未经允许的前提下对程序代码进行修改和用于商业用途;也不允许对程序代码修改后以任何形式任何目的的再发布。 - * 为了尊重作者的劳动成果,LuckyFrame关键版权信息严禁篡改 - * 有任何疑问欢迎联系作者讨论。 QQ:1573584944 seagull1985 - * ================================================================= - * - * @author: seagull - * @date 2018年3月1日 - */ -public class MailSendInitialization { - - public static void sendMailInitialization(String subject, String content, String taskid, TaskScheduling taskScheduling, int[] taskCount) { - boolean isSend = false; - if (null == taskCount) { - isSend = true; - } else { - if (taskCount.length == 5 && null != taskScheduling) { - Integer sendCondition = taskScheduling.getEmailSendCondition(); - // 用例全部成功了发送, casecount != casesuc - if (null!=sendCondition&&1 == sendCondition) { - if (taskCount[0] == taskCount[1]) { - isSend = true; - } - } - // 用例部分失败了发送 - if (null!=sendCondition&&2 == sendCondition) { - if (taskCount[2] > 0) { - isSend = true; - } - } - // 全发 - if (null!=sendCondition&&0 == sendCondition) { - isSend = true; - } - } - } - if (!isSend) { - LogUtil.APP.info("当前任务不需要发送邮件通知!"); - return; - } - String[] addresses = serverOperation.getEmailAddress(taskid); - Properties properties = SysConfig.getConfiguration(); - if (addresses != null) { - LogUtil.APP.info("准备将测试结果发送邮件通知!请稍等..."); - //这个类主要是设置邮件 - MailSenderInfo mailInfo = new MailSenderInfo(); - //这个类主要来发送邮件 - SimpleMailSender sms = new SimpleMailSender(); - mailInfo.setMailServerHost(properties.getProperty("mail.smtp.ip")); - mailInfo.setMailServerPort(properties.getProperty("mail.smtp.port")); - mailInfo.setSslenable(properties.getProperty("mail.smtp.ssl.enable").equals("true")); - mailInfo.setValidate(true); - mailInfo.setUserName(properties.getProperty("mail.smtp.username")); - //您的邮箱密码 - mailInfo.setPassword(properties.getProperty("mail.smtp.password")); - mailInfo.setFromAddress(properties.getProperty("mail.smtp.username")); - //标题 - mailInfo.setSubject(subject); - //内容 - mailInfo.setContent(content); - mailInfo.setToAddresses(addresses); - //sms.sendHtmlMail(mailInfo); - - StringBuilder stringBuilder = new StringBuilder(); - for (String address : addresses) { - stringBuilder.append(address).append(";"); - } - String addressesmail = stringBuilder.toString(); - if (sms.sendHtmlMail(mailInfo)) { - LogUtil.APP.info("给{}的测试结果通知邮件发送完成!",addressesmail); - } else { - LogUtil.APP.warn("给{}的测试结果通知邮件发送失败!",addressesmail); - } - } else { - LogUtil.APP.info("当前任务不需要发送邮件通知!"); - } - } - -} +package luckyclient.tool.mail; + +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +import cn.hutool.core.util.StrUtil; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import luckyclient.remote.api.serverOperation; +import luckyclient.remote.entity.ProjectProtocolTemplate; +import luckyclient.remote.entity.TaskScheduling; +import luckyclient.utils.LogUtil; +import luckyclient.utils.config.SysConfig; +import luckyclient.utils.httputils.HttpClientTools; + +/** + * ================================================================= + * 这是一个受限制的自由软件!您不能在任何未经允许的前提下对程序代码进行修改和用于商业用途;也不允许对程序代码修改后以任何形式任何目的的再发布。 + * 为了尊重作者的劳动成果,LuckyFrame关键版权信息严禁篡改 + * 有任何疑问欢迎联系作者讨论。 QQ:1573584944 seagull1985 + * ================================================================= + * + * @author: seagull + * @date 2018年3月1日 + */ +public class MailSendInitialization { + + public static void sendMailInitialization(String subject, String content, String taskid, TaskScheduling taskScheduling, int[] taskCount, String time, String buildStatus, String restartStatus) { + boolean isSend = false; + if (null == taskCount) { + isSend = true; + } else { + if (taskCount.length == 5 && null != taskScheduling) { + Integer sendCondition = taskScheduling.getEmailSendCondition(); + // 用例全部成功了发送, casecount != casesuc + if (null != sendCondition && 1 == sendCondition) { + if (taskCount[0] == taskCount[1]) { + isSend = true; + } + } + // 用例部分失败了发送 + if (null != sendCondition && 2 == sendCondition) { + if (taskCount[2] > 0) { + isSend = true; + } + } + // 全发 + if (null != sendCondition && 0 == sendCondition) { + isSend = true; + } + } + } + if (!isSend) { + LogUtil.APP.info("当前任务不需要发送邮件或是推送通知!"); + return; + } + //向第三方推送消息 + MailSendInitialization msi= new MailSendInitialization(); + msi.pushMessage(taskScheduling, content, taskCount, time, buildStatus, restartStatus); + + String[] addresses = serverOperation.getEmailAddress(taskScheduling,taskid); + Properties properties = SysConfig.getConfiguration(); + if (addresses != null) { + LogUtil.APP.info("准备将测试结果发送邮件通知!请稍等..."); + //这个类主要是设置邮件 + MailSenderInfo mailInfo = new MailSenderInfo(); + //这个类主要来发送邮件 + SimpleMailSender sms = new SimpleMailSender(); + mailInfo.setMailServerHost(properties.getProperty("mail.smtp.ip")); + mailInfo.setMailServerPort(properties.getProperty("mail.smtp.port")); + mailInfo.setSslenable(properties.getProperty("mail.smtp.ssl.enable").equals("true")); + mailInfo.setValidate(true); + mailInfo.setUserName(properties.getProperty("mail.smtp.username")); + //您的邮箱密码 + mailInfo.setPassword(properties.getProperty("mail.smtp.password")); + mailInfo.setFromAddress(properties.getProperty("mail.smtp.username")); + //标题 + mailInfo.setSubject(subject); + //内容 + mailInfo.setContent(content); + mailInfo.setToAddresses(addresses); + //sms.sendHtmlMail(mailInfo); + + StringBuilder stringBuilder = new StringBuilder(); + for (String address : addresses) { + stringBuilder.append(address).append(";"); + } + String addressesmail = stringBuilder.toString(); + if (sms.sendHtmlMail(mailInfo)) { + LogUtil.APP.info("给{}的测试结果通知邮件发送完成!", addressesmail); + } else { + LogUtil.APP.warn("给{}的测试结果通知邮件发送失败!", addressesmail); + } + } else { + LogUtil.APP.info("当前任务不需要发送邮件通知!"); + } + } + + private void pushMessage(TaskScheduling taskScheduling, String content, int[] taskCount, String time, String buildStatus, String restartStatus) { + try { + Map headmsg = new HashMap<>(0); + Properties properties = SysConfig.getConfiguration(); + LogUtil.APP.info("准备初始化第三方消息推送的数据..."); + + String pushUrl = taskScheduling.getPushUrl(); + + if(StrUtil.isNotBlank(pushUrl)){ + String ip = properties.getProperty("server.web.ip"); + String port = properties.getProperty("server.web.port"); + + ProjectProtocolTemplate ppt = new ProjectProtocolTemplate(); + ppt.setEncoding("utf-8"); + ppt.setTimeout(60); + ppt.setIsResponseHead(1); + ppt.setIsResponseCode(1); + HttpClientTools hct = new HttpClientTools(); + + Map parameters = new HashMap<>(0); + if(null != taskCount){ + content = "LuckyFrame自动化测试任务【" + taskScheduling.getSchedulingName() + "】执行结果\n" + + "自动构建状态:【" + buildStatus + "】\n" + + "自动重启TOMCAT状态:【" + restartStatus + "】\n" + + "本次任务预期执行用例共【" + taskCount[0] + "】条,耗時【" + time + "】\n" + + "用例执行成功:【" + taskCount[1] + "】\n" + + "用例执行失败:【" + taskCount[2] + "】\n" + + "用例有可能由于脚本原因未成功解析被锁定:【" + taskCount[3] + "】\n" + + "用例由于长时间未收到接口Response未执行完成:【" + taskCount[4] + "】\n" + + "详情请前往自动化测试平台查看!http://" + ip + ":" + port; + } + JSONObject contentJson = JSON.parseObject("{\"content\": \"" + content + "\"}"); + + JSONObject atJson = JSON.parseObject("{\"atMobiles\": [],\"isAtAll\":true}"); + + parameters.put("msgtype", "text"); + parameters.put("text", contentJson); + parameters.put("at", atJson); + LogUtil.APP.info("开始向第三方平台推送任务执行情况..."); + hct.httpClientPostJson(pushUrl, parameters, headmsg, ppt); + LogUtil.APP.info("向第三方平台推送任务执行数据成功..."); + }else{ + LogUtil.APP.warn("推送地址配置为空,取消第三方推送..."); + } + + } catch (Exception e) { + LogUtil.APP.error("向第三方平台推送任务执行情况出现异常,请检查!", e); + } + } + +} diff --git a/src/main/java/luckyclient/utils/httputils/HttpClientTools.java b/src/main/java/luckyclient/utils/httputils/HttpClientTools.java index f2e13a1..8d6701f 100644 --- a/src/main/java/luckyclient/utils/httputils/HttpClientTools.java +++ b/src/main/java/luckyclient/utils/httputils/HttpClientTools.java @@ -1,5 +1,6 @@ package luckyclient.utils.httputils; +import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import luckyclient.remote.entity.ProjectProtocolTemplate; @@ -1745,7 +1746,7 @@ public class HttpClientTools { } else if (urlParam.startsWith("https://")) { //采用绕过验证的方式处理https请求 SSLContext sslContext; - if (null == cerpath || "".equals(cerpath.trim())) { + if (StrUtil.isBlank(cerpath)) { LogUtil.APP.info("开始构建HTTPS单向认证请求..."); TrustManager[] trustManagers = {new MyX509TrustManager()}; sslContext = SSLContext.getInstance("TLS");