mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-23 07:45:49 +00:00
Añadido servicio de Owner y modificados los tests
This commit is contained in:
parent
48ae594b90
commit
5317a2a58d
3 changed files with 62 additions and 34 deletions
|
@ -0,0 +1,34 @@
|
|||
package org.springframework.cheapy.service;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cheapy.model.Owner;
|
||||
import org.springframework.cheapy.repository.OwnerRepository;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class OwnerService {
|
||||
private OwnerRepository ownerRepository;
|
||||
|
||||
|
||||
@Autowired
|
||||
public OwnerService(final OwnerRepository ownerRepository) {
|
||||
this.ownerRepository = ownerRepository;
|
||||
}
|
||||
|
||||
public Owner findOwnerById(final int id) {
|
||||
return this.ownerRepository.findById(id);
|
||||
}
|
||||
|
||||
public Collection<Owner> findByLastName(final String lastname) { //
|
||||
return this.ownerRepository.findByLastName(lastname);
|
||||
|
||||
}
|
||||
|
||||
public void saveOwner(final Owner owner) throws DataAccessException { //
|
||||
this.ownerRepository.save(owner);
|
||||
|
||||
}
|
||||
}
|
|
@ -15,8 +15,14 @@
|
|||
*/
|
||||
package org.springframework.cheapy.web;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
import org.springframework.cheapy.model.Owner;
|
||||
import org.springframework.cheapy.repository.OwnerRepository;
|
||||
import org.springframework.cheapy.service.OwnerService;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.validation.BindingResult;
|
||||
|
@ -27,10 +33,6 @@ import org.springframework.web.bind.annotation.PathVariable;
|
|||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @author Ken Krebs
|
||||
|
@ -42,12 +44,12 @@ public class OwnerController {
|
|||
|
||||
private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm";
|
||||
|
||||
private final OwnerRepository owners;
|
||||
private final OwnerService ownerService;
|
||||
|
||||
|
||||
|
||||
public OwnerController(OwnerRepository clinicService) {
|
||||
this.owners = clinicService;
|
||||
public OwnerController(final OwnerService ownerService) {
|
||||
this.ownerService = ownerService;
|
||||
|
||||
}
|
||||
|
||||
|
@ -69,7 +71,7 @@ public class OwnerController {
|
|||
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
else {
|
||||
this.owners.save(owner);
|
||||
this.ownerService.saveOwner(owner);
|
||||
return "redirect:/owners/" + owner.getId();
|
||||
}
|
||||
}
|
||||
|
@ -89,7 +91,7 @@ public class OwnerController {
|
|||
}
|
||||
|
||||
// find owners by last name
|
||||
Collection<Owner> results = this.owners.findByLastName(owner.getLastName());
|
||||
Collection<Owner> results = this.ownerService.findByLastName(owner.getLastName());
|
||||
if (results.isEmpty()) {
|
||||
// no owners found
|
||||
result.rejectValue("lastName", "notFound", "not found");
|
||||
|
@ -109,7 +111,7 @@ public class OwnerController {
|
|||
|
||||
@GetMapping("/owners/{ownerId}/edit")
|
||||
public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) {
|
||||
Owner owner = this.owners.findById(ownerId);
|
||||
Owner owner = this.ownerService.findOwnerById(ownerId);
|
||||
model.addAttribute(owner);
|
||||
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
|
@ -122,14 +124,14 @@ public class OwnerController {
|
|||
}
|
||||
else {
|
||||
owner.setId(ownerId);
|
||||
this.owners.save(owner);
|
||||
this.ownerService.saveOwner(owner);
|
||||
return "redirect:/owners/{ownerId}";
|
||||
}
|
||||
}
|
||||
@GetMapping("/owners/{ownerId}")
|
||||
public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) {
|
||||
ModelAndView mav = new ModelAndView("owners/ownerDetails");
|
||||
Owner owner = this.owners.findById(ownerId);
|
||||
Owner owner = this.ownerService.findOwnerById(ownerId);
|
||||
|
||||
mav.addObject(owner);
|
||||
return mav;
|
||||
|
|
|
@ -16,26 +16,8 @@
|
|||
|
||||
package org.springframework.cheapy.web;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.assertj.core.util.Lists;
|
||||
import org.hamcrest.BaseMatcher;
|
||||
import org.hamcrest.Description;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.cheapy.model.Owner;
|
||||
import org.springframework.cheapy.repository.OwnerRepository;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import static org.hamcrest.Matchers.empty;
|
||||
import static org.hamcrest.Matchers.hasProperty;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
|
@ -43,6 +25,16 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
|
||||
|
||||
import org.assertj.core.util.Lists;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.cheapy.model.Owner;
|
||||
import org.springframework.cheapy.service.OwnerService;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
/**
|
||||
* Test class for {@link OwnerController}
|
||||
*
|
||||
|
@ -57,7 +49,7 @@ class OwnerControllerTests {
|
|||
private MockMvc mockMvc;
|
||||
|
||||
@MockBean
|
||||
private OwnerRepository owners;
|
||||
private OwnerService ownerService;
|
||||
|
||||
|
||||
private Owner george;
|
||||
|
@ -72,7 +64,7 @@ class OwnerControllerTests {
|
|||
george.setCity("Madison");
|
||||
george.setTelephone("6085551023");
|
||||
|
||||
given(this.owners.findById(TEST_OWNER_ID)).willReturn(george);
|
||||
given(this.ownerService.findOwnerById(TEST_OWNER_ID)).willReturn(george);
|
||||
|
||||
|
||||
}
|
||||
|
@ -108,13 +100,13 @@ class OwnerControllerTests {
|
|||
|
||||
@Test
|
||||
void testProcessFindFormSuccess() throws Exception {
|
||||
given(this.owners.findByLastName("")).willReturn(Lists.newArrayList(george, new Owner()));
|
||||
given(this.ownerService.findByLastName("")).willReturn(Lists.newArrayList(george, new Owner()));
|
||||
mockMvc.perform(get("/owners")).andExpect(status().isOk()).andExpect(view().name("owners/ownersList"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testProcessFindFormByLastName() throws Exception {
|
||||
given(this.owners.findByLastName(george.getLastName())).willReturn(Lists.newArrayList(george));
|
||||
given(this.ownerService.findByLastName(george.getLastName())).willReturn(Lists.newArrayList(george));
|
||||
mockMvc.perform(get("/owners").param("lastName", "Franklin")).andExpect(status().is3xxRedirection())
|
||||
.andExpect(view().name("redirect:/owners/" + TEST_OWNER_ID));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue