@Test public void shouldPopulateWithLocationDataWhenShowingNewFacilityForm() { Location l1 = getLocation("C1", "R1", "D1", "P1"); Location l2 = getLocation("C1", "R1", "D2", "P2"); Location l3 = getLocation("C1", "R2", "D3", "P3"); Location l4 = getLocation("C1", "R2", "D4", "P4"); when(locationService.getAllLocations()).thenReturn(Arrays.asList(l1, l2, l3, l4)); ModelMap modelMap = new ModelMap(); String path = controller.viewAddFacilityForm(modelMap); assertEquals("/module/motechmodule/addfacility", path); Set<String> countries = (Set<String>) modelMap.get("countries"); assertTrue(countries.contains("C1")); Map<String, TreeSet<String>> m1 = (Map<String, TreeSet<String>>) modelMap.get("regions"); TreeSet<String> regions = m1.get("C1"); assertTrue(regions.containsAll(Arrays.asList("R1", "R2"))); Map<String, TreeSet<String>> m2 = (Map<String, TreeSet<String>>) modelMap.get("districts"); TreeSet<String> districts = m2.get("R1"); assertTrue(districts.containsAll(Arrays.asList("D1", "D2"))); Map<String, TreeSet<String>> m3 = (Map<String, TreeSet<String>>) modelMap.get("provinces"); TreeSet<String> provinces = m3.get("D1"); assertTrue(provinces.containsAll(Arrays.asList("P1"))); verify(contextService).getLocationService(); verify(locationService).getAllLocations(); }
@RequestMapping(value = "/module/motechmodule/addfacility.form", method = RequestMethod.GET) public String viewAddFacilityForm(ModelMap modelMap){ populateLocation(modelMap); modelMap.addAttribute("facility", new WebFacility()); return "/module/motechmodule/addfacility"; }
FacilityController { @RequestMapping(value = "/module/motechmodule/addfacility.form", method = RequestMethod.GET) public String viewAddFacilityForm(ModelMap modelMap){ populateLocation(modelMap); modelMap.addAttribute("facility", new WebFacility()); return "/module/motechmodule/addfacility"; } }
FacilityController { @RequestMapping(value = "/module/motechmodule/addfacility.form", method = RequestMethod.GET) public String viewAddFacilityForm(ModelMap modelMap){ populateLocation(modelMap); modelMap.addAttribute("facility", new WebFacility()); return "/module/motechmodule/addfacility"; } FacilityController();  FacilityController(ContextService contextService); }
FacilityController { @RequestMapping(value = "/module/motechmodule/addfacility.form", method = RequestMethod.GET) public String viewAddFacilityForm(ModelMap modelMap){ populateLocation(modelMap); modelMap.addAttribute("facility", new WebFacility()); return "/module/motechmodule/addfacility"; } 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.GET) public String viewAddFacilityForm(ModelMap modelMap){ populateLocation(modelMap); modelMap.addAttribute("facility", new WebFacility()); return "/module/motechmodule/addfacility"; } 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();  }