Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Antonio Rueda
/
UJACoin
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
8ebb7d16
authored
Dec 15, 2020
by
Antonio Rueda
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Tests iniciales del controlador (todavía incompletos)
parent
1f5f3cd5
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
126 additions
and
1 deletions
src/test/java/es/ujaen/dae/ujacoin/controladoresREST/ControladorClientesTest.java
src/test/java/es/ujaen/dae/ujacoin/servicios/ServicioLimpiadoBaseDatos.java
src/test/java/es/ujaen/dae/ujacoin/controladoresREST/ControladorClientesTest.java
0 → 100644
View file @
8ebb7d16
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package
es
.
ujaen
.
dae
.
ujacoin
.
controladoresREST
;
import
es.ujaen.dae.ujacoin.controladoresREST.DTO.DTOCliente
;
import
es.ujaen.dae.ujacoin.controladoresREST.DTO.DTOCuenta
;
import
es.ujaen.dae.ujacoin.controladoresREST.DTO.DTOTarjeta
;
import
es.ujaen.dae.ujacoin.servicios.ServicioLimpiadoBaseDatos
;
import
java.time.LocalDate
;
import
java.util.List
;
import
javax.annotation.PostConstruct
;
import
org.assertj.core.api.Assertions
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
import
org.springframework.boot.web.client.RestTemplateBuilder
;
import
org.springframework.boot.web.server.LocalServerPort
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
;
/**
* Test para controlador REST de clientes
* @author ajrueda
*/
@SpringBootTest
(
classes
=
es
.
ujaen
.
dae
.
ujacoin
.
app
.
UjaCoinApp
.
class
,
webEnvironment
=
SpringBootTest
.
WebEnvironment
.
RANDOM_PORT
)
public
class
ControladorClientesTest
{
@Autowired
ServicioLimpiadoBaseDatos
limpiadorBaseDatos
;
@LocalServerPort
int
localPort
;
@Autowired
MappingJackson2HttpMessageConverter
springBootJacksonConverter
;
TestRestTemplate
restTemplate
;
/** Crear un TestRestTemplate para las pruebas */
@PostConstruct
void
crearRestTemplate
()
{
RestTemplateBuilder
restTemplateBuilder
=
new
RestTemplateBuilder
()
.
rootUri
(
"http://localhost:"
+
localPort
+
"/ujacoin"
)
.
additionalMessageConverters
(
List
.
of
(
springBootJacksonConverter
));
restTemplate
=
new
TestRestTemplate
(
restTemplateBuilder
);
}
@Test
public
void
testAltaClienteInvalido
()
{
// Cliente con e-mail incorrecto!!!
DTOCliente
cliente
=
new
DTOCliente
(
"11995667D"
,
"Juan España España"
,
LocalDate
.
of
(
1990
,
11
,
1
),
"Cl La Luz, 13 - Jaén"
,
"988674533"
,
"jeegmail.com"
,
"clave"
);
ResponseEntity
<
DTOCuenta
>
respuesta
=
restTemplate
.
postForEntity
(
"/clientes"
,
cliente
,
DTOCuenta
.
class
);
Assertions
.
assertThat
(
respuesta
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
BAD_REQUEST
);
}
@Test
public
void
testAltaYLoginClienteCuenta
()
{
DTOCliente
cliente
=
new
DTOCliente
(
"11995667D"
,
"Juan España España"
,
LocalDate
.
of
(
1990
,
11
,
1
),
"Cl La Luz, 13 - Jaén"
,
"988674533"
,
"jee@gmail.com"
,
"clave"
);
ResponseEntity
<
DTOCuenta
>
respuestaAlta
=
restTemplate
.
postForEntity
(
"/clientes"
,
cliente
,
DTOCuenta
.
class
);
Assertions
.
assertThat
(
respuestaAlta
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
CREATED
);
ResponseEntity
<
DTOCliente
>
respuestaLogin
=
restTemplate
.
getForEntity
(
"/clientes/{dni}?clave={clave}"
,
DTOCliente
.
class
,
cliente
.
getDni
(),
cliente
.
getClave
());
Assertions
.
assertThat
(
respuestaLogin
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
DTOCliente
clienteLogin
=
respuestaLogin
.
getBody
();
Assertions
.
assertThat
(
clienteLogin
.
getDni
()).
isEqualTo
(
cliente
.
getDni
());
}
@Test
public
void
testAnadirTarjetaACliente
()
{
// Registrar cliente y realizar login
DTOCliente
cliente
=
new
DTOCliente
(
"11995667D"
,
"Juan España España"
,
LocalDate
.
of
(
1990
,
11
,
1
),
"Cl La Luz, 13 - Jaén"
,
"988674533"
,
"jee@gmail.com"
,
"clave"
);
restTemplate
.
postForEntity
(
"/clientes"
,
cliente
,
DTOCuenta
.
class
);
DTOTarjeta
tarjeta
=
new
DTOTarjeta
(
"4111111111111111"
,
cliente
.
getNombre
(),
LocalDate
.
of
(
2022
,
12
,
1
),
"365"
);
ResponseEntity
<
DTOTarjeta
>
respuesta
=
restTemplate
.
postForEntity
(
"/clientes/{dni}/tarjetas"
,
tarjeta
,
DTOTarjeta
.
class
,
cliente
.
getDni
());
Assertions
.
assertThat
(
respuesta
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
CREATED
);
respuesta
=
restTemplate
.
getForEntity
(
"/clientes/{dni}/tarjetas/{num}"
,
DTOTarjeta
.
class
,
cliente
.
getDni
(),
tarjeta
.
getNum
());
Assertions
.
assertThat
(
respuesta
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
DTOTarjeta
tarjetaRecibida
=
respuesta
.
getBody
();
Assertions
.
assertThat
(
tarjetaRecibida
.
getNum
()).
isEqualTo
(
tarjeta
.
getNum
());
Assertions
.
assertThat
(
tarjetaRecibida
.
getTitular
()).
isEqualTo
(
tarjeta
.
getTitular
());
Assertions
.
assertThat
(
tarjetaRecibida
.
getFechaCaducidad
()).
isEqualTo
(
tarjeta
.
getFechaCaducidad
());
Assertions
.
assertThat
(
tarjetaRecibida
.
getCvc
()).
isEqualTo
(
tarjeta
.
getCvc
());
}
@BeforeEach
void
limpiarBaseDatos
()
{
limpiadorBaseDatos
.
limpiar
();
}
}
src/test/java/es/ujaen/dae/ujacoin/servicios/ServicioLimpiadoBaseDatos.java
View file @
8ebb7d16
...
@@ -33,7 +33,7 @@ public class ServicioLimpiadoBaseDatos {
...
@@ -33,7 +33,7 @@ public class ServicioLimpiadoBaseDatos {
final
String
deleteFrom
=
"delete from "
;
final
String
deleteFrom
=
"delete from "
;
/** Realizar borrado */
/** Realizar borrado */
void
limpiar
()
{
public
void
limpiar
()
{
transactionTemplate
.
executeWithoutResult
(
transactionStatus
->
{
transactionTemplate
.
executeWithoutResult
(
transactionStatus
->
{
for
(
String
tabla
:
entidades
)
{
for
(
String
tabla
:
entidades
)
{
em
.
createQuery
(
deleteFrom
+
tabla
).
executeUpdate
();
em
.
createQuery
(
deleteFrom
+
tabla
).
executeUpdate
();
...
...
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