From 5317a2a58d6ad75697a3dd7eabefbc716d016196 Mon Sep 17 00:00:00 2001 From: abemorcardc Date: Thu, 18 Mar 2021 18:32:21 +0100 Subject: [PATCH] =?UTF-8?q?A=C3=B1adido=20servicio=20de=20Owner=20y=20modi?= =?UTF-8?q?ficados=20los=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cheapy/service/OwnerService.java | 34 ++++++++++++++++++ .../cheapy/web/OwnerController.java | 26 +++++++------- .../cheapy/web/OwnerControllerTests.java | 36 ++++++++----------- 3 files changed, 62 insertions(+), 34 deletions(-) create mode 100644 src/main/java/org/springframework/cheapy/service/OwnerService.java diff --git a/src/main/java/org/springframework/cheapy/service/OwnerService.java b/src/main/java/org/springframework/cheapy/service/OwnerService.java new file mode 100644 index 000000000..da5559f37 --- /dev/null +++ b/src/main/java/org/springframework/cheapy/service/OwnerService.java @@ -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 findByLastName(final String lastname) { // + return this.ownerRepository.findByLastName(lastname); + + } + + public void saveOwner(final Owner owner) throws DataAccessException { // + this.ownerRepository.save(owner); + + } +} diff --git a/src/main/java/org/springframework/cheapy/web/OwnerController.java b/src/main/java/org/springframework/cheapy/web/OwnerController.java index e12697ff4..d95e5d120 100644 --- a/src/main/java/org/springframework/cheapy/web/OwnerController.java +++ b/src/main/java/org/springframework/cheapy/web/OwnerController.java @@ -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 results = this.owners.findByLastName(owner.getLastName()); + Collection 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; diff --git a/src/test/java/org/springframework/cheapy/web/OwnerControllerTests.java b/src/test/java/org/springframework/cheapy/web/OwnerControllerTests.java index f6963accf..9e5997489 100644 --- a/src/test/java/org/springframework/cheapy/web/OwnerControllerTests.java +++ b/src/test/java/org/springframework/cheapy/web/OwnerControllerTests.java @@ -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)); }