Skip to content

Commit 03f344b

Browse files
test with POST too
1 parent 2164c7e commit 03f344b

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed

src/test/java/org/springframework/data/jpa/datatables/repository/EmployeeController.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,27 @@
44
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
55
import org.springframework.data.jpa.datatables.mapping.DataTablesOutput;
66
import org.springframework.data.jpa.datatables.model.Employee;
7-
import org.springframework.validation.annotation.Validated;
7+
import org.springframework.web.bind.annotation.RequestBody;
88
import org.springframework.web.bind.annotation.RequestMapping;
99
import org.springframework.web.bind.annotation.RequestMethod;
1010
import org.springframework.web.bind.annotation.RestController;
1111

1212
@RestController
13-
@Validated
1413
class EmployeeController {
15-
private final EmployeeRepository employeeRepository;
14+
private final EmployeeRepository employeeRepository;
1615

17-
public EmployeeController(EmployeeRepository employeeRepository) {
18-
this.employeeRepository = employeeRepository;
19-
}
16+
public EmployeeController(EmployeeRepository employeeRepository) {
17+
this.employeeRepository = employeeRepository;
18+
}
2019

21-
@RequestMapping(value = "/employees", method = RequestMethod.GET)
22-
public DataTablesOutput<Employee> findEmployees(@Valid DataTablesInput input) {
23-
return employeeRepository.findAll(input);
24-
}
20+
@RequestMapping(value = "/employees", method = RequestMethod.GET)
21+
public DataTablesOutput<Employee> findEmployees(@Valid DataTablesInput input) {
22+
return employeeRepository.findAll(input);
23+
}
24+
25+
@RequestMapping(value = "/employees", method = RequestMethod.POST)
26+
public DataTablesOutput<Employee> findEmployeesWithPOST(
27+
@Valid @RequestBody DataTablesInput input) {
28+
return employeeRepository.findAll(input);
29+
}
2530
}

src/test/java/org/springframework/data/jpa/datatables/repository/EmployeeControllerTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.springframework.boot.test.context.SpringBootTest;
99
import org.springframework.data.jpa.datatables.TestApplication;
1010
import org.springframework.data.jpa.datatables.model.Employee;
11+
import org.springframework.http.MediaType;
1112
import org.springframework.test.context.junit.jupiter.SpringExtension;
1213
import org.springframework.test.web.servlet.MockMvc;
1314
import org.springframework.util.MultiValueMap;
@@ -16,6 +17,7 @@
1617
import java.util.Map;
1718

1819
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
20+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
1921
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
2022

2123
@ExtendWith(SpringExtension.class)
@@ -93,4 +95,49 @@ void invalidStart(@Autowired MockMvc mvc) throws Exception {
9395
mvc.perform(get("/employees").queryParams(MultiValueMap.fromSingleValue(query)))
9496
.andExpect(status().is4xxClientError());
9597
}
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+
}
96143
}

0 commit comments

Comments
 (0)