From 8cf51fa0c77db2ba669aee48b43b932fa266158e Mon Sep 17 00:00:00 2001 From: abemorcardc Date: Fri, 16 Apr 2021 19:10:45 +0200 Subject: [PATCH] =?UTF-8?q?A=C3=B1adidos=20algunos=20test=20de=20cliente?= =?UTF-8?q?=20sobre=20su=20informaci=C3=B3n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springframework/cheapy/model/Client.java | 4 +- .../cheapy/repository/ClientRepository.java | 7 +- .../cheapy/service/ClientService.java | 8 +- .../cheapy/web/ClientController.java | 11 +- .../jsp/clients/createOrUpdateClientForm.jsp | 8 +- .../cheapy/web/ClientControllerTests.java | 153 ++++++++++++++++++ 6 files changed, 171 insertions(+), 20 deletions(-) create mode 100644 src/test/java/org/springframework/cheapy/web/ClientControllerTests.java diff --git a/src/main/java/org/springframework/cheapy/model/Client.java b/src/main/java/org/springframework/cheapy/model/Client.java index 2f1e69bc4..a23af67d7 100644 --- a/src/main/java/org/springframework/cheapy/model/Client.java +++ b/src/main/java/org/springframework/cheapy/model/Client.java @@ -12,9 +12,9 @@ import javax.persistence.JoinColumn; import javax.persistence.OneToMany; import javax.persistence.OneToOne; import javax.persistence.Table; -import javax.validation.constraints.Digits; import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; import org.springframework.format.annotation.DateTimeFormat; @@ -51,7 +51,7 @@ public class Client extends BaseEntity { private LocalTime finish; @NotEmpty - @Digits(fraction = 0, integer = 10) + @Pattern(regexp="\\d{9}",message="Debe tener 9 dígitos") private String telephone; @NotEmpty diff --git a/src/main/java/org/springframework/cheapy/repository/ClientRepository.java b/src/main/java/org/springframework/cheapy/repository/ClientRepository.java index 7b6775c45..3fd815228 100644 --- a/src/main/java/org/springframework/cheapy/repository/ClientRepository.java +++ b/src/main/java/org/springframework/cheapy/repository/ClientRepository.java @@ -1,15 +1,10 @@ package org.springframework.cheapy.repository; -import java.util.Optional; import java.util.List; - import org.springframework.cheapy.model.Client; -import org.springframework.cheapy.model.Code; -import org.springframework.cheapy.model.Usuario; import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.Repository; import org.springframework.transaction.annotation.Transactional; @@ -19,7 +14,7 @@ public interface ClientRepository extends Repository { @Transactional(readOnly = true) Client findByUsername(String username); - Optional findById(Integer id); + Client findById(Integer id); // void save(Client client); diff --git a/src/main/java/org/springframework/cheapy/service/ClientService.java b/src/main/java/org/springframework/cheapy/service/ClientService.java index e65eec3a7..799c1eb7c 100644 --- a/src/main/java/org/springframework/cheapy/service/ClientService.java +++ b/src/main/java/org/springframework/cheapy/service/ClientService.java @@ -1,13 +1,10 @@ package org.springframework.cheapy.service; import java.util.List; -import javax.validation.Valid; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cheapy.model.Client; import org.springframework.cheapy.model.Code; -import org.springframework.cheapy.model.Usuario; import org.springframework.cheapy.repository.ClientRepository; import org.springframework.cheapy.repository.CodeRepository; import org.springframework.dao.DataAccessException; @@ -53,6 +50,11 @@ public class ClientService { return this.clientRepository.findByUsername(username); } + @Transactional + public Client findById(Integer id) throws DataAccessException { + return this.clientRepository.findById(id); + } + @Transactional public List findAllClient() throws DataAccessException { return this.clientRepository.findAllClient(); diff --git a/src/main/java/org/springframework/cheapy/web/ClientController.java b/src/main/java/org/springframework/cheapy/web/ClientController.java index 7bc63dbc7..073d3cf8b 100644 --- a/src/main/java/org/springframework/cheapy/web/ClientController.java +++ b/src/main/java/org/springframework/cheapy/web/ClientController.java @@ -9,7 +9,6 @@ import javax.servlet.http.HttpServletRequest; import javax.validation.Valid; import org.springframework.beans.BeanUtils; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cheapy.model.Client; import org.springframework.cheapy.model.FoodOffer; import org.springframework.cheapy.model.Municipio; @@ -17,7 +16,6 @@ import org.springframework.cheapy.model.NuOffer; import org.springframework.cheapy.model.SpeedOffer; import org.springframework.cheapy.model.StatusOffer; import org.springframework.cheapy.model.TimeOffer; -import org.springframework.cheapy.repository.ClientRepository; import org.springframework.cheapy.service.ClientService; import org.springframework.cheapy.service.FoodOfferService; import org.springframework.cheapy.service.NuOfferService; @@ -45,8 +43,6 @@ public class ClientController { private final TimeOfferService timeOfferService; - @Autowired - private ClientRepository clientRepo; public ClientController(final ClientService clientService, FoodOfferService foodOfferService, @@ -101,6 +97,11 @@ public class ClientController { if(!this.checkTimes(clientEdit)) { result.rejectValue("finish","" ,"La hora de cierre debe ser posterior a la hora de apertura"); + } + + if(clientEdit.getUsuar().getPassword().equals("")) { + result.rejectValue("usuar.password","" ,"La contraseña no puede estar vacía"); + } @@ -162,7 +163,7 @@ public class ClientController { @GetMapping(value = "/restaurant/{clientId}") public String showRestaurant(final ModelMap model, @PathVariable("clientId") Integer id) { - Client client = this.clientRepo.findById(id).get(); + Client client = this.clientService.findById(id); model.put("client", client); return "clients/restaurantShow"; } diff --git a/src/main/webapp/WEB-INF/jsp/clients/createOrUpdateClientForm.jsp b/src/main/webapp/WEB-INF/jsp/clients/createOrUpdateClientForm.jsp index 91ed84126..670d62215 100644 --- a/src/main/webapp/WEB-INF/jsp/clients/createOrUpdateClientForm.jsp +++ b/src/main/webapp/WEB-INF/jsp/clients/createOrUpdateClientForm.jsp @@ -16,14 +16,14 @@
- + - - - + + +
diff --git a/src/test/java/org/springframework/cheapy/web/ClientControllerTests.java b/src/test/java/org/springframework/cheapy/web/ClientControllerTests.java new file mode 100644 index 000000000..8c5e7abad --- /dev/null +++ b/src/test/java/org/springframework/cheapy/web/ClientControllerTests.java @@ -0,0 +1,153 @@ +package org.springframework.cheapy.web; + +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; +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.result.MockMvcResultMatchers.model; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + +import java.time.LocalTime; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.BDDMockito; +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.configuration.SecurityConfiguration; +import org.springframework.cheapy.model.Client; +import org.springframework.cheapy.model.Code; +import org.springframework.cheapy.model.User; +import org.springframework.cheapy.service.ClientService; +import org.springframework.cheapy.service.FoodOfferService; +import org.springframework.cheapy.service.NuOfferService; +import org.springframework.cheapy.service.SpeedOfferService; +import org.springframework.cheapy.service.TimeOfferService; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.FilterType; +import org.springframework.security.config.annotation.web.WebSecurityConfigurer; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.web.servlet.MockMvc; + + + +@WebMvcTest(value = ClientController.class, +excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = WebSecurityConfigurer.class), +excludeAutoConfiguration = SecurityConfiguration.class) +class ClientControllerTest { + + private static final int TEST_CLIENT_ID = 1; + + @Autowired + private MockMvc mockMvc; + + @MockBean + private ClientService clientService; + + @MockBean + private FoodOfferService foodOfferService; + + @MockBean + private SpeedOfferService speedOfferService; + + @MockBean + private NuOfferService nuOfferService; + + @MockBean + private TimeOfferService timeOfferService; + + + @BeforeEach + void setup() { + User user1 = new User(); + Code code1 = new Code(); + code1.setActivo(true); + code1.setCode("codeTest1"); + user1.setUsername("user1"); + user1.setPassword("user1"); + Client client1 = new Client();; + client1.setId(TEST_CLIENT_ID); + client1.setName("client1"); + client1.setEmail("client1"); + client1.setAddress("client1"); + client1.setInit(LocalTime.of(01, 00)); + client1.setFinish(LocalTime.of(01, 01)); + client1.setTelephone("123456789"); + client1.setDescription("client1"); + client1.setCode(code1); + client1.setFood("client1"); + client1.setUsuar(user1); + BDDMockito.given(this.clientService.getCurrentClient()).willReturn(client1); + + + } + + @WithMockUser(value = "spring", authorities = "client") + @Test + void testShow() throws Exception { + mockMvc.perform(get("/clients/show")) + .andExpect(status().isOk()) + .andExpect(model().attributeExists("client")) + .andExpect(view().name("clients/clientShow")); + } + + @WithMockUser(value = "spring", authorities = "client") + @Test + void testInitUpdateForm() throws Exception { + mockMvc.perform(get("/clients/edit")) + .andExpect(status().isOk()) + .andExpect(model().attributeExists("client")) + .andExpect(view().name("clients/createOrUpdateClientForm")); + } + + @WithMockUser(value = "spring", authorities = "client") + @Test + void testProcessUpdateFormSuccess() throws Exception { + mockMvc.perform(post("/clients/edit") + .with(csrf()) + .param("usuar.password", "Contrasenya123") + .param("init", "11:30") + .param("finish", "23:30") + .param("name", "Restaurante Pepe") + .param("email", "pepe@hotmail.es") + .param("address", "Pirineos 10") + .param("telephone", "654999999") + .param("description", "Comida al mejor precio") + .param("food", "Americana") + .param("municipio", "Dos_Hermanas")) + .andExpect(status().is3xxRedirection()); + } + + + @WithMockUser(value = "spring", authorities = "client") + @Test + void testProcessCreationFormHasErrors() throws Exception { + mockMvc.perform(post("/clients/edit") + .with(csrf()) + .param("usuar.password", "") + .param("init", "24:30") + .param("finish", "a:30") + .param("name", "") + .param("email", "") + .param("address", "") + .param("telephone", "654999") + .param("description", "") + .param("food", "") + .param("municipio", "Dos Hermanas")) + .andExpect(model().attributeHasErrors("client")) + .andExpect(model().attributeHasFieldErrors("client", "usuar.password")) + .andExpect(model().attributeHasFieldErrors("client", "init")) + .andExpect(model().attributeHasFieldErrors("client", "finish")) + .andExpect(model().attributeHasFieldErrors("client", "name")) + .andExpect(model().attributeHasFieldErrors("client", "email")) + .andExpect(model().attributeHasFieldErrors("client", "address")) + .andExpect(model().attributeHasFieldErrors("client", "telephone")) + .andExpect(model().attributeHasFieldErrors("client", "description")) + .andExpect(model().attributeHasFieldErrors("client", "food")) + .andExpect(model().attributeHasFieldErrors("client", "municipio")) + + .andExpect(view().name("clients/createOrUpdateClientForm")); + } + +}