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

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

Source:DeleteTest.java Github

copy

Full Screen

...66 * @param response servlet response67 * @throws ServletException if a servlet-specific error occurs68 * @throws IOException if an I/O error occurs69 */70 protected void processRequest(HttpServletRequest request, HttpServletResponse response)71 throws ServletException, IOException, JSONException {72 JSONObject jsonResponse = new JSONObject();73 Answer ans = new Answer();74 PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);75 response.setContentType("application/json");76 // Calling Servlet Transversal Util.77 ServletUtil.servletStart(request);78 // Parsing and securing all required parameters.79 String key = policy.sanitize(request.getParameter("test"));80 // Checking all constrains before calling the services.81 if (StringUtil.isNull(key)) {82 ans.setResultMessage(83 new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED)84 .resolveDescription("ITEM", "Test")85 .resolveDescription("OPERATION", "Delete")86 .resolveDescription("REASON", "Test name is missing.")87 );88 } else {89 // All data seems cleans so we can call the services.90 ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());91 ITestService testService = appContext.getBean(ITestService.class);92 IParameterService parameterService = appContext.getBean(IParameterService.class);93 AnswerItem resp = testService.readByKey(key);94 if (!(resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && resp.getItem() != null)) {95 // Object could not be found. We stop here and report the error.96 ans.setResultMessage(97 new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED)98 .resolveDescription("ITEM", "Test")99 .resolveDescription("OPERATION", "Delete")100 .resolveDescription("REASON", "Test does not exist")101 );102 } else {103 // The service was able to perform the query and confirm the object exist104 Test testData = (Test) resp.getItem();105 // Check if there is no associated Test Cases defining Step which is used OUTSIDE of the deleting Test106 try {107 final Collection<TestCaseStep> externallyUsedTestCaseSteps = externallyUsedTestCaseSteps(testData);108 if (!externallyUsedTestCaseSteps.isEmpty()) {109 String cerberusUrlTemp = parameterService.getParameterStringByKey("cerberus_gui_url", "", "");110 if (StringUtil.isNullOrEmpty(cerberusUrlTemp)) {111 cerberusUrlTemp = parameterService.getParameterStringByKey("cerberus_url", "", "");112 }113 final String cerberusUrl = cerberusUrlTemp;114// final String cerberusUrl = appContext.getBean(IParameterService.class).findParameterByKey("cerberus_url", "").getValue();115 ans.setResultMessage(116 new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED)117 .resolveDescription("ITEM", "Test")118 .resolveDescription("OPERATION", "Delete")119 .resolveDescription(120 "REASON", "You are trying to remove a Test which contains Test Case Steps which are currently used by other Test Case Steps outside of the removing Test. Please remove this link before to proceed: "121 + Collections2.transform(externallyUsedTestCaseSteps, (@Nullable final TestCaseStep input) -> String.format(122 "<a href='%s/TestCaseScript.jsp?test=%s&testcase=%s&step=%s'>%s/%s#%s</a>",123 cerberusUrl,124 input.getTest(),125 input.getTestcase(),126 input.getStepId(),127 input.getTest(),128 input.getTestcase(),129 input.getStepId()130 ))131 )132 );133 } else {134 // Test seems clean, process to delete135 ans = testService.delete(testData);136 if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {137 // Delete was successful. Adding Log entry.138 ILogEventService logEventService = appContext.getBean(LogEventService.class);139 logEventService.createForPrivateCalls("/DeleteTest", "DELETE", "Delete Test : ['" + key + "']", request);140 }141 }142 } catch (final CerberusException e) {143 LOGGER.error(e.getMessage(), e);144 ans.setResultMessage(new MessageEvent(145 MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED)146 .resolveDescription("DESCRIPTION", "Unexpected error: " + e.getMessage())147 );148 }149 }150 }151 // Formating and returning the json result.152 jsonResponse.put("messageType", ans.getResultMessage().getMessage().getCodeString());153 jsonResponse.put("message", ans.getResultMessage().getDescription());154 response.getWriter().print(jsonResponse.toString());155 response.getWriter().flush();156 }157 /**158 * Get {@link TestCaseStep} which are using an other {@link TestCaseStep}159 * from the given {@link Test} but which are NOT included into this160 * {@link Test}161 *162 * @param test the {@link Test} from which getting externally used163 * {@link TestCaseStep}s164 * @return a {@link Collection} of {@link TestCaseStep} which are using an165 * other {@link TestCaseStep} from the given {@link Test} but which are NOT166 * included into this {@link Test}167 * @throws CerberusException if an unexpected error occurred168 */169 private Collection<TestCaseStep> externallyUsedTestCaseSteps(final Test test) throws CerberusException {170 // Get the associated ApplicationContext to this servlet171 final ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());172 // Get all TestCaseSteps which are using an other TestCaseSteps from given Test173 final ITestCaseStepService testCaseStepService = applicationContext.getBean(ITestCaseStepService.class);174 final List<TestCaseStep> stepsInUse = testCaseStepService.getTestCaseStepsUsingTestInParameter(test.getTest());175 // Filter the retrieved list to only retain those which are not included from the given Test176 return Collections2.filter(stepsInUse, new Predicate<TestCaseStep>() {177 @Override178 public boolean apply(@Nullable final TestCaseStep input) {179 return !input.getTest().equals(test.getTest());180 }181 @Override182 public boolean test(TestCaseStep t) {183 return Predicate.super.test(t); //To change body of generated methods, choose Tools | Templates.184 }185 });186 }187 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">188 /**189 * Handles the HTTP <code>GET</code> method.190 *191 * @param request servlet request192 * @param response servlet response193 * @throws ServletException if a servlet-specific error occurs194 * @throws IOException if an I/O error occurs195 */196 @Override197 protected void doGet(HttpServletRequest request, HttpServletResponse response)198 throws ServletException, IOException {199 try {200 processRequest(request, response);201 } catch (JSONException ex) {202 LOGGER.error(ex.getMessage(), ex);203 }204 }205 /**206 * Handles the HTTP <code>POST</code> method.207 *208 * @param request servlet request209 * @param response servlet response210 * @throws ServletException if a servlet-specific error occurs211 * @throws IOException if an I/O error occurs212 */213 @Override214 protected void doPost(HttpServletRequest request, HttpServletResponse response)215 throws ServletException, IOException {216 try {217 processRequest(request, response);218 } catch (JSONException ex) {219 LOGGER.error(ex.getMessage(), ex);220 }221 }222 /**223 * Returns a short description of the servlet.224 *225 * @return a String containing servlet description226 */227 @Override228 public String getServletInfo() {229 return "Short description";230 }// </editor-fold>231}...

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