@Test public void shouldSaveNewLocationForNoErrors() { ModelMap modelMap = new ModelMap(); when(webFacility.getFacility()).thenReturn(facility); when(webFacility.getName()).thenReturn("name"); when(facility.getLocation()).thenReturn(location); when(motechService.getLocationByName("name")).thenReturn(null); when(errors.hasErrors()).thenReturn(false); String path = controller.submitAddFacility(webFacility, errors, modelMap, null); assertEquals(path,"redirect:/module/motechmodule/facility.form"); verify(locationService).saveLocation(location); verify(registrarBean).saveNewFacility(facility); verify(locationService,never()).getAllLocations(); }
@RequestMapping(value = "/module/motechmodule/addfacility.form", method = RequestMethod.POST) public String submitAddFacility(@ModelAttribute("facility") WebFacility facility, Errors errors,ModelMap modelMap, SessionStatus status){ if(contextService.getMotechService().getLocationByName(facility.getName()) != null){ errors.rejectValue("name","motechmodule.Facility.duplicate.location"); } if(errors.hasErrors()){ populateLocation(modelMap); return "/module/motechmodule/addfacility"; } contextService.getLocationService().saveLocation(facility.getFacility().getLocation()); contextService.getRegistrarBean().saveNewFacility(facility.getFacility()); return "redirect:/module/motechmodule/facility.form"; }
FacilityController { @RequestMapping(value = "/module/motechmodule/addfacility.form", method = RequestMethod.POST) public String submitAddFacility(@ModelAttribute("facility") WebFacility facility, Errors errors,ModelMap modelMap, SessionStatus status){ if(contextService.getMotechService().getLocationByName(facility.getName()) != null){ errors.rejectValue("name","motechmodule.Facility.duplicate.location"); } if(errors.hasErrors()){ populateLocation(modelMap); return "/module/motechmodule/addfacility"; } contextService.getLocationService().saveLocation(facility.getFacility().getLocation()); contextService.getRegistrarBean().saveNewFacility(facility.getFacility()); return "redirect:/module/motechmodule/facility.form"; } }
FacilityController { @RequestMapping(value = "/module/motechmodule/addfacility.form", method = RequestMethod.POST) public String submitAddFacility(@ModelAttribute("facility") WebFacility facility, Errors errors,ModelMap modelMap, SessionStatus status){ if(contextService.getMotechService().getLocationByName(facility.getName()) != null){ errors.rejectValue("name","motechmodule.Facility.duplicate.location"); } if(errors.hasErrors()){ populateLocation(modelMap); return "/module/motechmodule/addfacility"; } contextService.getLocationService().saveLocation(facility.getFacility().getLocation()); contextService.getRegistrarBean().saveNewFacility(facility.getFacility()); return "redirect:/module/motechmodule/facility.form"; } FacilityController();  FacilityController(ContextService contextService); }
FacilityController { @RequestMapping(value = "/module/motechmodule/addfacility.form", method = RequestMethod.POST) public String submitAddFacility(@ModelAttribute("facility") WebFacility facility, Errors errors,ModelMap modelMap, SessionStatus status){ if(contextService.getMotechService().getLocationByName(facility.getName()) != null){ errors.rejectValue("name","motechmodule.Facility.duplicate.location"); } if(errors.hasErrors()){ populateLocation(modelMap); return "/module/motechmodule/addfacility"; } contextService.getLocationService().saveLocation(facility.getFacility().getLocation()); contextService.getRegistrarBean().saveNewFacility(facility.getFacility()); return "redirect:/module/motechmodule/facility.form"; } FacilityController();  FacilityController(ContextService contextService); @ModelAttribute("facilities") List<Facility> getFacilities(); @RequestMapping(value = "/module/motechmodule/facility.form", method = RequestMethod.GET) String viewFacilities(); @RequestMapping(value = "/module/motechmodule/addfacility.form", method = RequestMethod.GET) String viewAddFacilityForm(ModelMap modelMap); @RequestMapping(value = "/module/motechmodule/addfacility.form", method = RequestMethod.POST) String submitAddFacility(@ModelAttribute("facility") WebFacility facility, Errors errors,ModelMap modelMap, SessionStatus status); @ModelAttribute("locations") List<Location> getLocations(); }
FacilityController { @RequestMapping(value = "/module/motechmodule/addfacility.form", method = RequestMethod.POST) public String submitAddFacility(@ModelAttribute("facility") WebFacility facility, Errors errors,ModelMap modelMap, SessionStatus status){ if(contextService.getMotechService().getLocationByName(facility.getName()) != null){ errors.rejectValue("name","motechmodule.Facility.duplicate.location"); } if(errors.hasErrors()){ populateLocation(modelMap); return "/module/motechmodule/addfacility"; } contextService.getLocationService().saveLocation(facility.getFacility().getLocation()); contextService.getRegistrarBean().saveNewFacility(facility.getFacility()); return "redirect:/module/motechmodule/facility.form"; } FacilityController();  FacilityController(ContextService contextService); @ModelAttribute("facilities") List<Facility> getFacilities(); @RequestMapping(value = "/module/motechmodule/facility.form", method = RequestMethod.GET) String viewFacilities(); @RequestMapping(value = "/module/motechmodule/addfacility.form", method = RequestMethod.GET) String viewAddFacilityForm(ModelMap modelMap); @RequestMapping(value = "/module/motechmodule/addfacility.form", method = RequestMethod.POST) String submitAddFacility(@ModelAttribute("facility") WebFacility facility, Errors errors,ModelMap modelMap, SessionStatus status); @ModelAttribute("locations") List<Location> getLocations();  }