Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
María Hidalgo
/
carpooling-dae
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
5f3c07e9
authored
Jan 09, 2024
by
María Hidalgo
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
tests funcionando
parent
967b9a2f
Hide whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
233 additions
and
165 deletions
pom.xml
src/main/java/es/ujaen/dae/carpoolingdae/app/CarpoolingDaeApplication.java
src/main/java/es/ujaen/dae/carpoolingdae/entidades/Usuario.java
src/main/java/es/ujaen/dae/carpoolingdae/entidades/Viaje.java
src/main/java/es/ujaen/dae/carpoolingdae/repositorios/RepositorioViajes.java
src/main/java/es/ujaen/dae/carpoolingdae/servicios/SistemaCarpooling.java
src/main/resources/application.yml → src/main/resources/app.yml
src/main/resources/application-test.properties
src/main/resources/application-test.yml
src/main/resources/application.properties
src/main/resources/test.yml
src/test/java/es/ujaen/dae/carpoolingdae/entidades/UsuarioTest.java
src/test/java/es/ujaen/dae/carpoolingdae/entidades/ViajeTest.java
src/test/java/es/ujaen/dae/carpoolingdae/servicios/SistemaCarpoolingTest.java
pom.xml
View file @
5f3c07e9
...
...
@@ -11,7 +11,7 @@
<maven.compiler.target>
17
</maven.compiler.target>
</properties>
<parent>
<parent>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-parent
</artifactId>
<version>
3.1.4
</version>
...
...
@@ -45,6 +45,34 @@
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-validation
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.data
</groupId>
<artifactId>
spring-data-jpa
</artifactId>
<dependencies>
<!-- Jakarta Validation API -->
<dependency>
<groupId>
jakarta.validation
</groupId>
<artifactId>
jakarta.validation-api
</artifactId>
<version>
3.0.0
</version>
<!-- Utiliza la versión más reciente -->
</dependency>
<!-- Hibernate Validator (implementación de Jakarta Validation) -->
<dependency>
<groupId>
org.hibernate.validator
</groupId>
<artifactId>
hibernate-validator
</artifactId>
<version>
7.0.2.Final
</version>
<!-- Utiliza la versión más reciente -->
</dependency>
<!-- Otras dependencias de tu proyecto -->
</dependencies>
</dependency>
<dependency>
<groupId>
org.projectlombok
</groupId>
<artifactId>
lombok
</artifactId>
<optional>
true
</optional>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
...
...
@@ -68,4 +96,4 @@
</plugins>
</build>
</project>
\ No newline at end of file
</project>
src/main/java/es/ujaen/dae/carpoolingdae/app/CarpoolingDaeApplication.java
View file @
5f3c07e9
...
...
@@ -5,10 +5,12 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import
org.springframework.boot.autoconfigure.domain.EntityScan
;
@SpringBootApplication
(
scanBasePackages
=
{
"es.ujaen.dae.carpooling.servicios"
,
"es.ujaen.dae.carpooling.repositorios"
})
@EntityScan
(
basePackages
=
"es.ujaen.dae.ujacoin.entidades"
)
@SpringBootApplication
(
scanBasePackages
=
{
"es.ujaen.dae.carpoolingdae.servicios"
,
"es.ujaen.dae.carpoolingdae.repositorios"
})
@EntityScan
(
basePackages
=
"es.ujaen.dae.carpoolingdae.entidades"
)
public
class
CarpoolingDaeApplication
{
public
static
void
main
(
String
[]
args
)
throws
Exception
{
// Creación de servidor
...
...
src/main/java/es/ujaen/dae/carpoolingdae/entidades/Usuario.java
View file @
5f3c07e9
...
...
@@ -9,6 +9,8 @@ import jakarta.validation.constraints.Email;
import
jakarta.validation.constraints.Past
;
import
jakarta.validation.constraints.Pattern
;
import
jakarta.validation.constraints.Size
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
jakarta.validation.constraints.NotBlank
;
import
jakarta.validation.constraints.NotNull
;
...
...
@@ -20,13 +22,14 @@ import es.ujaen.dae.carpoolingdae.utils.CodificadorMd5;
import
es.ujaen.dae.carpoolingdae.utils.ExprReg
;
//import lombok.Data;
//import lombok.NoArgsConstructor;
import
jakarta.persistence.CascadeType
;
import
jakarta.persistence.Entity
;
import
jakarta.persistence.FetchType
;
import
jakarta.persistence.Id
;
import
jakarta.persistence.JoinColumn
;
import
jakarta.persistence.ManyToMany
;
import
jakarta.persistence.ManyToOne
;
import
jakarta.persistence.OneToMany
;
/**
* Usuario a la app de carpooling
...
...
@@ -34,7 +37,8 @@ import jakarta.persistence.ManyToOne;
* @author maria
*/
//@NoArgsConstructor@Data
@NoArgsConstructor
@Data
@Entity
public
class
Usuario
implements
Serializable
{
...
...
@@ -58,7 +62,7 @@ public class Usuario implements Serializable {
@NotNull
@Size
(
min
=
9
,
max
=
13
)
@Pattern
(
regexp
=
ExprReg
.
TLF
)
String
t
ele
f
;
String
t
l
f
;
@NotBlank
@Email
...
...
@@ -68,8 +72,8 @@ public class Usuario implements Serializable {
String
clave
;
// Viajes creados por el conductor
@
ManyToOne
(
fetch
=
FetchType
.
LAZY
)
@JoinColumn
(
name
=
"usuario_dni"
)
@
OneToMany
(
fetch
=
FetchType
.
EAGER
,
cascade
=
CascadeType
.
ALL
)
//
@JoinColumn(name = "usuario_dni")
List
<
Viaje
>
viajesComoConductor
;
public
Usuario
(
String
dni
,
String
nombre
,
String
apellidos
,
LocalDate
fecha_nac
,
String
telef
,
String
email
,
...
...
@@ -78,7 +82,7 @@ public class Usuario implements Serializable {
this
.
nombre
=
nombre
;
this
.
apellidos
=
apellidos
;
this
.
fecha_nac
=
fecha_nac
;
this
.
t
ele
f
=
telef
;
this
.
t
l
f
=
telef
;
this
.
email
=
email
;
this
.
clave
=
(
clave
!=
null
?
CodificadorMd5
.
codificar
(
clave
)
:
null
);
...
...
src/main/java/es/ujaen/dae/carpoolingdae/entidades/Viaje.java
View file @
5f3c07e9
...
...
@@ -12,14 +12,18 @@ import jakarta.persistence.GeneratedValue;
import
jakarta.persistence.GenerationType
;
import
jakarta.persistence.Id
;
import
jakarta.persistence.JoinColumn
;
import
jakarta.persistence.ManyToMany
;
import
jakarta.persistence.ManyToOne
;
import
jakarta.persistence.OneToMany
;
import
jakarta.validation.constraints.Past
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
jakarta.validation.constraints.NotBlank
;
import
jakarta.validation.constraints.NotNull
;
//
@Data
//
@NoArgsConstructor
@Data
@NoArgsConstructor
@Entity
public
class
Viaje
{
...
...
@@ -41,22 +45,24 @@ public class Viaje {
@NotNull
int
plazasDisponibles
;
@Not
Blank
@Not
Null
Double
precioAsiento
;
// Lista de usuarios que son confirmados en un viaje
@ManyToMany
(
cascade
=
CascadeType
.
ALL
)
@JoinColumn
(
name
=
"confirmado_usuario_dni"
)
public
List
<
Usuario
>
usuariosConfirmados
;
// Lista de usuarios que solicitan unirse a un viaje
@One
ToMany
(
cascade
=
CascadeType
.
ALL
)
@JoinColumn
(
name
=
"solicitante
s_viaje
"
)
@Many
ToMany
(
cascade
=
CascadeType
.
ALL
)
@JoinColumn
(
name
=
"solicitante
_usuario_dni
"
)
public
List
<
Usuario
>
usuariosSolicitantes
;
// Lista de usuarios que son confirmados en un viaje
@OneToMany
(
cascade
=
CascadeType
.
ALL
)
@JoinColumn
(
name
=
"confirmados_viaje"
)
public
List
<
Usuario
>
usuariosConfirmados
;
public
Viaje
(
int
id
,
String
destino
,
String
origen
,
LocalDateTime
fechaHora
,
int
plazasDisponibles
,
public
Viaje
(
String
destino
,
String
origen
,
LocalDateTime
fechaHora
,
int
plazasDisponibles
,
Double
precioAsiento
)
{
this
.
id
=
id
;
//
this.id = id;
this
.
origen
=
origen
;
this
.
destino
=
destino
;
this
.
fechaHora
=
fechaHora
;
...
...
@@ -99,14 +105,15 @@ public class Viaje {
public
void
confirmarPasajero
(
@NotNull
Usuario
pasajero
,
@NotNull
Usuario
conductor
)
{
// Verificar si el pasajero está en la lista de solicitantes
if
(
usuariosSolicitantes
.
contains
(
pasajero
))
{
if
(
usuariosSolicitantes
.
contains
(
pasajero
)
&&
plazasDisponibles
>
0
)
{
// Mover al pasajero de la lista de solicitantes a la lista de confirmados
usuariosSolicitantes
.
remove
(
pasajero
);
usuariosConfirmados
.
add
(
pasajero
);
// Al añadir al pasajero a la lista de confirmados reduzco el número de plazas
// disponibles
if
(
plazasDisponibles
>
0
)
{
usuariosConfirmados
.
add
(
pasajero
);
plazasDisponibles
--;
}
...
...
src/main/java/es/ujaen/dae/carpoolingdae/repositorios/RepositorioViajes.java
View file @
5f3c07e9
package
es
.
ujaen
.
dae
.
carpoolingdae
.
repositorios
;
import
java.time.LocalDate
;
import
java.time.LocalDateTime
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Optional
;
...
...
@@ -35,20 +36,23 @@ public class RepositorioViajes {
}
public
void
guardarViaje
(
Viaje
viaje
)
{
//viaje=em.merge(viaje);
em
.
persist
(
viaje
);
//em.flush();
}
public
List
<
Viaje
>
buscarViajes
(
String
origen
,
String
destino
,
LocalDate
fecha
)
{
public
List
<
Viaje
>
buscarViajes
(
String
origen
,
String
destino
,
LocalDate
Time
fecha
)
{
List
<
Viaje
>
viajes
=
new
ArrayList
<>();
try
{
Query
q
=
em
.
createQuery
(
"S
elect v from Viaje v where v.origen=:origen AND v.destino = :destino AND v.fecha = :fecha"
,
Viaje
.
class
);
q
.
setParameter
(
"origen"
,
origen
);
q
.
setParameter
(
"destino"
,
destino
);
q
.
setParameter
(
"fecha"
,
fecha
);
//
try {
Query
q
=
em
.
createQuery
(
"S
ELECT v FROM Viaje v WHERE v.origen = ?1 AND v.destino = ?2 "
);
q
.
setParameter
(
1
,
origen
);
q
.
setParameter
(
2
,
destino
);
//q.setParameter(3
, fecha);
viajes
=
q
.
getResultList
();
}
catch
(
Exception
e
)
{
/*
} catch (Exception e) {
logger.log(Level.SEVERE, e.getMessage(), e);
}
} */
return
viajes
;
}
...
...
src/main/java/es/ujaen/dae/carpoolingdae/servicios/SistemaCarpooling.java
View file @
5f3c07e9
package
es
.
ujaen
.
dae
.
carpoolingdae
.
servicios
;
import
jakarta.validation.Valid
;
//import javax.validation.Valid;
import
jakarta.validation.constraints.NotBlank
;
import
jakarta.validation.constraints.NotNull
;
import
java.time.LocalDate
;
import
java.time.LocalDateTime
;
import
java.util.ArrayList
;
import
java.util.Optional
;
...
...
@@ -17,6 +19,7 @@ import org.springframework.validation.annotation.Validated;
import
es.ujaen.dae.carpoolingdae.entidades.Usuario
;
import
es.ujaen.dae.carpoolingdae.entidades.Viaje
;
import
es.ujaen.dae.carpoolingdae.excepciones.UsuarioNoRegistrado
;
import
es.ujaen.dae.carpoolingdae.excepciones.UsuarioYaRegistrado
;
import
es.ujaen.dae.carpoolingdae.excepciones.ViajeNoExistente
;
import
es.ujaen.dae.carpoolingdae.repositorios.RepositorioUsuarios
;
...
...
@@ -42,15 +45,19 @@ public class SistemaCarpooling {
* @param usuario el usuario a dar de alta
*/
public
void
altaUsuario
(
@NotNull
Usuario
usuario
)
{
if
(
repositorioUsuarios
.
buscar
(
usuario
.
getDni
()).
isPresent
())
{
public
Usuario
altaUsuario
(
@NotNull
@Valid
Usuario
usuario
)
throws
UsuarioNoRegistrado
{
//log.info("Usuario con datos: " + usuario + "registrandose");
if
(
repositorioUsuarios
.
buscar
(
usuario
.
getDni
()).
isPresent
()){
throw
new
UsuarioYaRegistrado
();
}
else
{
repositorioUsuarios
.
guardar
(
usuario
);
//log.info("Usuario con datos: " + usuario + "registrado");
return
usuario
;
}
}
// Registrar usuario
repositorioUsuarios
.
guardar
(
usuario
);
}
/**
* Realiza un login de un usuario
...
...
@@ -60,7 +67,6 @@ public class SistemaCarpooling {
* @return el objeto de la clase Viaje asociado
*/
@Transactional
public
Optional
<
Usuario
>
loginUsuario
(
@NotBlank
String
dni
,
@NotBlank
String
clave
)
{
Optional
<
Usuario
>
usuarioLogin
=
repositorioUsuarios
.
buscar
(
dni
)
.
filter
((
cliente
)
->
cliente
.
claveValida
(
clave
));
...
...
@@ -68,13 +74,15 @@ public class SistemaCarpooling {
return
usuarioLogin
;
}
public
void
agregarViaje
(
@NotNull
Usuario
conductor
,
@NotNull
Viaje
viaje
)
{
// Agregar el viaje al repositorio de viajes
repositorioViajes
.
guardarViaje
(
viaje
);
// Llamar a la función en Usuario para agregar el viaje como conductor
conductor
.
agregarViajeComoConductor
(
viaje
);
// Agregar el viaje al repositorio de viajes
repositorioViajes
.
guardarViaje
(
viaje
);
repositorioUsuarios
.
actualizarUsuario
(
conductor
);
}
...
...
@@ -87,18 +95,18 @@ public class SistemaCarpooling {
* @return viajes encontrados con esos datos
*/
public
List
<
Viaje
>
buscarViajes
(
@NotBlank
String
origen
,
@NotBlank
String
destino
,
@Not
Blank
LocalDat
e
fecha
)
{
public
List
<
Viaje
>
buscarViajes
(
@NotBlank
String
origen
,
@NotBlank
String
destino
,
@Not
Null
LocalDateTim
e
fecha
)
{
List
<
Viaje
>
viajes
=
new
ArrayList
<>();
List
<
Viaje
>
listaViajes
=
repositorioViajes
.
buscarViajes
(
origen
,
destino
,
fecha
);
for
(
Viaje
viaje
:
listaViajes
)
{
// Iterar sobre la colección de viajes
/*
for (Viaje viaje : listaViajes) { // Iterar sobre la colección de viajes
if (viaje.getOrigen().equals(origen) && viaje.getDestino().equals(destino)
&& viaje.getfechaHora().toLocalDate().equals(fecha)) {
viajes.add(viaje);
} else {
throw new ViajeNoExistente();
}
}
return
v
iajes
;
}
*/
return
listaV
iajes
;
}
...
...
src/main/resources/app
lication
.yml
→
src/main/resources/app.yml
View file @
5f3c07e9
...
...
@@ -4,7 +4,7 @@ spring.datasource.url: jdbc:mysql://localhost:33060/carpooling
spring.datasource.username
:
carpooling
spring.datasource.password
:
secret
spring.jpa.properties.ja
vax
.persistence.schema-generation.database.action
:
drop-and-create
spring.jpa.properties.ja
karta
.persistence.schema-generation.database.action
:
drop-and-create
spring.data.jpa.repositories.bootstrap-mode
:
default
src/main/resources/application-test.properties
0 → 100644
View file @
5f3c07e9
spring.datasource.url
=
jdbc:mysql://localhost:3306/db_springboot_test
spring.datasource.username
=
root
spring.datasource.password
=
root
spring.jpa.hibernate.ddl-auto
=
create
spring.jpa.generate-ddl
=
true
spring.jpa.hibernate.ddl-auto
=
update
logging.level.org.springframework.transaction.interceptor
=
TRACE
spring.jpa.properties.jakarta.persistence.schema-generation.database.action
=
drop-and-create
\ No newline at end of file
src/main/resources/application-test.yml
deleted
100644 → 0
View file @
967b9a2f
## Fichero de configuración para carpooling durante testing
# spring.datasource.url: jdbc:mysql://localhost:33060/carpooling_test
spring.datasource.url
:
jdbc:h2:mem:carpooling_test;MODE=MYSQL;DATABASE_TO_LOWER=TRUE;DB_CLOSE_DELAY=-1
spring.jpa.properties.javax.persistence.schema-generation.database.action
:
drop-and-create
src/main/resources/application.properties
0 → 100644
View file @
5f3c07e9
spring.datasource.url
=
jdbc:mysql://localhost:3306/db_springboot_test
spring.datasource.username
=
root
spring.datasource.password
=
root
spring.jpa.hibernate.ddl-auto
=
create
spring.jpa.generate-ddl
=
true
spring.jpa.hibernate.ddl-auto
=
update
logging.level.org.springframework.transaction.interceptor
=
TRACE
\ No newline at end of file
src/main/resources/test.yml
0 → 100644
View file @
5f3c07e9
## Fichero de configuración para carpooling durante testing
spring.datasource.url
:
jdbc:mysql://localhost:33060/db_springboot_test
# spring.datasource.url: jdbc:h2:mem:carpooling_test;MODE=MYSQL;DATABASE_TO_LOWER=TRUE;DB_CLOSE_DELAY=-1
spring.jpa.properties.jakarta.persistence.schema-generation.database.action
:
drop-and-create
src/test/java/es/ujaen/dae/carpoolingdae/entidades/UsuarioTest.java
View file @
5f3c07e9
...
...
@@ -22,38 +22,55 @@ public class UsuarioTest {
public
UsuarioTest
()
{
}
private
Usuario
usuario
;
@BeforeEach
void
setUp
()
{
@Test
void
testValidacionUsuario
()
{
String
clave
=
"mariahm"
;
usuario
=
new
Usuario
(
"22334455H"
,
Usuario
usuario
=
new
Usuario
(
"22334455H"
,
"María"
,
"Hidalgo Morcillo"
,
LocalDate
.
of
(
1990
,
11
,
1
),
"
189273810
"
,
"
+34600123456
"
,
"mhm@red.ujaen.es"
,
clave
);
}
@Test
void
testValidacionUsuario
()
{
Validator
validator
=
Validation
.
buildDefaultValidatorFactory
().
getValidator
();
Set
<
ConstraintViolation
<
Usuario
>>
violations
=
validator
.
validate
(
usuario
);
Assertions
.
assertThat
(
violations
).
isEmpty
();
}
@Test
void
testComprobacionClave
()
{
Assertions
.
assertThat
(
usuario
.
claveValida
(
"mariahm"
)).
isTrue
();
String
clave
=
"mariahm"
;
Usuario
usuario
=
new
Usuario
(
"22334455H"
,
"María"
,
"Hidalgo Morcillo"
,
LocalDate
.
of
(
1990
,
11
,
1
),
"189273810"
,
"mhm@red.ujaen.es"
,
clave
);
Assertions
.
assertThat
(
usuario
.
claveValida
(
clave
)).
isTrue
();
}
@Test
public
void
testAgregarViajeComoConductor
()
{
Viaje
viaje
=
new
Viaje
(
12345
,
String
clave
=
"mariahm"
;
Usuario
usuario
=
new
Usuario
(
"22334455H"
,
"María"
,
"Hidalgo Morcillo"
,
LocalDate
.
of
(
1990
,
11
,
1
),
"189273810"
,
"mhm@red.ujaen.es"
,
clave
);
Viaje
viaje
=
new
Viaje
(
"Origen"
,
"Destino"
,
LocalDateTime
.
of
(
2023
,
9
,
30
,
8
,
0
),
...
...
@@ -65,12 +82,22 @@ public class UsuarioTest {
@Test
public
void
testAceptarPasajero
()
{
String
clave
=
"mariahm"
;
Usuario
usuario
=
new
Usuario
(
"22334455H"
,
"María"
,
"Hidalgo Morcillo"
,
LocalDate
.
of
(
1990
,
11
,
1
),
"+34600123456"
,
"mhm@red.ujaen.es"
,
clave
);
Usuario
pasajero
=
new
Usuario
(
"87654321B"
,
"Pasajero"
,
"Apellido"
,
LocalDate
.
of
(
1995
,
5
,
5
),
"
987654321
"
,
"
+34600999889
"
,
"pasajero@example.com"
,
"clave"
);
Usuario
conductor
=
new
Usuario
(
...
...
@@ -78,16 +105,17 @@ public class UsuarioTest {
"Conductor"
,
"Apellido"
,
LocalDate
.
of
(
1990
,
1
,
1
),
"
12345678
9"
,
"
+3460099999
9"
,
"conductor@example.com"
,
"clave"
);
Viaje
viaje
=
new
Viaje
(
12345
,
Viaje
viaje
=
new
Viaje
(
"Origen"
,
"Destino"
,
LocalDateTime
.
of
(
2023
,
9
,
30
,
8
,
0
),
2
,
50.0
);
usuario
.
agregarViajeComoConductor
(
viaje
);
viaje
.
enviarSolicitudParaSerPasajero
(
viaje
,
pasajero
);
viaje
.
confirmarPasajero
(
pasajero
,
conductor
);
assertTrue
(
viaje
.
usuariosConfirmados
.
contains
(
pasajero
));
}
...
...
src/test/java/es/ujaen/dae/carpoolingdae/entidades/ViajeTest.java
View file @
5f3c07e9
package
es
.
ujaen
.
dae
.
carpoolingdae
.
entidades
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertDoesNotThrow
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertFalse
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertThrows
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertTrue
;
...
...
@@ -33,7 +34,7 @@ public class ViajeTest {
@BeforeEach
void
setUp
()
{
// Direccion direccion = new Direccion("España", "Jaen", "SanJuan", 19);
viaje
=
new
Viaje
(
12345
,
viaje
=
new
Viaje
(
"Origen"
,
"Destino"
,
LocalDateTime
.
of
(
2023
,
9
,
30
,
8
,
0
),
...
...
@@ -102,26 +103,6 @@ public class ViajeTest {
assertFalse
(
viaje
.
usuariosSolicitantes
.
contains
(
pasajero
));
}
@Test
public
void
testConfirmarPasajeroNoSolicitante
()
{
Usuario
pasajero
=
new
Usuario
(
"87654321B"
,
"Pasajero"
,
"Apellido"
,
LocalDate
.
of
(
1995
,
5
,
5
),
"987654321"
,
"pasajero@example.com"
,
"clave"
);
Usuario
conductor
=
new
Usuario
(
"12345678A"
,
"Conductor"
,
"Apellido"
,
LocalDate
.
of
(
1990
,
1
,
1
),
"123456789"
,
"conductor@example.com"
,
"clave"
);
assertThrows
(
PlazaNoDisponible
.
class
,
()
->
viaje
.
confirmarPasajero
(
pasajero
,
conductor
));
}
@Test
public
void
testConfirmarPasajeroNoAvailableSeats
()
{
...
...
@@ -144,6 +125,14 @@ public class ViajeTest {
"clave"
);
viaje
.
setPlazasDisponibles
(
0
);
viaje
.
usuariosSolicitantes
.
add
(
pasajero
);
assertThrows
(
PlazaNoDisponible
.
class
,
()
->
viaje
.
confirmarPasajero
(
pasajero
,
conductor
));
viaje
.
confirmarPasajero
(
pasajero
,
conductor
);
// Verificación
assertFalse
(
viaje
.
getPlazasDisponibles
()
>
0
);
assertFalse
(
viaje
.
usuariosConfirmados
.
contains
(
pasajero
));
assertFalse
(
viaje
.
usuariosSolicitantes
.
isEmpty
());
}
}
src/test/java/es/ujaen/dae/carpoolingdae/servicios/SistemaCarpoolingTest.java
View file @
5f3c07e9
package
es
.
ujaen
.
dae
.
carpoolingdae
.
servicios
;
import
es.ujaen.dae.carpoolingdae.app.CarpoolingDaeApplication
;
import
es.ujaen.dae.carpoolingdae.entidades.Usuario
;
import
es.ujaen.dae.carpoolingdae.entidades.Viaje
;
import
es.ujaen.dae.carpoolingdae.excepciones.UsuarioNoRegistrado
;
//import jakarta.validation.ConstraintViolationException;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertTrue
;
...
...
@@ -8,83 +14,93 @@ import java.time.LocalDateTime;
import
java.util.List
;
import
java.util.Optional
;
import
jakarta.validation.ConstraintViolationException
;
import
org.assertj.core.api.Assertions
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.context.annotation.ComponentScan
;
import
org.springframework.test.annotation.DirtiesContext
;
import
org.springframework.test.annotation.DirtiesContext.MethodMode
;
import
org.springframework.test.context.ActiveProfiles
;
import
es.ujaen.dae.carpoolingdae.entidades.Usuario
;
//import es.ujaen.dae.carpoolingdae.utils.CodificadorMd5;
import
es.ujaen.dae.carpoolingdae.entidades.Viaje
;
import
jakarta.validation.ConstraintViolationException
;
//@Disabled
@SpringBootTest
(
classes
=
es
.
ujaen
.
dae
.
carpoolingdae
.
app
.
CarpoolingDaeApplication
.
class
)
@ActiveProfiles
(
profiles
=
{
"test"
})
public
class
SistemaCarpoolingTest
{
@Autowired
SistemaCarpooling
sistemaCarpooling
;
// Accedemos al sistema
@Test
public
void
testAccesoSistemaCarpooling
()
{
Assertions
.
assertThat
(
sistemaCarpooling
).
isNotNull
();
}
}
@Test
@DirtiesContext
(
methodMode
=
MethodMode
.
AFTER_METHOD
)
public
void
testAltaUsuarioInvalido
()
{
String
clave
=
"mariahm"
;
Usuario
usuario
=
new
Usuario
(
"22334455H"
,
"María"
,
"Hidalgo Morcillo"
,
LocalDate
.
of
(
1990
,
11
,
1
),
"
189273810
"
,
"m
hm@
red.ujaen.es"
,
clave
);
"
+34600123456
"
,
"mred.ujaen.es"
,
"clave"
);
Assertions
.
assertThatThrownBy
(()
->
sistemaCarpooling
.
altaUsuario
(
usuario
))
.
isInstanceOf
(
ConstraintViolationException
.
class
);
}
@Test
@DirtiesContext
(
methodMode
=
MethodMode
.
AFTER_METHOD
)
public
void
testAltaYLoginUsuario
()
{
String
clave
=
"mariahm"
;
Usuario
usuario
=
new
Usuario
(
Usuario
usuario
=
new
Usuario
(
"22334455H"
,
"María"
,
"Hidalgo Morcillo"
,
LocalDate
.
of
(
1990
,
11
,
1
),
"
189273810
"
,
"
+34600123456
"
,
"mhm@red.ujaen.es"
,
clave
);
sistemaCarpooling
.
altaUsuario
(
usuario
);
Optional
<
Usuario
>
usuarioLogin
=
sistemaCarpooling
.
loginUsuario
(
usuario
.
getEmail
(),
"clave"
);
Assertions
.
assertThat
(
usuarioLogin
.
isPresent
()).
isTrue
();
Assertions
.
assertThat
(
usuarioLogin
.
get
().
getDni
()).
isEqualTo
(
usuario
.
getDni
());
}
Usuario
usuario1
=
sistemaCarpooling
.
altaUsuario
(
usuario
);
Usuario
usuarioLogin
=
sistemaCarpooling
.
loginUsuario
(
usuario
.
getDni
(),
"mariahm"
)
.
orElseThrow
(()
->
new
UsuarioNoRegistrado
());
Assertions
.
assertThat
(
usuarioLogin
).
isNotNull
();
Assertions
.
assertThat
(
usuarioLogin
.
getDni
()).
isEqualTo
(
usuario1
.
getDni
());
Assertions
.
assertThat
(
usuarioLogin
.
getEmail
()).
isEqualTo
(
usuario1
.
getEmail
());
}
@Test
@DirtiesContext
(
methodMode
=
MethodMode
.
AFTER_METHOD
)
public
void
testAgregarViaje
()
{
// Crear un objeto Viaje válido
Viaje
nuevoViaje
=
new
Viaje
(
12345
,
// ID del viaje
"Origen"
,
Viaje
viaje
=
new
Viaje
(
// ID del viaje
"Destino"
,
"Origen"
,
LocalDateTime
.
of
(
2023
,
9
,
30
,
8
,
0
),
2
,
50.0
);
...
...
@@ -95,62 +111,28 @@ public class SistemaCarpoolingTest {
"María"
,
"Hidalgo Morcillo"
,
LocalDate
.
of
(
1990
,
11
,
1
),
"
189273810
"
,
"
+34600123456
"
,
"mhm@red.ujaen.es"
,
clave
);
// Agregar el viaje al sistema
sistemaCarpooling
.
agregarViaje
(
usuario
,
nuevoV
iaje
);
sistemaCarpooling
.
agregarViaje
(
usuario
,
v
iaje
);
// el viaje paraver si ha sido añadido
String
origenBusqueda
=
"Origen"
;
String
destinoBusqueda
=
"Destino"
;
LocalDate
fechaBusqueda
=
LocalDate
.
of
(
2023
,
9
,
3
0
);
LocalDate
Time
fechaBusqueda
=
LocalDateTime
.
of
(
2023
,
9
,
30
,
8
,
0
);
List
<
Viaje
>
viajesEncontrados
=
sistemaCarpooling
.
buscarViajes
(
origenBusqueda
,
destinoBusqueda
,
fechaBusqueda
);
Viaje
viajeEnc
=
viajesEncontrados
.
get
(
0
);
Assertions
.
assertThat
(
viajeEnc
.
getDestino
()).
isEqualTo
(
viaje
.
getDestino
());
// Verificar que se encuentren los viajes esperados
Assertions
.
assertThat
(
viajesEncontrados
).
contains
(
nuevoV
iaje
);
// Assertions.assertThat(viajesEncontrados).contains(v
iaje);
}
@Test
@DirtiesContext
(
methodMode
=
MethodMode
.
AFTER_METHOD
)
public
void
testBuscarViajes
()
{
Viaje
viaje
=
new
Viaje
(
12345
,
// ID del viaje
"Origen"
,
"Destino"
,
LocalDateTime
.
of
(
2023
,
9
,
30
,
8
,
0
),
2
,
50.0
);
// Realizar la búsqueda de viajes
String
origenBusqueda
=
"Origen"
;
String
destinoBusqueda
=
"Destino"
;
LocalDate
fechaBusqueda
=
LocalDate
.
of
(
2023
,
9
,
30
);
Viaje
viaje2
=
new
Viaje
(
54321
,
// ID del viaje
"Origen2"
,
"Destino2"
,
LocalDateTime
.
of
(
2023
,
9
,
30
,
8
,
0
),
3
,
30.0
);
// Realizar la búsqueda de viajes
/* String origenBusqueda2 = "Origen2";
String destinoBusqueda2 = "Destino2";
LocalDate fechaBusqueda2 = LocalDate.of(2023, 9, 30); */
List
<
Viaje
>
viajesEncontrados
=
sistemaCarpooling
.
buscarViajes
(
origenBusqueda
,
destinoBusqueda
,
fechaBusqueda
);
// Verificar que se encuentren los viajes esperados
Assertions
.
assertThat
(
viajesEncontrados
).
containsExactly
(
viaje
);
Assertions
.
assertThat
(
viajesEncontrados
).
containsExactly
(
viaje2
);
}
@Test
@DirtiesContext
(
methodMode
=
MethodMode
.
AFTER_METHOD
)
...
...
@@ -161,7 +143,7 @@ public class SistemaCarpoolingTest {
"Conductor"
,
"Apellido"
,
LocalDate
.
of
(
1990
,
1
,
1
),
"
12345678
9"
,
"
+3460099999
9"
,
"conductor@example.com"
,
"clave"
);
...
...
@@ -171,12 +153,12 @@ public class SistemaCarpoolingTest {
"Pasajero"
,
"Apellido"
,
LocalDate
.
of
(
1995
,
5
,
5
),
"
987654321
"
,
"
+34600999889
"
,
"pasajero@example.com"
,
"clave"
);
// Crear un viaje
Viaje
viaje
=
new
Viaje
(
12345
,
"Origen"
,
"Destino"
,
LocalDateTime
.
of
(
2023
,
9
,
30
,
8
,
0
),
...
...
@@ -201,7 +183,7 @@ public class SistemaCarpoolingTest {
"Conductor"
,
"Apellido"
,
LocalDate
.
of
(
1990
,
1
,
1
),
"
12345678
9"
,
"
+3460099999
9"
,
"conductor@example.com"
,
"clave"
);
...
...
@@ -211,12 +193,12 @@ public class SistemaCarpoolingTest {
"Pasajero"
,
"Apellido"
,
LocalDate
.
of
(
1995
,
5
,
5
),
"
987654321
"
,
"
+34600999889
"
,
"pasajero@example.com"
,
"clave"
);
// Crear un viaje
Viaje
viaje
=
new
Viaje
(
12345
,
"Origen"
,
"Destino"
,
LocalDateTime
.
of
(
2023
,
9
,
30
,
8
,
0
),
...
...
@@ -230,5 +212,5 @@ public class SistemaCarpoolingTest {
sistemaCarpooling
.
enviarSolicitudViaje
(
viaje
,
pasajero
);
sistemaCarpooling
.
aceptarPasajeroViaje
(
viaje
,
pasajero
,
conductor
);
assertTrue
(
viaje
.
usuariosConfirmados
.
contains
(
pasajero
));
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment