提交 c63bc857 authored 作者: 黄承天's avatar 黄承天

增加测试报告下载接口

上级 e09e9cc8
......@@ -26,6 +26,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
......
......@@ -3,15 +3,21 @@ package com.zjty.automatedtesting.controller;
import com.google.common.collect.ImmutableMap;
import com.zjty.automatedtesting.pojo.report.ReportVo;
import com.zjty.automatedtesting.service.ReportService;
import com.zjty.automatedtesting.util.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
@Slf4j
@SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
@RequestMapping("/report")
@RestController
......@@ -23,28 +29,45 @@ public class ReportController {
ReportService reportService;
@ApiOperation(value = "获取全部测试报告.")
@GetMapping(value="/get")
public ResponseEntity<List<ReportVo>> get(){
@GetMapping(value = "/get")
public ResponseEntity<List<ReportVo>> get() {
return ResponseEntity.ok(reportService.findAll());
}
@ApiOperation(value = "按分页获取测试报告.页码从0开始.")
@GetMapping(value="/get/page/{page}")
public ResponseEntity<List<ReportVo>> get(@PathVariable Integer page){
@GetMapping(value = "/get/page/{page}")
public ResponseEntity<List<ReportVo>> get(@PathVariable Integer page) {
return ResponseEntity.ok(reportService.findByPage(page));
}
@ApiOperation(value = "获取单个测试报告.")
@GetMapping(value="/get/{id}")
public ResponseEntity<ReportVo> getById(@PathVariable Integer id){
@GetMapping(value = "/get/{id}")
public ResponseEntity<ReportVo> getById(@PathVariable Integer id) {
return ResponseEntity.ok(reportService.findById(id));
}
@ApiOperation(value = "删除测试报告.")
@DeleteMapping(value="/delete/{id}")
public ResponseEntity<Map<String,String>> findTestText(@PathVariable Integer id){
@DeleteMapping(value = "/delete/{id}")
public ResponseEntity<Map<String, String>> delete(@PathVariable Integer id) {
reportService.delete(id);
return ResponseEntity.ok(ImmutableMap.of("message","success"));
return ResponseEntity.ok(ImmutableMap.of("message", "success"));
}
@ApiOperation(value = "下载测试报告.")
@GetMapping(value = "/download/{id}")
public ResponseEntity<Map<String, String>> download(@PathVariable Integer id, HttpServletResponse response) {
try {
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream");
String filename = "report-" + id.toString() + ".html";
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
OutputStream os = response.getOutputStream();
String reportHtmlContent = FileUtil.getRepotHtmlContent(reportService.findById(id));
FileUtil.output(reportHtmlContent, os);
} catch (Exception e) {
log.error("error:" + e);
return ResponseEntity.ok(ImmutableMap.of("message", "error:" + e.getMessage()));
}
return ResponseEntity.ok(null);
}
}
package com.zjty.automatedtesting.service;
import com.alibaba.fastjson.JSON;
import com.zjty.automatedtesting.pojo.report.Measure;
import com.zjty.automatedtesting.pojo.report.Report;
import com.zjty.automatedtesting.pojo.report.ReportVo;
......@@ -48,7 +49,7 @@ public class TransHelper {
}
public ReportVo toReportVo(Report report){
List<Measure> measures = JsonUtil.readValueToList(report.getMeasures(),Measure.class);
List<Measure> measures = JSON.parseArray(report.getMeasures(), Measure.class);
return new ReportVo(
report.getId(),
report.getTitle(),
......
package com.zjty.automatedtesting.util;
import com.zjty.automatedtesting.pojo.report.Measure;
import com.zjty.automatedtesting.pojo.report.Report;
import com.zjty.automatedtesting.pojo.report.ReportVo;
import com.zjty.automatedtesting.pojo.test.Case;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
......@@ -13,15 +18,145 @@ import java.util.List;
* <p>Date : 2020/1/16 15:27
* <p>@author : C
*/
@Slf4j
public class FileUtil {
private final static String WORK_PATH = System.getProperty("user.dir") + "\\";
private Case parseFilToTestCase(String fileName) throws IOException {
private final static String REPORT_HTML_TEMP_FIRST_PART = "<html>\n" +
"<head>\n" +
"<style>\n" +
".table15_12 table {\n" +
"\twidth:100%;\n" +
"\tmargin:15px 0;\n" +
"\tborder:0;\n" +
"}\n" +
".table15_12 th {\n" +
"\tfont-weight:bold;\n" +
"\tbackground-color:#d8f1fe;\n" +
"\tcolor:#20a0fe\n" +
"}\n" +
".table15_12,.table15_12 th,.table15_12 td {\n" +
"\tfont-size:0.95em;\n" +
"\ttext-align:center;\n" +
"\tpadding:4px;\n" +
"\tborder-collapse:collapse;\n" +
"}\n" +
".table15_12 th {\n" +
"\tborder: 1px solid #d8f1fe;\n" +
"\tborder-width:1px\n" +
"}\n" +
".table15_12 td {\n" +
"\tborder: 1px solid #d8f1fe;\n" +
"\tborder-width:1px\n" +
"}\n" +
".table15_12 tr {\n" +
"\tborder: 1px solid #ffffff;\n" +
"}\n" +
".table15_12 tr:nth-child(odd){\n" +
"\tbackground-color:#f7f7f7;\n" +
"}\n" +
".table15_12 tr:nth-child(even){\n" +
"\tbackground-color:#ffffff;\n" +
"}\n" +
"</style>\n" +
"</head>\n" +
"<body>\n" +
"<div>ID:#{id}</div>\n" +
"<div>标题:#{title}</div>\n" +
"<div>浏览器:#{browser}</div>\n" +
"<div>地址:#{url}</div>\n" +
"\n" +
"<br/>\n" +
"<div>测试步骤详情</div>\n" +
"<table class=table15_12>\n" +
"<tr>\n" +
"\t<th>编号</th>\n" +
"\t<th>步骤名</th>\n" +
"\t<th>预期值</th>\n" +
"\t<th>实际值</th>\n" +
"\t<th>结果</th>\n" +
"\t<th>相关信息</th>\n" +
"</tr>\n";
private final static String REPORT_HTML_TEMP_LAST_PART = "\n" +
"</table>\n" +
"\n" +
"</body>\n" +
"\n" +
"</html>";
private Case parseFileToTestCase(String fileName) throws IOException {
List<String> lines = Files.readAllLines(Paths.get(WORK_PATH + fileName));
StringBuilder builder = new StringBuilder();
lines.forEach(builder::append);
String data = builder.toString();
return JsonUtil.readValue(data, Case.class);
}
public static String getRepotHtmlContent(ReportVo reportVo) {
StringBuilder content = new StringBuilder();
String first_part = REPORT_HTML_TEMP_FIRST_PART
.replace("#{id}", reportVo.getId().toString())
.replace("#{title}", reportVo.getTitle())
.replace("#{browser}", reportVo.getBrowser())
.replace("#{url}", reportVo.getUrl());
content.append(first_part);
String rowTemp = "<tr>\n" +
"\t<td>#{order}</td>\n" +
"\t<td>#{title}</td>\n" +
"\t<td>#{expected}</td>\n" +
"\t<td>#{practice}</td>\n" +
"\t<td>#{success}</td>\n" +
"\t<td>#{message}</td>\n" +
"</tr>\n";
List<Measure> measures = reportVo.getMeasures();
for (Measure measure : measures) {
String row = rowTemp
.replace("#{order}", measure.getOrder().toString())
.replace("#{title}", measure.getTitle())
.replace("#{expected}", measure.getExpected())
.replace("#{practice}", measure.getPractice())
.replace("#{success}", measure.getSuccess() ? "成功" : "失败")
.replace("#{message}", measure.getMessage());
content.append(row);
}
content.append(REPORT_HTML_TEMP_LAST_PART);
return content.toString();
}
public static void output(String text, OutputStream os) {
byte[] buffer = new byte[1024];
InputStream is = null;
BufferedInputStream bis = null;
try {
is = new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8));
bis = new BufferedInputStream(is);
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
} catch (Exception e) {
log.error("error:{}", e.getMessage());
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
......@@ -2,6 +2,7 @@ package com.zjty.automatedtesting.util;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import java.util.Arrays;
import java.util.Collections;
......@@ -41,9 +42,9 @@ public final class JsonUtil {
public static <T> List<T> readValueToList(String jsonStr, Class<T> valueType) {
T[] arrays = readValue(jsonStr, new TypeReference<T[]>() {
});
List<T> list = Collections.emptyList();
List<T> list = Lists.newArrayList();
if (arrays != null) {
list = Arrays.asList(arrays);
list = Lists.newArrayList(arrays);
}
return list;
}
......
#server.address=0.0.0.0
server.address=0.0.0.0
server.port=13500
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/automated_testing?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
spring.datasource.url=jdbc:mysql://localhost:3306/automated_testing?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论