Skip to content

Commit 0dbc8cf

Browse files
Made Validation errors unit testable
1 parent ec6d134 commit 0dbc8cf

File tree

4 files changed

+33
-29
lines changed

4 files changed

+33
-29
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<parent>
1616
<groupId>org.springframework.boot</groupId>
1717
<artifactId>spring-boot-starter-parent</artifactId>
18-
<version>2.3.0.RELEASE</version>
18+
<version>2.3.2.RELEASE</version>
1919
<relativePath /> <!-- lookup parent from repository -->
2020
</parent>
2121

spring-lemon-commons-web/src/main/java/com/naturalprogrammer/spring/lemon/commonsweb/security/LemonCommonsWebTokenAuthenticationFilter.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.naturalprogrammer.spring.lemon.exceptions.util.LexUtils;
88
import com.nimbusds.jwt.JWTClaimsSet;
99
import lombok.AllArgsConstructor;
10+
import lombok.extern.slf4j.Slf4j;
1011
import org.apache.commons.logging.Log;
1112
import org.apache.commons.logging.LogFactory;
1213
import org.springframework.http.HttpHeaders;
@@ -26,11 +27,10 @@
2627
* Filter for token authentication
2728
*/
2829
@AllArgsConstructor
30+
@Slf4j
2931
public class LemonCommonsWebTokenAuthenticationFilter extends OncePerRequestFilter {
3032

31-
private static final Log log = LogFactory.getLog(LemonCommonsWebTokenAuthenticationFilter.class);
32-
33-
private BlueTokenService blueTokenService;
33+
private final BlueTokenService blueTokenService;
3434

3535
@Override
3636
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
@@ -54,7 +54,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
5454

5555
} catch (Exception e) {
5656

57-
log.debug("Token authentication failed - " + e.getMessage());
57+
log.debug("Token authentication failed - {}", e.getMessage());
5858

5959
response.sendError(HttpServletResponse.SC_UNAUTHORIZED,
6060
"Authentication Failed: " + e.getMessage());
@@ -83,9 +83,6 @@ protected Authentication createAuthToken(String token) {
8383

8484
/**
8585
* Default behaviour is to throw error. To be overridden in auth service.
86-
*
87-
* @param username
88-
* @return
8986
*/
9087
protected UserDto fetchUserDto(JWTClaimsSet claims) {
9188
throw new AuthenticationCredentialsNotFoundException(

spring-lemon-exceptions/src/main/java/com/naturalprogrammer/spring/lemon/exceptions/LemonExceptionsAutoConfiguration.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.naturalprogrammer.spring.lemon.exceptions.handlers.AbstractExceptionHandler;
44
import com.naturalprogrammer.spring.lemon.exceptions.util.LexUtils;
5+
import lombok.extern.slf4j.Slf4j;
56
import org.apache.commons.logging.Log;
67
import org.apache.commons.logging.LogFactory;
78
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
@@ -18,10 +19,9 @@
1819
@Configuration
1920
@AutoConfigureBefore({ValidationAutoConfiguration.class})
2021
@ComponentScan(basePackageClasses=AbstractExceptionHandler.class)
22+
@Slf4j
2123
public class LemonExceptionsAutoConfiguration {
2224

23-
private static final Log log = LogFactory.getLog(LemonExceptionsAutoConfiguration.class);
24-
2525
public LemonExceptionsAutoConfiguration() {
2626
log.info("Created");
2727
}
@@ -48,13 +48,7 @@ ErrorResponseComposer<T> errorResponseComposer(List<AbstractExceptionHandler<T>>
4848
public ExceptionIdMaker exceptionIdMaker() {
4949

5050
log.info("Configuring ExceptionIdMaker");
51-
return ex -> {
52-
53-
if (ex == null)
54-
return null;
55-
56-
return ex.getClass().getSimpleName();
57-
};
51+
return LexUtils.EXCEPTION_ID_MAKER;
5852
}
5953

6054

spring-lemon-exceptions/src/main/java/com/naturalprogrammer/spring/lemon/exceptions/util/LexUtils.java

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,40 @@
22

33
import com.naturalprogrammer.spring.lemon.exceptions.ExceptionIdMaker;
44
import com.naturalprogrammer.spring.lemon.exceptions.MultiErrorException;
5-
import org.apache.commons.logging.Log;
6-
import org.apache.commons.logging.LogFactory;
5+
import lombok.extern.slf4j.Slf4j;
76
import org.springframework.context.MessageSource;
87
import org.springframework.context.i18n.LocaleContextHolder;
98
import org.springframework.http.HttpStatus;
109
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
1110

1211
import javax.annotation.PostConstruct;
1312
import javax.validation.ConstraintViolationException;
13+
import javax.validation.Validation;
14+
import javax.validation.Validator;
1415
import java.util.function.Supplier;
1516

1617
/**
1718
* Useful helper methods
1819
*
1920
* @author Sanjay Patel
2021
*/
22+
@Slf4j
2123
public class LexUtils {
22-
23-
private static final Log log = LogFactory.getLog(LexUtils.class);
2424

2525
private static MessageSource messageSource;
2626
private static LocalValidatorFactoryBean validator;
2727
private static ExceptionIdMaker exceptionIdMaker;
28-
28+
29+
private static final Validator DEFAULT_VALIDATOR = Validation.buildDefaultValidatorFactory().getValidator();
30+
public static final ExceptionIdMaker EXCEPTION_ID_MAKER = ex -> {
31+
32+
if (ex == null)
33+
return null;
34+
35+
return ex.getClass().getSimpleName();
36+
};
37+
38+
2939
public static final MultiErrorException NOT_FOUND_EXCEPTION = new MultiErrorException();
3040

3141
/**
@@ -59,8 +69,8 @@ public void postConstruct() {
5969
*/
6070
public static String getMessage(String messageKey, Object... args) {
6171

62-
if (messageSource == null)
63-
return "ApplicationContext unavailable, probably unit test going on";
72+
if (messageSource == null) // ApplicationContext unavailable, probably unit test going on
73+
return messageKey;
6474

6575
// http://stackoverflow.com/questions/10792551/how-to-obtain-a-current-user-locale-from-spring-without-passing-it-as-a-paramete
6676
return messageSource.getMessage(messageKey, args,
@@ -105,7 +115,7 @@ public static <T> MultiErrorException validateBean(String beanName, T bean, Clas
105115
*/
106116
public static <T> void ensureFound(T entity) {
107117

108-
LexUtils.validate(entity != null,
118+
validate(entity != null,
109119
"com.naturalprogrammer.spring.notFound")
110120
.httpStatus(HttpStatus.NOT_FOUND).go();
111121
}
@@ -123,22 +133,25 @@ public static Supplier<MultiErrorException> notFoundSupplier() {
123133
public static String getExceptionId(Throwable ex) {
124134

125135
Throwable root = getRootException(ex);
136+
137+
if (exceptionIdMaker == null) // in unit tests
138+
return EXCEPTION_ID_MAKER.make(ex);
139+
126140
return exceptionIdMaker.make(root);
127141
}
128142

129143

130144
private static Throwable getRootException(Throwable ex) {
131145

132-
if (ex == null) return null;
133-
134146
while(ex.getCause() != null)
135147
ex = ex.getCause();
136148

137149
return ex;
138150
}
139151

140152

141-
public static LocalValidatorFactoryBean validator() {
142-
return validator;
153+
public static Validator validator() {
154+
return validator == null ? // e.g. in unit tests
155+
DEFAULT_VALIDATOR : validator;
143156
}
144157
}

0 commit comments

Comments
 (0)