[JAVA-8148] Code clean up

This commit is contained in:
Haroon Khan 2022-04-28 17:20:37 +01:00
parent 003d00daa9
commit 39f17dd3d1
6 changed files with 80 additions and 91 deletions

View File

@ -12,6 +12,4 @@ public class Employee {
private String id; private String id;
private String name; private String name;
// standard getters and setters
} }

View File

@ -26,13 +26,11 @@ public class EmployeeWebSecurityConfig {
@Bean @Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http.csrf() http
.disable() .csrf().disable()
.authorizeExchange() .authorizeExchange()
.pathMatchers(HttpMethod.POST, "/employees/update") .pathMatchers(HttpMethod.POST, "/employees/update").hasRole("ADMIN")
.hasRole("ADMIN") .pathMatchers("/**").permitAll()
.pathMatchers("/**")
.permitAll()
.and() .and()
.httpBasic(); .httpBasic();
return http.build(); return http.build();

View File

@ -64,11 +64,10 @@ public class EmployeeFunctionalConfig {
@Bean @Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http.csrf() http
.disable() .csrf().disable()
.authorizeExchange() .authorizeExchange()
.anyExchange() .anyExchange().permitAll();
.permitAll();
return http.build(); return http.build();
} }
} }

View File

@ -22,7 +22,7 @@ import static org.mockito.Mockito.verifyNoInteractions;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT, classes= EmployeeSpringApplication.class) @SpringBootTest(webEnvironment = RANDOM_PORT, classes = EmployeeSpringApplication.class)
public class EmployeeControllerIntegrationTest { public class EmployeeControllerIntegrationTest {
@Autowired @Autowired
@ -64,7 +64,7 @@ public class EmployeeControllerIntegrationTest {
} }
@Test @Test
@WithMockUser(username = "admin", roles = {"ADMIN"}) @WithMockUser(username = "admin", roles = { "ADMIN" })
public void givenValidUser_whenUpdateEmployee_thenEmployeeUpdated() { public void givenValidUser_whenUpdateEmployee_thenEmployeeUpdated() {
Employee employee = new Employee("10", "Employee 10 Updated"); Employee employee = new Employee("10", "Employee 10 Updated");

View File

@ -2,10 +2,8 @@ package com.baeldung.reactive.webflux.functional;
import com.baeldung.reactive.webflux.Employee; import com.baeldung.reactive.webflux.Employee;
import com.baeldung.reactive.webflux.EmployeeRepository; import com.baeldung.reactive.webflux.EmployeeRepository;
import org.junit.FixMethodOrder;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean; 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.BDDMockito.given;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = EmployeeSpringFunctionalApplication.class) @SpringBootTest(webEnvironment = RANDOM_PORT, classes = EmployeeSpringFunctionalApplication.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class EmployeeSpringFunctionalIntegrationTest { public class EmployeeSpringFunctionalIntegrationTest {
@Autowired @Autowired
@ -44,10 +42,8 @@ public class EmployeeSpringFunctionalIntegrationTest {
client.get() client.get()
.uri("/employees/1") .uri("/employees/1")
.exchange() .exchange()
.expectStatus() .expectStatus().isOk()
.isOk() .expectBody(Employee.class).isEqualTo(employee);
.expectBody(Employee.class)
.isEqualTo(employee);
} }
@Test @Test
@ -58,7 +54,8 @@ public class EmployeeSpringFunctionalIntegrationTest {
List<Employee> employees = Arrays.asList( List<Employee> employees = Arrays.asList(
new Employee("1", "Employee 1"), new Employee("1", "Employee 1"),
new Employee("2", "Employee 2")); 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);
@ -66,10 +63,8 @@ public class EmployeeSpringFunctionalIntegrationTest {
client.get() client.get()
.uri("/employees") .uri("/employees")
.exchange() .exchange()
.expectStatus() .expectStatus().isOk()
.isOk() .expectBodyList(Employee.class).isEqualTo(employees);
.expectBodyList(Employee.class)
.isEqualTo(employees);
} }
@Test @Test
@ -84,8 +79,7 @@ public class EmployeeSpringFunctionalIntegrationTest {
.uri("/employees/update") .uri("/employees/update")
.body(Mono.just(employee), Employee.class) .body(Mono.just(employee), Employee.class)
.exchange() .exchange()
.expectStatus() .expectStatus().isOk();
.isOk();
verify(employeeRepository).updateEmployee(employee); verify(employeeRepository).updateEmployee(employee);
} }