Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Rubén Ramírez
/
MangAffinity
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
25ef7c1b
authored
Feb 24, 2025
by
Rubén Ramírez
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
feat: [UsuariosController]: Creadas funciones auxiliares en el controlador con su test
parent
b02af0b2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
122 additions
and
0 deletions
src/main/java/com/ujaen/tfg/mangaffinity/rest/UsuariosController.java
src/test/java/com/ujaen/tfg/mangaffinity/rest/TestUsuariosController.java
src/main/java/com/ujaen/tfg/mangaffinity/rest/UsuariosController.java
View file @
25ef7c1b
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
();
}
}
}
src/test/java/com/ujaen/tfg/mangaffinity/rest/TestUsuariosController.java
View file @
25ef7c1b
...
...
@@ -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
);
}
}
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