Skip to content

Commit

Permalink
impl find owner. Closes springframeworkguru#54
Browse files Browse the repository at this point in the history
  • Loading branch information
springframeworkguru committed Sep 22, 2018
1 parent 327abc1 commit 545a411
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
import guru.springframework.sfgpetclinic.model.Owner;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

/**
* Created by jt on 8/5/18.
*/
public interface OwnerRepository extends CrudRepository<Owner, Long> {

Owner findByLastName(String lastName);

List<Owner> findAllByLastNameLike(String lastName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import guru.springframework.sfgpetclinic.model.Owner;

import java.util.List;


/**
* Created by jt on 7/18/18.
Expand All @@ -10,4 +12,5 @@ public interface OwnerService extends CrudService<Owner, Long> {

Owner findByLastName(String lastName);

}
List<Owner> findAllByLastNameLike(String lastName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
Expand Down Expand Up @@ -81,4 +82,11 @@ public Owner findByLastName(String lastName) {
.findFirst()
.orElse(null);
}

@Override
public List<Owner> findAllByLastNameLike(String lastName) {

//todo - impl
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
Expand All @@ -34,6 +35,11 @@ public Owner findByLastName(String lastName) {
return ownerRepository.findByLastName(lastName);
}

@Override
public List<Owner> findAllByLastNameLike(String lastName) {
return ownerRepository.findAllByLastNameLike(lastName);
}

@Override
public Set<Owner> findAll() {
Set<Owner> owners = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package guru.springframework.sfgpetclinic.controllers;

import guru.springframework.sfgpetclinic.model.Owner;
import guru.springframework.sfgpetclinic.services.OwnerService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
* Created by jt on 7/22/18.
*/
Expand All @@ -21,17 +27,41 @@ public OwnerController(OwnerService ownerService) {
this.ownerService = ownerService;
}

@RequestMapping({"", "/", "/index", "/index.html"})
public String listOwners(Model model){
@InitBinder
public void setAllowedFields(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields("id");
}

model.addAttribute("owners", ownerService.findAll());

return "owners/index";
@RequestMapping("/find")
public String findOwners(Model model){
model.addAttribute("owner", Owner.builder().build());
return "owners/findOwners";
}

@RequestMapping("/find")
public String findOwners(){
return "notimplemented";
@GetMapping
public String processFindForm(Owner owner, BindingResult result, Model model){
// allow parameterless GET request for /owners to return all records
if (owner.getLastName() == null) {
owner.setLastName(""); // empty string signifies broadest possible search
}

// find owners by last name
List<Owner> results = ownerService.findAllByLastNameLike(owner.getLastName());

if (results.isEmpty()) {
// no owners found
result.rejectValue("lastName", "notFound", "not found");
return "owners/findOwners";
} else if (results.size() == 1) {
// 1 owner found
owner = results.get(0);
return "redirect:/owners/" + owner.getId();
} else {
// multiple owners found
model.addAttribute("selections", results);
return "owners/ownersList";
}
}

@GetMapping("/{ownerId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import static org.hamcrest.Matchers.*;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
Expand Down Expand Up @@ -46,33 +48,34 @@ void setUp() {
}

@Test
void listOwners() throws Exception {
when(ownerService.findAll()).thenReturn(owners);

mockMvc.perform(get("/owners"))
void findOwners() throws Exception {
mockMvc.perform(get("/owners/find"))
.andExpect(status().isOk())
.andExpect(view().name("owners/index"))
.andExpect(model().attribute("owners", hasSize(2)));
.andExpect(view().name("owners/findOwners"))
.andExpect(model().attributeExists("owner"));

verifyZeroInteractions(ownerService);
}

@Test
void listOwnersByIndex() throws Exception {
when(ownerService.findAll()).thenReturn(owners);
void processFindFormReturnMany() throws Exception {
when(ownerService.findAllByLastNameLike(anyString()))
.thenReturn(Arrays.asList(Owner.builder().id(1l).build(),
Owner.builder().id(2l).build()));

mockMvc.perform(get("/owners/index"))
mockMvc.perform(get("/owners"))
.andExpect(status().isOk())
.andExpect(view().name("owners/index"))
.andExpect(model().attribute("owners", hasSize(2)));
.andExpect(view().name("owners/ownersList"))
.andExpect(model().attribute("selections", hasSize(2)));
}

@Test
void findOwners() throws Exception {
mockMvc.perform(get("/owners/find"))
.andExpect(status().isOk())
.andExpect(view().name("notimplemented"));
void processFindFormReturnOne() throws Exception {
when(ownerService.findAllByLastNameLike(anyString())).thenReturn(Arrays.asList(Owner.builder().id(1l).build()));

verifyZeroInteractions(ownerService);
mockMvc.perform(get("/owners"))
.andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/owners/1"));
}

@Test
Expand All @@ -84,4 +87,7 @@ void displayOwner() throws Exception {
.andExpect(view().name("owners/ownerDetails"))
.andExpect(model().attribute("owner", hasProperty("id", is(1l))));
}



}

0 comments on commit 545a411

Please sign in to comment.