Añadido servicio de Owner y modificados los tests

This commit is contained in:
abemorcardc 2021-03-18 18:32:21 +01:00
parent 48ae594b90
commit 5317a2a58d
3 changed files with 62 additions and 34 deletions

View file

@ -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);
}
}

View file

@ -15,8 +15,14 @@
*/ */
package org.springframework.cheapy.web; 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.model.Owner;
import org.springframework.cheapy.repository.OwnerRepository; import org.springframework.cheapy.repository.OwnerRepository;
import org.springframework.cheapy.service.OwnerService;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.validation.BindingResult; 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.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import javax.validation.Valid;
import java.util.Collection;
import java.util.Map;
/** /**
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Ken Krebs * @author Ken Krebs
@ -42,12 +44,12 @@ public class OwnerController {
private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm"; private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm";
private final OwnerRepository owners; private final OwnerService ownerService;
public OwnerController(OwnerRepository clinicService) { public OwnerController(final OwnerService ownerService) {
this.owners = clinicService; this.ownerService = ownerService;
} }
@ -69,7 +71,7 @@ public class OwnerController {
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
} }
else { else {
this.owners.save(owner); this.ownerService.saveOwner(owner);
return "redirect:/owners/" + owner.getId(); return "redirect:/owners/" + owner.getId();
} }
} }
@ -89,7 +91,7 @@ public class OwnerController {
} }
// find owners by last name // find owners by last name
Collection<Owner> results = this.owners.findByLastName(owner.getLastName()); Collection<Owner> results = this.ownerService.findByLastName(owner.getLastName());
if (results.isEmpty()) { if (results.isEmpty()) {
// no owners found // no owners found
result.rejectValue("lastName", "notFound", "not found"); result.rejectValue("lastName", "notFound", "not found");
@ -109,7 +111,7 @@ public class OwnerController {
@GetMapping("/owners/{ownerId}/edit") @GetMapping("/owners/{ownerId}/edit")
public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) { public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) {
Owner owner = this.owners.findById(ownerId); Owner owner = this.ownerService.findOwnerById(ownerId);
model.addAttribute(owner); model.addAttribute(owner);
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
} }
@ -122,14 +124,14 @@ public class OwnerController {
} }
else { else {
owner.setId(ownerId); owner.setId(ownerId);
this.owners.save(owner); this.ownerService.saveOwner(owner);
return "redirect:/owners/{ownerId}"; return "redirect:/owners/{ownerId}";
} }
} }
@GetMapping("/owners/{ownerId}") @GetMapping("/owners/{ownerId}")
public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) { public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) {
ModelAndView mav = new ModelAndView("owners/ownerDetails"); ModelAndView mav = new ModelAndView("owners/ownerDetails");
Owner owner = this.owners.findById(ownerId); Owner owner = this.ownerService.findOwnerById(ownerId);
mav.addObject(owner); mav.addObject(owner);
return mav; return mav;

View file

@ -16,26 +16,8 @@
package org.springframework.cheapy.web; 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.hasProperty;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.mockito.BDDMockito.given; 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.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; 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.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; 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} * Test class for {@link OwnerController}
* *
@ -57,7 +49,7 @@ class OwnerControllerTests {
private MockMvc mockMvc; private MockMvc mockMvc;
@MockBean @MockBean
private OwnerRepository owners; private OwnerService ownerService;
private Owner george; private Owner george;
@ -72,7 +64,7 @@ class OwnerControllerTests {
george.setCity("Madison"); george.setCity("Madison");
george.setTelephone("6085551023"); 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 @Test
void testProcessFindFormSuccess() throws Exception { 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")); mockMvc.perform(get("/owners")).andExpect(status().isOk()).andExpect(view().name("owners/ownersList"));
} }
@Test @Test
void testProcessFindFormByLastName() throws Exception { 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()) mockMvc.perform(get("/owners").param("lastName", "Franklin")).andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/owners/" + TEST_OWNER_ID)); .andExpect(view().name("redirect:/owners/" + TEST_OWNER_ID));
} }