diff --git a/spring-reactive/src/main/java/com/baeldung/reactive/webflux/Employee.java b/spring-reactive/src/main/java/com/baeldung/reactive/webflux/Employee.java index d41a4f2791..dd88dcb668 100644 --- a/spring-reactive/src/main/java/com/baeldung/reactive/webflux/Employee.java +++ b/spring-reactive/src/main/java/com/baeldung/reactive/webflux/Employee.java @@ -11,7 +11,5 @@ public class Employee { private String id; private String name; - - // standard getters and setters - + } diff --git a/spring-reactive/src/main/java/com/baeldung/reactive/webflux/annotation/EmployeeWebClient.java b/spring-reactive/src/main/java/com/baeldung/reactive/webflux/annotation/EmployeeWebClient.java index 62978e1f58..5ae193dc7c 100644 --- a/spring-reactive/src/main/java/com/baeldung/reactive/webflux/annotation/EmployeeWebClient.java +++ b/spring-reactive/src/main/java/com/baeldung/reactive/webflux/annotation/EmployeeWebClient.java @@ -16,17 +16,17 @@ public class EmployeeWebClient { public void consume() { Mono<Employee> employeeMono = client.get() - .uri("/employees/{id}", "1") - .retrieve() - .bodyToMono(Employee.class); + .uri("/employees/{id}", "1") + .retrieve() + .bodyToMono(Employee.class); employeeMono.subscribe(employee -> LOGGER.info("Employee: {}", employee)); Flux<Employee> employeeFlux = client.get() - .uri("/employees") - .retrieve() - .bodyToFlux(Employee.class); + .uri("/employees") + .retrieve() + .bodyToFlux(Employee.class); employeeFlux.subscribe(employee -> LOGGER.info("Employee: {}", employee)); } -} \ No newline at end of file +} diff --git a/spring-reactive/src/main/java/com/baeldung/reactive/webflux/annotation/EmployeeWebSecurityConfig.java b/spring-reactive/src/main/java/com/baeldung/reactive/webflux/annotation/EmployeeWebSecurityConfig.java index 8dfa455ce3..fc98b70c0f 100644 --- a/spring-reactive/src/main/java/com/baeldung/reactive/webflux/annotation/EmployeeWebSecurityConfig.java +++ b/spring-reactive/src/main/java/com/baeldung/reactive/webflux/annotation/EmployeeWebSecurityConfig.java @@ -13,33 +13,31 @@ import org.springframework.security.web.server.SecurityWebFilterChain; @EnableWebFluxSecurity public class EmployeeWebSecurityConfig { - + @Bean public MapReactiveUserDetailsService userDetailsService() { UserDetails user = User - .withUsername("admin") - .password(passwordEncoder().encode("password")) - .roles("ADMIN") - .build(); + .withUsername("admin") + .password(passwordEncoder().encode("password")) + .roles("ADMIN") + .build(); return new MapReactiveUserDetailsService(user); } @Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { - http.csrf() - .disable() - .authorizeExchange() - .pathMatchers(HttpMethod.POST, "/employees/update") - .hasRole("ADMIN") - .pathMatchers("/**") - .permitAll() - .and() - .httpBasic(); + http + .csrf().disable() + .authorizeExchange() + .pathMatchers(HttpMethod.POST, "/employees/update").hasRole("ADMIN") + .pathMatchers("/**").permitAll() + .and() + .httpBasic(); return http.build(); } - + @Bean public PasswordEncoder passwordEncoder() { - return new BCryptPasswordEncoder(); + return new BCryptPasswordEncoder(); } } diff --git a/spring-reactive/src/main/java/com/baeldung/reactive/webflux/functional/EmployeeFunctionalConfig.java b/spring-reactive/src/main/java/com/baeldung/reactive/webflux/functional/EmployeeFunctionalConfig.java index f97d40e4e7..8b5c7233d6 100644 --- a/spring-reactive/src/main/java/com/baeldung/reactive/webflux/functional/EmployeeFunctionalConfig.java +++ b/spring-reactive/src/main/java/com/baeldung/reactive/webflux/functional/EmployeeFunctionalConfig.java @@ -25,50 +25,49 @@ 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)) - .doOnNext(employeeRepository()::updateEmployee) - .then(ok().build())); + 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"), + 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(POST("/employees/update"), - req -> req.body(toMono(Employee.class)) - .doOnNext(employeeRepository()::updateEmployee) - .then(ok().build()))); + + .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)) + .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(); } } diff --git a/spring-reactive/src/test/java/com/baeldung/reactive/webflux/annotation/EmployeeControllerIntegrationTest.java b/spring-reactive/src/test/java/com/baeldung/reactive/webflux/annotation/EmployeeControllerIntegrationTest.java index c4a11e7f3b..699bc9c154 100644 --- a/spring-reactive/src/test/java/com/baeldung/reactive/webflux/annotation/EmployeeControllerIntegrationTest.java +++ b/spring-reactive/src/test/java/com/baeldung/reactive/webflux/annotation/EmployeeControllerIntegrationTest.java @@ -22,7 +22,7 @@ import static org.mockito.Mockito.verifyNoInteractions; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; @RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = RANDOM_PORT, classes= EmployeeSpringApplication.class) +@SpringBootTest(webEnvironment = RANDOM_PORT, classes = EmployeeSpringApplication.class) public class EmployeeControllerIntegrationTest { @Autowired @@ -39,10 +39,10 @@ public class EmployeeControllerIntegrationTest { given(employeeRepository.findEmployeeById("1")).willReturn(Mono.just(employee)); testClient.get() - .uri("/employees/1") - .exchange() - .expectStatus().isOk() - .expectBody(Employee.class).isEqualTo(employee); + .uri("/employees/1") + .exchange() + .expectStatus().isOk() + .expectBody(Employee.class).isEqualTo(employee); } @Test @@ -57,14 +57,14 @@ public class EmployeeControllerIntegrationTest { given(employeeRepository.findAllEmployees()).willReturn(employeeFlux); testClient.get() - .uri("/employees") - .exchange() - .expectStatus().isOk() - .expectBodyList(Employee.class).isEqualTo(employeeList); + .uri("/employees") + .exchange() + .expectStatus().isOk() + .expectBodyList(Employee.class).isEqualTo(employeeList); } @Test - @WithMockUser(username = "admin", roles = {"ADMIN"}) + @WithMockUser(username = "admin", roles = { "ADMIN" }) public void givenValidUser_whenUpdateEmployee_thenEmployeeUpdated() { Employee employee = new Employee("10", "Employee 10 Updated"); diff --git a/spring-reactive/src/test/java/com/baeldung/reactive/webflux/functional/EmployeeSpringFunctionalIntegrationTest.java b/spring-reactive/src/test/java/com/baeldung/reactive/webflux/functional/EmployeeSpringFunctionalIntegrationTest.java index da866724cc..198ec0d081 100644 --- a/spring-reactive/src/test/java/com/baeldung/reactive/webflux/functional/EmployeeSpringFunctionalIntegrationTest.java +++ b/spring-reactive/src/test/java/com/baeldung/reactive/webflux/functional/EmployeeSpringFunctionalIntegrationTest.java @@ -2,10 +2,8 @@ package com.baeldung.reactive.webflux.functional; import com.baeldung.reactive.webflux.Employee; import com.baeldung.reactive.webflux.EmployeeRepository; -import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runner.RunWith; -import org.junit.runners.MethodSorters; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; @@ -19,10 +17,10 @@ import java.util.List; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.verify; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; @RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = EmployeeSpringFunctionalApplication.class) -@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@SpringBootTest(webEnvironment = RANDOM_PORT, classes = EmployeeSpringFunctionalApplication.class) public class EmployeeSpringFunctionalIntegrationTest { @Autowired @@ -34,58 +32,54 @@ public class EmployeeSpringFunctionalIntegrationTest { @Test public void givenEmployeeId_whenGetEmployeeById_thenCorrectEmployee() { WebTestClient client = WebTestClient - .bindToRouterFunction(config.getEmployeeByIdRoute()) - .build(); + .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(); + .bindToRouterFunction(config.getAllEmployeesRoute()) + .build(); 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); 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(); + .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); }