Skip to content

Commit 2295818

Browse files
test: add integration tests
1 parent a6333b4 commit 2295818

File tree

4 files changed

+198
-0
lines changed

4 files changed

+198
-0
lines changed

pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,24 @@
131131
<scope>test</scope>
132132
</dependency>
133133

134+
<dependency>
135+
<groupId>org.springframework.boot</groupId>
136+
<artifactId>spring-boot-starter-web</artifactId>
137+
<scope>test</scope>
138+
</dependency>
139+
140+
<dependency>
141+
<groupId>org.springframework.boot</groupId>
142+
<artifactId>spring-boot-starter-validation</artifactId>
143+
<scope>test</scope>
144+
</dependency>
145+
146+
<dependency>
147+
<groupId>org.springframework.boot</groupId>
148+
<artifactId>spring-boot-starter-test</artifactId>
149+
<scope>test</scope>
150+
</dependency>
151+
134152
<dependency>
135153
<groupId>org.springframework</groupId>
136154
<artifactId>spring-test</artifactId>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.springframework.data.jpa.datatables;
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
5+
@SpringBootApplication
6+
public class TestApplication {
7+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.springframework.data.jpa.datatables.repository;
2+
3+
import jakarta.validation.Valid;
4+
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
5+
import org.springframework.data.jpa.datatables.mapping.DataTablesOutput;
6+
import org.springframework.data.jpa.datatables.model.Employee;
7+
import org.springframework.web.bind.annotation.RequestBody;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RequestMethod;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
@RestController
13+
class EmployeeController {
14+
private final EmployeeRepository employeeRepository;
15+
16+
public EmployeeController(EmployeeRepository employeeRepository) {
17+
this.employeeRepository = employeeRepository;
18+
}
19+
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+
}
30+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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

Comments
 (0)