Commit 25ef7c1b by Rubén Ramírez

feat: [UsuariosController]: Creadas funciones auxiliares en el controlador con su test

parent b02af0b2
package com.ujaen.tfg.mangaffinity.rest;
import com.ujaen.tfg.mangaffinity.entidades.BibliotecaPersonal;
import com.ujaen.tfg.mangaffinity.entidades.Usuario;
import com.ujaen.tfg.mangaffinity.excepciones.UsuarioNoExiste;
import com.ujaen.tfg.mangaffinity.excepciones.UsuarioYaRegistrado;
import com.ujaen.tfg.mangaffinity.rest.DTO.DTOBibliotecaPersonal;
import com.ujaen.tfg.mangaffinity.rest.DTO.DTOLoginRespuesta;
import com.ujaen.tfg.mangaffinity.rest.DTO.DTOUsuario;
import com.ujaen.tfg.mangaffinity.rest.DTO.Mapper;
......@@ -60,4 +63,27 @@ public class UsuariosController {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
@GetMapping("/{usuarioId}/biblioteca")
public ResponseEntity<DTOBibliotecaPersonal> obtenerBiblioteca(@PathVariable Long usuarioId) {
BibliotecaPersonal biblioteca = servicioUsuarios.obtenerBibliotecaDeUsuario(usuarioId);
if (biblioteca == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
return ResponseEntity.ok(new DTOBibliotecaPersonal(biblioteca));
}
@GetMapping("/email/{email}")
public ResponseEntity<Long> obtenerIdPorEmail(@PathVariable String email) {
try {
Usuario usuario = servicioUsuarios.buscaUsuario(email);
return ResponseEntity.ok(usuario.getId());
} catch (UsuarioNoExiste e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
}
}
......@@ -2,6 +2,7 @@ package com.ujaen.tfg.mangaffinity.rest;
import com.ujaen.tfg.mangaffinity.MangAffinityApplication;
import com.ujaen.tfg.mangaffinity.config.JpaTestConfig;
import com.ujaen.tfg.mangaffinity.entidades.BibliotecaPersonal;
import com.ujaen.tfg.mangaffinity.entidades.Usuario;
import com.ujaen.tfg.mangaffinity.rest.DTO.DTOLoginRespuesta;
import com.ujaen.tfg.mangaffinity.rest.DTO.DTOUsuario;
......@@ -23,15 +24,40 @@ import jakarta.annotation.PostConstruct;
import java.util.Map;
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.http.*;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.http.ResponseEntity;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
import com.ujaen.tfg.mangaffinity.entidades.BibliotecaPersonal;
import com.ujaen.tfg.mangaffinity.rest.DTO.DTOLoginRespuesta;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SpringBootTest(classes = {MangAffinityApplication.class, JpaTestConfig.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
public class TestUsuariosController {
@LocalServerPort
int localPort;
@Autowired
private TestRestTemplate restTemplate;
@Autowired
TestRestTemplate restTemplateUsuarios;
@PostConstruct
void crearRestTemplate() {
var restTemplateBuilder = new RestTemplateBuilder()
......@@ -121,4 +147,74 @@ public class TestUsuariosController {
Assertions.assertThat(respuestaValida.getBody().getToken()).isNotEmpty();
}
@Test
@DirtiesContext
void testObtenerIdPorEmail() {
// Registro un usuario
ResponseEntity<Void> respuestaRegistro = restTemplate.postForEntity("/usuarios/", Map.of(
"email", "usuario@example.com",
"nombreUsuario", "usuarioTest",
"contrasenia", "password"
), Void.class);
assertThat(respuestaRegistro.getStatusCode()).isEqualTo(HttpStatus.CREATED);
ResponseEntity<Long> respuestaUsuarioId = restTemplate.exchange(
"/usuarios/email/{email}", HttpMethod.GET, null, Long.class, "usuario@example.com"
);
// Valido respuesta
assertThat(respuestaUsuarioId.getStatusCode()).isEqualTo(HttpStatus.OK);
Long usuarioId = respuestaUsuarioId.getBody();
assertThat(usuarioId).isNotNull();
// Pruebo con con un email inexistente
ResponseEntity<Long> respuestaNoExiste = restTemplate.exchange(
"/usuarios/email/{email}", HttpMethod.GET, null, Long.class, "noexiste@example.com"
);
assertThat(respuestaNoExiste.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
}
@Test
@DirtiesContext
void testObtenerBibliotecaDeUsuario() {
// Registro un usuario
ResponseEntity<Void> respuestaRegistro = restTemplate.postForEntity("/usuarios/", Map.of(
"email", "usuario@example.com",
"nombreUsuario", "usuarioTest",
"contrasenia", "password"
), Void.class);
assertThat(respuestaRegistro.getStatusCode()).isEqualTo(HttpStatus.CREATED);
// Inicio sesión
var authResponse = restTemplate.postForEntity("/usuarios/usuario@example.com", Map.of(
"clave", "password"
), DTOLoginRespuesta.class);
assertThat(authResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
String token = "Bearer " + authResponse.getBody().getToken();
ResponseEntity<Long> respuestaUsuarioId = restTemplate.exchange(
"/usuarios/email/{email}", HttpMethod.GET, null, Long.class, "usuario@example.com"
);
assertThat(respuestaUsuarioId.getStatusCode()).isEqualTo(HttpStatus.OK);
Long usuarioId = respuestaUsuarioId.getBody();
assertThat(usuarioId).isNotNull();
// Obtengo la biblioteca personal del usuario
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", token);
HttpEntity<Void> requestEntity = new HttpEntity<>(headers);
ResponseEntity<BibliotecaPersonal> respuestaBiblioteca = restTemplate.exchange(
"/usuarios/{usuarioId}/biblioteca", HttpMethod.GET, requestEntity, BibliotecaPersonal.class, usuarioId
);
// Verifico la respuesta
assertThat(respuestaBiblioteca.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(respuestaBiblioteca.getBody().getUsuario().getId()).isEqualTo(usuarioId);
}
}
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