Skip to content

Commit bab3c32

Browse files
author
zahar.kalosha
committed
Add QuestionController test
1 parent 37174b5 commit bab3c32

File tree

1 file changed

+124
-13
lines changed

1 file changed

+124
-13
lines changed

src/test/java/com/example/postgresdemo/controller/QuestionControllerTest.java

Lines changed: 124 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,19 @@
55

66
import org.hamcrest.Matchers;
77
import org.junit.jupiter.api.Test;
8-
import org.mockito.Mockito;
98
import org.springframework.beans.factory.annotation.Autowired;
109
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
1110
import org.springframework.boot.test.context.SpringBootTest;
12-
import org.springframework.boot.test.mock.mockito.MockBean;
13-
import org.springframework.data.domain.Page;
14-
import org.springframework.data.domain.PageImpl;
15-
import org.springframework.data.domain.PageRequest;
16-
import org.springframework.data.domain.Pageable;
1711
import org.springframework.http.MediaType;
1812
import org.springframework.test.web.servlet.MockMvc;
19-
import org.springframework.test.web.servlet.ResultMatcher;
2013
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
2114
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
2215
import org.springframework.web.context.WebApplicationContext;
2316

2417

25-
import java.util.Arrays;
26-
import java.util.List;
18+
import java.nio.CharBuffer;
2719

2820
import static org.junit.jupiter.api.Assertions.assertNotNull;
29-
import static org.mockito.Mockito.when;
3021
import static org.springframework.test.web.client.match.MockRestRequestMatchers.jsonPath;
3122
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
3223
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
@@ -75,8 +66,16 @@ private boolean deleteQuestions() {
7566
}
7667
}
7768

