@Test public void testAddDiseaseTrait() throws Exception { when(diseaseTraitRepository.findByTraitIgnoreCase(Matchers.anyString())).thenReturn(null); mockMvc.perform(post("/diseasetraits") .param("trait", NEW_DISEASE_TRAIT.getTrait())) .andExpect(status().is3xxRedirection()) .andExpect(flash().attribute("diseaseTraitSaved", "Trait Urate levels added to database")) .andExpect(view().name("redirect:/diseasetraits")); ArgumentCaptor<DiseaseTrait> diseaseTraitArgumentCaptor = ArgumentCaptor.forClass(DiseaseTrait.class); verify(diseaseTraitRepository).save(diseaseTraitArgumentCaptor.capture()); assertEquals(NEW_DISEASE_TRAIT.getTrait(), diseaseTraitArgumentCaptor.getValue().getTrait()); verifyZeroInteractions(studyRepository); }
@RequestMapping(produces = MediaType.TEXT_HTML_VALUE, method = RequestMethod.POST) public String addDiseaseTrait(@Valid @ModelAttribute DiseaseTrait diseaseTrait, BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes) { DiseaseTrait existingDiseaseTrait = diseaseTraitRepository.findByTraitIgnoreCase(diseaseTrait.getTrait()); String existingTrait = null; if (existingDiseaseTrait != null) { existingTrait = existingDiseaseTrait.getTrait(); } if (bindingResult.hasErrors()) { model.addAttribute("diseaseTraits", diseaseTraitRepository.findAll(sortByTraitAsc())); return "disease_traits"; } else if (existingTrait != null && !existingTrait.isEmpty()) { String message = "Trait already exists in database: database value = " + existingTrait + ", value entered = " + diseaseTrait.getTrait(); redirectAttributes.addFlashAttribute("diseaseTraitExists", message); return "redirect:/diseasetraits"; } else { diseaseTraitRepository.save(diseaseTrait); String message = "Trait " + diseaseTrait.getTrait() + " added to database"; redirectAttributes.addFlashAttribute("diseaseTraitSaved", message); return "redirect:/diseasetraits"; } }
DiseaseTraitController { @RequestMapping(produces = MediaType.TEXT_HTML_VALUE, method = RequestMethod.POST) public String addDiseaseTrait(@Valid @ModelAttribute DiseaseTrait diseaseTrait, BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes) { DiseaseTrait existingDiseaseTrait = diseaseTraitRepository.findByTraitIgnoreCase(diseaseTrait.getTrait()); String existingTrait = null; if (existingDiseaseTrait != null) { existingTrait = existingDiseaseTrait.getTrait(); } if (bindingResult.hasErrors()) { model.addAttribute("diseaseTraits", diseaseTraitRepository.findAll(sortByTraitAsc())); return "disease_traits"; } else if (existingTrait != null && !existingTrait.isEmpty()) { String message = "Trait already exists in database: database value = " + existingTrait + ", value entered = " + diseaseTrait.getTrait(); redirectAttributes.addFlashAttribute("diseaseTraitExists", message); return "redirect:/diseasetraits"; } else { diseaseTraitRepository.save(diseaseTrait); String message = "Trait " + diseaseTrait.getTrait() + " added to database"; redirectAttributes.addFlashAttribute("diseaseTraitSaved", message); return "redirect:/diseasetraits"; } } }
DiseaseTraitController { @RequestMapping(produces = MediaType.TEXT_HTML_VALUE, method = RequestMethod.POST) public String addDiseaseTrait(@Valid @ModelAttribute DiseaseTrait diseaseTrait, BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes) { DiseaseTrait existingDiseaseTrait = diseaseTraitRepository.findByTraitIgnoreCase(diseaseTrait.getTrait()); String existingTrait = null; if (existingDiseaseTrait != null) { existingTrait = existingDiseaseTrait.getTrait(); } if (bindingResult.hasErrors()) { model.addAttribute("diseaseTraits", diseaseTraitRepository.findAll(sortByTraitAsc())); return "disease_traits"; } else if (existingTrait != null && !existingTrait.isEmpty()) { String message = "Trait already exists in database: database value = " + existingTrait + ", value entered = " + diseaseTrait.getTrait(); redirectAttributes.addFlashAttribute("diseaseTraitExists", message); return "redirect:/diseasetraits"; } else { diseaseTraitRepository.save(diseaseTrait); String message = "Trait " + diseaseTrait.getTrait() + " added to database"; redirectAttributes.addFlashAttribute("diseaseTraitSaved", message); return "redirect:/diseasetraits"; } } @Autowired  DiseaseTraitController(DiseaseTraitRepository diseaseTraitRepository, StudyRepository studyRepository); }
DiseaseTraitController { @RequestMapping(produces = MediaType.TEXT_HTML_VALUE, method = RequestMethod.POST) public String addDiseaseTrait(@Valid @ModelAttribute DiseaseTrait diseaseTrait, BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes) { DiseaseTrait existingDiseaseTrait = diseaseTraitRepository.findByTraitIgnoreCase(diseaseTrait.getTrait()); String existingTrait = null; if (existingDiseaseTrait != null) { existingTrait = existingDiseaseTrait.getTrait(); } if (bindingResult.hasErrors()) { model.addAttribute("diseaseTraits", diseaseTraitRepository.findAll(sortByTraitAsc())); return "disease_traits"; } else if (existingTrait != null && !existingTrait.isEmpty()) { String message = "Trait already exists in database: database value = " + existingTrait + ", value entered = " + diseaseTrait.getTrait(); redirectAttributes.addFlashAttribute("diseaseTraitExists", message); return "redirect:/diseasetraits"; } else { diseaseTraitRepository.save(diseaseTrait); String message = "Trait " + diseaseTrait.getTrait() + " added to database"; redirectAttributes.addFlashAttribute("diseaseTraitSaved", message); 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(produces = MediaType.TEXT_HTML_VALUE, method = RequestMethod.POST) public String addDiseaseTrait(@Valid @ModelAttribute DiseaseTrait diseaseTrait, BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes) { DiseaseTrait existingDiseaseTrait = diseaseTraitRepository.findByTraitIgnoreCase(diseaseTrait.getTrait()); String existingTrait = null; if (existingDiseaseTrait != null) { existingTrait = existingDiseaseTrait.getTrait(); } if (bindingResult.hasErrors()) { model.addAttribute("diseaseTraits", diseaseTraitRepository.findAll(sortByTraitAsc())); return "disease_traits"; } else if (existingTrait != null && !existingTrait.isEmpty()) { String message = "Trait already exists in database: database value = " + existingTrait + ", value entered = " + diseaseTrait.getTrait(); redirectAttributes.addFlashAttribute("diseaseTraitExists", message); return "redirect:/diseasetraits"; } else { diseaseTraitRepository.save(diseaseTrait); String message = "Trait " + diseaseTrait.getTrait() + " added to database"; redirectAttributes.addFlashAttribute("diseaseTraitSaved", message); 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();  }