-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReviewControllerTest.java
91 lines (77 loc) · 3.44 KB
/
ReviewControllerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.music.review.app.controllers;
import com.music.review.app.domain.entities.artists.Artist;
import com.music.review.app.domain.entities.musics.Music;
import com.music.review.app.domain.entities.musics.enums.MusicGen;
import com.music.review.app.domain.entities.reviews.dtos.ReviewCreateDTO;
import com.music.review.app.domain.repositories.ArtistRepository;
import com.music.review.app.domain.repositories.MusicRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.json.AutoConfigureJsonTesters;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.json.JacksonTester;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
@SpringBootTest
@WithMockUser
@Transactional
@AutoConfigureJsonTesters
@AutoConfigureMockMvc
@ActiveProfiles("test")
class ReviewControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private JacksonTester<ReviewCreateDTO> reviewCreateDTOJacksonTester;
@Autowired
private MusicRepository musicRepository;
@Autowired
private ArtistRepository artistRepository;
private Artist artist;
private Music music;
@BeforeEach
void setUp(){
Artist artist = new Artist
(null, "Nome do Artista", "1968", "Brasil", "Bio");
this.artist = this.artistRepository.save(artist);
Music music = new Music
(null, "Nome da musica", MusicGen.OUTRO, this.artist);
this.music = this.musicRepository.save(music);
}
@Test
@DisplayName("Tenta criar uma review para uma música que não existe - código 404")
void createReviewFailed() throws Exception {
ReviewCreateDTO reviewCreateDTO = new ReviewCreateDTO
("MusicaInexistente", "Comentário da Review");
var response = this.getResponsePost(reviewCreateDTO);
assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND.value());
}
@Test
@DisplayName("Cria review válida - Código 201 Created")
void createReviewSucess() throws Exception{
ReviewCreateDTO reviewCreateDTO = new ReviewCreateDTO
("Comment", this.music.getNameMusic());
var response = this.getResponsePost(reviewCreateDTO);
assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED.value());
}
private MockHttpServletResponse getResponsePost(
ReviewCreateDTO dto) throws Exception{
return this.mockMvc.perform(post("/v1/reviews")
.contentType(MediaType.APPLICATION_JSON)
.content(this.reviewCreateDTOJacksonTester
.write(dto)
.getJson()))
.andReturn()
.getResponse();
}
}