11package com .example .postgresdemo .controller ;
22
33import com .example .postgresdemo .model .Question ;
4+ import com .example .postgresdemo .model .User ;
45import com .example .postgresdemo .repository .QuestionRepository ;
6+ import com .example .postgresdemo .repository .UserRepository ;
57
68import org .hamcrest .Matchers ;
79import org .junit .jupiter .api .AfterEach ;
10+ import org .junit .jupiter .api .BeforeEach ;
811import org .junit .jupiter .api .Test ;
912import org .springframework .beans .factory .annotation .Autowired ;
1013import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
1417import org .springframework .test .web .servlet .request .MockMvcRequestBuilders ;
1518import org .springframework .test .web .servlet .result .MockMvcResultMatchers ;
1619
17-
1820import java .nio .CharBuffer ;
1921
2022import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
2123import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
2224import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
2325
2426@ SpringBootTest
25-
2627@ AutoConfigureMockMvc
2728public class QuestionControllerTest {
2829 @ Autowired
2930 private QuestionRepository questionRepository ;
3031
32+ @ Autowired
33+ private UserRepository userRepository ;
34+
3135 @ Autowired
3236 private MockMvc mockMvc ;
3337
38+ private Long userId ;
39+
40+ @ BeforeEach
41+ void setup () {
42+ User user = new User ();
43+ user .setFirstname ("John" );
44+ user .setLastname ("Doe" );
45+ userRepository .save (user );
46+ userId = user .getId ();
47+ }
48+
3449 @ AfterEach
35- void deleteQuestions () {
50+ void deleteAll () {
3651 questionRepository .deleteAll ();
52+ userRepository .deleteAll ();
3753 }
3854
3955 @ Test
@@ -75,7 +91,8 @@ void testCreateCorrectQuestion() throws Exception {
7591 .contentType (MediaType .APPLICATION_JSON )
7692 .content ("{\n " +
7793 " \" title\" : \" Question 1\" ,\n " +
78- " \" description\" : \" Description 1\" \n " +
94+ " \" description\" : \" Description 1\" ,\n " +
95+ " \" authorId\" : \" " + userId + "\" \n " +
7996 "}" ))
8097 .andExpect (status ().isOk ())
8198 .andExpect (content ().contentType (MediaType .APPLICATION_JSON_VALUE ))
@@ -87,32 +104,35 @@ void testCreateQuestionWithoutTitle() throws Exception {
87104 mockMvc .perform (MockMvcRequestBuilders .post ("/questions" )
88105 .contentType (MediaType .APPLICATION_JSON )
89106 .content ("{\n " +
90- " \" body\" : \" \\ nDescription\" \n " +
107+ " \" description\" : \" Description\" ,\n " +
108+ " \" authorId\" : \" " + userId + "\" \n " +
91109 "}" ))
92110 .andExpect (status ().is4xxClientError ());
93111 }
94112
95113 @ Test
96- void testCreateQuestionWithTitleLesThenThreeChars () throws Exception {
114+ void testCreateQuestionWithTitleLessThanThreeChars () throws Exception {
97115 mockMvc .perform (MockMvcRequestBuilders .post ("/questions" )
98116 .contentType (MediaType .APPLICATION_JSON )
99117 .content ("{\n " +
100118 " \" title\" : \" Te\" ,\n " +
101- " \" description\" : \" Description\" \n " +
119+ " \" description\" : \" Description\" ,\n " +
120+ " \" authorId\" : \" " + userId + "\" \n " +
102121 "}" ))
103122 .andExpect (status ().is4xxClientError ());
104123 }
105124
106125 @ Test
107- void testCreateQuestionWithTitleMoreThenHundredChars () throws Exception {
126+ void testCreateQuestionWithTitleMoreThanHundredChars () throws Exception {
108127 int numberOfChars = 101 ;
109128 String title = CharBuffer .allocate (numberOfChars ).toString ().replace ('\0' , 'T' );
110129
111130 mockMvc .perform (MockMvcRequestBuilders .post ("/questions" )
112131 .contentType (MediaType .APPLICATION_JSON )
113132 .content ("{\n " +
114133 " \" title\" : \" " + title + "\" ,\n " +
115- " \" description\" : \" Description\" \n " +
134+ " \" description\" : \" Description\" ,\n " +
135+ " \" authorId\" : \" " + userId + "\" \n " +
116136 "}" ))
117137 .andExpect (status ().is4xxClientError ());
118138 }
@@ -122,11 +142,24 @@ void testCreateQuestionWithoutDescription() throws Exception {
122142 mockMvc .perform (MockMvcRequestBuilders .post ("/questions" )
123143 .contentType (MediaType .APPLICATION_JSON )
124144 .content ("{\n " +
125- " \" title\" : \" Question 1\" \n " +
145+ " \" title\" : \" Question 1\" ,\n " +
146+ " \" authorId\" : \" " + userId + "\" \n " +
126147 "}" ))
127148 .andExpect (status ().isOk ())
128149 .andExpect (content ().contentType (MediaType .APPLICATION_JSON_VALUE ))
129- .andExpect (MockMvcResultMatchers .jsonPath ("$.body" , Matchers .equalTo ("Question 1\n null" )));}
150+ .andExpect (MockMvcResultMatchers .jsonPath ("$.body" , Matchers .equalTo ("Question 1\n null" )));
151+ }
152+
153+ @ Test
154+ void testCreateQuestionWithoutAuthor () throws Exception {
155+ mockMvc .perform (MockMvcRequestBuilders .post ("/questions" )
156+ .contentType (MediaType .APPLICATION_JSON )
157+ .content ("{\n " +
158+ " \" title\" : \" Question 1\" ,\n " +
159+ " \" description\" : \" Description 1\" \n " +
160+ "}" ))
161+ .andExpect (status ().is4xxClientError ());
162+ }
130163
131164 @ Test
132165 void testUpdateQuestion () throws Exception {
@@ -137,7 +170,8 @@ void testUpdateQuestion() throws Exception {
137170 .contentType (MediaType .APPLICATION_JSON )
138171 .content ("{\n " +
139172 " \" title\" : \" Edited Question 1\" ,\n " +
140- " \" description\" : \" Edited Description 1\" \n " +
173+ " \" description\" : \" Edited Description 1\" ,\n " +
174+ " \" authorId\" : \" " + userId + "\" \n " +
141175 "}" ))
142176 .andExpect (status ().isOk ())
143177 .andExpect (content ().contentType (MediaType .APPLICATION_JSON_VALUE ))
@@ -153,7 +187,8 @@ void testUpdateQuestionWithNonExistingId() throws Exception {
153187 .contentType (MediaType .APPLICATION_JSON )
154188 .content ("{\n " +
155189 " \" title\" : \" Edited Question 1\" ,\n " +
156- " \" description\" : \" Edited Description 1\" \n " +
190+ " \" description\" : \" Edited Description 1\" ,\n " +
191+ " \" authorId\" : \" " + userId + "\" \n " +
157192 "}" ))
158193 .andExpect (status ().is4xxClientError ());
159194 }
@@ -173,6 +208,7 @@ private void fillQuestions(Integer number) {
173208 Question question = new Question ();
174209 question .setTitle ("Question " + i );
175210 question .setDescription ("Description " + i );
211+ question .setUser (userRepository .findById (userId ).orElseThrow ());
176212 questionRepository .save (question );
177213 }
178214 }
0 commit comments