Commit 53517a44 by Antonio Rueda

Implementar resto de tests

parent 41bedd45
/*
* 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.DTOMovimiento;
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 ControladorRESTTest {
@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);
}
/** Intento de creación de un cliente inválido */
@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 de alta y login de cliente */
@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());
}
/** Creación de cuenta adicional */
@Test
public void testCuentaAdicional() {
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
);
ResponseEntity<DTOCuenta> respuesta = restTemplate.postForEntity(
"/clientes/{dni}/cuentas",
null,
DTOCuenta.class,
cliente.getDni()
);
Assertions.assertThat(respuesta.getStatusCode()).isEqualTo(HttpStatus.CREATED);
DTOCuenta[] cuentas = restTemplate.getForEntity(
"/clientes/{dni}/cuentas",
DTOCuenta[].class,
cliente.getDni()
).getBody();
Assertions.assertThat(cuentas).hasSize(2);
Assertions.assertThat(cuentas[0].getNum()).isNotEqualTo(cuentas[1].getNum());
}
/** Test de creación de tarjeta */
@Test
public void testAnadirTarjetaACliente() {
// Registrar cliente
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());
}
/** Test de ingreso en cuenta */
@Test
public void testIngreso() {
// Registrar cliente
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"
);
DTOCuenta cuenta = restTemplate.postForEntity(
"/clientes",
cliente,
DTOCuenta.class
).getBody();
DTOTarjeta tarjeta = new DTOTarjeta(
"4111111111111111",
cliente.getNombre(),
LocalDate.of(2022, 12, 1),
"365"
);
restTemplate.postForEntity(
"/clientes/{dni}/tarjetas",
tarjeta,
DTOTarjeta.class,
cliente.getDni()
);
// Realizar ingreso y comprobar estado de la cuenta
DTOMovimiento ingreso = DTOMovimiento.ingreso(tarjeta.getNum(), 1000);
ResponseEntity<Void> respuestaRegistroMovimiento = restTemplate.postForEntity(
"/clientes/{dni}/cuentas/{num}/movimientos",
ingreso,
Void.class,
cliente.getDni(), cuenta.getNum()
);
Assertions.assertThat(respuestaRegistroMovimiento.getStatusCode()).isEqualTo(HttpStatus.CREATED);
// Refrescar estado de la cuenta y comprobar saldo
cuenta = restTemplate.getForObject("/clientes/{dni}/cuentas/{num}",
DTOCuenta.class,
cliente.getDni(), cuenta.getNum());
Assertions.assertThat(cuenta.getSaldo()).isEqualTo(1000);
ResponseEntity<DTOMovimiento[]> respuestaListadoMovimientos = restTemplate.getForEntity(
"/clientes/{dni}/cuentas/{num}/movimientos",
DTOMovimiento[].class,
cliente.getDni(), cuenta.getNum()
);
DTOMovimiento[] movimientos = respuestaListadoMovimientos.getBody();
Assertions.assertThat(movimientos).hasSize(1);
Assertions.assertThat(movimientos[0].getTipo()).isEqualTo(DTOMovimiento.INGRESO);
Assertions.assertThat(movimientos[0].getImporte()).isEqualTo(ingreso.getImporte());
}
/** Test de transferencia entre cuentas */
@Test
public void testTransferencia() {
// Registrar cliente
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"
);
DTOCuenta cuentaOrigen = restTemplate.postForEntity(
"/clientes",
cliente,
DTOCuenta.class
).getBody();
DTOTarjeta tarjeta = new DTOTarjeta(
"4111111111111111",
cliente.getNombre(),
LocalDate.of(2022, 12, 1),
"365"
);
restTemplate.postForEntity(
"/clientes/{dni}/tarjetas",
tarjeta,
DTOTarjeta.class,
cliente.getDni()
);
// Realizar ingreso y comprobar estado de la cuenta
DTOMovimiento ingreso = DTOMovimiento.ingreso(tarjeta.getNum(), 1000);
restTemplate.postForEntity(
"/clientes/{dni}/cuentas/{num}/movimientos",
ingreso,
Void.class,
cliente.getDni(), cuentaOrigen.getNum()
);
// Crear segundo cliente
DTOCliente cliente2 = new DTOCliente(
"99207668E",
"Pedro Jaén, Jaén",
LocalDate.of(1992, 1, 2),
"Cl La Paz, 20 - Jaén",
"670701570",
"pjj@gmail.com",
"clavezzz");
DTOCuenta cuentaDestino = restTemplate.postForEntity(
"/clientes",
cliente2,
DTOCuenta.class
).getBody();
// Realizar transferencia
DTOMovimiento transferencia = DTOMovimiento.transferencia(cuentaDestino.getNum(), 500);
restTemplate.postForEntity(
"/clientes/{dni}/cuentas/{num}/movimientos",
transferencia,
Void.class,
cliente.getDni(), cuentaOrigen.getNum()
);
// Refrescar cuenta origen y destino
// Refrescar estados de la cuentas y comprobar saldos
cuentaOrigen = restTemplate.getForObject("/clientes/{dni}/cuentas/{num}",
DTOCuenta.class,
cliente.getDni(), cuentaOrigen.getNum());
Assertions.assertThat(cuentaOrigen.getSaldo()).isEqualTo(500);
cuentaDestino = restTemplate.getForObject("/clientes/{dni}/cuentas/{num}",
DTOCuenta.class,
cliente2.getDni(), cuentaDestino.getNum());
Assertions.assertThat(cuentaDestino.getSaldo()).isEqualTo(500);
// Listar movimientos de la cuenta origen
DTOMovimiento[] movimientos = restTemplate.getForEntity(
"/clientes/{dni}/cuentas/{num}/movimientos",
DTOMovimiento[].class,
cliente.getDni(), cuentaOrigen.getNum()
).getBody();
Assertions.assertThat(movimientos).hasSize(2);
Assertions.assertThat(movimientos[1].getTipo()).isEqualTo(DTOMovimiento.TRANSFERENCIA_EMITIDA);
// Listar movimientos de la cuenta destino
movimientos = restTemplate.getForEntity(
"/clientes/{dni}/cuentas/{num}/movimientos",
DTOMovimiento[].class,
cliente2.getDni(), cuentaDestino.getNum()
).getBody();
Assertions.assertThat(movimientos).hasSize(1);
Assertions.assertThat(movimientos[0].getTipo()).isEqualTo(DTOMovimiento.TRANSFERENCIA_RECIBIDA);
}
/** Reintegro sin saldo suficiente */
@Test
public void testReintegroSinSaldo() {
// Registrar cliente
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"
);
DTOCuenta cuenta = restTemplate.postForEntity(
"/clientes",
cliente,
DTOCuenta.class
).getBody();
DTOTarjeta tarjeta = new DTOTarjeta(
"4111111111111111",
cliente.getNombre(),
LocalDate.of(2022, 12, 1),
"365"
);
restTemplate.postForEntity(
"/clientes/{dni}/tarjetas",
tarjeta,
DTOTarjeta.class,
cliente.getDni()
);
// Realizar ingreso en cuenta
DTOMovimiento ingreso = DTOMovimiento.ingreso(tarjeta.getNum(), 1000);
restTemplate.postForEntity(
"/clientes/{dni}/cuentas/{num}/movimientos",
ingreso,
Void.class,
cliente.getDni(), cuenta.getNum()
);
// Primer reintegro correcto
DTOMovimiento reintegro = DTOMovimiento.reintegro(tarjeta.getNum(), 1000);
ResponseEntity<Void> respuesta = restTemplate.postForEntity(
"/clientes/{dni}/cuentas/{num}/movimientos",
reintegro,
Void.class,
cliente.getDni(), cuenta.getNum()
);
Assertions.assertThat(respuesta.getStatusCode()).isEqualTo(HttpStatus.CREATED);
// No hay saldo suficiente para el segundo reintegro
respuesta = restTemplate.postForEntity(
"/clientes/{dni}/cuentas/{num}/movimientos",
reintegro,
Void.class,
cliente.getDni(), cuenta.getNum()
);
Assertions.assertThat(respuesta.getStatusCode()).isEqualTo(HttpStatus.PRECONDITION_FAILED);
}
/** Test de listado de movimientos con paginación */
@Test
public void testListadoMovimientosPaginado() {
// Registrar cliente
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"
);
DTOCuenta cuenta = restTemplate.postForEntity(
"/clientes",
cliente,
DTOCuenta.class
).getBody();
DTOTarjeta tarjeta = new DTOTarjeta(
"4111111111111111",
cliente.getNombre(),
LocalDate.of(2022, 12, 1),
"365"
);
restTemplate.postForEntity(
"/clientes/{dni}/tarjetas",
tarjeta,
DTOTarjeta.class,
cliente.getDni()
);
// Realizar ingreso en cuenta
DTOMovimiento ingreso = DTOMovimiento.ingreso(tarjeta.getNum(), 1000);
float[] importeIngresos = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120};
for (float importe: importeIngresos) {
restTemplate.postForEntity(
"/clientes/{dni}/cuentas/{num}/movimientos",
DTOMovimiento.ingreso(tarjeta.getNum(), importe),
Void.class,
cliente.getDni(), cuenta.getNum()
);
}
// Obtener primera página (10 elementos)
ResponseEntity<DTOMovimiento[]> respuesta1 = restTemplate.getForEntity(
"/clientes/{dni}/cuentas/{num}/movimientos",
DTOMovimiento[].class,
cliente.getDni(), cuenta.getNum()
);
DTOMovimiento[] movimientos = respuesta1.getBody();
Assertions.assertThat(movimientos).hasSize(10);
Assertions.assertThat(movimientos[0].getImporte()).isEqualTo(10);
// Obtener segunda página (2 selementos)
ResponseEntity<DTOMovimiento[]> respuesta2 = restTemplate.getForEntity(
"/clientes/{dni}/cuentas/{num}/movimientos?pag=2",
DTOMovimiento[].class,
cliente.getDni(), cuenta.getNum()
);
movimientos = respuesta2.getBody();
Assertions.assertThat(movimientos).hasSize(2);
Assertions.assertThat(movimientos[0].getImporte()).isEqualTo(110);
// Obtener primera página con 5 elementos solo
ResponseEntity<DTOMovimiento[]> respuesta3 = restTemplate.getForEntity(
"/clientes/{dni}/cuentas/{num}/movimientos?pag=1&num=5",
DTOMovimiento[].class,
cliente.getDni(), cuenta.getNum()
);
movimientos = respuesta3.getBody();
Assertions.assertThat(movimientos).hasSize(5);
Assertions.assertThat(movimientos[0].getImporte()).isEqualTo(10);
}
@BeforeEach
void limpiarBaseDatos() {
limpiadorBaseDatos.limpiar();
}
}
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