Skip to content

Commit 898de9a

Browse files
committed
FINERACT-2326: Use Hibernate Validator
1 parent db5d8dd commit 898de9a

File tree

14 files changed

+95
-196
lines changed

14 files changed

+95
-196
lines changed

buildSrc/src/main/groovy/org.apache.fineract.dependencies.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ dependencyManagement {
123123
dependency 'jakarta.jms:jakarta.jms-api:3.1.0'
124124
dependency 'jakarta.ws.rs:jakarta.ws.rs-api:3.1.0'
125125
dependency 'org.glassfish.jaxb:jaxb-runtime:2.3.6' // Swagger needs exactly this version
126-
dependency 'org.apache.bval:org.apache.bval.bundle:3.0.2'
127126
dependency 'joda-time:joda-time:2.13.1'
128127

129128
dependency 'io.github.classgraph:classgraph:4.8.179'

fineract-cob/dependencies.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ dependencies {
8888

8989
// runtimeOnly dependencies are things that Fineract code has no direct compile time dependency on, but which must be present at run-time
9090
runtimeOnly(
91-
'org.apache.bval:org.apache.bval.bundle',
92-
9391
// Although fineract (at the time of writing) doesn't have any compile time dep. on httpclient,
9492
// it's useful to have this for the Spring Boot TestRestTemplate http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-rest-templates-test-utility
9593
'org.apache.httpcomponents:httpclient'

fineract-core/dependencies.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ dependencies {
8787

8888
// runtimeOnly dependencies are things that Fineract code has no direct compile time dependency on, but which must be present at run-time
8989
runtimeOnly(
90-
'org.apache.bval:org.apache.bval.bundle',
9190

9291
// Although fineract (at the time of writing) doesn't have any compile time dep. on httpclient,
9392
// it's useful to have this for the Spring Boot TestRestTemplate http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-rest-templates-test-utility

fineract-core/src/main/java/org/apache/fineract/infrastructure/core/exceptionmapper/JakartaValidationExceptionMapper.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
import org.apache.fineract.infrastructure.core.data.ApiGlobalErrorResponse;
3131
import org.apache.fineract.infrastructure.core.data.ApiParameterError;
3232
import org.apache.fineract.infrastructure.core.exception.ErrorHandler;
33-
import org.springframework.context.MessageSource;
34-
import org.springframework.context.i18n.LocaleContextHolder;
3533
import org.springframework.stereotype.Component;
3634

3735
@Slf4j
@@ -40,8 +38,6 @@
4038
@RequiredArgsConstructor
4139
public class JakartaValidationExceptionMapper implements FineractExceptionMapper, ExceptionMapper<ConstraintViolationException> {
4240

43-
private final MessageSource messageSource;
44-
4541
@Override
4642
public Response toResponse(final ConstraintViolationException exception) {
4743
log.warn("Exception occurred", ErrorHandler.findMostSpecificException(exception));
@@ -61,9 +57,7 @@ private List<ApiParameterError> getApiParameterErrors(final ConstraintViolationE
6157
final String messageTemplate = violation.getMessageTemplate();
6258
final String messageKey = messageTemplate.replace("{", "").replace("}", "");
6359

64-
final String interpolatedMessage = messageSource.getMessage(messageKey, null, LocaleContextHolder.getLocale());
65-
66-
return ApiParameterError.parameterError(messageKey, interpolatedMessage, violation.getPropertyPath().toString());
60+
return ApiParameterError.parameterError(messageKey, violation.getMessage(), violation.getPropertyPath().toString());
6761
}).toList();
6862
}
6963

fineract-e2e-tests-core/src/test/java/org/apache/fineract/test/helper/ErrorMessageHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public static String setIncorrectBusinessDateFailure() {
6464
}
6565

6666
public static String setIncorrectBusinessDateMandatoryFailure() {
67-
return "The parameter 'date' is mandatory: '${validatedValue}'.";
67+
return "The parameter 'date' is mandatory.";
6868
}
6969

7070
public static String setCurrencyEmptyValueFailure() {

fineract-provider/dependencies.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,6 @@ dependencies {
186186

187187
// runtimeOnly dependencies are things that Fineract code has no direct compile time dependency on, but which must be present at run-time
188188
runtimeOnly(
189-
'org.apache.bval:org.apache.bval.bundle',
190-
191189
// Although fineract (at the time of writing) doesn't have any compile time dep. on httpclient,
192190
// it's useful to have this for the Spring Boot TestRestTemplate http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-rest-templates-test-utility
193191
'org.apache.httpcomponents:httpclient'

fineract-provider/src/test/java/org/apache/fineract/infrastructure/businessdate/validation/BusinessDateValidationTest.java

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,69 +20,84 @@
2020

2121
import static org.assertj.core.api.Assertions.assertThat;
2222

23+
import jakarta.validation.Validation;
24+
import jakarta.validation.Validator;
2325
import lombok.extern.slf4j.Slf4j;
2426
import org.apache.fineract.infrastructure.businessdate.data.BusinessDateUpdateRequest;
2527
import org.apache.fineract.infrastructure.businessdate.domain.BusinessDateType;
26-
import org.apache.fineract.validation.config.ValidationConfig;
28+
import org.hibernate.validator.HibernateValidator;
2729
import org.junit.jupiter.api.Test;
2830
import org.springframework.beans.factory.annotation.Autowired;
31+
import org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration;
2932
import org.springframework.boot.test.context.SpringBootTest;
33+
import org.springframework.context.annotation.Bean;
34+
import org.springframework.context.annotation.Configuration;
35+
import org.springframework.context.annotation.Import;
3036
import org.springframework.test.context.ContextConfiguration;
31-
import org.springframework.validation.Validator;
3237

3338
@Slf4j
3439
@SpringBootTest
35-
@ContextConfiguration(classes = { ValidationConfig.class })
40+
@ContextConfiguration(classes = { BusinessDateValidationTest.TestConfig.class })
3641
class BusinessDateValidationTest {
3742

43+
@Configuration
44+
@Import({ MessageSourceAutoConfiguration.class })
45+
static class TestConfig {
46+
47+
@Bean
48+
public jakarta.validation.Validator validator() {
49+
return Validation.byProvider(HibernateValidator.class).configure().buildValidatorFactory().getValidator();
50+
}
51+
}
52+
3853
@Autowired
3954
private Validator validator;
4055

4156
@Test
4257
void invalidAllBlank() {
4358
var request = BusinessDateUpdateRequest.builder().dateFormat("").type(null).date(" ").locale(null).build();
4459

45-
var errors = validator.validateObject(request);
60+
var errors = validator.validate(request);
4661

47-
assertThat(errors.getFieldErrorCount()).isEqualTo(5);
62+
assertThat(errors).hasSize(6);
4863

49-
assertThat(errors.getFieldErrors()).anyMatch(e -> e.getField().equals("date"));
50-
assertThat(errors.getFieldErrors()).anyMatch(e -> e.getField().equals("dateFormat"));
51-
assertThat(errors.getFieldErrors()).anyMatch(e -> e.getField().equals("locale"));
52-
assertThat(errors.getFieldErrors()).anyMatch(e -> e.getField().equals("type"));
64+
assertThat(errors).anyMatch(e -> e.getPropertyPath().toString().equals("date"));
65+
assertThat(errors).anyMatch(e -> e.getPropertyPath().toString().equals("dateFormat"));
66+
assertThat(errors).anyMatch(e -> e.getPropertyPath().toString().equals("locale"));
67+
assertThat(errors).anyMatch(e -> e.getPropertyPath().toString().equals("type"));
5368
}
5469

5570
@Test
5671
void invalidLocale() {
5772
var request = BusinessDateUpdateRequest.builder().dateFormat("dd-MM-yyyy").type(BusinessDateType.BUSINESS_DATE).date("12-05-2025")
5873
.locale("INVALID").build();
5974

60-
var errors = validator.validateObject(request);
75+
var errors = validator.validate(request);
6176

62-
assertThat(errors.getFieldErrorCount()).isEqualTo(1);
77+
assertThat(errors).hasSize(1);
6378

64-
assertThat(errors.getFieldErrors()).anyMatch(e -> e.getField().equals("locale"));
79+
assertThat(errors).anyMatch(e -> e.getPropertyPath().toString().equals("locale"));
6580
}
6681

6782
@Test
6883
void invalidDateFormat() {
6984
var request = BusinessDateUpdateRequest.builder().dateFormat("dd/MM/yyyy").type(BusinessDateType.BUSINESS_DATE).date("12-05-2025")
7085
.locale("en").build();
7186

72-
var errors = validator.validateObject(request);
87+
var errors = validator.validate(request);
7388

74-
assertThat(errors.getErrorCount()).isEqualTo(1);
89+
assertThat(errors).hasSize(1);
7590

76-
assertThat(errors.getAllErrors()).anyMatch(e -> "Wrong local date fields.".equals(e.getDefaultMessage()));
91+
assertThat(errors).anyMatch(e -> "Wrong local date fields.".equals(e.getMessage()));
7792
}
7893

7994
@Test
8095
void valid() {
8196
var request = BusinessDateUpdateRequest.builder().dateFormat("dd-MM-yyyy").type(BusinessDateType.BUSINESS_DATE).date("12-05-2025")
8297
.locale("en").build();
8398

84-
var errors = validator.validateObject(request);
99+
var errors = validator.validate(request);
85100

86-
assertThat(errors.getFieldErrorCount()).isEqualTo(0);
101+
assertThat(errors).hasSize(0);
87102
}
88103
}

fineract-validation/src/main/java/org/apache/fineract/validation/config/ValidationConfig.java

Lines changed: 0 additions & 91 deletions
This file was deleted.

fineract-validation/src/main/java/org/apache/fineract/validation/constraints/LocalDateValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import org.apache.commons.lang3.StringUtils;
3131

3232
@Slf4j
33-
class LocalDateValidator implements ConstraintValidator<LocalDate, Object> {
33+
public class LocalDateValidator implements ConstraintValidator<LocalDate, Object> {
3434

3535
private String dateField;
3636
private String formatField;

fineract-validation/src/main/java/org/apache/fineract/validation/constraints/LocaleValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import java.util.Arrays;
2424
import org.apache.commons.lang3.StringUtils;
2525

26-
class LocaleValidator implements ConstraintValidator<Locale, String> {
26+
public class LocaleValidator implements ConstraintValidator<Locale, String> {
2727

2828
@Override
2929
public boolean isValid(String value, ConstraintValidatorContext context) {

0 commit comments

Comments
 (0)