|
| 1 | +package org.springframework.data.jpa.datatables.repository; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.BeforeEach; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 8 | +import org.springframework.boot.test.context.SpringBootTest; |
| 9 | +import org.springframework.data.jpa.datatables.TestApplication; |
| 10 | +import org.springframework.data.jpa.datatables.model.Employee; |
| 11 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 12 | +import org.springframework.test.web.servlet.MockMvc; |
| 13 | +import org.springframework.util.MultiValueMap; |
| 14 | + |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.Map; |
| 17 | + |
| 18 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 19 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; |
| 20 | + |
| 21 | +@ExtendWith(SpringExtension.class) |
| 22 | +@SpringBootTest( |
| 23 | + webEnvironment = SpringBootTest.WebEnvironment.MOCK, |
| 24 | + classes = TestApplication.class) |
| 25 | +@AutoConfigureMockMvc |
| 26 | +class EmployeeControllerTest { |
| 27 | + |
| 28 | + @Autowired private EmployeeRepository employeeRepository; |
| 29 | + |
| 30 | + @BeforeEach |
| 31 | + public void init() { |
| 32 | + this.employeeRepository.saveAll(Employee.ALL); |
| 33 | + } |
| 34 | + |
| 35 | + private static Map<String, String> createQuery() { |
| 36 | + var query = new HashMap<String, String>(); |
| 37 | + |
| 38 | + query.put("draw", "1"); |
| 39 | + query.put("start", "0"); |
| 40 | + query.put("length", "10"); |
| 41 | + |
| 42 | + query.put("search.value", ""); |
| 43 | + query.put("search.regex", "false"); |
| 44 | + |
| 45 | + query.put("order[0].column", "0"); |
| 46 | + query.put("order[0].dir", "asc"); |
| 47 | + |
| 48 | + query.put("columns[0].data", "id"); |
| 49 | + query.put("columns[0].searchable", "true"); |
| 50 | + query.put("columns[0].orderable", "true"); |
| 51 | + query.put("columns[0].search.value", ""); |
| 52 | + query.put("columns[0].search.regex", "false"); |
| 53 | + |
| 54 | + return query; |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void basic(@Autowired MockMvc mvc) throws Exception { |
| 59 | + var query = createQuery(); |
| 60 | + |
| 61 | + mvc.perform(get("/employees").queryParams(MultiValueMap.fromSingleValue(query))) |
| 62 | + .andExpect(status().isOk()) |
| 63 | + .andExpect(jsonPath("draw").value(1)) |
| 64 | + .andExpect(jsonPath("recordsTotal").value(6)) |
| 65 | + .andExpect(jsonPath("recordsFiltered").value(6)) |
| 66 | + .andExpect(jsonPath("data[0].firstName").value("Brenden")) |
| 67 | + .andExpect(jsonPath("error").isEmpty()); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void page(@Autowired MockMvc mvc) throws Exception { |
| 72 | + var query = createQuery(); |
| 73 | + |
| 74 | + query.put("draw", "2"); |
| 75 | + query.put("start", "1"); |
| 76 | + query.put("length", "1"); |
| 77 | + |
| 78 | + mvc.perform(get("/employees").queryParams(MultiValueMap.fromSingleValue(query))) |
| 79 | + .andExpect(status().isOk()) |
| 80 | + .andExpect(jsonPath("draw").value(2)) |
| 81 | + .andExpect(jsonPath("recordsTotal").value(6)) |
| 82 | + .andExpect(jsonPath("recordsFiltered").value(6)) |
| 83 | + .andExpect(jsonPath("data[0].firstName").value("Ashton")) |
| 84 | + .andExpect(jsonPath("error").isEmpty()); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void invalidStart(@Autowired MockMvc mvc) throws Exception { |
| 89 | + var query = createQuery(); |
| 90 | + |
| 91 | + query.put("start", "-1"); |
| 92 | + |
| 93 | + mvc.perform(get("/employees").queryParams(MultiValueMap.fromSingleValue(query))) |
| 94 | + .andExpect(status().is4xxClientError()); |
| 95 | + } |
| 96 | +} |
0 commit comments