LocationController { @RequestMapping(method = {RequestMethod.GET, RequestMethod.OPTIONS}, value = "/villages") public ResponseEntity<VillagesDTO> villagesForANM(@RequestParam("anm-id") String anmIdentifier) { List villagesForANM = anmService.getVillagesForANM(anmIdentifier); VillagesDTO villagesDTO = null; if (villagesForANM != null) { Location anmLocation = (Location) villagesForANM.get(0); List<String> villages = collect(villagesForANM, on(Location.class).village()); villagesDTO = new VillagesDTO(anmLocation.district().toLowerCase(), anmLocation.phcName(), anmLocation.phc().phcIdentifier(), anmLocation.subCenter(), villages); } return new ResponseEntity<>(villagesDTO, OK); } @Autowired  LocationController(ANMService anmService); @RequestMapping(method = {RequestMethod.GET, RequestMethod.OPTIONS}, value = "/villages") ResponseEntity<VillagesDTO> villagesForANM(@RequestParam("anm-id") String anmIdentifier);  }
@Test public void shouldReturnListOfVillagesForANM() throws Exception { when(anmService.getVillagesForANM("demo1")).thenReturn( asList( new Location("village1", "sc", phc, "taluk", "District", "state"), new Location("village2", "sc", phc, "taluk", "District", "state") )); when(anmService.getLocation("demo1")).thenReturn(location); when(location.phc()).thenReturn(phc); when(location.subCenter()).thenReturn("sc"); when(phc.phcIdentifier()).thenReturn("phc1"); when(phc.name()).thenReturn("PHC X"); when(location.phcName()).thenReturn("PHC X"); ResponseEntity<VillagesDTO> response = controller.villagesForANM("demo1"); VillagesDTO villagesDTO = new VillagesDTO("district", "PHC X", "phc1", "sc", asList("village1", "village2")); assertEquals(villagesDTO, response.getBody()); }