JAVA-8149: Update Introduction to the Functional Web Framework in Spring
5
This commit is contained in:
parent
a7cae2e766
commit
b14e8332f8
|
@ -25,49 +25,39 @@ public class EmployeeFunctionalConfig {
|
|||
|
||||
@Bean
|
||||
RouterFunction<ServerResponse> getAllEmployeesRoute() {
|
||||
return route(GET("/employees"),
|
||||
req -> ok().body(
|
||||
employeeRepository().findAllEmployees(), Employee.class));
|
||||
return route(GET("/employees"), req -> ok().body(employeeRepository().findAllEmployees(), Employee.class));
|
||||
}
|
||||
|
||||
@Bean
|
||||
RouterFunction<ServerResponse> getEmployeeByIdRoute() {
|
||||
return route(GET("/employees/{id}"),
|
||||
req -> ok().body(
|
||||
employeeRepository().findEmployeeById(req.pathVariable("id")), Employee.class));
|
||||
return route(GET("/employees/{id}"), req -> ok().body(employeeRepository().findEmployeeById(req.pathVariable("id")), Employee.class));
|
||||
}
|
||||
|
||||
@Bean
|
||||
RouterFunction<ServerResponse> updateEmployeeRoute() {
|
||||
return route(POST("/employees/update"),
|
||||
req -> req.body(toMono(Employee.class))
|
||||
return route(POST("/employees/update"), req -> req.body(toMono(Employee.class))
|
||||
.doOnNext(employeeRepository()::updateEmployee)
|
||||
.then(ok().build()));
|
||||
}
|
||||
|
||||
@Bean
|
||||
RouterFunction<ServerResponse> composedRoutes() {
|
||||
return
|
||||
route(GET("/employees"),
|
||||
req -> ok().body(
|
||||
employeeRepository().findAllEmployees(), Employee.class))
|
||||
return route(GET("/employees"), req -> ok().body(employeeRepository().findAllEmployees(), Employee.class))
|
||||
|
||||
.and(route(GET("/employees/{id}"),
|
||||
req -> ok().body(
|
||||
employeeRepository().findEmployeeById(req.pathVariable("id")), Employee.class)))
|
||||
.and(route(GET("/employees/{id}"), req -> ok().body(employeeRepository().findEmployeeById(req.pathVariable("id")), Employee.class)))
|
||||
|
||||
.and(route(POST("/employees/update"),
|
||||
req -> req.body(toMono(Employee.class))
|
||||
.and(route(POST("/employees/update"), req -> req.body(toMono(Employee.class))
|
||||
.doOnNext(employeeRepository()::updateEmployee)
|
||||
.then(ok().build())));
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
|
||||
http
|
||||
.csrf().disable()
|
||||
http.csrf()
|
||||
.disable()
|
||||
.authorizeExchange()
|
||||
.anyExchange().permitAll();
|
||||
.anyExchange()
|
||||
.permitAll();
|
||||
return http.build();
|
||||
}
|
||||
}
|
|
@ -31,8 +31,7 @@ public class EmployeeSpringFunctionalIntegrationTest {
|
|||
|
||||
@Test
|
||||
public void givenEmployeeId_whenGetEmployeeById_thenCorrectEmployee() {
|
||||
WebTestClient client = WebTestClient
|
||||
.bindToRouterFunction(config.getEmployeeByIdRoute())
|
||||
WebTestClient client = WebTestClient.bindToRouterFunction(config.getEmployeeByIdRoute())
|
||||
.build();
|
||||
|
||||
Employee employee = new Employee("1", "Employee 1");
|
||||
|
@ -42,20 +41,18 @@ public class EmployeeSpringFunctionalIntegrationTest {
|
|||
client.get()
|
||||
.uri("/employees/1")
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody(Employee.class).isEqualTo(employee);
|
||||
.expectStatus()
|
||||
.isOk()
|
||||
.expectBody(Employee.class)
|
||||
.isEqualTo(employee);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetAllEmployees_thenCorrectEmployees() {
|
||||
WebTestClient client = WebTestClient
|
||||
.bindToRouterFunction(config.getAllEmployeesRoute())
|
||||
WebTestClient client = WebTestClient.bindToRouterFunction(config.getAllEmployeesRoute())
|
||||
.build();
|
||||
|
||||
List<Employee> employees = Arrays.asList(
|
||||
new Employee("1", "Employee 1"),
|
||||
new Employee("2", "Employee 2")
|
||||
);
|
||||
List<Employee> employees = Arrays.asList(new Employee("1", "Employee 1"), new Employee("2", "Employee 2"));
|
||||
|
||||
Flux<Employee> employeeFlux = Flux.fromIterable(employees);
|
||||
given(employeeRepository.findAllEmployees()).willReturn(employeeFlux);
|
||||
|
@ -63,14 +60,15 @@ public class EmployeeSpringFunctionalIntegrationTest {
|
|||
client.get()
|
||||
.uri("/employees")
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBodyList(Employee.class).isEqualTo(employees);
|
||||
.expectStatus()
|
||||
.isOk()
|
||||
.expectBodyList(Employee.class)
|
||||
.isEqualTo(employees);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUpdateEmployee_thenEmployeeUpdated() {
|
||||
WebTestClient client = WebTestClient
|
||||
.bindToRouterFunction(config.updateEmployeeRoute())
|
||||
WebTestClient client = WebTestClient.bindToRouterFunction(config.updateEmployeeRoute())
|
||||
.build();
|
||||
|
||||
Employee employee = new Employee("1", "Employee 1 Updated");
|
||||
|
@ -79,7 +77,8 @@ public class EmployeeSpringFunctionalIntegrationTest {
|
|||
.uri("/employees/update")
|
||||
.body(Mono.just(employee), Employee.class)
|
||||
.exchange()
|
||||
.expectStatus().isOk();
|
||||
.expectStatus()
|
||||
.isOk();
|
||||
|
||||
verify(employeeRepository).updateEmployee(employee);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue