Best Cerberus-source code snippet using org.cerberus.servlet.crud.usermanagement.CreateUser.processRequest
Source:CreateUser.java
...63 * @param response servlet response64 * @throws ServletException if a servlet-specific error occurs65 * @throws IOException if an I/O error occurs66 */67 protected void processRequest(HttpServletRequest request, HttpServletResponse response)68 throws ServletException, IOException, CerberusException, JSONException {69 ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());70 JSONObject jsonResponse = new JSONObject();71 Answer ans = new Answer();72 MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);73 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));74 ans.setResultMessage(msg);75 PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);76 String charset = request.getCharacterEncoding();77 IParameterService parameterService = appContext.getBean(ParameterService.class);78 IEmailService emailService = appContext.getBean(IEmailService.class);79 String system = "";80 String password = parameterService.findParameterByKey("cerberus_accountcreation_defaultpassword", system).getValue();81 String newPassword = ParameterParserUtil.parseStringParam(request.getParameter("newPassword"), "Y");82 String login = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("login"), "", charset);83 String email = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("email"), "", charset);84 String defaultSystem = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("defaultSystem"), "", charset);85 String name = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("name"), "", charset);86 String team = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("team"), "", charset);87 JSONArray JSONSystems = new JSONArray(ParameterParserUtil.parseStringParam(request.getParameter("systems"), null));88 JSONArray JSONGroups = new JSONArray(ParameterParserUtil.parseStringParam(request.getParameter("groups"), null));89 boolean userHasPermissions = request.isUserInRole("Administrator");90 /**91 * Checking all constrains before calling the services.92 */93 if (StringUtil.isNullOrEmpty(login)) {94 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);95 msg.setDescription(msg.getDescription().replace("%ITEM%", "User")96 .replace("%OPERATION%", "Create")97 .replace("%REASON%", "User name is missing!"));98 ans.setResultMessage(msg);99 } else if (!userHasPermissions) {100 msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);101 msg.setDescription(msg.getDescription().replace("%ITEM%", "User")102 .replace("%OPERATION%", "Create")103 .replace("%REASON%", "You don't have the right to do that"));104 ans.setResultMessage(msg);105 } else {106 /**107 * All data seems cleans so we can call the services.108 */109 IUserService userService = appContext.getBean(IUserService.class);110 IFactoryUser factoryUser = appContext.getBean(IFactoryUser.class);111 IFactoryUserGroup factoryGroup = new FactoryUserGroup();112 IFactoryUserSystem userSystemFactory = appContext.getBean(IFactoryUserSystem.class);113 IUserGroupService userGroupService = appContext.getBean(UserGroupService.class);114 IUserSystemService userSystemService = appContext.getBean(IUserSystemService.class);115 LinkedList<UserGroup> newGroups = new LinkedList<>();116 for (int i = 0; i < JSONGroups.length(); i++) {117 newGroups.add(factoryGroup.create(login, JSONGroups.getString(i)));118 }119 LinkedList<UserSystem> newSystems = new LinkedList<>();120 for (int i = 0; i < JSONSystems.length(); i++) {121 newSystems.add(userSystemFactory.create(login, JSONSystems.getString(i)));122 }123 User userData = factoryUser.create(0, login, password, "", newPassword, name, team, "en", "", "", "", "", "", "", "", defaultSystem, email, null, null);124 ans = userService.create(userData);125 if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {126 /**127 * Send Email to explain how to connect Cerberus if128 * activateNotification is set to Y129 */130 String sendNotification = parameterService.findParameterByKey("cerberus_notification_accountcreation_activatenotification", system).getValue();131 if (sendNotification.equalsIgnoreCase("Y")) {132 Answer msgSent = new Answer(emailService.generateAndSendAccountCreationEmail(userData));133 ans = AnswerUtil.agregateAnswer(ans, msgSent);134 }135 /**136 * Object updated. Adding Log entry.137 */138 ILogEventService logEventService = appContext.getBean(LogEventService.class);139 logEventService.createForPrivateCalls("/CreateUser", "CREATE", "Create User : ['" + login + "']", request);140 ans = AnswerUtil.agregateAnswer(ans, userGroupService.updateGroupsByUser(userData, newGroups));141 ans = AnswerUtil.agregateAnswer(ans, userSystemService.updateSystemsByUser(userData, newSystems));142 }143 }144 /**145 * Formating and returning the json result.146 */147 jsonResponse.put("messageType", ans.getResultMessage().getMessage().getCodeString());148 jsonResponse.put("message", ans.getResultMessage().getDescription());149 response.getWriter().print(jsonResponse);150 response.getWriter().flush();151 }152 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">153 /**154 * Handles the HTTP <code>GET</code> method.155 *156 * @param request servlet request157 * @param response servlet response158 * @throws ServletException if a servlet-specific error occurs159 * @throws IOException if an I/O error occurs160 */161 @Override162 protected void doGet(HttpServletRequest request, HttpServletResponse response)163 throws ServletException, IOException {164 try {165 processRequest(request, response);166 } catch (CerberusException ex) {167 LOG.warn(ex);168 } catch (JSONException ex) {169 LOG.warn(ex);170 }171 }172 /**173 * Handles the HTTP <code>POST</code> method.174 *175 * @param request servlet request176 * @param response servlet response177 * @throws ServletException if a servlet-specific error occurs178 * @throws IOException if an I/O error occurs179 */180 @Override181 protected void doPost(HttpServletRequest request, HttpServletResponse response)182 throws ServletException, IOException {183 try {184 processRequest(request, response);185 } catch (CerberusException ex) {186 LOG.warn(ex);187 } catch (JSONException ex) {188 LOG.warn(ex);189 }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";...
processRequest
Using AI Code Generation
1 public void testCreateUser() {2 HttpServletRequest request = mock(HttpServletRequest.class);3 HttpServletResponse response = mock(HttpServletResponse.class);4 ServletContext servletContext = mock(ServletContext.class);5 ServletConfig servletConfig = mock(ServletConfig.class);6 when(servletConfig.getServletContext()).thenReturn(servletContext);7 HttpSession session = mock(HttpSession.class);8 when(request.getSession()).thenReturn(session);9 ServletContext context = mock(ServletContext.class);10 when(request.getServletContext()).thenReturn(context);11 ServletContext context = mock(ServletContext.class);12 when(request.getServletContext()).thenReturn(context);13 when(request.getSession()).thenReturn(session);14 ServletContext context = mock(ServletContext.class);15 when(request.getServletContext()).thenReturn(context);16 when(request.getSession()).thenReturn(session);17 ServletContext context = mock(ServletContext.class);18 when(request.getServletContext()).thenReturn(context);19 when(request.getSession()).thenReturn(session);20 ServletContext context = mock(ServletContext.class);21 when(request.getServletContext()).thenReturn(context);22 when(request.getSession()).thenReturn(session);23 ServletContext context = mock(ServletContext.class);24 when(request.getServletContext()).thenReturn(context);25 when(request.getSession()).thenReturn(session);26 ServletContext context = mock(ServletContext.class);27 when(request.getServletContext()).thenReturn(context);28 when(request.getSession()).thenReturn(session);29 ServletContext context = mock(ServletContext.class);30 when(request.getServletContext()).thenReturn(context);31 when(request.getSession()).thenReturn(session);32 ServletContext context = mock(ServletContext.class);33 when(request.getServletContext()).thenReturn(context);34 when(request.getSession()).thenReturn(session);35 ServletContext context = mock(ServletContext.class);36 when(request.getServletContext()).thenReturn(context);37 when(request.getSession()).thenReturn(session);38 ServletContext context = mock(ServletContext.class);39 when(request.getServletContext()).thenReturn
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!!