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
c1dbc3d3
authored
Feb 24, 2025
by
Rubén Ramírez
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
feat: [TestBibliotecaPersonalController]: Empezado el controlador de los nuevos métodos
parent
111fdbb8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
436 additions
and
0 deletions
src/main/java/com/ujaen/tfg/mangaffinity/rest/BibliotecaPersonalController.java
src/test/java/com/ujaen/tfg/mangaffinity/rest/TestBibliotecaPersonalController.java
src/main/java/com/ujaen/tfg/mangaffinity/rest/BibliotecaPersonalController.java
0 → 100644
View file @
c1dbc3d3
package
com
.
ujaen
.
tfg
.
mangaffinity
.
rest
;
import
com.ujaen.tfg.mangaffinity.entidades.BibliotecaPersonal
;
import
com.ujaen.tfg.mangaffinity.entidades.BibliotecaPersonalRecurso
;
import
com.ujaen.tfg.mangaffinity.entidades.Categoria
;
import
com.ujaen.tfg.mangaffinity.entidades.Recurso
;
import
com.ujaen.tfg.mangaffinity.excepciones.RecursoNoExiste
;
import
com.ujaen.tfg.mangaffinity.rest.DTO.DTORecursoEnBiblioteca
;
import
com.ujaen.tfg.mangaffinity.rest.DTO.Mapper
;
import
com.ujaen.tfg.mangaffinity.servicios.ServicioBibliotecaPersonal
;
import
com.ujaen.tfg.mangaffinity.servicios.ServicioRecursos
;
import
com.ujaen.tfg.mangaffinity.servicios.ServicioUsuarios
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.*
;
import
java.util.List
;
import
java.util.stream.Collectors
;
@RestController
@RequestMapping
(
"/biblioteca"
)
public
class
BibliotecaPersonalController
{
@Autowired
private
ServicioBibliotecaPersonal
servicioBibliotecaPersonal
;
@Autowired
private
ServicioRecursos
servicioRecursos
;
@Autowired
private
ServicioUsuarios
servicioUsuarios
;
@Autowired
private
Mapper
mapper
;
@PostMapping
(
"/{usuarioId}/recursos/{recursoId}/categoria"
)
public
ResponseEntity
<
String
>
anadirRecursoBiblioteca
(
@PathVariable
Long
usuarioId
,
@PathVariable
Long
recursoId
,
@RequestBody
DTORecursoEnBiblioteca
dtoRecursoEnBiblioteca
)
{
try
{
BibliotecaPersonal
biblioteca
=
servicioUsuarios
.
obtenerBibliotecaDeUsuario
(
usuarioId
);
if
(
biblioteca
==
null
)
{
return
ResponseEntity
.
status
(
HttpStatus
.
NOT_FOUND
).
build
();
}
Recurso
recurso
=
servicioRecursos
.
buscarRecursoPorId
(
recursoId
);
if
(
recurso
==
null
)
{
return
ResponseEntity
.
status
(
HttpStatus
.
NOT_FOUND
).
build
();
}
servicioBibliotecaPersonal
.
anadirRecursoBiblioteca
(
biblioteca
,
recurso
,
dtoRecursoEnBiblioteca
.
getCategoria
());
return
ResponseEntity
.
status
(
HttpStatus
.
CREATED
).
build
();
}
catch
(
RecursoNoExiste
e
)
{
return
ResponseEntity
.
status
(
HttpStatus
.
NOT_FOUND
).
build
();
}
catch
(
Exception
e
)
{
return
ResponseEntity
.
status
(
HttpStatus
.
INTERNAL_SERVER_ERROR
).
build
();
}
}
@GetMapping
(
"/{usuarioId}/recursos/categoria/{categoria}"
)
public
ResponseEntity
<
List
<
DTORecursoEnBiblioteca
>>
listarRecursosPorCategoria
(
@PathVariable
Long
usuarioId
,
@PathVariable
Categoria
categoria
)
{
try
{
BibliotecaPersonal
biblioteca
=
servicioUsuarios
.
obtenerBibliotecaDeUsuario
(
usuarioId
);
if
(
biblioteca
==
null
)
{
return
ResponseEntity
.
status
(
HttpStatus
.
NOT_FOUND
).
build
();
}
List
<
BibliotecaPersonalRecurso
>
recursos
=
servicioBibliotecaPersonal
.
listarPorCategoria
(
biblioteca
.
getId
(),
categoria
);
List
<
DTORecursoEnBiblioteca
>
dtoRecursos
=
recursos
.
stream
()
.
map
(
bpr
->
new
DTORecursoEnBiblioteca
(
bpr
.
getRecurso
().
getId
(),
bpr
.
getRecurso
().
getTitulo
(),
bpr
.
getCategoria
()))
.
collect
(
Collectors
.
toList
());
return
ResponseEntity
.
ok
(
dtoRecursos
);
}
catch
(
Exception
e
)
{
return
ResponseEntity
.
status
(
HttpStatus
.
INTERNAL_SERVER_ERROR
).
build
();
}
}
@DeleteMapping
(
"/{usuarioId}/recursos/{recursoId}"
)
public
ResponseEntity
<
String
>
eliminarRecursoDeBiblioteca
(
@PathVariable
Long
usuarioId
,
@PathVariable
Long
recursoId
)
{
try
{
BibliotecaPersonal
biblioteca
=
servicioUsuarios
.
obtenerBibliotecaDeUsuario
(
usuarioId
);
if
(
biblioteca
==
null
)
{
return
ResponseEntity
.
status
(
HttpStatus
.
NOT_FOUND
).
build
();
}
servicioBibliotecaPersonal
.
eliminarRecurso
(
biblioteca
.
getId
(),
recursoId
);
return
ResponseEntity
.
status
(
HttpStatus
.
OK
).
body
(
"Recurso eliminado correctamente"
);
}
catch
(
Exception
e
)
{
return
ResponseEntity
.
status
(
HttpStatus
.
INTERNAL_SERVER_ERROR
).
build
();
}
}
@PutMapping
(
"/{usuarioId}/recursos/{recursoId}/categoria"
)
public
ResponseEntity
<
String
>
modificarCategoriaDeRecurso
(
@PathVariable
Long
usuarioId
,
@PathVariable
Long
recursoId
,
@RequestBody
DTORecursoEnBiblioteca
dtoRecurso
)
{
try
{
BibliotecaPersonal
biblioteca
=
servicioUsuarios
.
obtenerBibliotecaDeUsuario
(
usuarioId
);
if
(
biblioteca
==
null
)
{
return
ResponseEntity
.
status
(
HttpStatus
.
NOT_FOUND
).
build
();
}
servicioBibliotecaPersonal
.
modificarCategoria
(
biblioteca
.
getId
(),
recursoId
,
dtoRecurso
.
getCategoria
());
return
ResponseEntity
.
status
(
HttpStatus
.
OK
).
build
();
}
catch
(
Exception
e
)
{
return
ResponseEntity
.
status
(
HttpStatus
.
INTERNAL_SERVER_ERROR
).
body
(
"Error interno"
);
}
}
}
src/test/java/com/ujaen/tfg/mangaffinity/rest/TestBibliotecaPersonalController.java
0 → 100644
View file @
c1dbc3d3
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.Categoria
;
import
com.ujaen.tfg.mangaffinity.entidades.Recurso
;
import
com.ujaen.tfg.mangaffinity.rest.DTO.DTOLoginRespuesta
;
import
com.ujaen.tfg.mangaffinity.rest.DTO.DTORecurso
;
import
com.ujaen.tfg.mangaffinity.rest.DTO.DTORecursoEnBiblioteca
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.beans.factory.annotation.Autowired
;
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.http.*
;
import
org.springframework.test.annotation.DirtiesContext
;
import
org.springframework.test.context.ActiveProfiles
;
import
java.time.LocalDate
;
import
java.util.Map
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
@SpringBootTest
(
classes
=
{
MangAffinityApplication
.
class
,
JpaTestConfig
.
class
},
webEnvironment
=
SpringBootTest
.
WebEnvironment
.
RANDOM_PORT
)
@ActiveProfiles
(
"test"
)
@DirtiesContext
(
classMode
=
DirtiesContext
.
ClassMode
.
AFTER_EACH_TEST_METHOD
)
public
class
TestBibliotecaPersonalController
{
@LocalServerPort
int
localPort
;
@Autowired
private
TestRestTemplate
restTemplate
;
@Test
@DirtiesContext
void
testAnadirRecursoBiblioteca
()
{
// 🔹 Registrar administrador
restTemplate
.
postForEntity
(
"/usuarios/"
,
Map
.
of
(
"email"
,
"admin@example.com"
,
"nombreUsuario"
,
"admin"
,
"contrasenia"
,
"adminpassword"
),
Void
.
class
);
// 🔹 Iniciar sesión del administrador
var
authAdmin
=
restTemplate
.
postForEntity
(
"/usuarios/admin@example.com"
,
Map
.
of
(
"clave"
,
"adminpassword"
),
DTOLoginRespuesta
.
class
);
assertThat
(
authAdmin
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
String
tokenAdmin
=
"Bearer "
+
authAdmin
.
getBody
().
getToken
();
// 🔹 Registrar usuario normal
ResponseEntity
<
Void
>
respuestaUsuario
=
restTemplate
.
postForEntity
(
"/usuarios/"
,
Map
.
of
(
"email"
,
"usuario@example.com"
,
"nombreUsuario"
,
"usuario"
,
"contrasenia"
,
"password"
),
Void
.
class
);
assertThat
(
respuestaUsuario
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
CREATED
);
// 🔹 Iniciar sesión del usuario
var
authUser
=
restTemplate
.
postForEntity
(
"/usuarios/usuario@example.com"
,
Map
.
of
(
"clave"
,
"password"
),
DTOLoginRespuesta
.
class
);
assertThat
(
authUser
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
String
tokenUser
=
"Bearer "
+
authUser
.
getBody
().
getToken
();
// 🔹 Buscar el usuario creado para obtener su ID
ResponseEntity
<
Long
>
respuestaUsuarioId
=
restTemplate
.
exchange
(
"/usuarios/email/{email}"
,
HttpMethod
.
GET
,
null
,
Long
.
class
,
"usuario@example.com"
);
assertThat
(
respuestaUsuarioId
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
Long
usuarioId
=
respuestaUsuarioId
.
getBody
();
assertThat
(
usuarioId
).
isNotNull
();
// 🔹 Crear un recurso con el administrador
Recurso
nuevoRecurso
=
new
Recurso
(
"Titulo Prueba"
,
"Descripción de prueba"
,
LocalDate
.
now
(),
"Autor Prueba"
);
HttpHeaders
headersAdmin
=
new
HttpHeaders
();
headersAdmin
.
set
(
"Authorization"
,
tokenAdmin
);
headersAdmin
.
setContentType
(
MediaType
.
APPLICATION_JSON
);
HttpEntity
<
Recurso
>
requestAdmin
=
new
HttpEntity
<>(
nuevoRecurso
,
headersAdmin
);
var
respuestaRecurso
=
restTemplate
.
exchange
(
"/recursos/"
,
HttpMethod
.
POST
,
requestAdmin
,
Void
.
class
);
assertThat
(
respuestaRecurso
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
CREATED
);
// 🔹 Buscar el recurso creado para obtener su ID
ResponseEntity
<
DTORecurso
[]>
respuestaBusqueda
=
restTemplate
.
exchange
(
"/recursos/titulo/{titulo}"
,
HttpMethod
.
GET
,
null
,
DTORecurso
[].
class
,
"Titulo Prueba"
);
assertThat
(
respuestaBusqueda
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
respuestaBusqueda
.
getBody
()).
isNotEmpty
();
Long
recursoId
=
respuestaBusqueda
.
getBody
()[
0
].
getId
();
assertThat
(
recursoId
).
isNotNull
();
// 🔹 Usuario añade el recurso a su biblioteca
DTORecursoEnBiblioteca
dtoRecurso
=
new
DTORecursoEnBiblioteca
(
null
,
"Titulo Prueba"
,
Categoria
.
AL_DIA
);
HttpHeaders
headersUser
=
new
HttpHeaders
();
headersUser
.
set
(
"Authorization"
,
tokenUser
);
headersUser
.
setContentType
(
MediaType
.
APPLICATION_JSON
);
HttpEntity
<
DTORecursoEnBiblioteca
>
bibliotecaRequest
=
new
HttpEntity
<>(
dtoRecurso
,
headersUser
);
ResponseEntity
<
Void
>
respuestaBiblioteca
=
restTemplate
.
exchange
(
"/biblioteca/{usuarioId}/recursos/{recursoId}/categoria"
,
HttpMethod
.
POST
,
bibliotecaRequest
,
Void
.
class
,
usuarioId
,
recursoId
);
// 🔹 Verificar que se añadió correctamente
assertThat
(
respuestaBiblioteca
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
CREATED
);
}
@Test
@DirtiesContext
void
testListarRecursosPorCategoria
()
{
// 🔹 Registrar un administrador
restTemplate
.
postForEntity
(
"/usuarios/"
,
Map
.
of
(
"email"
,
"admin@example.com"
,
"nombreUsuario"
,
"admin"
,
"contrasenia"
,
"adminpassword"
),
Void
.
class
);
// 🔹 Iniciar sesión del administrador
var
authAdmin
=
restTemplate
.
postForEntity
(
"/usuarios/admin@example.com"
,
Map
.
of
(
"clave"
,
"adminpassword"
),
DTOLoginRespuesta
.
class
);
assertThat
(
authAdmin
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
String
tokenAdmin
=
"Bearer "
+
authAdmin
.
getBody
().
getToken
();
// 🔹 Registrar un usuario normal
restTemplate
.
postForEntity
(
"/usuarios/"
,
Map
.
of
(
"email"
,
"usuario@example.com"
,
"nombreUsuario"
,
"usuario"
,
"contrasenia"
,
"password"
),
Void
.
class
);
// 🔹 Iniciar sesión del usuario
var
authUser
=
restTemplate
.
postForEntity
(
"/usuarios/usuario@example.com"
,
Map
.
of
(
"clave"
,
"password"
),
DTOLoginRespuesta
.
class
);
assertThat
(
authUser
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
String
tokenUser
=
"Bearer "
+
authUser
.
getBody
().
getToken
();
// 🔹 Crear el recurso con el administrador
HttpHeaders
headersAdmin
=
new
HttpHeaders
();
headersAdmin
.
set
(
"Authorization"
,
tokenAdmin
);
Recurso
recurso
=
new
Recurso
(
"Titulo Prueba"
,
"Descripción de prueba"
,
LocalDate
.
now
(),
"Autor Prueba"
);
HttpEntity
<
Recurso
>
requestAdmin
=
new
HttpEntity
<>(
recurso
,
headersAdmin
);
restTemplate
.
exchange
(
"/recursos/"
,
HttpMethod
.
POST
,
requestAdmin
,
Void
.
class
);
// 🔹 Buscar el recurso creado para obtener su ID
ResponseEntity
<
DTORecurso
[]>
respuestaBusqueda
=
restTemplate
.
exchange
(
"/recursos/titulo/{titulo}"
,
HttpMethod
.
GET
,
null
,
DTORecurso
[].
class
,
"Titulo Prueba"
);
// ✅ Verificar que el recurso fue encontrado antes de obtener su ID
assertThat
(
respuestaBusqueda
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
respuestaBusqueda
.
getBody
()).
isNotNull
().
isNotEmpty
();
Long
recursoId
=
respuestaBusqueda
.
getBody
()[
0
].
getId
();
assertThat
(
recursoId
).
isNotNull
();
// 🔹 Usuario añade el recurso a su biblioteca
HttpHeaders
headersUser
=
new
HttpHeaders
();
headersUser
.
set
(
"Authorization"
,
tokenUser
);
DTORecursoEnBiblioteca
dtoRecurso
=
new
DTORecursoEnBiblioteca
(
null
,
"Titulo Prueba"
,
Categoria
.
AL_DIA
);
HttpEntity
<
DTORecursoEnBiblioteca
>
bibliotecaRequest
=
new
HttpEntity
<>(
dtoRecurso
,
headersUser
);
ResponseEntity
<
Void
>
respuestaA
ñ
adir
=
restTemplate
.
exchange
(
"/biblioteca/{usuarioId}/recursos/{recursoId}/categoria"
,
HttpMethod
.
POST
,
bibliotecaRequest
,
Void
.
class
,
1
,
recursoId
);
assertThat
(
respuestaA
ñ
adir
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
CREATED
);
// 🔹 Listar los recursos de la biblioteca personal filtrados por categoría
ResponseEntity
<
DTORecursoEnBiblioteca
[]>
respuestaLista
=
restTemplate
.
exchange
(
"/biblioteca/{usuarioId}/recursos/categoria/{categoria}"
,
HttpMethod
.
GET
,
new
HttpEntity
<>(
headersUser
),
DTORecursoEnBiblioteca
[].
class
,
1
,
Categoria
.
AL_DIA
.
name
()
);
// 🔹 Validaciones del test
assertThat
(
respuestaLista
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
respuestaLista
.
getBody
()).
isNotNull
().
isNotEmpty
();
assertThat
(
respuestaLista
.
getBody
()[
0
].
getTitulo
()).
isEqualTo
(
"Titulo Prueba"
);
assertThat
(
respuestaLista
.
getBody
()[
0
].
getCategoria
()).
isEqualTo
(
Categoria
.
AL_DIA
);
}
@Test
@DirtiesContext
void
testEliminarRecursoDeBiblioteca
()
{
// Registrar usuario
restTemplate
.
postForEntity
(
"/usuarios/"
,
Map
.
of
(
"email"
,
"usuario@example.com"
,
"nombreUsuario"
,
"usuario"
,
"contrasenia"
,
"password"
),
Void
.
class
);
// Iniciar sesión del usuario
var
authUser
=
restTemplate
.
postForEntity
(
"/usuarios/usuario@example.com"
,
Map
.
of
(
"clave"
,
"password"
),
DTOLoginRespuesta
.
class
);
assertThat
(
authUser
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
String
tokenUser
=
"Bearer "
+
authUser
.
getBody
().
getToken
();
// Crear un recurso
Recurso
recurso
=
new
Recurso
(
"Manga de prueba"
,
"Descripción"
,
LocalDate
.
now
(),
"Autor"
);
HttpHeaders
headersUser
=
new
HttpHeaders
();
headersUser
.
set
(
"Authorization"
,
tokenUser
);
HttpEntity
<
Recurso
>
requestRecurso
=
new
HttpEntity
<>(
recurso
,
headersUser
);
var
respuestaRecurso
=
restTemplate
.
exchange
(
"/recursos/"
,
HttpMethod
.
POST
,
requestRecurso
,
Void
.
class
);
assertThat
(
respuestaRecurso
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
CREATED
);
// Buscar el recurso creado para obtener su ID
ResponseEntity
<
Recurso
[]>
respuestaBusqueda
=
restTemplate
.
getForEntity
(
"/recursos/titulo/Manga de prueba"
,
Recurso
[].
class
);
assertThat
(
respuestaBusqueda
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
Long
recursoId
=
respuestaBusqueda
.
getBody
()[
0
].
getId
();
assertThat
(
recursoId
).
isNotNull
();
// Añadir el recurso a la biblioteca
DTORecursoEnBiblioteca
dtoRecurso
=
new
DTORecursoEnBiblioteca
(
recursoId
,
"Manga de prueba"
,
Categoria
.
AL_DIA
);
HttpEntity
<
DTORecursoEnBiblioteca
>
bibliotecaRequest
=
new
HttpEntity
<>(
dtoRecurso
,
headersUser
);
ResponseEntity
<
Void
>
respuestaA
ñ
adir
=
restTemplate
.
exchange
(
"/biblioteca/{usuarioId}/recursos/{recursoId}/categoria"
,
HttpMethod
.
POST
,
bibliotecaRequest
,
Void
.
class
,
1
,
recursoId
);
assertThat
(
respuestaA
ñ
adir
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
CREATED
);
// Eliminar el recurso de la biblioteca
ResponseEntity
<
String
>
respuestaEliminar
=
restTemplate
.
exchange
(
"/biblioteca/{usuarioId}/recursos/{recursoId}"
,
HttpMethod
.
DELETE
,
new
HttpEntity
<>(
headersUser
),
String
.
class
,
1
,
recursoId
);
assertThat
(
respuestaEliminar
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
respuestaEliminar
.
getBody
()).
isEqualTo
(
"Recurso eliminado correctamente"
);
// Verificar que el recurso ya no está en la biblioteca
ResponseEntity
<
DTORecursoEnBiblioteca
[]>
respuestaLista
=
restTemplate
.
exchange
(
"/biblioteca/{usuarioId}/recursos/categoria/{categoria}"
,
HttpMethod
.
GET
,
new
HttpEntity
<>(
headersUser
),
DTORecursoEnBiblioteca
[].
class
,
1
,
Categoria
.
AL_DIA
.
name
()
);
assertThat
(
respuestaLista
.
getBody
()).
isEmpty
();
}
@Test
@DirtiesContext
void
testModificarCategoriaDeRecurso
()
{
// Registrar usuario
restTemplate
.
postForEntity
(
"/usuarios/"
,
Map
.
of
(
"email"
,
"usuario@example.com"
,
"nombreUsuario"
,
"usuario"
,
"contrasenia"
,
"password"
),
Void
.
class
);
// Iniciar sesión del usuario
var
authUser
=
restTemplate
.
postForEntity
(
"/usuarios/usuario@example.com"
,
Map
.
of
(
"clave"
,
"password"
),
DTOLoginRespuesta
.
class
);
assertThat
(
authUser
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
String
tokenUser
=
"Bearer "
+
authUser
.
getBody
().
getToken
();
// Crear un recurso
Recurso
recurso
=
new
Recurso
(
"Manga de prueba"
,
"Descripción"
,
LocalDate
.
now
(),
"Autor"
);
HttpHeaders
headersUser
=
new
HttpHeaders
();
headersUser
.
set
(
"Authorization"
,
tokenUser
);
HttpEntity
<
Recurso
>
requestRecurso
=
new
HttpEntity
<>(
recurso
,
headersUser
);
var
respuestaRecurso
=
restTemplate
.
exchange
(
"/recursos/"
,
HttpMethod
.
POST
,
requestRecurso
,
Void
.
class
);
assertThat
(
respuestaRecurso
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
CREATED
);
// Buscar el recurso creado para obtener su ID
ResponseEntity
<
Recurso
[]>
respuestaBusqueda
=
restTemplate
.
getForEntity
(
"/recursos/titulo/Manga de prueba"
,
Recurso
[].
class
);
assertThat
(
respuestaBusqueda
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
Long
recursoId
=
respuestaBusqueda
.
getBody
()[
0
].
getId
();
assertThat
(
recursoId
).
isNotNull
();
// Añadir el recurso a la biblioteca con una categoría inicial
DTORecursoEnBiblioteca
dtoRecurso
=
new
DTORecursoEnBiblioteca
(
recursoId
,
"Manga de prueba"
,
Categoria
.
AL_DIA
);
HttpEntity
<
DTORecursoEnBiblioteca
>
bibliotecaRequest
=
new
HttpEntity
<>(
dtoRecurso
,
headersUser
);
ResponseEntity
<
Void
>
respuestaA
ñ
adir
=
restTemplate
.
exchange
(
"/biblioteca/{usuarioId}/recursos/{recursoId}/categoria"
,
HttpMethod
.
POST
,
bibliotecaRequest
,
Void
.
class
,
1
,
recursoId
);
assertThat
(
respuestaA
ñ
adir
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
CREATED
);
// Modificar la categoría del recurso en la biblioteca
DTORecursoEnBiblioteca
dtoModificado
=
new
DTORecursoEnBiblioteca
(
recursoId
,
"Manga de prueba"
,
Categoria
.
COMPLETADO
);
HttpEntity
<
DTORecursoEnBiblioteca
>
categoriaRequest
=
new
HttpEntity
<>(
dtoModificado
,
headersUser
);
ResponseEntity
<
String
>
respuestaModificar
=
restTemplate
.
exchange
(
"/biblioteca/{usuarioId}/recursos/{recursoId}/categoria"
,
HttpMethod
.
PUT
,
categoriaRequest
,
String
.
class
,
1
,
recursoId
);
assertThat
(
respuestaModificar
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
// Verificar que la categoría ha cambiado
ResponseEntity
<
DTORecursoEnBiblioteca
[]>
respuestaLista
=
restTemplate
.
exchange
(
"/biblioteca/{usuarioId}/recursos/categoria/{categoria}"
,
HttpMethod
.
GET
,
new
HttpEntity
<>(
headersUser
),
DTORecursoEnBiblioteca
[].
class
,
1
,
Categoria
.
COMPLETADO
.
name
()
);
assertThat
(
respuestaLista
.
getBody
()).
isNotEmpty
();
assertThat
(
respuestaLista
.
getBody
()[
0
].
getCategoria
()).
isEqualTo
(
Categoria
.
COMPLETADO
);
}
}
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