How to use processRequest method of org.cerberus.servlet.crud.test.testcase.ImportTestCase class

Best Cerberus-source code snippet using org.cerberus.servlet.crud.test.testcase.ImportTestCase.processRequest

Source:ImportTestCase.java Github

copy

Full Screen

...69 * @param httpServletResponse70 * @throws ServletException if a servlet-specific error occurs71 * @throws IOException if an I/O error occurs72 */73 protected void processRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)74 throws ServletException, IOException {75 JSONObject jsonResponse = new JSONObject();76 try {77 try {78 ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());79 testcaseService = appContext.getBean(ITestCaseService.class);80 Answer ans = new Answer();81 MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);82 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));83 ans.setResultMessage(msg);84 ///Get files85// List<String> files = getFiles(httpServletRequest);86 HashMap<String, String> param = getParams(httpServletRequest);87 String userCreated = httpServletRequest.getUserPrincipal().getName();88 // Prepare the final answer.89 MessageEvent msg1 = new MessageEvent(MessageEventEnum.GENERIC_OK);90 Answer finalAnswer = new Answer(msg1);91 String importOption = param.get("importOption");92 for (Map.Entry<String, String> entry : param.entrySet()) {93 String key = entry.getKey();94 String val = entry.getValue();95 if (key.startsWith("file")) {96 JSONObject json = new JSONObject(val);97 if (isCompatible(json)) {98 //Remove attribute not in the Object99 JSONObject tcJson;100 // Testcase moved from ROOT / "testCase" to ROOT / testcases [] in order to support multiple testcase export to a single file. 101 if (json.has("testCase")) {102 tcJson = json.getJSONObject("testCase");103 } else {104 tcJson = json.getJSONArray("testcases").getJSONObject(0);105 }106 JSONArray bugs = tcJson.getJSONArray("bugs");107 tcJson.remove("bugs");108 JSONArray condOpts = tcJson.getJSONArray("conditionOptions");109 tcJson.remove("conditionOptions");110 if ("2".equalsIgnoreCase(importOption)) {111 String targetTestcase = testcaseService.getNextAvailableTestcaseId(tcJson.getString("test"));112 tcJson.put("testcase", targetTestcase);113 }114 ObjectMapper mapper = new ObjectMapper();115 TestCase tcInfo = mapper.readValue(tcJson.toString(), TestCase.class);116 tcInfo.setBugs(bugs);117 tcInfo.setConditionOptions(condOpts);118 LOG.debug(tcInfo.toJson().toString(1));119 try {120 testcaseService.createTestcaseWithDependencies(tcInfo);121 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);122 msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCase " + tcInfo.getTest() + " - " + tcInfo.getTestcase())123 .replace("%OPERATION%", "Import"));124 ans.setResultMessage(msg);125 finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, ans);126 } catch (CerberusException ex) {127 LOG.error("Cerberus Exception during testcase import.", ex);128 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);129 msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCase " + tcInfo.getTest() + " - " + tcInfo.getTestcase())130 .replace("%OPERATION%", "Import")131 .replace("%REASON%", ex.getMessageError().getDescription()));132 ans.setResultMessage(msg);133 finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, ans);134 }135 } else {136 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);137 msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCase ")138 .replace("%OPERATION%", "Import")139 .replace("%REASON%", "The file you're trying to import is not supported or is not in a compatible version format."));140 ans.setResultMessage(msg);141 finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, ans);142 }143 }144 }145 jsonResponse.put("messageType", finalAnswer.getResultMessage().getMessage().getCodeString());146 jsonResponse.put("message", finalAnswer.getResultMessage().getDescription());147 } catch (Exception ex) {148 jsonResponse.put("messageType", MessageEventEnum.GENERIC_ERROR.getCodeString());149 jsonResponse.put("message", MessageEventEnum.GENERIC_ERROR.getDescription().replace("%REASON%", ex.toString()));150 LOG.error("General Exception during testcase import.", ex);151 }152 } catch (JSONException e) {153 LOG.error("JSONException during testcase import.", e);154 //returns a default error message with the json format that is able to be parsed by the client-side155 httpServletResponse.setContentType("application/json");156 httpServletResponse.getWriter().print(AnswerUtil.createGenericErrorAnswer());157 }158 httpServletResponse.getWriter().print(jsonResponse.toString());159 }160 public static JSONObject parseJSONFile(String filename) throws JSONException, IOException {161 String content = new String(Files.readAllBytes(Paths.get(filename)));162 return new JSONObject(content);163 }164 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">165 /**166 * Handles the HTTP <code>GET</code> method.167 *168 * @param request servlet request169 * @param response servlet response170 * @throws ServletException if a servlet-specific error occurs171 * @throws IOException if an I/O error occurs172 */173 @Override174 protected void doGet(HttpServletRequest request, HttpServletResponse response)175 throws ServletException, IOException {176 processRequest(request, response);177 }178 /**179 * Handles the HTTP <code>POST</code> method.180 *181 * @param request servlet request182 * @param response servlet response183 * @throws ServletException if a servlet-specific error occurs184 * @throws IOException if an I/O error occurs185 */186 @Override187 protected void doPost(HttpServletRequest request, HttpServletResponse response)188 throws ServletException, IOException {189 processRequest(request, response);190 }191 /**192 * Returns a short description of the servlet.193 *194 * @return a String containing servlet description195 */196 @Override197 public String getServletInfo() {198 return "Short description";199 }// </editor-fold>200 private HashMap<String, String> getParams(HttpServletRequest httpServletRequest) {201 HashMap<String, String> result = new HashMap<>();202 try {203 if (ServletFileUpload.isMultipartContent(httpServletRequest)) {...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Cerberus-source automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful