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
3a026517
authored
Feb 23, 2025
by
Rubén Ramírez
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
feat: [TestRecursosController]:Implementado el test de modificar recurso en el controlador
parent
fc669a00
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
74 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 @
3a026517
...
...
@@ -19,6 +19,7 @@ import org.springframework.test.context.ActiveProfiles;
import
java.time.LocalDate
;
import
java.util.Arrays
;
import
java.util.Map
;
import
java.util.Set
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
...
...
@@ -343,5 +344,78 @@ public class TestRecursosController {
assertThat
(
deleteAgainResponse
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
NOT_FOUND
);
}
@Test
@DirtiesContext
void
testModificarRecurso
()
{
// Crear un administrador
restTemplate
.
postForEntity
(
"/usuarios/"
,
Map
.
of
(
"email"
,
"admin@example.com"
,
"nombreUsuario"
,
"admin"
,
"contrasenia"
,
"adminpassword"
),
Void
.
class
);
// Inicio sesión con 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 un recurso con autenticación
Recurso
recurso
=
new
Recurso
(
"Titulo Original"
,
"Descripción Original"
,
LocalDate
.
of
(
2022
,
5
,
10
),
"Autor Original"
);
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
set
(
"Authorization"
,
token
);
HttpEntity
<
Recurso
>
request
=
new
HttpEntity
<>(
recurso
,
headers
);
var
respuestaCreacion
=
restTemplate
.
exchange
(
"/recursos/"
,
HttpMethod
.
POST
,
request
,
Void
.
class
);
assertThat
(
respuestaCreacion
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
CREATED
);
// Buscar el recurso por título
ResponseEntity
<
DTORecurso
[]>
respuestaBusqueda
=
restTemplate
.
getForEntity
(
"/recursos/titulo/{titulo}"
,
DTORecurso
[].
class
,
"Titulo Original"
);
assertThat
(
respuestaBusqueda
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
respuestaBusqueda
.
getBody
()).
isNotEmpty
();
Long
recursoId
=
respuestaBusqueda
.
getBody
()[
0
].
getId
();
// Caso 1: Modificar todos los campos
DTORecurso
nuevosDatos
=
new
DTORecurso
(
recursoId
,
"Titulo Modificado"
,
"Descripción Modificada"
,
LocalDate
.
of
(
2023
,
1
,
1
),
"Autor Modificado"
,
Set
.
of
(
Genero
.
ACCION
,
Genero
.
AVENTURA
));
HttpEntity
<
DTORecurso
>
updateRequest
=
new
HttpEntity
<>(
nuevosDatos
,
headers
);
ResponseEntity
<
DTORecurso
>
respuestaModificacion
=
restTemplate
.
exchange
(
"/recursos/{id}"
,
HttpMethod
.
PUT
,
updateRequest
,
DTORecurso
.
class
,
recursoId
);
assertThat
(
respuestaModificacion
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
respuestaModificacion
.
getBody
()).
isNotNull
();
assertThat
(
respuestaModificacion
.
getBody
().
getTitulo
()).
isEqualTo
(
"Titulo Modificado"
);
assertThat
(
respuestaModificacion
.
getBody
().
getDescripcion
()).
isEqualTo
(
"Descripción Modificada"
);
assertThat
(
respuestaModificacion
.
getBody
().
getFechaPublicacion
()).
isEqualTo
(
LocalDate
.
of
(
2023
,
1
,
1
));
assertThat
(
respuestaModificacion
.
getBody
().
getAutor
()).
isEqualTo
(
"Autor Modificado"
);
assertThat
(
respuestaModificacion
.
getBody
().
getGeneros
()).
containsExactlyInAnyOrder
(
Genero
.
ACCION
,
Genero
.
AVENTURA
);
// Caso 2: Intentar modificar un recurso que NO existe
DTORecurso
datosFalsos
=
new
DTORecurso
(
null
,
"Titulo Inexistente"
,
"Descripción"
,
LocalDate
.
of
(
2023
,
1
,
1
),
"Autor"
,
Set
.
of
(
Genero
.
DRAMA
));
HttpEntity
<
DTORecurso
>
fakeUpdateRequest
=
new
HttpEntity
<>(
datosFalsos
,
headers
);
ResponseEntity
<
Void
>
respuestaNoEncontrado
=
restTemplate
.
exchange
(
"/recursos/{id}"
,
HttpMethod
.
PUT
,
fakeUpdateRequest
,
Void
.
class
,
9999
);
assertThat
(
respuestaNoEncontrado
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
NOT_FOUND
);
// Caso 3: Intentar modificar sin autenticación
HttpEntity
<
DTORecurso
>
noAuthRequest
=
new
HttpEntity
<>(
nuevosDatos
);
ResponseEntity
<
Void
>
respuestaNoAutenticado
=
restTemplate
.
exchange
(
"/recursos/{id}"
,
HttpMethod
.
PUT
,
noAuthRequest
,
Void
.
class
,
recursoId
);
assertThat
(
respuestaNoAutenticado
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
UNAUTHORIZED
);
}
}
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