Use spring-javaformat to format and check code

Resolves: #1450
This commit is contained in:
Vedran Pavic
2019-06-17 23:44:55 +02:00
parent 0eaeb98b0c
commit 822db7fbbf
241 changed files with 2961 additions and 4660 deletions

View File

@@ -49,10 +49,9 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { RestMockMvcTests.Config.class, SecurityConfig.class,
MvcConfig.class })
@ContextConfiguration(classes = { RestMockMvcTests.Config.class, SecurityConfig.class, MvcConfig.class })
@WebAppConfiguration
public class RestMockMvcTests {
class RestMockMvcTests {
private static final String DOCKER_IMAGE = "redis:5.0.5";
@@ -65,27 +64,26 @@ public class RestMockMvcTests {
private MockMvc mvc;
@BeforeEach
public void setup() {
void setup() {
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).alwaysDo(print())
.addFilters(this.sessionRepositoryFilter).apply(springSecurity()).build();
}
@Test
public void noSessionOnNoCredentials() throws Exception {
void noSessionOnNoCredentials() throws Exception {
this.mvc.perform(get("/")).andExpect(header().doesNotExist("X-Auth-Token"))
.andExpect(status().isUnauthorized());
}
@WithMockUser
@Test
public void autheticatedAnnotation() throws Exception {
void autheticatedAnnotation() throws Exception {
this.mvc.perform(get("/")).andExpect(content().string("{\"username\":\"user\"}"));
}
@Test
public void autheticatedRequestPostProcessor() throws Exception {
this.mvc.perform(get("/").with(user("user")))
.andExpect(content().string("{\"username\":\"user\"}"));
void autheticatedRequestPostProcessor() throws Exception {
this.mvc.perform(get("/").with(user("user"))).andExpect(content().string("{\"username\":\"user\"}"));
}
@Configuration
@@ -94,8 +92,7 @@ public class RestMockMvcTests {
@Bean
public GenericContainer redisContainer() {
GenericContainer redisContainer = new GenericContainer(DOCKER_IMAGE)
.withExposedPorts(6379);
GenericContainer redisContainer = new GenericContainer(DOCKER_IMAGE).withExposedPorts(6379);
redisContainer.start();
return redisContainer;
}

View File

@@ -37,10 +37,12 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* @author Pool Dolorier
*/
public class RestTests {
class RestTests {
private static final String AUTHORIZATION = "Authorization";
private static final String BASIC = "Basic ";
private static final String X_AUTH_TOKEN = "X-Auth-Token";
private RestTemplate restTemplate;
@@ -48,72 +50,65 @@ public class RestTests {
private String baseUrl;
@BeforeEach
public void setUp() {
void setUp() {
this.baseUrl = "http://localhost:" + System.getProperty("app.port");
this.restTemplate = new RestTemplate();
}
@Test
public void unauthenticatedUserSentToLogInPage() {
void unauthenticatedUserSentToLogInPage() {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
assertThatExceptionOfType(HttpClientErrorException.class)
.isThrownBy(() -> getForUser(this.baseUrl + "/", headers, String.class))
.satisfies((e) -> assertThat(e.getStatusCode())
.isEqualTo(HttpStatus.UNAUTHORIZED));
.satisfies((e) -> assertThat(e.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED));
}
@Test
public void authenticateWithBasicWorks() {
void authenticateWithBasicWorks() {
String auth = getAuth("user", "password");
HttpHeaders headers = getHttpHeaders();
headers.set(AUTHORIZATION, BASIC + auth);
ResponseEntity<User> entity = getForUser(this.baseUrl + "/",
headers, User.class);
ResponseEntity<User> entity = getForUser(this.baseUrl + "/", headers, User.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getHeaders().containsKey(X_AUTH_TOKEN)).isTrue();
assertThat(entity.getBody().getUsername()).isEqualTo("user");
}
@Test
public void authenticateWithXAuthTokenWorks() {
void authenticateWithXAuthTokenWorks() {
String auth = getAuth("user", "password");
HttpHeaders headers = getHttpHeaders();
headers.set(AUTHORIZATION, BASIC + auth);
ResponseEntity<User> entity = getForUser(this.baseUrl + "/",
headers, User.class);
ResponseEntity<User> entity = getForUser(this.baseUrl + "/", headers, User.class);
String token = entity.getHeaders().getFirst(X_AUTH_TOKEN);
HttpHeaders authTokenHeader = new HttpHeaders();
authTokenHeader.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
authTokenHeader.set(X_AUTH_TOKEN, token);
ResponseEntity<User> authTokenResponse = getForUser(this.baseUrl + "/",
authTokenHeader, User.class);
ResponseEntity<User> authTokenResponse = getForUser(this.baseUrl + "/", authTokenHeader, User.class);
assertThat(authTokenResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(authTokenResponse.getBody().getUsername()).isEqualTo("user");
}
@Test
public void logout() {
void logout() {
String auth = getAuth("user", "password");
HttpHeaders headers = getHttpHeaders();
headers.set(AUTHORIZATION, BASIC + auth);
ResponseEntity<User> entity = getForUser(this.baseUrl + "/",
headers, User.class);
ResponseEntity<User> entity = getForUser(this.baseUrl + "/", headers, User.class);
String token = entity.getHeaders().getFirst(X_AUTH_TOKEN);
HttpHeaders logoutHeader = getHttpHeaders();
logoutHeader.set(X_AUTH_TOKEN, token);
ResponseEntity<User> logoutResponse = getForUser(this.baseUrl + "/logout",
logoutHeader, User.class);
ResponseEntity<User> logoutResponse = getForUser(this.baseUrl + "/logout", logoutHeader, User.class);
assertThat(logoutResponse.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
}
private <T> ResponseEntity<T> getForUser(String resourceUrl, HttpHeaders headers, Class<T> type) {
return this.restTemplate.exchange(resourceUrl,
HttpMethod.GET, new HttpEntity<T>(headers), type);
return this.restTemplate.exchange(resourceUrl, HttpMethod.GET, new HttpEntity<T>(headers), type);
}
private HttpHeaders getHttpHeaders() {
@@ -126,4 +121,5 @@ public class RestTests {
String auth = user + ":" + password;
return Base64.getEncoder().encodeToString(auth.getBytes());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2017 the original author or authors.
* Copyright 2014-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,4 +30,5 @@ public class User {
public void setUsername(String username) {
this.username = username;
}
}

View File

@@ -32,8 +32,7 @@ public class EmbeddedRedisConfig {
@Bean
public GenericContainer redisContainer() {
GenericContainer redisContainer = new GenericContainer(DOCKER_IMAGE)
.withExposedPorts(6379);
GenericContainer redisContainer = new GenericContainer(DOCKER_IMAGE).withExposedPorts(6379);
redisContainer.start();
return redisContainer;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2017 the original author or authors.
* Copyright 2014-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -39,5 +39,6 @@ public class HttpSessionConfig {
public HttpSessionIdResolver httpSessionIdResolver() {
return HeaderHttpSessionIdResolver.xAuthToken(); // <3>
}
}
// end::class[]

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2018 the original author or authors.
* Copyright 2014-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -43,7 +43,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser(User.withUsername("user")
.password("{noop}password").roles("USER").build());
auth.inMemoryAuthentication()
.withUser(User.withUsername("user").password("{noop}password").roles("USER").build());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 the original author or authors.
* Copyright 2014-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -27,4 +27,5 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@EnableWebMvc
@ComponentScan
public class MvcConfig {
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 the original author or authors.
* Copyright 2014-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@ import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatche
* @author Rob Winch
*/
public class MvcInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
// tag::config[]
@Override
protected Class<?>[] getRootConfigClasses() {
@@ -41,4 +42,5 @@ public class MvcInitializer extends AbstractAnnotationConfigDispatcherServletIni
protected String[] getServletMappings() {
return new String[] { "/" };
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2017 the original author or authors.
* Copyright 2014-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -45,4 +45,5 @@ public class RestDemoController {
public void logout(HttpSession session) {
session.invalidate();
}
}