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()
|
||||
.authorizeExchange()
|
||||
.anyExchange().permitAll();
|
||||
http.csrf()
|
||||
.disable()
|
||||
.authorizeExchange()
|
||||
.anyExchange()
|
||||
.permitAll();
|
||||
return http.build();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -31,55 +31,54 @@ public class EmployeeSpringFunctionalIntegrationTest {
|
|||
|
||||
@Test
|
||||
public void givenEmployeeId_whenGetEmployeeById_thenCorrectEmployee() {
|
||||
WebTestClient client = WebTestClient
|
||||
.bindToRouterFunction(config.getEmployeeByIdRoute())
|
||||
.build();
|
||||
WebTestClient client = WebTestClient.bindToRouterFunction(config.getEmployeeByIdRoute())
|
||||
.build();
|
||||
|
||||
Employee employee = new Employee("1", "Employee 1");
|
||||
|
||||
given(employeeRepository.findEmployeeById("1")).willReturn(Mono.just(employee));
|
||||
|
||||
client.get()
|
||||
.uri("/employees/1")
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody(Employee.class).isEqualTo(employee);
|
||||
.uri("/employees/1")
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isOk()
|
||||
.expectBody(Employee.class)
|
||||
.isEqualTo(employee);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetAllEmployees_thenCorrectEmployees() {
|
||||
WebTestClient client = WebTestClient
|
||||
.bindToRouterFunction(config.getAllEmployeesRoute())
|
||||
.build();
|
||||
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);
|
||||
|
||||
client.get()
|
||||
.uri("/employees")
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBodyList(Employee.class).isEqualTo(employees);
|
||||
.uri("/employees")
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isOk()
|
||||
.expectBodyList(Employee.class)
|
||||
.isEqualTo(employees);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUpdateEmployee_thenEmployeeUpdated() {
|
||||
WebTestClient client = WebTestClient
|
||||
.bindToRouterFunction(config.updateEmployeeRoute())
|
||||
.build();
|
||||
WebTestClient client = WebTestClient.bindToRouterFunction(config.updateEmployeeRoute())
|
||||
.build();
|
||||
|
||||
Employee employee = new Employee("1", "Employee 1 Updated");
|
||||
|
||||
client.post()
|
||||
.uri("/employees/update")
|
||||
.body(Mono.just(employee), Employee.class)
|
||||
.exchange()
|
||||
.expectStatus().isOk();
|
||||
.uri("/employees/update")
|
||||
.body(Mono.just(employee), Employee.class)
|
||||
.exchange()
|
||||
.expectStatus()
|
||||
.isOk();
|
||||
|
||||
verify(employeeRepository).updateEmployee(employee);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue