package edu.nju.controller; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.http.HttpServletResponse; import edu.nju.entities.*; import edu.nju.model.PageExamVO; import edu.nju.model.enums.CollaborativeType; import lombok.extern.slf4j.Slf4j; import org.json.JSONArray; import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import edu.nju.service.ExtraService; import org.springframework.web.multipart.MultipartFile; import java.net.URL; import java.io.BufferedInputStream; import java.io.FileOutputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URLConnection; /** * 上传用例报告相关接口/extra */ @Slf4j @Controller @RequestMapping(value = "/extra") @CrossOrigin(origins = "*", maxAge = 3600, allowCredentials = "true") public class ExtraController { @Autowired ExtraService extraService; /** * 上传测试报告 /uploadReport 返回测试报告id * @param case_id 用例id * @param task_id 任务id * @param case_take_id 用例-任务id * @param worker_id 人员id * @param name 报告名称 * @param device_model 设备品牌 * @param device_brand 设备名称 * @param device_os 操作系统 * @param script_location * @param report_location * @param log_location * @param response */ @RequestMapping(value = "/uploadReport", method = RequestMethod.POST) @ResponseBody public void uploadReport(@RequestParam String case_id, @RequestParam String task_id, @RequestParam String case_take_id, @RequestParam String worker_id, @RequestParam String name, @RequestParam String device_model, @RequestParam String device_brand, @RequestParam String device_os, String script_location, String report_location, String log_location, HttpServletResponse response) { try { String id = extraService.saveReport(case_id, task_id, case_take_id, worker_id, name, device_model, device_brand, device_os, script_location, report_location, log_location); PrintWriter out = response.getWriter(); JSONObject result = new JSONObject(); result.put("id", id); out.print(result); out.flush(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 更新测试报告 /updateReport 只返回200或500 * @param report_id 报告id * @param case_id 用例id * @param task_id 任务id * @param case_take_id 用例-任务id * @param worker_id 人员id * @param name 报告名称 * @param device_model 设备品牌 * @param device_brand 设备名称 * @param device_os 操作系统 * @param script_location * @param report_location * @param log_location * @param response */ @RequestMapping(value = "/updateReport", method = RequestMethod.POST) @ResponseBody public void updateReport(String report_id, String case_id, String task_id, String case_take_id, String worker_id, String name, String device_model, String device_brand, String device_os, String script_location, String report_location, String log_location, HttpServletResponse response) { try { boolean bool = extraService.updateReport(report_id, case_id, task_id, case_take_id, worker_id, name, device_model, device_brand, device_os, script_location, report_location, log_location); PrintWriter out = response.getWriter(); JSONObject result = new JSONObject(); if(bool) { result.put("status", 200); } else { result.put("status", 500); } out.print(result); out.flush(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 获取测试报告 /getReport * @param report_id 报告id * @param response */ @RequestMapping(value = "/getReport") @ResponseBody public void getReport(String report_id, HttpServletResponse response) { try { PrintWriter out = response.getWriter(); JSONObject result = new JSONObject(); Report report = extraService.getReport(report_id); if(report != null) { result.put("status", 200); result.put("result", new JSONObject(report)); } else { result.put("status", 500); } out.print(result); out.flush(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //获得一场众测三级页面信息 @RequestMapping(value = "/getPageVo") @ResponseBody public PageExamVO findPage(Long examId, Long caseId) { return extraService.findPageAndExam(examId,caseId); } /** * /findByWorker 使用case_take_id 和 worker_id获取测试报告信息 * @param case_take_id * @param worker_id * @param response */ @GetMapping(value = "/findByWorker") @ResponseBody public void findByWorker(String case_take_id, String worker_id, HttpServletResponse response) { try { PrintWriter out = response.getWriter(); JSONObject result = new JSONObject(); Report report = extraService.findByWorker(case_take_id, worker_id); if(report != null) { result.put("status", 200); result.put("result", new JSONObject(report)); } else { result.put("status", 500); } out.print(result); out.flush(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 获取测试用例列表 /getCaseList 返回报告下的所有用例信息 * @param report_id 报告id * @param response */ @GetMapping(value = "/getCaseList") @ResponseBody public void getCaseList(String report_id, HttpServletResponse response) { try { PrintWriter out = response.getWriter(); JSONObject result = new JSONObject(); List caseList = extraService.getCaseList(report_id); if(caseList != null) { result.put("status", 200); result.put("result", new JSONArray(caseList)); } else { result.put("status", 500); } out.print(result); out.flush(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 获取测试Bug列表 /getBugList 返回报告下的所有bug * @param report_id 报告id * @param case_take_id 用例-任务id * @param response */ @GetMapping(value = "/getBugList") @ResponseBody public void getBugList(String report_id, String case_take_id, HttpServletResponse response) { try { PrintWriter out = response.getWriter(); JSONObject result = new JSONObject(); List bugList = extraService.getBugList(report_id, case_take_id); if(bugList != null && bugList.size() > 0) { result.put("status", 200); result.put("result", new JSONArray(bugList)); } else { result.put("status", 500); } out.print(result); out.flush(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 47.99.140.117:9001/Bug/api/extra/uploadExam * * @param file * 文件流 * @param case_id * 测试目标id * @param file_name * 上传excel的文件名 * @param paper_type * 试卷类型,即试卷的显示矩阵 * @param test_type * 测试分类,如工具 * @param description * 测试说明 * @param app_name * 测试目标的名称 * @return JSONArray */ @RequestMapping(value = "/uploadExam", method = RequestMethod.POST) @ResponseBody public String uploadExam(@RequestParam("file") MultipartFile file, String file_name, String paper_type, String case_id, String test_type, String description, String app_name) { try { File dest = new File("/Users/guochao/Downloads/" + file_name); if(!dest.getParentFile().exists()) { dest.getParentFile().mkdirs(); } if(!file.isEmpty()) { file.transferTo(dest); } String json = extraService.saveExam(case_id, dest.getPath(), app_name, paper_type, test_type, description, CollaborativeType.IS_COLLABORATIVE.getId()+""); return json; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return ""; } } /** * 创建任务 * @param file 文件oss的url * @param file_name * @param paper_type * @param case_id * @param test_type * @param description * @param app_name * @param collaborative_type * @return */ @RequestMapping(value = "/uploadExamUrl", method = RequestMethod.POST) @ResponseBody public String uploadExamUrl(String file, String file_name, String paper_type, String case_id, String test_type, String description, String app_name, @PathVariable(required = false) String collaborative_type) { try { System.out.println("file " + file); System.out.println("file_name " + file_name); File dest = new File("/Users/guochao/Downloads/" + file_name); if(!dest.getParentFile().exists()) { dest.getParentFile().mkdirs(); } //从oss下载文件 // 统一资源 URL url = new URL(file); // 连接类的父类,抽象类 URLConnection urlConnection = url.openConnection(); // http的连接类 HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection; //设置超时 httpURLConnection.setConnectTimeout(1000*5); //设置请求方式,默认是GET // httpURLConnection.setRequestMethod("GET"); // 设置字符编码 httpURLConnection.setRequestProperty("Charset", "UTF-8"); // 打开到此 URL引用的资源的通信链接(如果尚未建立这样的连接)。 httpURLConnection.connect(); // 文件大小 int fileLength = httpURLConnection.getContentLength(); // 建立链接从请求中获取数据 URLConnection con = url.openConnection(); BufferedInputStream bin = new BufferedInputStream(httpURLConnection.getInputStream()); // 指定文件名称(有需求可以自定义) // 指定存放位置(有需求可以自定义) OutputStream out = new FileOutputStream(dest); int size = 0; int len = 0; byte[] buf = new byte[2048]; while ((size = bin.read(buf)) != -1) { len += size; out.write(buf, 0, size); } // 关闭资源 bin.close(); out.close(); if(collaborative_type == null || collaborative_type == ""){ collaborative_type = CollaborativeType.IS_COLLABORATIVE.getId() + ""; } System.out.println(dest.getPath()); String json = extraService.saveExam(case_id, dest.getPath(), app_name, paper_type, test_type, description, collaborative_type); return json; } catch (IOException e) { e.printStackTrace(); return ""; } } // @RequestMapping(value = "/uploadExamUrl", method = RequestMethod.POST) // @ResponseBody // public String uploadExamUrl1(String file, String file_name, String paper_type, // String case_id, String test_type, String description, String app_name, // String if_test_case,String if_bug) { // try { // // File dest = new File("/Users/guochao/Downloads/" + file_name); // if(!dest.getParentFile().exists()) { dest.getParentFile().mkdirs(); } // //从oss下载文件 // // 统一资源 // URL url = new URL(file); // // 连接类的父类,抽象类 // URLConnection urlConnection = url.openConnection(); // // http的连接类 // HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection; // //设置超时 // httpURLConnection.setConnectTimeout(1000*5); // //设置请求方式,默认是GET //// httpURLConnection.setRequestMethod("GET"); // // 设置字符编码 // httpURLConnection.setRequestProperty("Charset", "UTF-8"); // // 打开到此 URL引用的资源的通信链接(如果尚未建立这样的连接)。 // httpURLConnection.connect(); // // 文件大小 // int fileLength = httpURLConnection.getContentLength(); // // // 建立链接从请求中获取数据 // URLConnection con = url.openConnection(); // BufferedInputStream bin = new BufferedInputStream(httpURLConnection.getInputStream()); // // 指定文件名称(有需求可以自定义) // // 指定存放位置(有需求可以自定义) // // // OutputStream out = new FileOutputStream(dest); // int size = 0; // int len = 0; // byte[] buf = new byte[2048]; // while ((size = bin.read(buf)) != -1) { // len += size; // out.write(buf, 0, size); // } // // 关闭资源 // bin.close(); // out.close(); // String json = extraService.saveExam1(case_id, dest.getPath(), app_name, paper_type, test_type, description,if_test_case,if_bug); // return json; // } catch (IOException e) { // e.printStackTrace(); // return ""; // } // } /** * 47.99.140.117:9001/Bug/api/extra/getExamList * * @return 200 成功; 500 失败 */ @RequestMapping(value = "/getExamList", method = RequestMethod.GET) @ResponseBody public void getExamList(HttpServletResponse response) { try { PrintWriter out = response.getWriter(); JSONObject result = new JSONObject(); result.put("data", extraService.getExamList()); result.put("status", 200); out.print(result); out.flush(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * /getExam 获取考试的操作类型信息 * @param id 考试id * @param response */ @GetMapping(value = "/getExam") @ResponseBody public void getExam(String id, HttpServletResponse response) { try { PrintWriter out = response.getWriter(); JSONObject result = new JSONObject(); Exam exam = extraService.getExam(id); if(exam != null) { result.put("status", 200); result.put("result", new JSONObject(exam)); } else { result.put("status", 500); } out.print(result); out.flush(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 上传测试用例 /uploadTestCase * @param report_id 报告id * @param name 用例名称 * @param front 前置条件 * @param behind 测试步骤 * @param description 预期结果 * @param if_execute 是否执行 * @param if_bug 是否是bug * @param response */ @RequestMapping(value = "/uploadTestCase", method = RequestMethod.POST) @ResponseBody public void uploadTestCase(String report_id, String name, String front, String behind, String description,String if_execute,String if_bug, HttpServletResponse response) { try { System.out.println(if_execute); System.out.println(if_bug); String id = extraService.saveTestCase(report_id, name, front, behind, description); PrintWriter out = response.getWriter(); JSONObject result = new JSONObject(); result.put("id", id); out.print(result); out.flush(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 获取指定用例id的用例信息 /getTestCase * @param id * @param response */ @GetMapping(value = "/getTestCase") @ResponseBody public void getTestCase(String id, HttpServletResponse response) { try { PrintWriter out = response.getWriter(); JSONObject result = new JSONObject(); TestCase testCase = extraService.getTestCase(id); if(testCase != null) { result.put("status", 200); result.put("result", new JSONObject(testCase)); } else { result.put("status", 500); } out.print(result); out.flush(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 更新测试用例 /updateTestCase 返回200或500 * @param id 用例id * @param report_id 报告id * @param name 用例名称 * @param front 前置条件 * @param behind 测试步骤 * @param description 预期结果 * @param response */ @RequestMapping(value = "/updateTestCase", method = RequestMethod.POST) @ResponseBody public void updateTestCase(String id, String report_id, String name, String front, String behind, String description, HttpServletResponse response) { try { PrintWriter out = response.getWriter(); JSONObject result = new JSONObject(); if(extraService.updateTestCase(id, report_id, name, front, behind, description)) { result.put("status", 200); } else { result.put("status", 500); } out.print(result); out.flush(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @RequestMapping(value = "/updateTask", method = RequestMethod.POST) @ResponseBody public void updateTask(String id,double writeMins, HttpServletResponse response) { try { PrintWriter out = response.getWriter(); JSONObject result = new JSONObject(); if(extraService.updateTask(id, writeMins)) { result.put("status", 200); } else { result.put("status", 500); } out.print(result); out.flush(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 获取任务相关信息 * @param id 任务ID * @param response */ @RequestMapping(value = "/getTask") @ResponseBody public void getTask(String id, HttpServletResponse response) { try { PrintWriter out = response.getWriter(); JSONObject result = new JSONObject(); Task task=extraService.saveTask(id); if(task!=null) { result.put("status", 200); result.put("result",new JSONObject(task)); } else { result.put("status", 500); } out.print(result); out.flush(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @RequestMapping(value = "/reportsToReviewPaper") @ResponseBody public String getTask(String case_take_id) { String uploadUrl = extraService.reportsToReviewPaper(case_take_id); return uploadUrl; } @RequestMapping(value = "/pageUrl") @ResponseBody public void getPageUrl(String caseId,HttpServletResponse response){ try { PrintWriter out = response.getWriter(); JSONObject result = new JSONObject(); result.put("status", 200); result.put("result",extraService.getPageUrl(caseId)); out.print(result); out.flush(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }