|
| 1 | +package com.example.postgresdemo.controller; |
| 2 | + |
| 3 | +import com.example.postgresdemo.model.Question; |
| 4 | +import com.example.postgresdemo.repository.QuestionRepository; |
| 5 | + |
| 6 | +import org.hamcrest.Matchers; |
| 7 | +import org.junit.jupiter.api.AfterEach; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
| 10 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 11 | +import org.springframework.boot.test.context.SpringBootTest; |
| 12 | +import org.springframework.http.MediaType; |
| 13 | +import org.springframework.test.web.servlet.MockMvc; |
| 14 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
| 15 | +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; |
| 16 | + |
| 17 | + |
| 18 | +import java.nio.CharBuffer; |
| 19 | + |
| 20 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 21 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
| 22 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 23 | + |
| 24 | +@SpringBootTest |
| 25 | + |
| 26 | +@AutoConfigureMockMvc |
| 27 | +public class QuestionControllerTest { |
| 28 | + @Autowired |
| 29 | + private QuestionRepository questionRepository; |
| 30 | + |
| 31 | + @Autowired |
| 32 | + private MockMvc mockMvc; |
| 33 | + |
| 34 | + @AfterEach |
| 35 | + void deleteQuestions() { |
| 36 | + questionRepository.deleteAll(); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void testGetQuestionsWithAmountLessThanPageSize() throws Exception { |
| 41 | + int assertionNumber = 10; |
| 42 | + int pageSize = 20; |
| 43 | + |
| 44 | + fillQuestions(assertionNumber); |
| 45 | + |
| 46 | + mockMvc.perform(get("/questions") |
| 47 | + .contentType(MediaType.APPLICATION_JSON)) |
| 48 | + .andExpect(status().isOk()) |
| 49 | + .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE)) |
| 50 | + .andExpect(MockMvcResultMatchers.jsonPath("$.content.length()", Matchers.equalTo(assertionNumber))) |
| 51 | + .andExpect(MockMvcResultMatchers.jsonPath("$.totalElements", Matchers.equalTo(assertionNumber))) |
| 52 | + .andExpect(MockMvcResultMatchers.jsonPath("$.totalPages", Matchers.equalTo(1))); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void testGetQuestionsWithAmountMoreThanPageSize() throws Exception { |
| 57 | + int assertionNumber = 30; |
| 58 | + int pageSize = 20; |
| 59 | + int totalPages = (int) Math.ceil(assertionNumber / (double) pageSize); |
| 60 | + |
| 61 | + fillQuestions(assertionNumber); |
| 62 | + |
| 63 | + mockMvc.perform(get("/questions") |
| 64 | + .contentType(MediaType.APPLICATION_JSON)) |
| 65 | + .andExpect(status().isOk()) |
| 66 | + .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE)) |
| 67 | + .andExpect(MockMvcResultMatchers.jsonPath("$.content.length()", Matchers.equalTo(pageSize))) |
| 68 | + .andExpect(MockMvcResultMatchers.jsonPath("$.totalElements", Matchers.equalTo(assertionNumber))) |
| 69 | + .andExpect(MockMvcResultMatchers.jsonPath("$.totalPages", Matchers.equalTo(totalPages))); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void testCreateCorrectQuestion() throws Exception { |
| 74 | + mockMvc.perform(MockMvcRequestBuilders.post("/questions") |
| 75 | + .contentType(MediaType.APPLICATION_JSON) |
| 76 | + .content("{\n" + |
| 77 | + " \"title\": \"Question 1\",\n" + |
| 78 | + " \"description\": \"Description 1\"\n" + |
| 79 | + "}")) |
| 80 | + .andExpect(status().isOk()) |
| 81 | + .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE)) |
| 82 | + .andExpect(MockMvcResultMatchers.jsonPath("$.title", Matchers.equalTo("Question 1"))) |
| 83 | + .andExpect(MockMvcResultMatchers.jsonPath("$.description", Matchers.equalTo("Description 1"))); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void testCreateQuestionWithoutTitle() throws Exception { |
| 88 | + mockMvc.perform(MockMvcRequestBuilders.post("/questions") |
| 89 | + .contentType(MediaType.APPLICATION_JSON) |
| 90 | + .content("{\n" + |
| 91 | + " \"description\": \"Description\"\n" + |
| 92 | + "}")) |
| 93 | + .andExpect(status().is4xxClientError()); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void testCreateQuestionWithTitleLesThenThreeChars() throws Exception { |
| 98 | + mockMvc.perform(MockMvcRequestBuilders.post("/questions") |
| 99 | + .contentType(MediaType.APPLICATION_JSON) |
| 100 | + .content("{\n" + |
| 101 | + " \"title\": \"Te\",\n" + |
| 102 | + " \"description\": \"Description\"\n" + |
| 103 | + "}")) |
| 104 | + .andExpect(status().is4xxClientError()); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void testCreateQuestionWithTitleMoreThenHundredChars() throws Exception { |
| 109 | + int numberOfChars = 101; |
| 110 | + String title = CharBuffer.allocate(numberOfChars).toString().replace('\0', 'T'); |
| 111 | + |
| 112 | + mockMvc.perform(MockMvcRequestBuilders.post("/questions") |
| 113 | + .contentType(MediaType.APPLICATION_JSON) |
| 114 | + .content("{\n" + |
| 115 | + " \"title\": \"" + title + "\",\n" + |
| 116 | + " \"description\": \"Description\"\n" + |
| 117 | + "}")) |
| 118 | + .andExpect(status().is4xxClientError()); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + void testCreateQuestionWithoutDescription() throws Exception { |
| 123 | + mockMvc.perform(MockMvcRequestBuilders.post("/questions") |
| 124 | + .contentType(MediaType.APPLICATION_JSON) |
| 125 | + .content("{\n" + |
| 126 | + " \"title\": \"Question 1\"\n" + |
| 127 | + "}")) |
| 128 | + .andExpect(status().isOk()) |
| 129 | + .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE)) |
| 130 | + .andExpect(MockMvcResultMatchers.jsonPath("$.title", Matchers.equalTo("Question 1"))) |
| 131 | + .andExpect(MockMvcResultMatchers.jsonPath("$.description", Matchers.equalTo(null))); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + void testUpdateQuestion() throws Exception { |
| 136 | + fillQuestions(1); |
| 137 | + long questionId = questionRepository.findAll().get(0).getId(); |
| 138 | + |
| 139 | + mockMvc.perform(MockMvcRequestBuilders.put("/questions/" + questionId) |
| 140 | + .contentType(MediaType.APPLICATION_JSON) |
| 141 | + .content("{\n" + |
| 142 | + " \"title\": \"Edited Question 1\",\n" + |
| 143 | + " \"description\": \"Edited Description 1\"\n" + |
| 144 | + "}")) |
| 145 | + .andExpect(status().isOk()) |
| 146 | + .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE)) |
| 147 | + .andExpect(MockMvcResultMatchers.jsonPath("$.title", Matchers.equalTo("Edited Question 1"))) |
| 148 | + .andExpect(MockMvcResultMatchers.jsonPath("$.description", Matchers.equalTo("Edited Description 1"))); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + void testUpdateQuestionWithNonExistingId() throws Exception { |
| 153 | + fillQuestions(1); |
| 154 | + long questionId = questionRepository.findAll().get(0).getId(); |
| 155 | + |
| 156 | + mockMvc.perform(MockMvcRequestBuilders.put("/questions/" + (questionId + 1)) |
| 157 | + .contentType(MediaType.APPLICATION_JSON) |
| 158 | + .content("{\n" + |
| 159 | + " \"title\": \"Edited Question 1\",\n" + |
| 160 | + " \"description\": \"Edited Description 1\"\n" + |
| 161 | + "}")) |
| 162 | + .andExpect(status().is4xxClientError()); |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + void testDeleteQuestion() throws Exception { |
| 167 | + fillQuestions(1); |
| 168 | + long questionId = questionRepository.findAll().get(0).getId(); |
| 169 | + |
| 170 | + mockMvc.perform(MockMvcRequestBuilders.delete("/questions/" + questionId) |
| 171 | + .contentType(MediaType.APPLICATION_JSON)) |
| 172 | + .andExpect(status().isOk()); |
| 173 | + } |
| 174 | + |
| 175 | + private void fillQuestions(Integer number) { |
| 176 | + for (int i = 0; i < number; i++) { |
| 177 | + Question question = new Question(); |
| 178 | + question.setTitle("Question " + i); |
| 179 | + question.setDescription("Description " + i); |
| 180 | + questionRepository.save(question); |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments