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
3da3ccae
authored
Feb 23, 2025
by
Rubén Ramírez
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
feat: [TestRecursosControlador]: Añadido el test para añadir nuevos capítulos en el controlador
parent
cbf0ddd3
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
53 additions
and
1 deletions
src/test/java/com/ujaen/tfg/mangaffinity/rest/TestRecursosController.java
src/test/java/com/ujaen/tfg/mangaffinity/rest/TestRecursosController.java
View file @
3da3ccae
...
@@ -4,6 +4,9 @@ import com.ujaen.tfg.mangaffinity.MangAffinityApplication;
...
@@ -4,6 +4,9 @@ import com.ujaen.tfg.mangaffinity.MangAffinityApplication;
import
com.ujaen.tfg.mangaffinity.config.JpaTestConfig
;
import
com.ujaen.tfg.mangaffinity.config.JpaTestConfig
;
import
com.ujaen.tfg.mangaffinity.entidades.Genero
;
import
com.ujaen.tfg.mangaffinity.entidades.Genero
;
import
com.ujaen.tfg.mangaffinity.entidades.Recurso
;
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.DTOLoginRespuesta
;
import
com.ujaen.tfg.mangaffinity.rest.DTO.DTORecurso
;
import
com.ujaen.tfg.mangaffinity.rest.DTO.DTORecurso
;
import
org.assertj.core.api.Assertions
;
import
org.assertj.core.api.Assertions
;
...
@@ -18,6 +21,7 @@ import org.springframework.test.context.ActiveProfiles;
...
@@ -18,6 +21,7 @@ import org.springframework.test.context.ActiveProfiles;
import
java.time.LocalDate
;
import
java.time.LocalDate
;
import
java.util.Arrays
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.Set
;
...
@@ -451,11 +455,59 @@ public class TestRecursosController {
...
@@ -451,11 +455,59 @@ public class TestRecursosController {
assertThat
(
respuestaAdmin
.
getBody
()).
isNotNull
();
assertThat
(
respuestaAdmin
.
getBody
()).
isNotNull
();
assertThat
(
respuestaAdmin
.
getBody
().
length
).
isLessThanOrEqualTo
(
15
);
assertThat
(
respuestaAdmin
.
getBody
().
length
).
isLessThanOrEqualTo
(
15
);
//
🔹
Caso 2: Obtener recursos sin autenticación
// Caso 2: Obtener recursos sin autenticación
ResponseEntity
<
Void
>
respuestaNoAuth
=
restTemplate
.
exchange
(
ResponseEntity
<
Void
>
respuestaNoAuth
=
restTemplate
.
exchange
(
"/recursos"
,
HttpMethod
.
GET
,
HttpEntity
.
EMPTY
,
Void
.
class
"/recursos"
,
HttpMethod
.
GET
,
HttpEntity
.
EMPTY
,
Void
.
class
);
);
assertThat
(
respuestaNoAuth
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
UNAUTHORIZED
);
assertThat
(
respuestaNoAuth
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
UNAUTHORIZED
);
}
}
@Test
@DirtiesContext
void
testAnadirCapitulo
()
{
// 🔹 Registro de 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 recurso con autenticación de administrador
Recurso
recurso
=
new
Recurso
(
"Manga Ejemplo"
,
"Sinopsis"
,
LocalDate
.
now
(),
"Autor X"
);
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
set
(
"Authorization"
,
token
);
HttpEntity
<
Recurso
>
request
=
new
HttpEntity
<>(
recurso
,
headers
);
var
respuestaAdmin
=
restTemplate
.
exchange
(
"/recursos/"
,
HttpMethod
.
POST
,
request
,
Void
.
class
);
assertThat
(
respuestaAdmin
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
CREATED
);
// 🔹 Obtener el ID del recurso recién creado
ResponseEntity
<
DTORecurso
[]>
respuestaBusqueda
=
restTemplate
.
exchange
(
"/recursos/titulo/{titulo}"
,
HttpMethod
.
GET
,
null
,
DTORecurso
[].
class
,
"Manga Ejemplo"
);
assertThat
(
respuestaBusqueda
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
Long
recursoId
=
respuestaBusqueda
.
getBody
()[
0
].
getId
();
assertThat
(
recursoId
).
isNotNull
();
// 🔹 Intentar añadir un capítulo con autenticación de administrador
DTOCapitulo
nuevoCapitulo
=
new
DTOCapitulo
(
null
,
1
,
"Capítulo 1"
,
TipoRecurso
.
MANGA
,
List
.
of
(
new
DTOFuenteCapitulo
(
"Crunchyroll"
,
"https://crunchyroll.com/cap1"
))
);
HttpEntity
<
DTOCapitulo
>
capituloRequest
=
new
HttpEntity
<>(
nuevoCapitulo
,
headers
);
ResponseEntity
<
Void
>
respuestaCapitulo
=
restTemplate
.
exchange
(
"/recursos/{id}/capitulos"
,
HttpMethod
.
POST
,
capituloRequest
,
Void
.
class
,
recursoId
);
assertThat
(
respuestaCapitulo
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
CREATED
);
}
}
}
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