|
| 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.http.MediaType; |
| 12 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 13 | +import org.springframework.test.web.servlet.MockMvc; |
| 14 | +import org.springframework.util.MultiValueMap; |
| 15 | + |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 20 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 21 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; |
| 22 | + |
| 23 | +@ExtendWith(SpringExtension.class) |
| 24 | +@SpringBootTest( |
| 25 | + webEnvironment = SpringBootTest.WebEnvironment.MOCK, |
| 26 | + classes = TestApplication.class) |
| 27 | +@AutoConfigureMockMvc |
| 28 | +class EmployeeControllerTest { |
| 29 | + |
| 30 | + @Autowired private EmployeeRepository employeeRepository; |
| 31 | + |
| 32 | + @BeforeEach |
| 33 | + public void init() { |
| 34 | + this.employeeRepository.saveAll(Employee.ALL); |
| 35 | + } |
| 36 | + |
| 37 | + private static Map<String, String> createQuery() { |
| 38 | + var query = new HashMap<String, String>(); |
| 39 | + |
| 40 | + query.put("draw", "1"); |
| 41 | + query.put("start", "0"); |
| 42 | + query.put("length", "10"); |
| 43 | + |
| 44 | + query.put("search.value", ""); |
| 45 | + query.put("search.regex", "false"); |
| 46 | + |
| 47 | + query.put("order[0].column", "0"); |
| 48 | + query.put("order[0].dir", "asc"); |
| 49 | + |
| 50 | + query.put("columns[0].data", "id"); |
| 51 | + query.put("columns[0].searchable", "true"); |
| 52 | + query.put("columns[0].orderable", "true"); |
| 53 | + query.put("columns[0].search.value", ""); |
| 54 | + query.put("columns[0].search.regex", "false"); |
| 55 | + |
| 56 | + return query; |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void basic(@Autowired MockMvc mvc) throws Exception { |
| 61 | + var query = createQuery(); |
| 62 | + |
| 63 | + mvc.perform(get("/employees").queryParams(MultiValueMap.fromSingleValue(query))) |
| 64 | + .andExpect(status().isOk()) |
| 65 | + .andExpect(jsonPath("draw").value(1)) |
| 66 | + .andExpect(jsonPath("recordsTotal").value(6)) |
| 67 | + .andExpect(jsonPath("recordsFiltered").value(6)) |
| 68 | + .andExpect(jsonPath("data[0].firstName").value("Brenden")) |
| 69 | + .andExpect(jsonPath("error").isEmpty()); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void page(@Autowired MockMvc mvc) throws Exception { |
| 74 | + var query = createQuery(); |
| 75 | + |
| 76 | + query.put("draw", "2"); |
| 77 | + query.put("start", "1"); |
| 78 | + query.put("length", "1"); |
| 79 | + |
| 80 | + mvc.perform(get("/employees").queryParams(MultiValueMap.fromSingleValue(query))) |
| 81 | + .andExpect(status().isOk()) |
| 82 | + .andExpect(jsonPath("draw").value(2)) |
| 83 | + .andExpect(jsonPath("recordsTotal").value(6)) |
| 84 | + .andExpect(jsonPath("recordsFiltered").value(6)) |
| 85 | + .andExpect(jsonPath("data[0].firstName").value("Ashton")) |
| 86 | + .andExpect(jsonPath("error").isEmpty()); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void invalidStart(@Autowired MockMvc mvc) throws Exception { |
| 91 | + var query = createQuery(); |
| 92 | + |
| 93 | + query.put("start", "-1"); |
| 94 | + |
| 95 | + mvc.perform(get("/employees").queryParams(MultiValueMap.fromSingleValue(query))) |
| 96 | + .andExpect(status().is4xxClientError()); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void withPOST(@Autowired MockMvc mvc) throws Exception { |
| 101 | + mvc.perform( |
| 102 | + post("/employees") |
| 103 | + .contentType(MediaType.APPLICATION_JSON) |
| 104 | + .content( |
| 105 | +""" |
| 106 | +{ |
| 107 | + "draw": "1", |
| 108 | + "start": "0", |
| 109 | + "length": "10", |
| 110 | +
|
| 111 | + "search": { |
| 112 | + "value": "", |
| 113 | + "regex": false |
| 114 | + }, |
| 115 | +
|
| 116 | + "order": [ |
| 117 | + { |
| 118 | + "column": 0, |
| 119 | + "dir": "asc" |
| 120 | + } |
| 121 | + ], |
| 122 | +
|
| 123 | + "columns": [ |
| 124 | + { |
| 125 | + "data": "id", |
| 126 | + "searchable": true, |
| 127 | + "orderable": true, |
| 128 | + "search": { |
| 129 | + "value": "", |
| 130 | + "regex": false |
| 131 | + } |
| 132 | + } |
| 133 | + ] |
| 134 | +} |
| 135 | +""")) |
| 136 | + .andExpect(status().isOk()) |
| 137 | + .andExpect(jsonPath("draw").value(1)) |
| 138 | + .andExpect(jsonPath("recordsTotal").value(6)) |
| 139 | + .andExpect(jsonPath("recordsFiltered").value(6)) |
| 140 | + .andExpect(jsonPath("data[0].firstName").value("Brenden")) |
| 141 | + .andExpect(jsonPath("error").isEmpty()); |
| 142 | + } |
| 143 | +} |
0 commit comments