mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-23 15:55:49 +00:00
Añadidos algunos test de cliente sobre su información
This commit is contained in:
parent
39b8515549
commit
8cf51fa0c7
6 changed files with 171 additions and 20 deletions
|
@ -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
|
||||
|
|
|
@ -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<Client, Integer> {
|
|||
@Transactional(readOnly = true)
|
||||
Client findByUsername(String username);
|
||||
|
||||
Optional<Client> findById(Integer id);
|
||||
Client findById(Integer id);
|
||||
|
||||
|
||||
// void save(Client client);
|
||||
|
|
|
@ -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<Client> findAllClient() throws DataAccessException {
|
||||
return this.clientRepository.findAllClient();
|
||||
|
|
|
@ -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";
|
||||
}
|
||||
|
|
|
@ -16,14 +16,14 @@
|
|||
<form:form modelAttribute="client" class="form-horizontal" id="add-client-form">
|
||||
<div class="form-group has-feedback">
|
||||
|
||||
<cheapy:passwordField label="Contrase<EFBFBD>a" placeholder="Restaurante pepito" name="usuar.password"/>
|
||||
<cheapy:passwordField label="Contraseña" placeholder="Restaurante pepito" name="usuar.password"/>
|
||||
<cheapy:inputField label="Hora de inicio" placeholder="HH:mm" name="init"/>
|
||||
<cheapy:inputField label="Hora de fin" placeholder="HH:mm" name="finish"/>
|
||||
<cheapy:inputField label="Nombre" placeholder="Restaurante pepito" name="name"/>
|
||||
<cheapy:inputField label="Email" placeholder="" name="email"/>
|
||||
<cheapy:inputField label="Direcci<EFBFBD>n" placeholder="" name="address"/>
|
||||
<cheapy:inputField label="Tel<EFBFBD>fono" placeholder="" name="telephone"/>
|
||||
<cheapy:inputField label="descripci<EFBFBD>n" placeholder="" name="description"/>
|
||||
<cheapy:inputField label="Dirección" placeholder="" name="address"/>
|
||||
<cheapy:inputField label="Teléfono" placeholder="" name="telephone"/>
|
||||
<cheapy:inputField label="descripción" placeholder="" name="description"/>
|
||||
<cheapy:inputField label="Comida" placeholder="food" name="food"/>
|
||||
<div class="form-group">
|
||||
<label class="col-sm-2 control-label">Municipio: </label>
|
||||
|
|
|
@ -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"));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue