From 9a09cc3ce3a85f5df09bb504e8bda6406f53de95 Mon Sep 17 00:00:00 2001 From: Dhawal Kapil Date: Mon, 17 Sep 2018 11:07:02 +0530 Subject: [PATCH] BAEL-2304 Improve functional webflux article - Updated tests and minor spell fix --- .../functional/EmployeeFunctionalConfig.java | 2 +- ...ployeeSpringFunctionalIntegrationTest.java | 76 ++++++++++++++++++- 2 files changed, 74 insertions(+), 4 deletions(-) diff --git a/spring-5-reactive-security/src/main/java/com/baeldung/reactive/functional/EmployeeFunctionalConfig.java b/spring-5-reactive-security/src/main/java/com/baeldung/reactive/functional/EmployeeFunctionalConfig.java index 8d7860ccdc..76b697c1aa 100644 --- a/spring-5-reactive-security/src/main/java/com/baeldung/reactive/functional/EmployeeFunctionalConfig.java +++ b/spring-5-reactive-security/src/main/java/com/baeldung/reactive/functional/EmployeeFunctionalConfig.java @@ -39,7 +39,7 @@ public class EmployeeFunctionalConfig { } @Bean - RouterFunction updateEmployee() { + RouterFunction updateEmployeeRoute() { return route(POST("/employees/update"), req -> req.body(toMono(Employee.class)) .doOnNext(employeeRepository()::updateEmployee) diff --git a/spring-5-reactive-security/src/test/java/com/baeldung/reactive/functional/EmployeeSpringFunctionalIntegrationTest.java b/spring-5-reactive-security/src/test/java/com/baeldung/reactive/functional/EmployeeSpringFunctionalIntegrationTest.java index 1197820066..6e73e8072c 100644 --- a/spring-5-reactive-security/src/test/java/com/baeldung/reactive/functional/EmployeeSpringFunctionalIntegrationTest.java +++ b/spring-5-reactive-security/src/test/java/com/baeldung/reactive/functional/EmployeeSpringFunctionalIntegrationTest.java @@ -1,13 +1,37 @@ package com.baeldung.reactive.functional; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.verify; + +import java.util.ArrayList; +import java.util.List; + +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; +import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.reactive.server.WebTestClient; import com.baeldung.webflux.Employee; +import com.baeldung.webflux.EmployeeRepository; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = EmployeeSpringFunctionalApplication.class) +@FixMethodOrder(MethodSorters.NAME_ASCENDING) public class EmployeeSpringFunctionalIntegrationTest { - private static EmployeeFunctionalConfig config = new EmployeeFunctionalConfig(); + @Autowired + private EmployeeFunctionalConfig config; + + @MockBean + private EmployeeRepository employeeRepository; @Test public void givenEmployeeId_whenGetEmployeeById_thenCorrectEmployee() { @@ -15,7 +39,9 @@ public class EmployeeSpringFunctionalIntegrationTest { .bindToRouterFunction(config.getEmployeeByIdRoute()) .build(); - Employee expected = new Employee("1", "Employee 1"); + Employee employee = new Employee("1", "Employee 1"); + + given(employeeRepository.findEmployeeById("1")).willReturn(Mono.just(employee)); client.get() .uri("/employees/1") @@ -23,6 +49,50 @@ public class EmployeeSpringFunctionalIntegrationTest { .expectStatus() .isOk() .expectBody(Employee.class) - .isEqualTo(expected); + .isEqualTo(employee); + } + + @Test + public void whenGetAllEmployees_thenCorrectEmployees() { + WebTestClient client = WebTestClient + .bindToRouterFunction(config.getAllEmployeesRoute()) + .build(); + + List employeeList = new ArrayList<>(); + + Employee employee1 = new Employee("1", "Employee 1"); + Employee employee2 = new Employee("2", "Employee 2"); + + employeeList.add(employee1); + employeeList.add(employee2); + + Flux employeeFlux = Flux.fromIterable(employeeList); + given(employeeRepository.findAllEmployees()).willReturn(employeeFlux); + + client.get() + .uri("/employees") + .exchange() + .expectStatus() + .isOk() + .expectBodyList(Employee.class) + .isEqualTo(employeeList); + } + + @Test + public void whenUpdateEmployee_thenEmployeeUpdated() { + 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(); + + verify(employeeRepository).updateEmployee(employee); } }