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
6798eaeb
authored
Apr 03, 2025
by
Rubén Ramírez
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
feat: [TestRecursosController]: Testeada la función del ranking del controlador
parent
a9721011
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
102 additions
and
0 deletions
src/test/java/com/ujaen/tfg/mangaffinity/rest/TestRecursosController.java
src/test/java/com/ujaen/tfg/mangaffinity/rest/TestRecursosController.java
View file @
6798eaeb
...
@@ -950,4 +950,106 @@ public class TestRecursosController {
...
@@ -950,4 +950,106 @@ public class TestRecursosController {
assertThat
(
respuesta
.
getBody
()).
isNotEmpty
();
assertThat
(
respuesta
.
getBody
()).
isNotEmpty
();
}
}
@Test
@DirtiesContext
void
testObtenerRanking
()
{
// Registro un administrador
restTemplate
.
postForEntity
(
"/usuarios/"
,
Map
.
of
(
"email"
,
"admin@example.com"
,
"nombreUsuario"
,
"admin"
,
"contrasenia"
,
"adminpassword"
),
Void
.
class
);
// Inicio sesión con el administrador
var
authResponse
=
restTemplate
.
postForEntity
(
"/usuarios/admin@example.com"
,
Map
.
of
(
"clave"
,
"adminpassword"
),
DTOLoginRespuesta
.
class
);
assertThat
(
authResponse
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
String
token
=
"Bearer "
+
authResponse
.
getBody
().
getToken
();
// Crear recursos con 0 accesos
for
(
int
i
=
1
;
i
<=
5
;
i
++)
{
MultiValueMap
<
String
,
Object
>
formData
=
new
LinkedMultiValueMap
<>();
DTORecurso
dtoRecurso
=
new
DTORecurso
(
null
,
"Recurso "
+
i
,
"Descripción "
+
i
,
LocalDate
.
now
(),
"Autor "
+
i
,
null
,
new
HashSet
<>()
);
formData
.
add
(
"recurso"
,
dtoRecurso
);
formData
.
add
(
"foto"
,
new
ByteArrayResource
(
new
byte
[
0
])
{
@Override
public
String
getFilename
()
{
return
"dummy.jpg"
;
}
});
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
set
(
"Authorization"
,
token
);
headers
.
setContentType
(
MediaType
.
MULTIPART_FORM_DATA
);
HttpEntity
<
MultiValueMap
<
String
,
Object
>>
request
=
new
HttpEntity
<>(
formData
,
headers
);
restTemplate
.
exchange
(
"/recursos/"
,
HttpMethod
.
POST
,
request
,
Void
.
class
);
}
// Verifico si los recursos fueron creados correctamente
ResponseEntity
<
DTORecurso
[]>
respuesta
=
restTemplate
.
exchange
(
"/recursos"
,
HttpMethod
.
GET
,
null
,
DTORecurso
[].
class
);
assertThat
(
respuesta
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
respuesta
.
getBody
()).
isNotEmpty
();
ResponseEntity
<
DTORecurso
[]>
rankingResponse
=
restTemplate
.
exchange
(
"/recursos/ranking"
,
HttpMethod
.
GET
,
null
,
DTORecurso
[].
class
);
if
(
rankingResponse
.
getStatusCode
().
equals
(
HttpStatus
.
NO_CONTENT
))
{
assertThat
(
rankingResponse
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
NO_CONTENT
);
}
else
{
assertThat
(
rankingResponse
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
rankingResponse
.
getBody
()).
isNotEmpty
();
assertThat
(
rankingResponse
.
getBody
().
length
).
isEqualTo
(
5
);
// Debe devolver 5 recursos
for
(
DTORecurso
recurso
:
rankingResponse
.
getBody
())
{
assertThat
(
recurso
).
isNotNull
();
assertThat
(
recurso
.
getTitulo
()).
isNotBlank
();
assertThat
(
recurso
.
getAutor
()).
isNotBlank
();
assertThat
(
recurso
.
getFechaPublicacion
()).
isNotNull
();
}
}
if
(
rankingResponse
.
getStatusCode
().
equals
(
HttpStatus
.
NO_CONTENT
))
{
for
(
int
i
=
6
;
i
<=
8
;
i
++)
{
MultiValueMap
<
String
,
Object
>
formData
=
new
LinkedMultiValueMap
<>();
DTORecurso
dtoRecurso
=
new
DTORecurso
(
null
,
"Recurso Sin Accesos "
+
i
,
"Descripción "
+
i
,
LocalDate
.
now
(),
"Autor "
+
i
,
null
,
new
HashSet
<>()
);
formData
.
add
(
"recurso"
,
dtoRecurso
);
formData
.
add
(
"foto"
,
new
ByteArrayResource
(
new
byte
[
0
])
{
@Override
public
String
getFilename
()
{
return
"dummy.jpg"
;
}
});
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
set
(
"Authorization"
,
token
);
headers
.
setContentType
(
MediaType
.
MULTIPART_FORM_DATA
);
HttpEntity
<
MultiValueMap
<
String
,
Object
>>
request
=
new
HttpEntity
<>(
formData
,
headers
);
restTemplate
.
exchange
(
"/recursos/"
,
HttpMethod
.
POST
,
request
,
Void
.
class
);
}
rankingResponse
=
restTemplate
.
exchange
(
"/recursos/ranking"
,
HttpMethod
.
GET
,
null
,
DTORecurso
[].
class
);
assertThat
(
rankingResponse
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
rankingResponse
.
getBody
()).
isNotEmpty
();
assertThat
(
rankingResponse
.
getBody
().
length
).
isEqualTo
(
8
);
// Ahora deberían devolver 8 recursos
for
(
DTORecurso
recurso
:
rankingResponse
.
getBody
())
{
assertThat
(
recurso
).
isNotNull
();
assertThat
(
recurso
.
getTitulo
()).
isNotBlank
();
assertThat
(
recurso
.
getAutor
()).
isNotBlank
();
assertThat
(
recurso
.
getFechaPublicacion
()).
isNotNull
();
}
}
}
}
}
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