ExtraController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import org.json.JSONArray;
  9. import org.json.JSONObject;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.stereotype.Controller;
  12. import org.springframework.web.bind.annotation.*;
  13. import edu.nju.entities.Bug;
  14. import edu.nju.entities.Exam;
  15. import edu.nju.entities.Report;
  16. import edu.nju.entities.TestCase;
  17. import edu.nju.service.ExtraService;
  18. import org.springframework.web.multipart.MultipartFile;
  19. import java.net.URL;
  20. import java.io.BufferedInputStream;
  21. import java.io.FileOutputStream;
  22. import java.io.OutputStream;
  23. import java.net.HttpURLConnection;
  24. import java.net.URLConnection;
  25. @Controller
  26. @RequestMapping(value = "/extra")
  27. @CrossOrigin(origins = "*", maxAge = 3600, allowCredentials = "true")
  28. public class ExtraController {
  29. @Autowired
  30. ExtraService extraService;
  31. //上传测试报告
  32. @RequestMapping(value = "/uploadReport", method = RequestMethod.POST)
  33. @ResponseBody
  34. public void uploadReport(String case_id, String task_id, String case_take_id, String worker_id, String name, String device_model,
  35. String device_brand, String device_os, String script_location, String report_location,
  36. String log_location, HttpServletResponse response) {
  37. try {
  38. 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);
  39. PrintWriter out = response.getWriter();
  40. JSONObject result = new JSONObject();
  41. result.put("id", id);
  42. out.print(result);
  43. out.flush();
  44. out.close();
  45. } catch (IOException e) {
  46. // TODO Auto-generated catch block
  47. e.printStackTrace();
  48. }
  49. }
  50. //上传测试报告
  51. @RequestMapping(value = "/updateReport", method = RequestMethod.POST)
  52. @ResponseBody
  53. public void updateReport(String report_id, String case_id, String task_id, String case_take_id, String worker_id, String name, String device_model,
  54. String device_brand, String device_os, String script_location, String report_location,
  55. String log_location, HttpServletResponse response) {
  56. try {
  57. boolean bool = extraService.updateReport(report_id, case_id, task_id, case_take_id, worker_id, name,
  58. device_model, device_brand, device_os, script_location, report_location, log_location);
  59. PrintWriter out = response.getWriter();
  60. JSONObject result = new JSONObject();
  61. if(bool) { result.put("status", 200); }
  62. else { result.put("status", 500); }
  63. out.print(result);
  64. out.flush();
  65. out.close();
  66. } catch (IOException e) {
  67. // TODO Auto-generated catch block
  68. e.printStackTrace();
  69. }
  70. }
  71. //获取测试报告
  72. @RequestMapping(value = "/getReport")
  73. @ResponseBody
  74. public void getReport(String report_id, HttpServletResponse response) {
  75. try {
  76. PrintWriter out = response.getWriter();
  77. JSONObject result = new JSONObject();
  78. Report report = extraService.getReport(report_id);
  79. if(report != null) {
  80. result.put("status", 200);
  81. result.put("result", new JSONObject(report));
  82. }
  83. else { result.put("status", 500); }
  84. out.print(result);
  85. out.flush();
  86. out.close();
  87. } catch (IOException e) {
  88. // TODO Auto-generated catch block
  89. e.printStackTrace();
  90. }
  91. }
  92. //使用worker_id获取测试报告
  93. @RequestMapping(value = "/findByWorker")
  94. @ResponseBody
  95. public void findByWorker(String case_take_id, String worker_id, HttpServletResponse response) {
  96. try {
  97. PrintWriter out = response.getWriter();
  98. JSONObject result = new JSONObject();
  99. Report report = extraService.findByWorker(case_take_id, worker_id);
  100. if(report != null) {
  101. result.put("status", 200);
  102. result.put("result", new JSONObject(report));
  103. }
  104. else { result.put("status", 500); }
  105. out.print(result);
  106. out.flush();
  107. out.close();
  108. } catch (IOException e) {
  109. // TODO Auto-generated catch block
  110. e.printStackTrace();
  111. }
  112. }
  113. //获取测试用例列表
  114. @RequestMapping(value = "/getCaseList")
  115. @ResponseBody
  116. public void getCaseList(String report_id, HttpServletResponse response) {
  117. try {
  118. PrintWriter out = response.getWriter();
  119. JSONObject result = new JSONObject();
  120. List<TestCase> caseList = extraService.getCaseList(report_id);
  121. if(caseList != null) {
  122. result.put("status", 200);
  123. result.put("result", new JSONArray(caseList));
  124. }
  125. else { result.put("status", 500); }
  126. out.print(result);
  127. out.flush();
  128. out.close();
  129. } catch (IOException e) {
  130. // TODO Auto-generated catch block
  131. e.printStackTrace();
  132. }
  133. }
  134. //获取测试Bug列表
  135. @RequestMapping(value = "/getBugList")
  136. @ResponseBody
  137. public void getBugList(String report_id, String case_take_id, HttpServletResponse response) {
  138. try {
  139. PrintWriter out = response.getWriter();
  140. JSONObject result = new JSONObject();
  141. List<Bug> bugList = extraService.getBugList(report_id, case_take_id);
  142. if(bugList != null && bugList.size() > 0) {
  143. result.put("status", 200);
  144. result.put("result", new JSONArray(bugList));
  145. }
  146. else { result.put("status", 500); }
  147. out.print(result);
  148. out.flush();
  149. out.close();
  150. } catch (IOException e) {
  151. // TODO Auto-generated catch block
  152. e.printStackTrace();
  153. }
  154. }
  155. /**
  156. * 47.99.140.117:9001/Bug/api/extra/uploadExam
  157. *
  158. * @param file
  159. * 文件流
  160. * @param case_id
  161. * 测试目标id
  162. * @param file_name
  163. * 上传excel的文件名
  164. * @param paper_type
  165. * 试卷类型,即试卷的显示矩阵
  166. * @param test_type
  167. * 测试分类,如工具
  168. * @param description
  169. * 测试说明
  170. * @param app_name
  171. * 测试目标的名称
  172. * @return JSONArray
  173. */
  174. @RequestMapping(value = "/uploadExam", method = RequestMethod.POST)
  175. @ResponseBody
  176. public String uploadExam(@RequestParam("file") MultipartFile file, String file_name, String paper_type,
  177. String case_id, String test_type, String description, String app_name) {
  178. try {
  179. File dest = new File("/Users/hannatao/Downloads/" + file_name);
  180. if(!dest.getParentFile().exists()) { dest.getParentFile().mkdirs(); }
  181. if(!file.isEmpty()) { file.transferTo(dest); }
  182. String json = extraService.saveExam(case_id, dest.getPath(), app_name, paper_type, test_type, description);
  183. return json;
  184. } catch (IOException e) {
  185. // TODO Auto-generated catch block
  186. e.printStackTrace();
  187. return "";
  188. }
  189. }
  190. /**
  191. *
  192. * @param file 文件oss的url
  193. * @param file_name
  194. * @param paper_type
  195. * @param case_id
  196. * @param test_type
  197. * @param description
  198. * @param app_name
  199. * @return
  200. */
  201. @RequestMapping(value = "/uploadExamUrl", method = RequestMethod.POST)
  202. @ResponseBody
  203. public String uploadExamUrl(String file, String file_name, String paper_type,
  204. String case_id, String test_type, String description, String app_name) {
  205. try {
  206. File dest = new File("/Users/hannatao/Downloads/" + file_name);
  207. if(!dest.getParentFile().exists()) { dest.getParentFile().mkdirs(); }
  208. //从oss下载文件
  209. // 统一资源
  210. URL url = new URL(file);
  211. // 连接类的父类,抽象类
  212. URLConnection urlConnection = url.openConnection();
  213. // http的连接类
  214. HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
  215. //设置超时
  216. httpURLConnection.setConnectTimeout(1000*5);
  217. //设置请求方式,默认是GET
  218. // httpURLConnection.setRequestMethod("GET");
  219. // 设置字符编码
  220. httpURLConnection.setRequestProperty("Charset", "UTF-8");
  221. // 打开到此 URL引用的资源的通信链接(如果尚未建立这样的连接)。
  222. httpURLConnection.connect();
  223. // 文件大小
  224. int fileLength = httpURLConnection.getContentLength();
  225. // 建立链接从请求中获取数据
  226. URLConnection con = url.openConnection();
  227. BufferedInputStream bin = new BufferedInputStream(httpURLConnection.getInputStream());
  228. // 指定文件名称(有需求可以自定义)
  229. // 指定存放位置(有需求可以自定义)
  230. OutputStream out = new FileOutputStream(dest);
  231. int size = 0;
  232. int len = 0;
  233. byte[] buf = new byte[2048];
  234. while ((size = bin.read(buf)) != -1) {
  235. len += size;
  236. out.write(buf, 0, size);
  237. }
  238. // 关闭资源
  239. bin.close();
  240. out.close();
  241. String json = extraService.saveExam(case_id, dest.getPath(), app_name, paper_type, test_type, description);
  242. return json;
  243. } catch (IOException e) {
  244. e.printStackTrace();
  245. return "";
  246. }
  247. }
  248. // @RequestMapping(value = "/uploadExamUrl", method = RequestMethod.POST)
  249. // @ResponseBody
  250. // public String uploadExamUrl1(String file, String file_name, String paper_type,
  251. // String case_id, String test_type, String description, String app_name,
  252. // String if_test_case,String if_bug) {
  253. // try {
  254. //
  255. // File dest = new File("/Users/hannatao/Downloads/" + file_name);
  256. // if(!dest.getParentFile().exists()) { dest.getParentFile().mkdirs(); }
  257. // //从oss下载文件
  258. // // 统一资源
  259. // URL url = new URL(file);
  260. // // 连接类的父类,抽象类
  261. // URLConnection urlConnection = url.openConnection();
  262. // // http的连接类
  263. // HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
  264. // //设置超时
  265. // httpURLConnection.setConnectTimeout(1000*5);
  266. // //设置请求方式,默认是GET
  267. //// httpURLConnection.setRequestMethod("GET");
  268. // // 设置字符编码
  269. // httpURLConnection.setRequestProperty("Charset", "UTF-8");
  270. // // 打开到此 URL引用的资源的通信链接(如果尚未建立这样的连接)。
  271. // httpURLConnection.connect();
  272. // // 文件大小
  273. // int fileLength = httpURLConnection.getContentLength();
  274. //
  275. // // 建立链接从请求中获取数据
  276. // URLConnection con = url.openConnection();
  277. // BufferedInputStream bin = new BufferedInputStream(httpURLConnection.getInputStream());
  278. // // 指定文件名称(有需求可以自定义)
  279. // // 指定存放位置(有需求可以自定义)
  280. //
  281. //
  282. // OutputStream out = new FileOutputStream(dest);
  283. // int size = 0;
  284. // int len = 0;
  285. // byte[] buf = new byte[2048];
  286. // while ((size = bin.read(buf)) != -1) {
  287. // len += size;
  288. // out.write(buf, 0, size);
  289. // }
  290. // // 关闭资源
  291. // bin.close();
  292. // out.close();
  293. // String json = extraService.saveExam1(case_id, dest.getPath(), app_name, paper_type, test_type, description,if_test_case,if_bug);
  294. // return json;
  295. // } catch (IOException e) {
  296. // e.printStackTrace();
  297. // return "";
  298. // }
  299. // }
  300. /**
  301. * 47.99.140.117:9001/Bug/api/extra/getExamList
  302. *
  303. * @return 200 成功; 500 失败
  304. */
  305. @RequestMapping(value = "/getExamList", method = RequestMethod.GET)
  306. @ResponseBody
  307. public void getExamList(HttpServletResponse response) {
  308. try {
  309. PrintWriter out = response.getWriter();
  310. JSONObject result = new JSONObject();
  311. result.put("data", extraService.getExamList());
  312. result.put("status", 200);
  313. out.print(result);
  314. out.flush();
  315. out.close();
  316. } catch (IOException e) {
  317. // TODO Auto-generated catch block
  318. e.printStackTrace();
  319. }
  320. }
  321. //获取题目信息
  322. @RequestMapping(value = "/getExam")
  323. @ResponseBody
  324. public void getExam(String id, HttpServletResponse response) {
  325. try {
  326. PrintWriter out = response.getWriter();
  327. JSONObject result = new JSONObject();
  328. Exam exam = extraService.getExam(id);
  329. if(exam != null) {
  330. result.put("status", 200);
  331. result.put("result", new JSONObject(exam));
  332. }
  333. else { result.put("status", 500); }
  334. out.print(result);
  335. out.flush();
  336. out.close();
  337. } catch (IOException e) {
  338. // TODO Auto-generated catch block
  339. e.printStackTrace();
  340. }
  341. }
  342. //上传测试用例
  343. @RequestMapping(value = "/uploadTestCase", method = RequestMethod.POST)
  344. @ResponseBody
  345. public void uploadTestCase(String report_id, String name, String front, String behind,
  346. String description,String if_execute,String if_bug, HttpServletResponse response) {
  347. try {
  348. System.out.println(if_execute);
  349. System.out.println(if_bug);
  350. String id = extraService.saveTestCase(report_id, name, front, behind, description);
  351. PrintWriter out = response.getWriter();
  352. JSONObject result = new JSONObject();
  353. result.put("id", id);
  354. out.print(result);
  355. out.flush();
  356. out.close();
  357. } catch (IOException e) {
  358. // TODO Auto-generated catch block
  359. e.printStackTrace();
  360. }
  361. }
  362. //获取测试用例
  363. @RequestMapping(value = "/getTestCase")
  364. @ResponseBody
  365. public void getTestCase(String id, HttpServletResponse response) {
  366. try {
  367. PrintWriter out = response.getWriter();
  368. JSONObject result = new JSONObject();
  369. TestCase testCase = extraService.getTestCase(id);
  370. if(testCase != null) {
  371. result.put("status", 200);
  372. result.put("result", new JSONObject(testCase));
  373. }
  374. else { result.put("status", 500); }
  375. out.print(result);
  376. out.flush();
  377. out.close();
  378. } catch (IOException e) {
  379. // TODO Auto-generated catch block
  380. e.printStackTrace();
  381. }
  382. }
  383. //更新测试用例
  384. @RequestMapping(value = "/updateTestCase", method = RequestMethod.POST)
  385. @ResponseBody
  386. public void updateTestCase(String id, String report_id, String name, String front, String behind,
  387. String description, HttpServletResponse response) {
  388. try {
  389. PrintWriter out = response.getWriter();
  390. JSONObject result = new JSONObject();
  391. if(extraService.updateTestCase(id, report_id, name, front, behind, description)) {
  392. result.put("status", 200);
  393. } else { result.put("status", 500); }
  394. out.print(result);
  395. out.flush();
  396. out.close();
  397. } catch (IOException e) {
  398. // TODO Auto-generated catch block
  399. e.printStackTrace();
  400. }
  401. }
  402. //获取页面json
  403. @RequestMapping(value = "/getJson")
  404. @ResponseBody
  405. public void getJson(String id, HttpServletResponse response) {
  406. try {
  407. PrintWriter out = response.getWriter();
  408. JSONObject result = new JSONObject();
  409. Exam exam = extraService.getExam(id);
  410. if(exam != null) {
  411. result.put("status", 200);
  412. result.put("result", exam.getJson());
  413. }
  414. else { result.put("status", 500); }
  415. out.print(result);
  416. out.flush();
  417. out.close();
  418. } catch (IOException e) {
  419. // TODO Auto-generated catch block
  420. e.printStackTrace();
  421. }
  422. }
  423. @RequestMapping(value = "/uploadJob", method = RequestMethod.POST)
  424. @ResponseBody
  425. public String uploadExamUrl(String jobJson) {
  426. return null;
  427. }
  428. }