JAVA-8149: Update Introduction to the Functional Web Framework in Spring

5
This commit is contained in:
sampadawagde 2022-05-30 18:21:38 +05:30
parent a7cae2e766
commit b14e8332f8
2 changed files with 36 additions and 47 deletions

View File

@ -25,49 +25,39 @@ public class EmployeeFunctionalConfig {
@Bean @Bean
RouterFunction<ServerResponse> getAllEmployeesRoute() { RouterFunction<ServerResponse> getAllEmployeesRoute() {
return route(GET("/employees"), return route(GET("/employees"), req -> ok().body(employeeRepository().findAllEmployees(), Employee.class));
req -> ok().body(
employeeRepository().findAllEmployees(), Employee.class));
} }
@Bean @Bean
RouterFunction<ServerResponse> getEmployeeByIdRoute() { RouterFunction<ServerResponse> getEmployeeByIdRoute() {
return route(GET("/employees/{id}"), return route(GET("/employees/{id}"), req -> ok().body(employeeRepository().findEmployeeById(req.pathVariable("id")), Employee.class));
req -> ok().body(
employeeRepository().findEmployeeById(req.pathVariable("id")), Employee.class));
} }
@Bean @Bean
RouterFunction<ServerResponse> updateEmployeeRoute() { RouterFunction<ServerResponse> updateEmployeeRoute() {
return route(POST("/employees/update"), return route(POST("/employees/update"), req -> req.body(toMono(Employee.class))
req -> req.body(toMono(Employee.class))
.doOnNext(employeeRepository()::updateEmployee) .doOnNext(employeeRepository()::updateEmployee)
.then(ok().build())); .then(ok().build()));
} }
@Bean @Bean
RouterFunction<ServerResponse> composedRoutes() { RouterFunction<ServerResponse> composedRoutes() {
return return route(GET("/employees"), req -> ok().body(employeeRepository().findAllEmployees(), Employee.class))
route(GET("/employees"),
req -> ok().body(
employeeRepository().findAllEmployees(), Employee.class))
.and(route(GET("/employees/{id}"), .and(route(GET("/employees/{id}"), req -> ok().body(employeeRepository().findEmployeeById(req.pathVariable("id")), Employee.class)))
req -> ok().body(
employeeRepository().findEmployeeById(req.pathVariable("id")), Employee.class)))
.and(route(POST("/employees/update"), .and(route(POST("/employees/update"), req -> req.body(toMono(Employee.class))
req -> req.body(toMono(Employee.class))
.doOnNext(employeeRepository()::updateEmployee) .doOnNext(employeeRepository()::updateEmployee)
.then(ok().build()))); .then(ok().build())));
} }
@Bean @Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http http.csrf()
.csrf().disable() .disable()
.authorizeExchange() .authorizeExchange()
.anyExchange().permitAll(); .anyExchange()
.permitAll();
return http.build(); return http.build();
} }
} }

View File

@ -31,8 +31,7 @@ public class EmployeeSpringFunctionalIntegrationTest {
@Test @Test
public void givenEmployeeId_whenGetEmployeeById_thenCorrectEmployee() { public void givenEmployeeId_whenGetEmployeeById_thenCorrectEmployee() {
WebTestClient client = WebTestClient WebTestClient client = WebTestClient.bindToRouterFunction(config.getEmployeeByIdRoute())
.bindToRouterFunction(config.getEmployeeByIdRoute())
.build(); .build();
Employee employee = new Employee("1", "Employee 1"); Employee employee = new Employee("1", "Employee 1");
@ -42,20 +41,18 @@ public class EmployeeSpringFunctionalIntegrationTest {
client.get() client.get()
.uri("/employees/1") .uri("/employees/1")
.exchange() .exchange()
.expectStatus().isOk() .expectStatus()
.expectBody(Employee.class).isEqualTo(employee); .isOk()
.expectBody(Employee.class)
.isEqualTo(employee);
} }
@Test @Test
public void whenGetAllEmployees_thenCorrectEmployees() { public void whenGetAllEmployees_thenCorrectEmployees() {
WebTestClient client = WebTestClient WebTestClient client = WebTestClient.bindToRouterFunction(config.getAllEmployeesRoute())
.bindToRouterFunction(config.getAllEmployeesRoute())
.build(); .build();
List<Employee> employees = Arrays.asList( List<Employee> employees = Arrays.asList(new Employee("1", "Employee 1"), new Employee("2", "Employee 2"));
new Employee("1", "Employee 1"),
new Employee("2", "Employee 2")
);
Flux<Employee> employeeFlux = Flux.fromIterable(employees); Flux<Employee> employeeFlux = Flux.fromIterable(employees);
given(employeeRepository.findAllEmployees()).willReturn(employeeFlux); given(employeeRepository.findAllEmployees()).willReturn(employeeFlux);
@ -63,14 +60,15 @@ public class EmployeeSpringFunctionalIntegrationTest {
client.get() client.get()
.uri("/employees") .uri("/employees")
.exchange() .exchange()
.expectStatus().isOk() .expectStatus()
.expectBodyList(Employee.class).isEqualTo(employees); .isOk()
.expectBodyList(Employee.class)
.isEqualTo(employees);
} }
@Test @Test
public void whenUpdateEmployee_thenEmployeeUpdated() { public void whenUpdateEmployee_thenEmployeeUpdated() {
WebTestClient client = WebTestClient WebTestClient client = WebTestClient.bindToRouterFunction(config.updateEmployeeRoute())
.bindToRouterFunction(config.updateEmployeeRoute())
.build(); .build();
Employee employee = new Employee("1", "Employee 1 Updated"); Employee employee = new Employee("1", "Employee 1 Updated");
@ -79,7 +77,8 @@ public class EmployeeSpringFunctionalIntegrationTest {
.uri("/employees/update") .uri("/employees/update")
.body(Mono.just(employee), Employee.class) .body(Mono.just(employee), Employee.class)
.exchange() .exchange()
.expectStatus().isOk(); .expectStatus()
.isOk();
verify(employeeRepository).updateEmployee(employee); verify(employeeRepository).updateEmployee(employee);
} }