RoleController extends AbstractFormController { @Override protected ModelAndView processFormSubmission(HttpServletRequest aReq, HttpServletResponse aRes, Object aCmd, BindException aError) throws Exception { RoleCommand roleCmd = (RoleCommand) aCmd; if (roleCmd != null) { if (aError.hasErrors()) { ModelAndView mav = new ModelAndView(); List agencies = agencyUserManager.getAgenciesForLoggedInUser(); mav.addObject(RoleCommand.MDL_AGENCIES, agencies); mav.addObject(Constants.GBL_CMD_DATA, aError.getTarget()); mav.addObject(Constants.GBL_ERRORS, aError); mav.setViewName("AddRole"); return mav; } else if (RoleCommand.ACTION_NEW.equals(roleCmd.getAction())) { List agencies = agencyUserManager.getAgenciesForLoggedInUser(); ModelAndView mav = new ModelAndView(); mav.addObject(RoleCommand.MDL_AGENCIES, agencies); mav.setViewName("AddRole"); return mav; } else if (RoleCommand.ACTION_SAVE.equals(roleCmd.getAction())) { return saveRole(aReq, aRes, roleCmd, aError); } else if (RoleCommand.ACTION_DELETE.equals(roleCmd.getAction())) { return deleteRole(aReq, aRes, roleCmd, aError); } else if (RoleCommand.ACTION_VIEW.equals(roleCmd.getAction())|| RoleCommand.ACTION_EDIT.equals(roleCmd.getAction())) { return editRole(aReq, aRes, roleCmd, aError); } else if (RoleCommand.ACTION_FILTER.equals(roleCmd.getAction())){ aReq.getSession().setAttribute(RoleCommand.PARAM_AGENCY_FILTER, roleCmd.getAgencyFilter()); return defaultView(roleCmd.getAgencyFilter()); } } return null; } RoleController(); @Override void initBinder(HttpServletRequest request, ServletRequestDataBinder binder); ModelAndView defaultView(String agencyFilter); void setAgencyUserManager(AgencyUserManager agencyUserManager); void setMessageSource(MessageSource messageSource); void setAuthorityManager(AuthorityManager authorityManager);  }
@Test public final void testProcessFormSubmission() { try { MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); BindException aError = new BindException(new RoleCommand(), RoleCommand.ACTION_EDIT); this.testSetAgencyUserManager(); this.testSetAuthorityManager(); this.testSetMessageSource(); RoleCommand aCommand = new RoleCommand(); aCommand.setAction(RoleCommand.ACTION_NEW); aCommand.setOid(new Long(3000)); ModelAndView mav = testInstance.processFormSubmission(request, response, aCommand, aError); assertTrue(mav != null); assertTrue(mav.getViewName().equals("AddRole")); List<Agency> agencies = (List<Agency>)mav.getModel().get("agencies"); assertTrue(agencies != null); assertTrue(agencies.size() > 0); aCommand = new RoleCommand(); aCommand.setAction(RoleCommand.ACTION_VIEW); aCommand.setOid(new Long(3000)); mav = testInstance.processFormSubmission(request, response, aCommand, aError); assertTrue(mav != null); assertTrue(mav.getViewName().equals("AddRole")); agencies = (List<Agency>)mav.getModel().get("agencies"); assertTrue(agencies != null); assertTrue(agencies.size() > 0); RoleCommand newCommand = (RoleCommand)mav.getModel().get("command"); assertTrue(newCommand != null); assertTrue(newCommand.getViewOnlyMode()); aCommand = new RoleCommand(); aCommand.setAction(RoleCommand.ACTION_EDIT); aCommand.setOid(new Long(3000)); mav = testInstance.processFormSubmission(request, response, aCommand, aError); assertTrue(mav != null); assertTrue(mav.getViewName().equals("AddRole")); agencies = (List<Agency>)mav.getModel().get("agencies"); assertTrue(agencies != null); assertTrue(agencies.size() > 0); newCommand = (RoleCommand)mav.getModel().get("command"); assertTrue(newCommand != null); assertFalse(newCommand.getViewOnlyMode()); aCommand = new RoleCommand(); aCommand.setAction(RoleCommand.ACTION_SAVE); aCommand.setRoleName("New Test Role"); aCommand.setAgency(AuthUtil.getRemoteUserObject().getAgency().getOid()); String[] scopedPrivileges = {}; aCommand.setScopedPrivileges(scopedPrivileges); mav = testInstance.processFormSubmission(request, response, aCommand, aError); assertTrue(mav != null); assertTrue(mav.getViewName().equals("Roles")); List<Role> roles = (List<Role>)mav.getModel().get("roles"); assertTrue(roles != null); assertTrue(roles.size() > 0); aCommand = new RoleCommand(); aCommand.setAction(RoleCommand.ACTION_FILTER); String agencyFilter = "Dummy"; aCommand.setAgencyFilter(agencyFilter); mav = testInstance.processFormSubmission(request, response, aCommand, aError); assertTrue(mav != null); assertTrue(mav.getViewName().equals("Roles")); RoleCommand command = (RoleCommand)mav.getModel().get("command"); assertTrue(command != null); assertTrue(agencyFilter.equals(command.getAgencyFilter())); assertTrue(agencyFilter.equals((String)request.getSession().getAttribute(RoleCommand.PARAM_AGENCY_FILTER))); } catch (Exception e) { String message = e.getClass().toString() + " - " + e.getMessage(); log.debug(message); fail(message); } }