/** * 执行数据批次的更新操作,id为批次id * * @param caseId * @param id * @param expectData * @return */ @RequestMapping(value = "/INTERFACE_CASE/update/{cid}/{id}", method = RequestMethod.POST) public String updateBatchData( @PathVariable Integer cid, @PathVariable Integer id, String expectData) { InterfaceBatchDataBean iDataBean = inBatchDataService.getInBatchBean(id); iDataBean.setExpectData(expectData); inBatchDataService.updateInBatch(iDataBean); System.out.println(expectData); return "redirect:/data/INTERFACE_CASE/list/" + cid; }
/** * 加载一个批次的数据,用于更新。cid用于更新完成后能返回到指定指定用例的列表 * * @param cid * @param id * @param model * @return */ @RequestMapping("/INTERFACE_CASE/update/{cid}/{id}") public String updateBatchBean(@PathVariable Integer cid, @PathVariable Integer id, Model model) { model.addAttribute(inBatchDataService.getInBatchBean(id)); model.addAttribute(caseService.getCaseBean(cid)); model.addAttribute("iBatchDataBean", new InterfaceBatchDataBean()); return "data/ibatchDataEdit"; }
/** * 根据接口用例的id,列出该接口用例下游多少批参数 * * @param id * @param model * @return */ @RequestMapping("/INTERFACE_CASE/list/{id}") public String iList(@PathVariable Integer id, Model model) { List<InterfaceBatchDataBean> iilist = inBatchDataService.getInBatchList(id); model.addAttribute("datas", iilist); model.addAttribute("dataCount", iilist.size()); model.addAttribute("caseId", id); return "data/ilist"; }
/** * 执行添加操作 * * @param caseId * @param expectData * @return */ @RequestMapping(value = "/INTERFACE_CASE/add/{caseId}", method = RequestMethod.POST) public String addBatch(@PathVariable Integer caseId, String expectData) { CaseBean caseBean = caseService.getCaseBean(caseId); InterfaceBatchDataBean interBatchDataBean = new InterfaceBatchDataBean(); interBatchDataBean.setCaseBean(caseBean); interBatchDataBean.setExpectData(expectData); inBatchDataService.addInBatch(interBatchDataBean); return "redirect:/data/INTERFACE_CASE/list/" + caseId; }
/** * 执行添加操作 * * @param dataName * @param dataContent * @param bid * @return */ @RequestMapping(value = "/INTERFACE_CASE/iblist/add/{bid}", method = RequestMethod.POST) public String addDataBean(String dataName, String dataContent, @PathVariable Integer bid) { InterfaceBatchDataBean interBatchDataBean = inBatchDataService.getInBatchBean(bid); InterfaceDataBean interDataBean = new InterfaceDataBean(); interDataBean.setDataContent(dataContent); interDataBean.setDataName(dataName); interDataBean.setInterfaceBatchDataBean(interBatchDataBean); inDataBeanService.addDataBean(interDataBean); return "redirect:/data/INTERFACE_CASE/iblist/" + bid; }
/** * 执行批量数据导入的操作,如果勾选了isRewrite,则会先清除表中原有的数据,然后再重新插入,<br> * 若没有勾选,则直接执行插入操作,与原有的数据共同保存在数据库中。 * * @param sheetContentDTO * @param br * @param attach * @param req * @param model * @return * @throws IOException */ @RequestMapping(value = "/import/{cid}", method = RequestMethod.POST) public String importDatas( @Valid SheetContentDTO sheetContentDTO, BindingResult br, @RequestParam("attachs") MultipartFile attach, HttpServletRequest req, Model model) { model.addAttribute("casename", sheetContentDTO.getCaseName()); model.addAttribute("caseid", sheetContentDTO.getCaseId()); if (br.hasErrors()) return "data/importData"; ExcelUtil excelUtil = new ExcelUtil(); String realpath = req.getSession().getServletContext().getRealPath("/resources/upload"); String errorInfo = null; if (attach.isEmpty()) errorInfo = "文件不能为空"; if (!attach.getOriginalFilename().endsWith(".xlsx") && !attach.getOriginalFilename().endsWith(".xls")) errorInfo = "请选择[xlsx、xls]格式的数据文件"; if (errorInfo != null) { model.addAttribute("errorInfo", errorInfo); return "data/importData"; } String filePath = realpath + "/" + req.getSession().getId() + "." + Files.getFileExtension(attach.getOriginalFilename()); ; File f = new File(filePath); SheetContentDTO sheetContent = null; try { FileUtils.copyInputStreamToFile(attach.getInputStream(), f); excelUtil.setImportExcelPath(filePath); sheetContent = excelUtil.getExcelContent(sheetContentDTO.getSheetName()); } catch (Exception e) { errorInfo = "发生异常,请检查数据表的数据格式和指定的Sheet页名称是否正确," + e.getClass().getSimpleName() + "," + e.getMessage(); model.addAttribute("errorInfo", errorInfo); f.delete(); return "data/importData"; } CaseBean caseBean = null; if (sheetContent.getCaseId() != null) caseBean = caseService.getCaseBean(Integer.parseInt(sheetContent.getCaseId())); else if (sheetContent.getCaseName() != null) caseBean = caseService.getCaseBeanByName(sheetContent.getCaseName()); if (!sheetContentDTO.getCaseId().equals(caseBean.getId() + "")) { errorInfo = "当前用例是:[ " + sheetContentDTO.getCaseName() + " ] ,数据表中的数据不属于当前用例"; model.addAttribute("errorInfo", errorInfo); f.delete(); return "data/importData"; } if (sheetContentDTO.getIsRewrite()) { inBatchDataService.deleteInBatchByCaseId(caseBean.getId()); for (String[] params : sheetContent.getParameters()) { InterfaceBatchDataBean tempBatchBean = inBatchDataService.addInBatch(new InterfaceBatchDataBean(caseBean, params[0])); for (int i = 1; i < sheetContent.getParamNames().length; i++) { inDataBeanService.addDataBean( new InterfaceDataBean(tempBatchBean, sheetContent.getParamNames()[i], params[i])); } } } else { for (String[] params : sheetContent.getParameters()) { InterfaceBatchDataBean tempBatchBean = inBatchDataService.addInBatch(new InterfaceBatchDataBean(caseBean, params[0])); for (int i = 1; i < sheetContent.getParamNames().length; i++) { inDataBeanService.addDataBean( new InterfaceDataBean(tempBatchBean, sheetContent.getParamNames()[i], params[i])); } } } f.delete(); return "redirect:/data/INTERFACE_CASE/list/" + caseBean.getId(); }
/** * 根据batchid加载一个数据批次 * * @param bid * @param model * @return */ @RequestMapping("/INTERFACE_CASE/iblist/{bid}") public String iBList(@PathVariable Integer bid, Model model) { model.addAttribute("batchBean", inBatchDataService.getInBatchBean(bid)); return "data/iblist"; }
/** * 根据单一批次的id,删除该批次的数据 * * @param id * @return */ @RequestMapping("/INTERFACE_CASE/dbatch/{cid}/{id}") public String deleteBatchBean(@PathVariable Integer cid, @PathVariable Integer id) { inBatchDataService.deleteInBatch(id); return "redirect:/data/INTERFACE_CASE/list/" + cid; }
@RequestMapping( value = "/export/{id}", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8") public @ResponseBody String exportData( @PathVariable Integer id, HttpServletRequest req, Model model) { CaseBean caseBean = caseService.getCaseBean(id); String fileName = GetNow.getCurrentTime("yyyyMMddHHmmss") + "_" + caseBean.getCaseName(); String filePath = req.getSession().getServletContext().getRealPath("/resources/upload/") + "/" + fileName + ".xlsx"; SheetContentDTO sheetDTO = new SheetContentDTO(); ExcelUtil excelUtil = new ExcelUtil(); List<String[]> parameters = new ArrayList<String[]>(); List<InterfaceBatchDataBean> ibatchDataList = inBatchDataService.getInBatchList(id); sheetDTO.setCaseName(caseBean.getCaseName()); sheetDTO.setCaseId(id + ""); String[] paramName = null; for (InterfaceBatchDataBean batchData : ibatchDataList) { List<InterfaceDataBean> idataBeanList = inDataBeanService.getDataBeans(batchData.getId()); String[] paramContent = new String[idataBeanList.size() + 1]; paramName = new String[idataBeanList.size() + 1]; paramName[0] = "期望值\\参数名"; paramContent[0] = batchData.getExpectData(); for (int i = 0; i < idataBeanList.size(); i++) { paramName[i + 1] = idataBeanList.get(i).getDataName(); paramContent[i + 1] = idataBeanList.get(i).getDataContent(); } parameters.add(paramContent); } sheetDTO.setParamNames(paramName); sheetDTO.setParameters(parameters); excelUtil.setExportExcelPath(filePath); try { excelUtil.exportExcel(sheetDTO); String fileUrl = "http://" + req.getServerName() + ":" + req.getServerPort() + "/phoenix_web/resources/upload/" + fileName + ".xlsx"; return JSON.toJSONString( new AjaxObj( 1, "导出接口用例[ " + caseBean.getCaseName() + " ]的数据成功!<br>点击下载:<a href='" + fileUrl + "'>" + fileUrl + "</a>", fileName)); } catch (Exception e) { return JSON.toJSONString( new AjaxObj(0, "导出接口用例[ " + caseBean.getCaseName() + " ]的数据失败!<br>" + e.getCause())); } }