Commit 567a164e by Rubén Ramírez

fix: [TestRecursoController]: Testeada la función para añadir las reseñas

parent 0fa6d82c
......@@ -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);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment