@Before public void setupSignup() { signupForm = new SignupForm(); signupForm.setId((long) 1500); signupForm.setFirstName("Capitain"); signupForm.setLastName("Awesome"); signupForm.setEmail("*****@*****.**"); signupForm.setBiography("I try to be .......... awesome..."); signupForm.setPassword("123456"); signupForm.setTimeSlots(getNewTimeSlotList()); signupForm.setUserCourseList(getNewUserCourseFormAttributeList()); }
/** * Validates all changes and tries to save them to the database. * * @param principal the user that edited his profile. * @param signupForm the form which contains all information about the principal. * @param result contains error messages. * @param redirectAttributes used to add some attributes to the model. * @param session the current session especially containing the username. * @return the profile page if validations succeeded else returns the editProfile page with * attached errors. */ @RequestMapping(value = "/editProfile", method = RequestMethod.POST) public ModelAndView postProfile( Principal principal, @Validated(SignupForm.SignupValidatorGroup.class) SignupForm signupForm, BindingResult result, RedirectAttributes redirectAttributes, HttpSession session) { User user = userService.getPrincipalUser(); signupForm.setEmail(user.getEmail()); ModelAndView model; try { if (!signupForm.getPassword().equals(signupForm.getPasswordVerify())) { redirectAttributes.addFlashAttribute("passwordVerifyError", "Your passwords do not match!"); redirectAttributes.addFlashAttribute("infoMessage", "Your passwords do not match!"); return new ModelAndView("redirect:/editProfile"); } } catch (Exception d) { } if (!result.hasErrors()) { try { User updatedUser = userService.saveFrom(signupForm, user); userService.createAndSaveUserCoursesFromForm(signupForm, user); redirectAttributes.addFlashAttribute( "infoMessage", "You successfully edited your profile!"); session.setAttribute("username", updatedUser.getWholeName()); model = new ModelAndView("redirect:/profile"); } catch (InvalidUserException e) { model = new ModelAndView("editProfile"); model.addObject("page_error", e.getMessage()); } } else { model = new ModelAndView("editProfile"); model.addObject("infoMessage", result.toString()); System.out.println("Form has errors:\n" + result.toString()); } return model; }
/** * Serve model with signupForm. * * @param principal the logged in principal. * @return a signupForm with filled name and biography fields. */ @ModelAttribute("signupForm") public SignupForm getSignupForm(Principal principal) { User user = userService.getPrincipalUser(); SignupForm form = new SignupForm(); form.setFirstName(user.getFirstName()); form.setLastName(user.getLastName()); form.setBiography(user.getBiography()); try { AutoPopulatingList<UserCourseFormAttribute> userCourseList = new AutoPopulatingList<UserCourseFormAttribute>(new UserCourseFormAttributeFactory()); Iterator<UserCourse> itr = userCourseDao.findByUser(user).iterator(); while (itr.hasNext()) { UserCourse tmpUserCourse = itr.next(); UserCourseFormAttribute formAttr = new UserCourseFormAttribute(); formAttr.setUniversity(tmpUserCourse.getCourse().getSubject().getUniversity().toString()); formAttr.setSubject(tmpUserCourse.getCourse().getSubject().toString()); formAttr.setCourse(tmpUserCourse.getCourse().toString()); formAttr.setGrade(String.valueOf(tmpUserCourse.getGrade())); formAttr.setTeaching(tmpUserCourse.isTeaching()); userCourseList.add(formAttr); } form.setUserCourseList(userCourseList); } catch (Exception e) { } try { AutoPopulatingList<TimeSlot> timeSlotList = new AutoPopulatingList<TimeSlot>(new TimeSlotFactory()); ListIterator<TimeSlot> iter = user.getTimeSlots().listIterator(); while (iter.hasNext()) { timeSlotList.add(iter.next()); } form.setTimeSlots(timeSlotList); } catch (Exception e) { } return form; }
@Test(expected = InvalidUserException.class) public void testEmptyName() { signupForm.setFirstName("a"); signupForm.setLastName("asdf"); userService.saveFrom(signupForm); }