@Test public void testDeleteDiseaseTrait() throws Exception { when(studyRepository.findByDiseaseTraitId(DISEASE_TRAIT_1.getId())).thenReturn(Collections.EMPTY_LIST); mockMvc.perform(post("/diseasetraits/799/delete") .param("id", "799")) .andExpect(status().is3xxRedirection()) .andExpect(view().name("redirect:/diseasetraits")); ArgumentCaptor<Long> diseaseTraitIdArgumentCaptor = ArgumentCaptor.forClass(Long.class); verify(diseaseTraitRepository).delete(diseaseTraitIdArgumentCaptor.capture()); assertEquals(799L, diseaseTraitIdArgumentCaptor.getValue().longValue()); }
@RequestMapping(value = "/{diseaseTraitId}/delete", produces = MediaType.TEXT_HTML_VALUE, method = RequestMethod.POST) public String deleteDiseaseTrait(@PathVariable Long diseaseTraitId, RedirectAttributes redirectAttributes) { Collection<Study> studiesWithDiseaseTrait = studyRepository.findByDiseaseTraitId(diseaseTraitId); if (!studiesWithDiseaseTrait.isEmpty()) { String message = "Trait is used in " + studiesWithDiseaseTrait.size() + " study/studies, cannot delete!"; redirectAttributes.addFlashAttribute("diseaseTraitUsed", message); return "redirect:/diseasetraits/" + diseaseTraitId + "/delete"; } else { diseaseTraitRepository.delete(diseaseTraitId); return "redirect:/diseasetraits"; } }
DiseaseTraitController { @RequestMapping(value = "/{diseaseTraitId}/delete", produces = MediaType.TEXT_HTML_VALUE, method = RequestMethod.POST) public String deleteDiseaseTrait(@PathVariable Long diseaseTraitId, RedirectAttributes redirectAttributes) { Collection<Study> studiesWithDiseaseTrait = studyRepository.findByDiseaseTraitId(diseaseTraitId); if (!studiesWithDiseaseTrait.isEmpty()) { String message = "Trait is used in " + studiesWithDiseaseTrait.size() + " study/studies, cannot delete!"; redirectAttributes.addFlashAttribute("diseaseTraitUsed", message); return "redirect:/diseasetraits/" + diseaseTraitId + "/delete"; } else { diseaseTraitRepository.delete(diseaseTraitId); return "redirect:/diseasetraits"; } } }
DiseaseTraitController { @RequestMapping(value = "/{diseaseTraitId}/delete", produces = MediaType.TEXT_HTML_VALUE, method = RequestMethod.POST) public String deleteDiseaseTrait(@PathVariable Long diseaseTraitId, RedirectAttributes redirectAttributes) { Collection<Study> studiesWithDiseaseTrait = studyRepository.findByDiseaseTraitId(diseaseTraitId); if (!studiesWithDiseaseTrait.isEmpty()) { String message = "Trait is used in " + studiesWithDiseaseTrait.size() + " study/studies, cannot delete!"; redirectAttributes.addFlashAttribute("diseaseTraitUsed", message); return "redirect:/diseasetraits/" + diseaseTraitId + "/delete"; } else { diseaseTraitRepository.delete(diseaseTraitId); return "redirect:/diseasetraits"; } } @Autowired  DiseaseTraitController(DiseaseTraitRepository diseaseTraitRepository, StudyRepository studyRepository); }
DiseaseTraitController { @RequestMapping(value = "/{diseaseTraitId}/delete", produces = MediaType.TEXT_HTML_VALUE, method = RequestMethod.POST) public String deleteDiseaseTrait(@PathVariable Long diseaseTraitId, RedirectAttributes redirectAttributes) { Collection<Study> studiesWithDiseaseTrait = studyRepository.findByDiseaseTraitId(diseaseTraitId); if (!studiesWithDiseaseTrait.isEmpty()) { String message = "Trait is used in " + studiesWithDiseaseTrait.size() + " study/studies, cannot delete!"; redirectAttributes.addFlashAttribute("diseaseTraitUsed", message); return "redirect:/diseasetraits/" + diseaseTraitId + "/delete"; } else { diseaseTraitRepository.delete(diseaseTraitId); return "redirect:/diseasetraits"; } } @Autowired  DiseaseTraitController(DiseaseTraitRepository diseaseTraitRepository, StudyRepository studyRepository); @RequestMapping(produces = MediaType.TEXT_HTML_VALUE, method = RequestMethod.GET) String allDiseaseTraits(Model model); @RequestMapping(produces = MediaType.TEXT_HTML_VALUE, method = RequestMethod.POST) String addDiseaseTrait(@Valid @ModelAttribute DiseaseTrait diseaseTrait,
                                  BindingResult bindingResult,
                                  Model model, RedirectAttributes redirectAttributes); @RequestMapping(value = "/{diseaseTraitId}", produces = MediaType.TEXT_HTML_VALUE, method = RequestMethod.GET) String viewDiseaseTrait(Model model, @PathVariable Long diseaseTraitId); @RequestMapping(value = "/{diseaseTraitId}", produces = MediaType.TEXT_HTML_VALUE, method = RequestMethod.POST) String editDiseaseTrait(@Valid @ModelAttribute DiseaseTrait diseaseTrait,
                                   BindingResult bindingResult, @PathVariable Long diseaseTraitId); @RequestMapping(value = "/{diseaseTraitId}/delete", produces = MediaType.TEXT_HTML_VALUE, method = RequestMethod.GET) String viewDiseaseTraitToDelete(Model model, @PathVariable Long diseaseTraitId); @RequestMapping(value = "/{diseaseTraitId}/delete", produces = MediaType.TEXT_HTML_VALUE, method = RequestMethod.POST) String deleteDiseaseTrait(@PathVariable Long diseaseTraitId, RedirectAttributes redirectAttributes); @ModelAttribute("diseaseTraits") List<DiseaseTrait> populateDiseaseTraits(); }
