Best Cerberus-source code snippet using org.cerberus.servlet.crud.transversaltables.UpdateInvariant.processRequest
Source: UpdateInvariant.java
...60 * @param response servlet response61 * @throws ServletException if a servlet-specific error occurs62 * @throws IOException if an I/O error occurs63 */64 protected void processRequest(HttpServletRequest request, HttpServletResponse response)65 throws ServletException, IOException, CerberusException, JSONException {66 JSONObject jsonResponse = new JSONObject();67 Answer ans = new Answer();68 MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);69 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));70 ans.setResultMessage(msg);71 PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);72 String charset = request.getCharacterEncoding() == null ? "UTF-8" : request.getCharacterEncoding();73 // Calling Servlet Transversal Util.74 ServletUtil.servletStart(request);75 String id = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("idName"), "", charset);76 String value = request.getParameter("value");77 String oriId = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("originalIdName"), "", charset);78 String oriValue = request.getParameter("originalValue");79 String description = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("description"), "", charset);80 String veryShortDescField = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("veryShortDesc"), "", charset);81 String gp1 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp1"), "", charset);82 String gp2 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp2"), "", charset);83 String gp3 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp3"), "", charset);84 String gp4 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp4"), "", charset);85 String gp5 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp5"), "", charset);86 String gp6 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp6"), "", charset);87 String gp7 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp7"), "", charset);88 String gp8 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp8"), "", charset);89 String gp9 = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("gp9"), "", charset);90 Integer sort = 10;91 boolean sort_error = false;92 try {93 if (request.getParameter("sort") != null && !request.getParameter("sort").equals("")) {94 sort = Integer.valueOf(policy.sanitize(request.getParameter("sort")));95 }96 } catch (Exception ex) {97 sort_error = true;98 }99 boolean userHasPermissions = request.isUserInRole("Administrator");100 // Prepare the final answer.101 MessageEvent msg1 = new MessageEvent(MessageEventEnum.GENERIC_OK);102 Answer finalAnswer = new Answer(msg1);103 /**104 * Checking all constrains before calling the services.105 */106 if (StringUtil.isNullOrEmpty(id)) {107 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);108 msg.setDescription(msg.getDescription().replace("%ITEM%", "Invariant")109 .replace("%OPERATION%", "Update")110 .replace("%REASON%", "Invariant name is missing!"));111 finalAnswer.setResultMessage(msg);112 } else if (sort_error) {113 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);114 msg.setDescription(msg.getDescription().replace("%ITEM%", "Invariant")115 .replace("%OPERATION%", "Update")116 .replace("%REASON%", "Could not manage to convert sort to an integer value!"));117 finalAnswer.setResultMessage(msg);118 } else if (!userHasPermissions) {119 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);120 msg.setDescription(msg.getDescription().replace("%ITEM%", "Invariant")121 .replace("%OPERATION%", "Update")122 .replace("%REASON%", "You don't have the right to do that"));123 finalAnswer.setResultMessage(msg);124 } else {125 /**126 * All data seems cleans so we can call the services.127 */128 ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());129 IInvariantService invariantService = appContext.getBean(IInvariantService.class);130 AnswerItem resp = invariantService.readByKey(oriId, oriValue);131 if (!(resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && resp.getItem() != null)) {132 /**133 * Object could not be found. We stop here and report the error.134 */135 finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) resp);136 } else {137 Invariant invariantData = (Invariant) resp.getItem();138 if (!(resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && resp.getItem() != null)) {139 /**140 * Object could not be found. We stop here and report the141 * error.142 */143 finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) resp);144 } else {145 if (invariantService.hasPermissionsUpdate(invariantData, request)) {146 invariantData.setIdName(id);147 invariantData.setValue(value);148 invariantData.setSort(sort);149 invariantData.setDescription(description);150 invariantData.setVeryShortDesc(veryShortDescField);151 invariantData.setGp1(gp1);152 invariantData.setGp2(gp2);153 invariantData.setGp3(gp3);154 invariantData.setGp4(gp4);155 invariantData.setGp5(gp5);156 invariantData.setGp6(gp6);157 invariantData.setGp7(gp7);158 invariantData.setGp8(gp8);159 invariantData.setGp9(gp9);160 ans = invariantService.update(oriId, oriValue, invariantData);161 finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);162 if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {163 /**164 * Object updated. Adding Log entry.165 */166 ILogEventService logEventService = appContext.getBean(LogEventService.class);167 logEventService.createForPrivateCalls("/UpdateInvariant", "UPDATE", "Update Invariant : ['" + id + "']", request);168 }169 } else {170 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);171 msg.setDescription(msg.getDescription().replace("%ITEM%", "Invariant")172 .replace("%OPERATION%", "Update")173 .replace("%REASON%", "The Invariant is not Public!"));174 ans.setResultMessage(msg);175 finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);176 }177 }178 }179 }180 /**181 * Formating and returning the json result.182 */183 jsonResponse.put("messageType", finalAnswer.getResultMessage().getMessage().getCodeString());184 jsonResponse.put("message", finalAnswer.getResultMessage().getDescription());185 response.getWriter().print(jsonResponse);186 response.getWriter().flush();187 }188 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">189 /**190 * Handles the HTTP <code>GET</code> method.191 *192 * @param request servlet request193 * @param response servlet response194 * @throws ServletException if a servlet-specific error occurs195 * @throws IOException if an I/O error occurs196 */197 @Override198 protected void doGet(HttpServletRequest request, HttpServletResponse response)199 throws ServletException, IOException {200 try {201 processRequest(request, response);202 } catch (CerberusException ex) {203 LOG.warn(ex);204 } catch (JSONException ex) {205 LOG.warn(ex);206 }207 }208 /**209 * Handles the HTTP <code>POST</code> method.210 *211 * @param request servlet request212 * @param response servlet response213 * @throws ServletException if a servlet-specific error occurs214 * @throws IOException if an I/O error occurs215 */216 @Override217 protected void doPost(HttpServletRequest request, HttpServletResponse response)218 throws ServletException, IOException {219 try {220 processRequest(request, response);221 } catch (CerberusException ex) {222 LOG.warn(ex);223 } catch (JSONException ex) {224 LOG.warn(ex);225 }226 }227 /**228 * Returns a short description of the servlet.229 *230 * @return a String containing servlet description231 */232 @Override233 public String getServletInfo() {234 return "Short description";...
Check out the latest blogs from LambdaTest on this topic:
Unit testing is typically software testing within the developer domain. As the QA role expands in DevOps, QAOps, DesignOps, or within an Agile team, QA testers often find themselves creating unit tests. QA testers may create unit tests within the code using a specified unit testing tool, or independently using a variety of methods.
If you pay close attention, you’ll notice that toggle switches are all around us because lots of things have two simple states: either ON or OFF (in binary 1 or 0).
Hey LambdaTesters! We’ve got something special for you this week. ????
The key to successful test automation is to focus on tasks that maximize the return on investment (ROI), ensuring that you are automating the right tests and automating them in the right way. This is where test automation strategies come into play.
We launched LT Browser in 2020, and we were overwhelmed by the response as it was awarded as the #5 product of the day on the ProductHunt platform. Today, after 74,585 downloads and 7,000 total test runs with an average of 100 test runs each day, the LT Browser has continued to help developers build responsive web designs in a jiffy.
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!