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
48b8624b
authored
Apr 20, 2025
by
Rubén Ramírez
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
feat: [TestUsuariosController]: Añadida la función para obtener las recoemndaciones
parent
675e0851
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
99 additions
and
2 deletions
src/test/java/com/ujaen/tfg/mangaffinity/rest/TestUsuariosController.java
src/test/java/com/ujaen/tfg/mangaffinity/rest/TestUsuariosController.java
View file @
48b8624b
...
...
@@ -3,6 +3,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.Genero
;
import
com.ujaen.tfg.mangaffinity.entidades.Usuario
;
import
com.ujaen.tfg.mangaffinity.rest.DTO.*
;
import
org.assertj.core.api.Assertions
;
...
...
@@ -14,6 +15,7 @@ import org.springframework.boot.test.context.SpringBootTest;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
import
org.springframework.boot.test.web.server.LocalServerPort
;
import
org.springframework.boot.web.client.RestTemplateBuilder
;
import
org.springframework.core.io.ByteArrayResource
;
import
org.springframework.http.*
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
org.springframework.security.web.SecurityFilterChain
;
...
...
@@ -21,7 +23,8 @@ import org.springframework.test.annotation.DirtiesContext;
import
org.springframework.test.context.ActiveProfiles
;
import
jakarta.annotation.PostConstruct
;
import
java.util.Map
;
import
java.time.LocalDate
;
import
java.util.*
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
...
@@ -39,7 +42,8 @@ import com.ujaen.tfg.mangaffinity.entidades.BibliotecaPersonal;
import
com.ujaen.tfg.mangaffinity.rest.DTO.DTOLoginRespuesta
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.util.LinkedMultiValueMap
;
import
org.springframework.util.MultiValueMap
;
@SpringBootTest
(
classes
=
{
MangAffinityApplication
.
class
,
JpaTestConfig
.
class
},
webEnvironment
=
SpringBootTest
.
WebEnvironment
.
RANDOM_PORT
)
...
...
@@ -320,5 +324,98 @@ public class TestUsuariosController {
assertThat
(
respBadPass
.
getBody
()).
isEqualTo
(
"contraseña incorrecta"
);
}
@Test
@DirtiesContext
void
testObtenerRecomendacionesDesdeControlador
()
{
// 1. Registro administrador
restTemplate
.
postForEntity
(
"/usuarios/"
,
Map
.
of
(
"email"
,
"admin@example.com"
,
"nombreUsuario"
,
"admin"
,
"contrasenia"
,
"adminpassword"
),
Void
.
class
);
// 2. Login administrador
var
authAdmin
=
restTemplate
.
postForEntity
(
"/usuarios/admin@example.com"
,
Map
.
of
(
"clave"
,
"adminpassword"
),
DTOLoginRespuesta
.
class
);
String
tokenAdmin
=
"Bearer "
+
authAdmin
.
getBody
().
getToken
();
// 3. Registro usuario normal
restTemplate
.
postForEntity
(
"/usuarios/"
,
Map
.
of
(
"email"
,
"user@test.com"
,
"nombreUsuario"
,
"userTest"
,
"contrasenia"
,
"testpass"
),
Void
.
class
);
// 4. Login usuario
var
authUser
=
restTemplate
.
postForEntity
(
"/usuarios/user@test.com"
,
Map
.
of
(
"clave"
,
"testpass"
),
DTOLoginRespuesta
.
class
);
String
tokenUser
=
"Bearer "
+
authUser
.
getBody
().
getToken
();
// 5. Obtener ID del usuario
Long
usuarioId
=
restTemplate
.
getForObject
(
"/usuarios/email/user@test.com"
,
Long
.
class
);
assertThat
(
usuarioId
).
isNotNull
();
// 6. Crear 2 recursos: uno favorito y uno similar
MultiValueMap
<
String
,
Object
>
formData1
=
new
LinkedMultiValueMap
<>();
DTORecurso
dtoFavorito
=
new
DTORecurso
(
null
,
"Favorito"
,
"desc"
,
LocalDate
.
now
(),
"Autor1"
,
null
,
Set
.
of
(
Genero
.
ACCION
),
0.0
);
formData1
.
add
(
"recurso"
,
dtoFavorito
);
formData1
.
add
(
"foto"
,
new
ByteArrayResource
(
new
byte
[
0
])
{
@Override
public
String
getFilename
()
{
return
"foto.jpg"
;
}
});
MultiValueMap
<
String
,
Object
>
formData2
=
new
LinkedMultiValueMap
<>();
DTORecurso
dtoSugerido
=
new
DTORecurso
(
null
,
"Sugerido"
,
"desc"
,
LocalDate
.
now
(),
"Autor1"
,
null
,
Set
.
of
(
Genero
.
AVENTURA
,
Genero
.
ACCION
),
0.0
);
formData2
.
add
(
"recurso"
,
dtoSugerido
);
formData2
.
add
(
"foto"
,
new
ByteArrayResource
(
new
byte
[
0
])
{
@Override
public
String
getFilename
()
{
return
"foto2.jpg"
;
}
});
HttpHeaders
headersAdmin
=
new
HttpHeaders
();
headersAdmin
.
set
(
"Authorization"
,
tokenAdmin
);
headersAdmin
.
setContentType
(
MediaType
.
MULTIPART_FORM_DATA
);
restTemplate
.
exchange
(
"/recursos/"
,
HttpMethod
.
POST
,
new
HttpEntity
<>(
formData1
,
headersAdmin
),
Void
.
class
);
restTemplate
.
exchange
(
"/recursos/"
,
HttpMethod
.
POST
,
new
HttpEntity
<>(
formData2
,
headersAdmin
),
Void
.
class
);
// 7. Obtener ID de ambos recursos
DTORecurso
[]
recursosFavorito
=
restTemplate
.
getForObject
(
"/recursos/titulo/Favorito"
,
DTORecurso
[].
class
);
DTORecurso
[]
recursosSugerido
=
restTemplate
.
getForObject
(
"/recursos/titulo/Sugerido"
,
DTORecurso
[].
class
);
assertThat
(
recursosFavorito
).
isNotNull
().
isNotEmpty
();
assertThat
(
recursosSugerido
).
isNotNull
().
isNotEmpty
();
Long
favoritoId
=
recursosFavorito
[
0
].
getId
();
Long
sugeridoId
=
recursosSugerido
[
0
].
getId
();
// 8. Añadir "Favorito" como favorito del usuario
HttpHeaders
headersUser
=
new
HttpHeaders
();
headersUser
.
set
(
"Authorization"
,
tokenUser
);
HttpEntity
<
Void
>
favRequest
=
new
HttpEntity
<>(
headersUser
);
ResponseEntity
<
Void
>
addFavResponse
=
restTemplate
.
exchange
(
"/biblioteca/{usuarioId}/favoritos/{recursoId}"
,
HttpMethod
.
PUT
,
favRequest
,
Void
.
class
,
usuarioId
,
favoritoId
);
assertThat
(
addFavResponse
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
// 9. Obtener recomendaciones
ResponseEntity
<
DTORecurso
[]>
respuesta
=
restTemplate
.
exchange
(
"/usuarios/{usuarioId}/recomendaciones"
,
HttpMethod
.
GET
,
favRequest
,
DTORecurso
[].
class
,
usuarioId
);
// 10. Validaciones finales
assertThat
(
respuesta
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
respuesta
.
getBody
()).
isNotNull
();
assertThat
(
respuesta
.
getBody
().
length
).
isGreaterThanOrEqualTo
(
1
);
boolean
contieneSugerido
=
Arrays
.
stream
(
respuesta
.
getBody
())
.
anyMatch
(
r
->
r
.
getId
().
equals
(
sugeridoId
));
assertThat
(
contieneSugerido
).
isTrue
();
}
}
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