@RequestMapping(value = "/add", method = RequestMethod.POST) public String add( Integer scenId, @Valid CaseDTO caseDTO, BindingResult br, Model model, HttpSession httpSession) { if (br.hasErrors()) { return "case/add"; } ScenarioBean scenaBean = new ScenarioBean(); scenaBean.setId(scenId); CaseBean caseBean = new CaseBean(); caseBean.setCreateDate(new Date()); caseBean.setScenarioBean(scenaBean); User user = (User) httpSession.getAttribute("loginUser"); caseBean.setUserId(user.getId()); caseBean.setCaseName(caseDTO.getCaseName()); caseBean.setRemark(caseDTO.getRemark()); caseBean.setStatus(caseDTO.getStatus()); caseBean.setDeleteMsg(caseDTO.isDeleteMsg()); caseBean.setMsgSendType(caseDTO.getMsgSendType()); caseService.addCase(caseBean); return "redirect:/case/list"; }
@RequestMapping(value = "/update/editor", method = RequestMethod.POST) public String update(@Valid CaseDTO caseDTO, BindingResult br, Model model) { if (br.hasErrors()) { return "/case/edit"; } ScenarioBean scenBean = new ScenarioBean(); scenBean.setId(caseDTO.getScenId()); String className = ""; try { className = MethodPattern.result( caseDTO.getCodeContent(), "public\\sclass\\s(.*)extends\\sWebElementActionProxy") .trim(); } catch (NullPointerException e) { } CaseBean caseBean = caseService.getCaseBean(caseDTO.getId()); caseBean.setCaseName(caseDTO.getCaseName()); caseBean.setCodeContent(caseDTO.getCodeContent()); caseBean.setRemark(caseDTO.getRemark()); caseBean.setStatus(caseDTO.getStatus()); caseBean.setScenarioBean(scenBean); caseBean.setClassName(className); caseBean.setDeleteMsg(caseDTO.isDeleteMsg()); caseBean.setMsgSendType(caseDTO.getMsgSendType()); caseService.updateCase(caseBean); return "redirect:/case/list"; }
@RequestMapping(value = "/add/{id}", method = RequestMethod.POST) public String add(@Valid LocatorDTO locatorDTO, BindingResult br) { if (br.hasErrors()) { return "locator/add"; } CaseBean caseBean = new CaseBean(); caseBean.setId(locatorDTO.getCaseId()); LocatorBean locatorBean = new LocatorBean(); locatorBean.setCaseBean(caseBean); locatorBean.setLocatorData(locatorDTO.getLocatorData()); locatorBean.setLocatorDataName(locatorDTO.getLocatorDataName()); locatorBean.setLocatorType(locatorDTO.getLocatorType()); locatorService.addLocator(locatorBean); return "redirect:/locator/case/" + locatorDTO.getCaseId(); }
@RequestMapping(value = "/add/{id}", method = RequestMethod.POST) public String add( @PathVariable Integer id, @Valid DataDTO dataDTO, BindingResult br, Model model) { if (br.hasErrors()) { model.addAttribute(caseService.getCaseBean(id)); return "data/add"; } CaseBean caseBean = new CaseBean(); DataBean dataBean = new DataBean(); caseBean.setId(dataDTO.getCaseId()); dataBean.setCaseBean(caseBean); dataBean.setDataContent(dataDTO.getDataContent()); dataBean.setDataName(dataDTO.getDataName()); dataService.addData(dataBean); return "redirect:/data/list/" + dataDTO.getCaseId(); }
@RequestMapping(value = "/update/{id}", method = RequestMethod.POST) public String update( @PathVariable Integer id, @Valid LocatorDTO locatorDTO, BindingResult br, Model model) { if (br.hasErrors()) { return "locator/edit"; } CaseBean caseBean = new CaseBean(); caseBean.setId(locatorDTO.getCaseId()); LocatorBean locatorBean = locatorService.getLocatorBean(id); locatorBean.setCaseBean(caseBean); locatorBean.setLocatorData(locatorDTO.getLocatorData()); locatorBean.setLocatorDataName(locatorDTO.getLocatorDataName()); locatorBean.setLocatorType(locatorDTO.getLocatorType()); locatorService.updateLocator(locatorBean); return "redirect:/locator/case/" + locatorDTO.getCaseId(); }
/** * 执行批量数据导入的操作,如果勾选了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(); }
@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())); } }