Skip to content

Commit 2164c7e

Browse files
test: add integration test
1 parent a6333b4 commit 2164c7e

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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.validation.annotation.Validated;
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+
@Validated
14+
class EmployeeController {
15+
private final EmployeeRepository employeeRepository;
16+
17+
public EmployeeController(EmployeeRepository employeeRepository) {
18+
this.employeeRepository = employeeRepository;
19+
}
20+
21+
@RequestMapping(value = "/employees", method = RequestMethod.GET)
22+
public DataTablesOutput<Employee> findEmployees(@Valid DataTablesInput input) {
23+
return employeeRepository.findAll(input);
24+
}
25+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)