DiseaseTraitController { @RequestMapping(value = "/{diseaseTraitId}/delete", produces = MediaType.TEXT_HTML_VALUE, method = RequestMethod.POST) public String deleteDiseaseTrait(@PathVariable Long diseaseTraitId, RedirectAttributes redirectAttributes) { Collection<Study> studiesWithDiseaseTrait = studyRepository.findByDiseaseTraitId(diseaseTraitId); if (!studiesWithDiseaseTrait.isEmpty()) { String message = "Trait is used in " + studiesWithDiseaseTrait.size() + " study/studies, cannot delete!"; redirectAttributes.addFlashAttribute("diseaseTraitUsed", message); return "redirect:/diseasetraits/" + diseaseTraitId + "/delete"; } else { diseaseTraitRepository.delete(diseaseTraitId); return "redirect:/diseasetraits"; } } @Autowired  DiseaseTraitController(DiseaseTraitRepository diseaseTraitRepository, StudyRepository studyRepository); @RequestMapping(produces = MediaType.TEXT_HTML_VALUE, method = RequestMethod.GET) String allDiseaseTraits(Model model); @RequestMapping(produces = MediaType.TEXT_HTML_VALUE, method = RequestMethod.POST) String addDiseaseTrait(@Valid @ModelAttribute DiseaseTrait diseaseTrait,
                                  BindingResult bindingResult,
                                  Model model, RedirectAttributes redirectAttributes); @RequestMapping(value = "/{diseaseTraitId}", produces = MediaType.TEXT_HTML_VALUE, method = RequestMethod.GET) String viewDiseaseTrait(Model model, @PathVariable Long diseaseTraitId); @RequestMapping(value = "/{diseaseTraitId}", produces = MediaType.TEXT_HTML_VALUE, method = RequestMethod.POST) String editDiseaseTrait(@Valid @ModelAttribute DiseaseTrait diseaseTrait,
                                   BindingResult bindingResult, @PathVariable Long diseaseTraitId); @RequestMapping(value = "/{diseaseTraitId}/delete", produces = MediaType.TEXT_HTML_VALUE, method = RequestMethod.GET) String viewDiseaseTraitToDelete(Model model, @PathVariable Long diseaseTraitId); @RequestMapping(value = "/{diseaseTraitId}/delete", produces = MediaType.TEXT_HTML_VALUE, method = RequestMethod.POST) String deleteDiseaseTrait(@PathVariable Long diseaseTraitId, RedirectAttributes redirectAttributes); @ModelAttribute("diseaseTraits") List<DiseaseTrait> populateDiseaseTraits();  }