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
567a164e
authored
Apr 07, 2025
by
Rubén Ramírez
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
fix: [TestRecursoController]: Testeada la función para añadir las reseñas
parent
0fa6d82c
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
81 additions
and
4 deletions
src/test/java/com/ujaen/tfg/mangaffinity/rest/TestRecursosController.java
src/test/java/com/ujaen/tfg/mangaffinity/rest/TestRecursosController.java
View file @
567a164e
...
...
@@ -5,10 +5,7 @@ import com.ujaen.tfg.mangaffinity.config.JpaTestConfig;
import
com.ujaen.tfg.mangaffinity.entidades.Genero
;
import
com.ujaen.tfg.mangaffinity.entidades.Recurso
;
import
com.ujaen.tfg.mangaffinity.entidades.TipoRecurso
;
import
com.ujaen.tfg.mangaffinity.rest.DTO.DTOCapitulo
;
import
com.ujaen.tfg.mangaffinity.rest.DTO.DTOFuenteCapitulo
;
import
com.ujaen.tfg.mangaffinity.rest.DTO.DTOLoginRespuesta
;
import
com.ujaen.tfg.mangaffinity.rest.DTO.DTORecurso
;
import
com.ujaen.tfg.mangaffinity.rest.DTO.*
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
...
...
@@ -1052,4 +1049,84 @@ public class TestRecursosController {
}
}
@Test
@DirtiesContext
void
testAnadirResena
()
{
// Registro e inicio de sesión del admin
restTemplate
.
postForEntity
(
"/usuarios/"
,
Map
.
of
(
"email"
,
"admin@example.com"
,
"nombreUsuario"
,
"admin"
,
"contrasenia"
,
"adminpassword"
),
Void
.
class
);
var
authResponse
=
restTemplate
.
postForEntity
(
"/usuarios/admin@example.com"
,
Map
.
of
(
"clave"
,
"adminpassword"
),
DTOLoginRespuesta
.
class
);
assertThat
(
authResponse
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
String
token
=
authResponse
.
getBody
().
getToken
();
// Creo un recurso
MultiValueMap
<
String
,
Object
>
formData
=
new
LinkedMultiValueMap
<>();
DTORecurso
dtoRecurso
=
new
DTORecurso
(
null
,
"Death Note"
,
"Shinigamis y cuadernos"
,
LocalDate
.
of
(
2003
,
12
,
1
),
"Tsugumi Ohba"
,
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
.
setBearerAuth
(
token
);
headers
.
setContentType
(
MediaType
.
MULTIPART_FORM_DATA
);
HttpEntity
<
MultiValueMap
<
String
,
Object
>>
request
=
new
HttpEntity
<>(
formData
,
headers
);
restTemplate
.
exchange
(
"/recursos/"
,
HttpMethod
.
POST
,
request
,
Void
.
class
);
// Busco el recurso por título
var
buscar
=
restTemplate
.
getForEntity
(
"/recursos/titulo/{titulo}"
,
DTORecurso
[].
class
,
"Death Note"
);
Long
recursoId
=
buscar
.
getBody
()[
0
].
getId
();
// Construyo la reseña
DTOResena
rese
ñ
a
=
new
DTOResena
();
rese
ñ
a
.
setUsuarioId
(
1L
);
rese
ñ
a
.
setRecursoId
(
recursoId
);
rese
ñ
a
.
setEstrellas
(
5
);
rese
ñ
a
.
setTexto
(
"Obra maestra"
);
rese
ñ
a
.
setFechaPublicacion
(
LocalDate
.
now
());
rese
ñ
a
.
setUsuarioNombre
(
"admin"
);
HttpHeaders
authHeaders
=
new
HttpHeaders
();
authHeaders
.
setBearerAuth
(
token
);
authHeaders
.
setContentType
(
MediaType
.
APPLICATION_JSON
);
HttpEntity
<
DTOResena
>
entity
=
new
HttpEntity
<>(
rese
ñ
a
,
authHeaders
);
// Envío la reseña
ResponseEntity
<
Void
>
respuesta
=
restTemplate
.
exchange
(
"/recursos/"
+
recursoId
+
"/resenas"
,
HttpMethod
.
POST
,
entity
,
Void
.
class
);
assertThat
(
respuesta
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
CREATED
);
// Usuario no existente
rese
ñ
a
.
setUsuarioId
(
9999L
);
HttpEntity
<
DTOResena
>
entityUsuarioFalso
=
new
HttpEntity
<>(
rese
ñ
a
,
authHeaders
);
ResponseEntity
<
Void
>
respuesta404Usuario
=
restTemplate
.
exchange
(
"/recursos/"
+
recursoId
+
"/resenas"
,
HttpMethod
.
POST
,
entityUsuarioFalso
,
Void
.
class
);
assertThat
(
respuesta404Usuario
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
NOT_FOUND
);
// Recurso no existente
rese
ñ
a
.
setUsuarioId
(
1L
);
HttpEntity
<
DTOResena
>
entityRecursoFalso
=
new
HttpEntity
<>(
rese
ñ
a
,
authHeaders
);
ResponseEntity
<
Void
>
respuesta404Recurso
=
restTemplate
.
exchange
(
"/recursos/9999/resenas"
,
HttpMethod
.
POST
,
entityRecursoFalso
,
Void
.
class
);
assertThat
(
respuesta404Recurso
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
NOT_FOUND
);
}
}
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