69+
private void deleteQuestionsWithExceptionOnFail() throws Exception {
70+
if (!deleteQuestions()) {
71+
throw new Exception("Failed to delete questions");
72+
}
73+
}
74+
7875
@Test
7976
public void testGetQuestions() throws Exception {
77+
deleteQuestionsWithExceptionOnFail();
78+
8079
for (int assertionNumber = 0; assertionNumber < 100; assertionNumber++) {
8180
int pageSize = 20;
8281

@@ -92,10 +91,122 @@ public void testGetQuestions() throws Exception {
9291
.andExpect(MockMvcResultMatchers.jsonPath("$.totalElements", Matchers.equalTo(assertionNumber))) // Assert total elements
9392
.andExpect(MockMvcResultMatchers.jsonPath("$.totalPages", Matchers.equalTo((int) Math.ceil(assertionNumber / (double) pageSize)))); // Assert total pages for 10 items per page
9493

95-
if (!deleteQuestions()) {
96-
throw new Exception("Failed to delete questions");
97-
}
94+
deleteQuestionsWithExceptionOnFail();
9895
}
9996
}
10097

98+
@Test
99+
public void testCreateCorrectQuestion() throws Exception {
100+
mockMvc.perform(MockMvcRequestBuilders.post("/questions")
101+
.contentType(MediaType.APPLICATION_JSON)
102+
.content("{\n" +
103+
" \"title\": \"Question 1\",\n" +
104+
" \"description\": \"Description 1\"\n" +
105+
"}"))
106+
.andExpect(status().isOk())
107+
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
108+
.andExpect(MockMvcResultMatchers.jsonPath("$.title", Matchers.equalTo("Question 1")))
109+
.andExpect(MockMvcResultMatchers.jsonPath("$.description", Matchers.equalTo("Description 1")));
110+
111+
deleteQuestionsWithExceptionOnFail();
112+
}
113+
114+
@Test
115+
public void testCreateQuestionWithoutTitle() throws Exception {
116+
mockMvc.perform(MockMvcRequestBuilders.post("/questions")
117+
.contentType(MediaType.APPLICATION_JSON)
118+
.content("{\n" +
119+
" \"description\": \"Description\"\n" +
120+
"}"))
121+
.andExpect(status().is4xxClientError());
122+
123+
deleteQuestionsWithExceptionOnFail();
124+
}
125+
126+
@Test
127+
public void testCreateQuestionWithTitleLesThenThreeChars() throws Exception {
128+
mockMvc.perform(MockMvcRequestBuilders.post("/questions")
129+
.contentType(MediaType.APPLICATION_JSON)
130+
.content("{\n" +
131+
" \"title\": \"Te\",\n" +
132+
" \"description\": \"Description\"\n" +
133+
"}"))
134+
.andExpect(status().is4xxClientError());
135+
136+
deleteQuestionsWithExceptionOnFail();
137+
}
138+
139+
@Test
140+
public void testCreateQuestionWithTitleMoreThenHundredChars() throws Exception {
141+
int numberOfChars = 101;
142+
String title = CharBuffer.allocate(numberOfChars).toString().replace('\0', 'T');
143+
mockMvc.perform(MockMvcRequestBuilders.post("/questions")
144+
.contentType(MediaType.APPLICATION_JSON)
145+
.content("{\n" +
146+
" \"title\": \"" + title + "\",\n" +
147+
" \"description\": \"Description\"\n" +
148+
"}"))
149+
.andExpect(status().is4xxClientError());
150+
151+
deleteQuestionsWithExceptionOnFail();
152+
}
153+
154+
@Test
155+
public void testCreateQuestionWithoutDescription() throws Exception {
156+
mockMvc.perform(MockMvcRequestBuilders.post("/questions")
157+
.contentType(MediaType.APPLICATION_JSON)
158+
.content("{\n" +
159+
" \"title\": \"Question 1\"\n" +
160+
"}"))
161+
.andExpect(status().isOk())
162+
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
163+
.andExpect(MockMvcResultMatchers.jsonPath("$.title", Matchers.equalTo("Question 1")))
164+
.andExpect(MockMvcResultMatchers.jsonPath("$.description", Matchers.equalTo(null)));
165+
166+
deleteQuestionsWithExceptionOnFail();
167+
}
168+
169+
@Test
170+
public void testUpdateQuestion() throws Exception {
171+
deleteQuestionsWithExceptionOnFail();
172+
fillQuestions(1);
173+
long questionId = questionRepository.findAll().get(0).getId();
174+
175+
mockMvc.perform(MockMvcRequestBuilders.put("/questions/" + questionId)
176+
.contentType(MediaType.APPLICATION_JSON)
177+
.content("{\n" +
178+
" \"title\": \"Edited Question 1\",\n" +
179+
" \"description\": \"Edited Description 1\"\n" +
180+
"}"))
181+
.andExpect(status().isOk())
182+
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
183+
.andExpect(MockMvcResultMatchers.jsonPath("$.title", Matchers.equalTo("Edited Question 1")))
184+
.andExpect(MockMvcResultMatchers.jsonPath("$.description", Matchers.equalTo("Edited Description 1")));
185+
186+
deleteQuestionsWithExceptionOnFail();
187+
}
188+
189+
@Test
190+
public void testUpdateQuestionWithNonExistingId() throws Exception {
191+
mockMvc.perform(MockMvcRequestBuilders.put("/questions/1")
192+
.contentType(MediaType.APPLICATION_JSON)
193+
.content("{\n" +
194+
" \"title\": \"Edited Question 1\",\n" +
195+
" \"description\": \"Edited Description 1\"\n" +
196+
"}"))
197+
.andExpect(status().is4xxClientError());
198+
}
199+
200+
@Test
201+
public void testDeleteQuestion() throws Exception {
202+
deleteQuestionsWithExceptionOnFail();
203+
fillQuestions(1);
204+
long questionId = questionRepository.findAll().get(0).getId();
205+
206+
mockMvc.perform(MockMvcRequestBuilders.delete("/questions/" + questionId)
207+
.contentType(MediaType.APPLICATION_JSON))
208+
.andExpect(status().isOk());
209+
210+
deleteQuestionsWithExceptionOnFail();
211+
}
101212
}

0 commit comments

Comments
 (0)