Commit ab329f6b by Antonio Rueda

Creado primer repositorio, operación de guardado e infraestructura para tests

parent d40f6cbc
...@@ -40,6 +40,17 @@ ...@@ -40,6 +40,17 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
......
package es.ujaen.dae.reservahoteles.repositorios;
import es.ujaen.dae.reservahoteles.entidades.Usuario;
import es.ujaen.dae.reservahoteles.excepciones.ClienteYaRegistrado;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.transaction.Transactional;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Repository;
/**
*
* @author administrador
*/
@Transactional
@Repository
public class RepositorioUsuarios {
@PersistenceContext
EntityManager em;
public void guardar(Usuario usuario) {
if (em.find(Usuario.class, usuario.email()) != null)
throw new ClienteYaRegistrado();
em.persist(usuario);
em.flush();
}
}
...@@ -6,6 +6,7 @@ import es.ujaen.dae.reservahoteles.entidades.Usuario; ...@@ -6,6 +6,7 @@ import es.ujaen.dae.reservahoteles.entidades.Usuario;
import es.ujaen.dae.reservahoteles.entidades.Hotel; import es.ujaen.dae.reservahoteles.entidades.Hotel;
import es.ujaen.dae.reservahoteles.entidades.Reserva; import es.ujaen.dae.reservahoteles.entidades.Reserva;
import es.ujaen.dae.reservahoteles.excepciones.ClienteYaRegistrado; import es.ujaen.dae.reservahoteles.excepciones.ClienteYaRegistrado;
import es.ujaen.dae.reservahoteles.repositorios.RepositorioUsuarios;
import es.ujaen.dae.reservahoteles.util.UtilString; import es.ujaen.dae.reservahoteles.util.UtilString;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import jakarta.validation.constraints.Email; import jakarta.validation.constraints.Email;
...@@ -18,6 +19,7 @@ import java.util.List; ...@@ -18,6 +19,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.TreeMap; import java.util.TreeMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -30,6 +32,9 @@ import org.springframework.validation.annotation.Validated; ...@@ -30,6 +32,9 @@ import org.springframework.validation.annotation.Validated;
@Service @Service
@Validated @Validated
public class ServicioReservas { public class ServicioReservas {
@Autowired
RepositorioUsuarios repositorioClientes;
Map<Integer, Hotel> hoteles; Map<Integer, Hotel> hoteles;
Map<String, Usuario> clientes; Map<String, Usuario> clientes;
...@@ -57,11 +62,13 @@ public class ServicioReservas { ...@@ -57,11 +62,13 @@ public class ServicioReservas {
// Evitar que se cree un usuario con la cuenta de direccion // Evitar que se cree un usuario con la cuenta de direccion
if (cliente.email().equals(direccion.email())) if (cliente.email().equals(direccion.email()))
throw new ClienteYaRegistrado(); throw new ClienteYaRegistrado();
/*
if (clientes.containsKey(cliente.email())) if (clientes.containsKey(cliente.email()))
throw new ClienteYaRegistrado(); throw new ClienteYaRegistrado();
clientes.put(cliente.email(), cliente); clientes.put(cliente.email(), cliente);
*/
repositorioClientes.guardar(cliente);
} }
public Optional<Usuario> login(@Email String email, String clave) { public Optional<Usuario> login(@Email String email, String clave) {
......
meses-historico: 3
spring.datasource.url: jdbc:h2:mem:ujacoin_test;MODE=MYSQL;DATABASE_TO_LOWER=TRUE;DB_CLOSE_DELAY=-1
spring.jpa.properties.jakarta.persistence.schema-generation.database.action: drop-and-create
...@@ -4,6 +4,6 @@ spring.datasource.url: jdbc:mysql://localhost:3306/reservas ...@@ -4,6 +4,6 @@ spring.datasource.url: jdbc:mysql://localhost:3306/reservas
spring.datasource.username: reservas_usr spring.datasource.username: reservas_usr
spring.datasource.password: secret spring.datasource.password: secret
spring.jpa.properties.jakarta.persistence.schema-generation.database.action: drop-and-create spring.jpa.properties.jakarta.persistence.schema-generation.database.action: none
...@@ -14,6 +14,7 @@ import org.junit.jupiter.api.Test; ...@@ -14,6 +14,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
/** /**
* Test de integración del servicio principal del sistema * Test de integración del servicio principal del sistema
...@@ -21,6 +22,7 @@ import org.springframework.test.annotation.DirtiesContext; ...@@ -21,6 +22,7 @@ import org.springframework.test.annotation.DirtiesContext;
*/ */
@SpringBootTest(classes = es.ujaen.dae.reservahoteles.app.ReservaHoteles.class) @SpringBootTest(classes = es.ujaen.dae.reservahoteles.app.ReservaHoteles.class)
@ActiveProfiles("test")
public class TestServicioReservas { public class TestServicioReservas {
@Autowired @Autowired
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment