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
5292b214
authored
Apr 20, 2025
by
Rubén Ramírez
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
feat: [BibliotecaPersonalController]: Testeadas las funciones para la gestión de favoritos
parent
f0aaca12
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
114 additions
and
0 deletions
src/test/java/com/ujaen/tfg/mangaffinity/rest/TestBibliotecaPersonalController.java
src/test/java/com/ujaen/tfg/mangaffinity/rest/TestBibliotecaPersonalController.java
View file @
5292b214
...
...
@@ -373,4 +373,118 @@ public class TestBibliotecaPersonalController {
assertThat
(
respuestaPostEliminacion
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
}
@Test
@DirtiesContext
void
testAnadirRecursoAFavoritos
()
{
// Registro y login
restTemplate
.
postForEntity
(
"/usuarios/"
,
Map
.
of
(
"email"
,
"usuario@example.com"
,
"nombreUsuario"
,
"usuario"
,
"contrasenia"
,
"password"
),
Void
.
class
);
var
auth
=
restTemplate
.
postForEntity
(
"/usuarios/usuario@example.com"
,
Map
.
of
(
"clave"
,
"password"
),
DTOLoginRespuesta
.
class
);
String
token
=
"Bearer "
+
auth
.
getBody
().
getToken
();
Long
usuarioId
=
restTemplate
.
getForObject
(
"/usuarios/email/usuario@example.com"
,
Long
.
class
);
// Crear recurso como admin
restTemplate
.
postForEntity
(
"/usuarios/"
,
Map
.
of
(
"email"
,
"admin@example.com"
,
"nombreUsuario"
,
"admin"
,
"contrasenia"
,
"adminpassword"
),
Void
.
class
);
var
authAdmin
=
restTemplate
.
postForEntity
(
"/usuarios/admin@example.com"
,
Map
.
of
(
"clave"
,
"adminpassword"
),
DTOLoginRespuesta
.
class
);
String
tokenAdmin
=
"Bearer "
+
authAdmin
.
getBody
().
getToken
();
DTORecurso
dto
=
new
DTORecurso
(
null
,
"Favorito Test"
,
"Desc"
,
LocalDate
.
now
(),
"Autor"
,
""
,
new
HashSet
<>(),
0.0
);
MultiValueMap
<
String
,
Object
>
formData
=
new
LinkedMultiValueMap
<>();
formData
.
add
(
"recurso"
,
dto
);
formData
.
add
(
"foto"
,
new
ByteArrayResource
(
new
byte
[
0
])
{
@Override
public
String
getFilename
()
{
return
"foto.jpg"
;
}
});
HttpHeaders
adminHeaders
=
new
HttpHeaders
();
adminHeaders
.
set
(
"Authorization"
,
tokenAdmin
);
adminHeaders
.
setContentType
(
MediaType
.
MULTIPART_FORM_DATA
);
restTemplate
.
exchange
(
"/recursos/"
,
HttpMethod
.
POST
,
new
HttpEntity
<>(
formData
,
adminHeaders
),
Void
.
class
);
Long
recursoId
=
restTemplate
.
getForObject
(
"/recursos/titulo/Favorito Test"
,
DTORecurso
[].
class
)[
0
].
getId
();
// Añadir a favoritos
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
set
(
"Authorization"
,
token
);
ResponseEntity
<
Void
>
respuesta
=
restTemplate
.
exchange
(
"/biblioteca/{usuarioId}/favoritos/{recursoId}"
,
HttpMethod
.
PUT
,
new
HttpEntity
<>(
headers
),
Void
.
class
,
usuarioId
,
recursoId
);
assertThat
(
respuesta
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
}
@Test
@DirtiesContext
void
testObtenerFavoritos
()
{
// Configuración previa igual que el anterior
testAnadirRecursoAFavoritos
();
// Reutiliza el flujo anterior
var
auth
=
restTemplate
.
postForEntity
(
"/usuarios/usuario@example.com"
,
Map
.
of
(
"clave"
,
"password"
),
DTOLoginRespuesta
.
class
);
String
token
=
"Bearer "
+
auth
.
getBody
().
getToken
();
Long
usuarioId
=
restTemplate
.
getForObject
(
"/usuarios/email/usuario@example.com"
,
Long
.
class
);
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
set
(
"Authorization"
,
token
);
ResponseEntity
<
DTORecurso
[]>
favoritos
=
restTemplate
.
exchange
(
"/biblioteca/{usuarioId}/favoritos"
,
HttpMethod
.
GET
,
new
HttpEntity
<>(
headers
),
DTORecurso
[].
class
,
usuarioId
);
assertThat
(
favoritos
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
favoritos
.
getBody
()).
isNotNull
();
assertThat
(
favoritos
.
getBody
()).
hasSize
(
1
);
assertThat
(
favoritos
.
getBody
()[
0
].
getTitulo
()).
isEqualTo
(
"Favorito Test"
);
}
@Test
@DirtiesContext
void
testEliminarFavorito
()
{
// Configuración previa
testAnadirRecursoAFavoritos
();
var
auth
=
restTemplate
.
postForEntity
(
"/usuarios/usuario@example.com"
,
Map
.
of
(
"clave"
,
"password"
),
DTOLoginRespuesta
.
class
);
String
token
=
"Bearer "
+
auth
.
getBody
().
getToken
();
Long
usuarioId
=
restTemplate
.
getForObject
(
"/usuarios/email/usuario@example.com"
,
Long
.
class
);
Long
recursoId
=
restTemplate
.
getForObject
(
"/recursos/titulo/Favorito Test"
,
DTORecurso
[].
class
)[
0
].
getId
();
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
set
(
"Authorization"
,
token
);
ResponseEntity
<
Void
>
respuesta
=
restTemplate
.
exchange
(
"/biblioteca/{usuarioId}/favoritos/{recursoId}"
,
HttpMethod
.
DELETE
,
new
HttpEntity
<>(
headers
),
Void
.
class
,
usuarioId
,
recursoId
);
assertThat
(
respuesta
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
NO_CONTENT
);
// Verifico que ya no está
ResponseEntity
<
DTORecurso
[]>
favoritos
=
restTemplate
.
exchange
(
"/biblioteca/{usuarioId}/favoritos"
,
HttpMethod
.
GET
,
new
HttpEntity
<>(
headers
),
DTORecurso
[].
class
,
usuarioId
);
assertThat
(
favoritos
.
getBody
()).
isEmpty
();
}
}
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