ExtraController.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. package edu.nju.controller;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.util.List;
  6. import javax.servlet.http.HttpServletResponse;
  7. import edu.nju.entities.*;
  8. import edu.nju.model.PageExamVO;
  9. import edu.nju.model.enums.CollaborativeType;
  10. import lombok.extern.slf4j.Slf4j;
  11. import org.json.JSONArray;
  12. import org.json.JSONObject;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.stereotype.Controller;
  15. import org.springframework.web.bind.annotation.*;
  16. import edu.nju.service.ExtraService;
  17. import org.springframework.web.multipart.MultipartFile;
  18. import java.net.URL;
  19. import java.io.BufferedInputStream;
  20. import java.io.FileOutputStream;
  21. import java.io.OutputStream;
  22. import java.net.HttpURLConnection;
  23. import java.net.URLConnection;
  24. /**
  25. * 上传用例报告相关接口/extra
  26. */
  27. @Slf4j
  28. @Controller
  29. @RequestMapping(value = "/extra")
  30. @CrossOrigin(origins = "*", maxAge = 3600, allowCredentials = "true")
  31. public class ExtraController {
  32. @Autowired
  33. ExtraService extraService;
  34. /**
  35. * 上传测试报告 /uploadReport 返回测试报告id
  36. * @param case_id 用例id
  37. * @param task_id 任务id
  38. * @param case_take_id 用例-任务id
  39. * @param worker_id 人员id
  40. * @param name 报告名称
  41. * @param device_model 设备品牌
  42. * @param device_brand 设备名称
  43. * @param device_os 操作系统
  44. * @param script_location
  45. * @param report_location
  46. * @param log_location
  47. * @param response
  48. */
  49. @RequestMapping(value = "/uploadReport", method = RequestMethod.POST)
  50. @ResponseBody
  51. public void uploadReport(@RequestParam String case_id, @RequestParam String task_id, @RequestParam String case_take_id, @RequestParam String worker_id,
  52. @RequestParam String name, @RequestParam String device_model, @RequestParam String device_brand, @RequestParam String device_os,
  53. String script_location, String report_location, String log_location, HttpServletResponse response) {
  54. try {
  55. 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);
  56. PrintWriter out = response.getWriter();
  57. JSONObject result = new JSONObject();
  58. result.put("id", id);
  59. out.print(result);
  60. out.flush();
  61. out.close();
  62. } catch (IOException e) {
  63. // TODO Auto-generated catch block
  64. e.printStackTrace();
  65. }
  66. }
  67. /**
  68. * 更新测试报告 /updateReport 只返回200或500
  69. * @param report_id 报告id
  70. * @param case_id 用例id
  71. * @param task_id 任务id
  72. * @param case_take_id 用例-任务id
  73. * @param worker_id 人员id
  74. * @param name 报告名称
  75. * @param device_model 设备品牌
  76. * @param device_brand 设备名称
  77. * @param device_os 操作系统
  78. * @param script_location
  79. * @param report_location
  80. * @param log_location
  81. * @param response
  82. */
  83. @RequestMapping(value = "/updateReport", method = RequestMethod.POST)
  84. @ResponseBody
  85. public void updateReport(String report_id, String case_id, String task_id, String case_take_id, String worker_id, String name, String device_model,
  86. String device_brand, String device_os, String script_location, String report_location,
  87. String log_location, HttpServletResponse response) {
  88. try {
  89. boolean bool = extraService.updateReport(report_id, case_id, task_id, case_take_id, worker_id, name,
  90. device_model, device_brand, device_os, script_location, report_location, log_location);
  91. PrintWriter out = response.getWriter();
  92. JSONObject result = new JSONObject();
  93. if(bool) { result.put("status", 200); }
  94. else { result.put("status", 500); }
  95. out.print(result);
  96. out.flush();
  97. out.close();
  98. } catch (IOException e) {
  99. // TODO Auto-generated catch block
  100. e.printStackTrace();
  101. }
  102. }
  103. /**
  104. * 获取测试报告 /getReport
  105. * @param report_id 报告id
  106. * @param response
  107. */
  108. @RequestMapping(value = "/getReport")
  109. @ResponseBody
  110. public void getReport(String report_id, HttpServletResponse response) {
  111. try {
  112. PrintWriter out = response.getWriter();
  113. JSONObject result = new JSONObject();
  114. Report report = extraService.getReport(report_id);
  115. if(report != null) {
  116. result.put("status", 200);
  117. result.put("result", new JSONObject(report));
  118. }
  119. else { result.put("status", 500); }
  120. out.print(result);
  121. out.flush();
  122. out.close();
  123. } catch (IOException e) {
  124. // TODO Auto-generated catch block
  125. e.printStackTrace();
  126. }
  127. }
  128. //获得一场众测三级页面信息
  129. @RequestMapping(value = "/getPageVo")
  130. @ResponseBody
  131. public PageExamVO findPage(Long examId, Long caseId) {
  132. return extraService.findPageAndExam(examId,caseId);
  133. }
  134. /**
  135. * /findByWorker 使用case_take_id 和 worker_id获取测试报告信息
  136. * @param case_take_id
  137. * @param worker_id
  138. * @param response
  139. */
  140. @GetMapping(value = "/findByWorker")
  141. @ResponseBody
  142. public void findByWorker(String case_take_id, String worker_id, HttpServletResponse response) {
  143. try {
  144. PrintWriter out = response.getWriter();
  145. JSONObject result = new JSONObject();
  146. Report report = extraService.findByWorker(case_take_id, worker_id);
  147. if(report != null) {
  148. result.put("status", 200);
  149. result.put("result", new JSONObject(report));
  150. }
  151. else { result.put("status", 500); }
  152. out.print(result);
  153. out.flush();
  154. out.close();
  155. } catch (IOException e) {
  156. // TODO Auto-generated catch block
  157. e.printStackTrace();
  158. }
  159. }
  160. /**
  161. * 获取测试用例列表 /getCaseList 返回报告下的所有用例信息
  162. * @param report_id 报告id
  163. * @param response
  164. */
  165. @GetMapping(value = "/getCaseList")
  166. @ResponseBody
  167. public void getCaseList(String report_id, HttpServletResponse response) {
  168. try {
  169. PrintWriter out = response.getWriter();
  170. JSONObject result = new JSONObject();
  171. List<TestCase> caseList = extraService.getCaseList(report_id);
  172. if(caseList != null) {
  173. result.put("status", 200);
  174. result.put("result", new JSONArray(caseList));
  175. }
  176. else { result.put("status", 500); }
  177. out.print(result);
  178. out.flush();
  179. out.close();
  180. } catch (IOException e) {
  181. // TODO Auto-generated catch block
  182. e.printStackTrace();
  183. }
  184. }
  185. /**
  186. * 获取测试Bug列表 /getBugList 返回报告下的所有bug
  187. * @param report_id 报告id
  188. * @param case_take_id 用例-任务id
  189. * @param response
  190. */
  191. @GetMapping(value = "/getBugList")
  192. @ResponseBody
  193. public void getBugList(String report_id, String case_take_id, HttpServletResponse response) {
  194. try {
  195. PrintWriter out = response.getWriter();
  196. JSONObject result = new JSONObject();
  197. List<Bug> bugList = extraService.getBugList(report_id, case_take_id);
  198. if(bugList != null && bugList.size() > 0) {
  199. result.put("status", 200);
  200. result.put("result", new JSONArray(bugList));
  201. }
  202. else { result.put("status", 500); }
  203. out.print(result);
  204. out.flush();
  205. out.close();
  206. } catch (IOException e) {
  207. // TODO Auto-generated catch block
  208. e.printStackTrace();
  209. }
  210. }
  211. /**
  212. * 47.99.140.117:9001/Bug/api/extra/uploadExam
  213. *
  214. * @param file
  215. * 文件流
  216. * @param case_id
  217. * 测试目标id
  218. * @param file_name
  219. * 上传excel的文件名
  220. * @param paper_type
  221. * 试卷类型,即试卷的显示矩阵
  222. * @param test_type
  223. * 测试分类,如工具
  224. * @param description
  225. * 测试说明
  226. * @param app_name
  227. * 测试目标的名称
  228. * @return JSONArray
  229. */
  230. @RequestMapping(value = "/uploadExam", method = RequestMethod.POST)
  231. @ResponseBody
  232. public String uploadExam(@RequestParam("file") MultipartFile file, String file_name, String paper_type,
  233. String case_id, String test_type, String description, String app_name) {
  234. try {
  235. File dest = new File("/Users/guochao/Downloads/" + file_name);
  236. if(!dest.getParentFile().exists()) { dest.getParentFile().mkdirs(); }
  237. if(!file.isEmpty()) { file.transferTo(dest); }
  238. String json = extraService.saveExam(case_id, dest.getPath(), app_name, paper_type, test_type, description, CollaborativeType.IS_COLLABORATIVE.getId()+"");
  239. return json;
  240. } catch (IOException e) {
  241. // TODO Auto-generated catch block
  242. e.printStackTrace();
  243. return "";
  244. }
  245. }
  246. /**
  247. * 创建任务
  248. * @param file 文件oss的url
  249. * @param file_name
  250. * @param paper_type
  251. * @param case_id
  252. * @param test_type
  253. * @param description
  254. * @param app_name
  255. * @param collaborative_type
  256. * @return
  257. */
  258. @RequestMapping(value = "/uploadExamUrl", method = RequestMethod.POST)
  259. @ResponseBody
  260. public String uploadExamUrl(String file, String file_name, String paper_type,
  261. String case_id, String test_type, String description, String app_name, @PathVariable(required = false) String collaborative_type) {
  262. try {
  263. System.out.println("file " + file);
  264. System.out.println("file_name " + file_name);
  265. File dest = new File("/Users/guochao/Downloads/" + file_name);
  266. if(!dest.getParentFile().exists()) { dest.getParentFile().mkdirs(); }
  267. //从oss下载文件
  268. // 统一资源
  269. URL url = new URL(file);
  270. // 连接类的父类,抽象类
  271. URLConnection urlConnection = url.openConnection();
  272. // http的连接类
  273. HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
  274. //设置超时
  275. httpURLConnection.setConnectTimeout(1000*5);
  276. //设置请求方式,默认是GET
  277. // httpURLConnection.setRequestMethod("GET");
  278. // 设置字符编码
  279. httpURLConnection.setRequestProperty("Charset", "UTF-8");
  280. // 打开到此 URL引用的资源的通信链接(如果尚未建立这样的连接)。
  281. httpURLConnection.connect();
  282. // 文件大小
  283. int fileLength = httpURLConnection.getContentLength();
  284. // 建立链接从请求中获取数据
  285. URLConnection con = url.openConnection();
  286. BufferedInputStream bin = new BufferedInputStream(httpURLConnection.getInputStream());
  287. // 指定文件名称(有需求可以自定义)
  288. // 指定存放位置(有需求可以自定义)
  289. OutputStream out = new FileOutputStream(dest);
  290. int size = 0;
  291. int len = 0;
  292. byte[] buf = new byte[2048];
  293. while ((size = bin.read(buf)) != -1) {
  294. len += size;
  295. out.write(buf, 0, size);
  296. }
  297. // 关闭资源
  298. bin.close();
  299. out.close();
  300. if(collaborative_type == null || collaborative_type == ""){
  301. collaborative_type = CollaborativeType.IS_COLLABORATIVE.getId() + "";
  302. }
  303. System.out.println(dest.getPath());
  304. String json = extraService.saveExam(case_id, dest.getPath(), app_name, paper_type, test_type, description, collaborative_type);
  305. return json;
  306. } catch (IOException e) {
  307. e.printStackTrace();
  308. return "";
  309. }
  310. }
  311. // @RequestMapping(value = "/uploadExamUrl", method = RequestMethod.POST)
  312. // @ResponseBody
  313. // public String uploadExamUrl1(String file, String file_name, String paper_type,
  314. // String case_id, String test_type, String description, String app_name,
  315. // String if_test_case,String if_bug) {
  316. // try {
  317. //
  318. // File dest = new File("/Users/guochao/Downloads/" + file_name);
  319. // if(!dest.getParentFile().exists()) { dest.getParentFile().mkdirs(); }
  320. // //从oss下载文件
  321. // // 统一资源
  322. // URL url = new URL(file);
  323. // // 连接类的父类,抽象类
  324. // URLConnection urlConnection = url.openConnection();
  325. // // http的连接类
  326. // HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
  327. // //设置超时
  328. // httpURLConnection.setConnectTimeout(1000*5);
  329. // //设置请求方式,默认是GET
  330. //// httpURLConnection.setRequestMethod("GET");
  331. // // 设置字符编码
  332. // httpURLConnection.setRequestProperty("Charset", "UTF-8");
  333. // // 打开到此 URL引用的资源的通信链接(如果尚未建立这样的连接)。
  334. // httpURLConnection.connect();
  335. // // 文件大小
  336. // int fileLength = httpURLConnection.getContentLength();
  337. //
  338. // // 建立链接从请求中获取数据
  339. // URLConnection con = url.openConnection();
  340. // BufferedInputStream bin = new BufferedInputStream(httpURLConnection.getInputStream());
  341. // // 指定文件名称(有需求可以自定义)
  342. // // 指定存放位置(有需求可以自定义)
  343. //
  344. //
  345. // OutputStream out = new FileOutputStream(dest);
  346. // int size = 0;
  347. // int len = 0;
  348. // byte[] buf = new byte[2048];
  349. // while ((size = bin.read(buf)) != -1) {
  350. // len += size;
  351. // out.write(buf, 0, size);
  352. // }
  353. // // 关闭资源
  354. // bin.close();
  355. // out.close();
  356. // String json = extraService.saveExam1(case_id, dest.getPath(), app_name, paper_type, test_type, description,if_test_case,if_bug);
  357. // return json;
  358. // } catch (IOException e) {
  359. // e.printStackTrace();
  360. // return "";
  361. // }
  362. // }
  363. /**
  364. * 47.99.140.117:9001/Bug/api/extra/getExamList
  365. *
  366. * @return 200 成功; 500 失败
  367. */
  368. @RequestMapping(value = "/getExamList", method = RequestMethod.GET)
  369. @ResponseBody
  370. public void getExamList(HttpServletResponse response) {
  371. try {
  372. PrintWriter out = response.getWriter();
  373. JSONObject result = new JSONObject();
  374. result.put("data", extraService.getExamList());
  375. result.put("status", 200);
  376. out.print(result);
  377. out.flush();
  378. out.close();
  379. } catch (IOException e) {
  380. // TODO Auto-generated catch block
  381. e.printStackTrace();
  382. }
  383. }
  384. /**
  385. * /getExam 获取考试的操作类型信息
  386. * @param id 考试id
  387. * @param response
  388. */
  389. @GetMapping(value = "/getExam")
  390. @ResponseBody
  391. public void getExam(String id, HttpServletResponse response) {
  392. try {
  393. PrintWriter out = response.getWriter();
  394. JSONObject result = new JSONObject();
  395. Exam exam = extraService.getExam(id);
  396. if(exam != null) {
  397. result.put("status", 200);
  398. result.put("result", new JSONObject(exam));
  399. }
  400. else { result.put("status", 500); }
  401. out.print(result);
  402. out.flush();
  403. out.close();
  404. } catch (IOException e) {
  405. // TODO Auto-generated catch block
  406. e.printStackTrace();
  407. }
  408. }
  409. /**
  410. * 上传测试用例 /uploadTestCase
  411. * @param report_id 报告id
  412. * @param name 用例名称
  413. * @param front 前置条件
  414. * @param behind 测试步骤
  415. * @param description 预期结果
  416. * @param if_execute 是否执行
  417. * @param if_bug 是否是bug
  418. * @param response
  419. */
  420. @RequestMapping(value = "/uploadTestCase", method = RequestMethod.POST)
  421. @ResponseBody
  422. public void uploadTestCase(String report_id, String name, String front, String behind,
  423. String description,String if_execute,String if_bug, HttpServletResponse response) {
  424. try {
  425. System.out.println(if_execute);
  426. System.out.println(if_bug);
  427. String id = extraService.saveTestCase(report_id, name, front, behind, description);
  428. PrintWriter out = response.getWriter();
  429. JSONObject result = new JSONObject();
  430. result.put("id", id);
  431. out.print(result);
  432. out.flush();
  433. out.close();
  434. } catch (IOException e) {
  435. // TODO Auto-generated catch block
  436. e.printStackTrace();
  437. }
  438. }
  439. /**
  440. * 获取指定用例id的用例信息 /getTestCase
  441. * @param id
  442. * @param response
  443. */
  444. @GetMapping(value = "/getTestCase")
  445. @ResponseBody
  446. public void getTestCase(String id, HttpServletResponse response) {
  447. try {
  448. PrintWriter out = response.getWriter();
  449. JSONObject result = new JSONObject();
  450. TestCase testCase = extraService.getTestCase(id);
  451. if(testCase != null) {
  452. result.put("status", 200);
  453. result.put("result", new JSONObject(testCase));
  454. }
  455. else { result.put("status", 500); }
  456. out.print(result);
  457. out.flush();
  458. out.close();
  459. } catch (IOException e) {
  460. // TODO Auto-generated catch block
  461. e.printStackTrace();
  462. }
  463. }
  464. /**
  465. * 更新测试用例 /updateTestCase 返回200或500
  466. * @param id 用例id
  467. * @param report_id 报告id
  468. * @param name 用例名称
  469. * @param front 前置条件
  470. * @param behind 测试步骤
  471. * @param description 预期结果
  472. * @param response
  473. */
  474. @RequestMapping(value = "/updateTestCase", method = RequestMethod.POST)
  475. @ResponseBody
  476. public void updateTestCase(String id, String report_id, String name, String front, String behind,
  477. String description, HttpServletResponse response) {
  478. try {
  479. PrintWriter out = response.getWriter();
  480. JSONObject result = new JSONObject();
  481. if(extraService.updateTestCase(id, report_id, name, front, behind, description)) {
  482. result.put("status", 200);
  483. } else { result.put("status", 500); }
  484. out.print(result);
  485. out.flush();
  486. out.close();
  487. } catch (IOException e) {
  488. // TODO Auto-generated catch block
  489. e.printStackTrace();
  490. }
  491. }
  492. @RequestMapping(value = "/updateTask", method = RequestMethod.POST)
  493. @ResponseBody
  494. public void updateTask(String id,double writeMins, HttpServletResponse response) {
  495. try {
  496. PrintWriter out = response.getWriter();
  497. JSONObject result = new JSONObject();
  498. if(extraService.updateTask(id, writeMins)) {
  499. result.put("status", 200);
  500. }
  501. else { result.put("status", 500); }
  502. out.print(result);
  503. out.flush();
  504. out.close();
  505. } catch (IOException e) {
  506. // TODO Auto-generated catch block
  507. e.printStackTrace();
  508. }
  509. }
  510. /**
  511. * 获取任务相关信息
  512. * @param id 任务ID
  513. * @param response
  514. */
  515. @RequestMapping(value = "/getTask")
  516. @ResponseBody
  517. public void getTask(String id, HttpServletResponse response) {
  518. try {
  519. PrintWriter out = response.getWriter();
  520. JSONObject result = new JSONObject();
  521. Task task=extraService.saveTask(id);
  522. if(task!=null) {
  523. result.put("status", 200);
  524. result.put("result",new JSONObject(task));
  525. }
  526. else { result.put("status", 500); }
  527. out.print(result);
  528. out.flush();
  529. out.close();
  530. } catch (IOException e) {
  531. // TODO Auto-generated catch block
  532. e.printStackTrace();
  533. }
  534. }
  535. @RequestMapping(value = "/reportsToReviewPaper")
  536. @ResponseBody
  537. public String getTask(String case_take_id) {
  538. String uploadUrl = extraService.reportsToReviewPaper(case_take_id);
  539. return uploadUrl;
  540. }
  541. @RequestMapping(value = "/pageUrl")
  542. @ResponseBody
  543. public void getPageUrl(String caseId,HttpServletResponse response){
  544. try {
  545. PrintWriter out = response.getWriter();
  546. JSONObject result = new JSONObject();
  547. result.put("status", 200);
  548. result.put("result",extraService.getPageUrl(caseId));
  549. out.print(result);
  550. out.flush();
  551. out.close();
  552. } catch (IOException e) {
  553. // TODO Auto-generated catch block
  554. e.printStackTrace();
  555. }
  556. }
  557. }