You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
4.5 KiB
139 lines
4.5 KiB
package com.example.wfwebapi.controller;
|
|
|
|
import com.example.wfwebapi.model.*;
|
|
import com.example.wfwebapi.repository.*;
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
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 java.time.LocalDate;
|
|
import java.util.List;
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
|
class CommentaryControllerTest {
|
|
|
|
@LocalServerPort
|
|
private int port;
|
|
|
|
@Autowired private TestRestTemplate restTemplate;
|
|
|
|
@Autowired private CommentaryRepository commentaryRepository;
|
|
@Autowired private QuoteRepository quoteRepository;
|
|
@Autowired private UserRepository userRepository;
|
|
@Autowired private ImageRepository imageRepository;
|
|
@Autowired private CaracterRepository caracterRepository;
|
|
@Autowired private SourceRepository sourceRepository;
|
|
|
|
private Quote quote;
|
|
private User user;
|
|
|
|
private String getUrl(String path) {
|
|
return "http://localhost:" + port + "/api/v1/commentary" + path;
|
|
}
|
|
|
|
@BeforeEach
|
|
void setup() {
|
|
clearDatabase();
|
|
|
|
Image image = new Image();
|
|
image.setImgPath("test.png");
|
|
imageRepository.save(image);
|
|
|
|
user = new User();
|
|
user.setUsername("user");
|
|
user.setEmail("user@mail.com");
|
|
user.setPassword("pass");
|
|
user.setCreation(LocalDate.now());
|
|
user.setImage(image);
|
|
userRepository.save(user);
|
|
|
|
Caracter caracter = new Caracter();
|
|
caracter.setCaracter("Fort");
|
|
caracter.setImage(image);
|
|
caracterRepository.save(caracter);
|
|
|
|
Source source = new Source();
|
|
source.setTitle("source");
|
|
source.setDateS(2000);
|
|
sourceRepository.save(source);
|
|
|
|
quote = new Quote();
|
|
quote.setContent("Ceci est une citation.");
|
|
quote.setLangue("fr");
|
|
quote.setIsValide(true);
|
|
quote.setLikes(0);
|
|
quote.setReason("valid");
|
|
quote.setCaracter(caracter);
|
|
quote.setSource(source);
|
|
quote.setUserVerif(user);
|
|
quoteRepository.save(quote);
|
|
}
|
|
|
|
void clearDatabase() {
|
|
commentaryRepository.deleteAll();
|
|
quoteRepository.deleteAll();
|
|
caracterRepository.deleteAll();
|
|
sourceRepository.deleteAll();
|
|
userRepository.deleteAll();
|
|
imageRepository.deleteAll();
|
|
}
|
|
|
|
@Test
|
|
void testAddAndGetCommentary() {
|
|
Commentary commentary = new Commentary();
|
|
commentary.setUser(user);
|
|
commentary.setComment("Super citation !");
|
|
commentary.setDateC(LocalDate.now());
|
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
HttpEntity<Commentary> request = new HttpEntity<>(commentary, headers);
|
|
|
|
ResponseEntity<Commentary> postResponse = restTemplate.postForEntity(
|
|
getUrl("?quote=" + quote.getId()), request, Commentary.class);
|
|
|
|
assertEquals(HttpStatus.OK, postResponse.getStatusCode());
|
|
assertNotNull(postResponse.getBody().getId());
|
|
|
|
ResponseEntity<Commentary[]> getResponse = restTemplate.getForEntity(
|
|
getUrl("/" + quote.getId()), Commentary[].class);
|
|
|
|
assertEquals(HttpStatus.OK, getResponse.getStatusCode());
|
|
assertEquals(1, getResponse.getBody().length);
|
|
}
|
|
|
|
@Test
|
|
void testDeleteCommentary() {
|
|
Commentary commentary = new Commentary();
|
|
commentary.setUser(user);
|
|
commentary.setQuote(quote);
|
|
commentary.setComment("À supprimer");
|
|
commentary.setDateC(LocalDate.now());
|
|
commentaryRepository.save(commentary);
|
|
|
|
ResponseEntity<Void> deleteResponse = restTemplate.exchange(
|
|
getUrl("?id=" + quote.getId()), HttpMethod.DELETE, null, Void.class);
|
|
|
|
assertEquals(HttpStatus.OK, deleteResponse.getStatusCode());
|
|
|
|
List<Commentary> remaining = commentaryRepository.findByQuote_Id(quote.getId());
|
|
assertTrue(remaining.isEmpty());
|
|
}
|
|
|
|
@Test
|
|
void testDeleteCommentaryNotFound() {
|
|
ResponseEntity<String> response = restTemplate.exchange(
|
|
getUrl("?id=99999"), HttpMethod.DELETE, null, String.class);
|
|
|
|
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
|
assertTrue(response.getBody().contains("Aucun commentaire trouvé"));
|
|
}
|
|
}
|