Skip to content

Commit 6456fa3

Browse files
authored
Merge pull request eugenp#5162 from dkapil/feature/BAEL-2304_1
Feature/bael 2304 1
2 parents e88ef76 + 5f0cc27 commit 6456fa3

3 files changed

Lines changed: 116 additions & 0 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.baeldung.reactive.functional;
2+
3+
import static org.springframework.web.reactive.function.BodyExtractors.toMono;
4+
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
5+
import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
6+
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
7+
import static org.springframework.web.reactive.function.server.ServerResponse.ok;
8+
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.security.config.web.server.ServerHttpSecurity;
12+
import org.springframework.security.web.server.SecurityWebFilterChain;
13+
import org.springframework.web.reactive.function.server.RouterFunction;
14+
import org.springframework.web.reactive.function.server.ServerResponse;
15+
16+
import com.baeldung.webflux.Employee;
17+
import com.baeldung.webflux.EmployeeRepository;
18+
19+
@Configuration
20+
public class EmployeeFunctionalConfig {
21+
22+
@Bean
23+
EmployeeRepository employeeRepository() {
24+
return new EmployeeRepository();
25+
}
26+
27+
@Bean
28+
RouterFunction<ServerResponse> getAllEmployeesRoute() {
29+
return route(GET("/employees"),
30+
req -> ok().body(
31+
employeeRepository().findAllEmployees(), Employee.class));
32+
}
33+
34+
@Bean
35+
RouterFunction<ServerResponse> getEmployeeByIdRoute() {
36+
return route(GET("/employees/{id}"),
37+
req -> ok().body(
38+
employeeRepository().findEmployeeById(req.pathVariable("id")), Employee.class));
39+
}
40+
41+
@Bean
42+
RouterFunction<ServerResponse> updateEmployee() {
43+
return route(POST("/employees/update"),
44+
req -> req.body(toMono(Employee.class))
45+
.doOnNext(employeeRepository()::updateEmployee)
46+
.then(ok().build()));
47+
}
48+
49+
@Bean
50+
RouterFunction<ServerResponse> composedRoutes() {
51+
return
52+
route(GET("/employees"),
53+
req -> ok().body(
54+
employeeRepository().findAllEmployees(), Employee.class))
55+
56+
.and(route(GET("/employees/{id}"),
57+
req -> ok().body(
58+
employeeRepository().findEmployeeById(req.pathVariable("id")), Employee.class)))
59+
60+
.and(route(POST("/employees/update"),
61+
req -> req.body(toMono(Employee.class))
62+
.doOnNext(employeeRepository()::updateEmployee)
63+
.then(ok().build())));
64+
}
65+
66+
@Bean
67+
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
68+
http.csrf()
69+
.disable()
70+
.authorizeExchange()
71+
.anyExchange()
72+
.permitAll();
73+
return http.build();
74+
}
75+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.reactive.functional;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class EmployeeSpringFunctionalApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(EmployeeSpringFunctionalApplication.class, args);
11+
}
12+
13+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.reactive.functional;
2+
3+
import org.junit.Test;
4+
import org.springframework.test.web.reactive.server.WebTestClient;
5+
6+
import com.baeldung.webflux.Employee;
7+
8+
public class EmployeeSpringFunctionalIntegrationTest {
9+
10+
private static EmployeeFunctionalConfig config = new EmployeeFunctionalConfig();
11+
12+
@Test
13+
public void givenEmployeeId_whenGetEmployeeById_thenCorrectEmployee() {
14+
WebTestClient client = WebTestClient
15+
.bindToRouterFunction(config.getEmployeeByIdRoute())
16+
.build();
17+
18+
Employee expected = new Employee("1", "Employee 1");
19+
20+
client.get()
21+
.uri("/employees/1")
22+
.exchange()
23+
.expectStatus()
24+
.isOk()
25+
.expectBody(Employee.class)
26+
.isEqualTo(expected);
27+
}
28+
}

0 commit comments

Comments
 (0)