11package it .aboutbits .springboot .testing .validation .core ;
22
3+ import jakarta .validation .ConstraintViolation ;
34import jakarta .validation .ConstraintViolationException ;
45import jakarta .validation .Valid ;
56import jakarta .validation .Validation ;
67import jakarta .validation .Validator ;
78import jakarta .validation .ValidatorFactory ;
8- import jakarta .validation .groups .Default ;
99import lombok .NonNull ;
1010import lombok .SneakyThrows ;
1111import org .springframework .lang .Nullable ;
@@ -83,6 +83,12 @@ void assertValidation(AssertionParameter<P> assertionParameter) {
8383
8484 var propertiesWithRules = getPropertyNamesThatHaveRules (rules );
8585
86+ assertThatSuppliedParameterIsValid (
87+ parameterUnderTest ,
88+ functionToCallWithParameter ,
89+ validator
90+ );
91+
8692 assertThatValidationIsCompliantForEachProperty (
8793 rules ,
8894 parameterUnderTest ,
@@ -108,6 +114,52 @@ private static HashSet<String> getPropertyNamesThatHaveRules(List<Rule> rules) {
108114 return propertiesWithRules ;
109115 }
110116
117+ private static <P > void assertThatSuppliedParameterIsValid (
118+ P parameterUnderTest ,
119+ Consumer <P > functionToCallWithParameter ,
120+ Validator validator
121+ ) {
122+ if (functionToCallWithParameter != null ) {
123+ try {
124+ functionToCallWithParameter .accept (parameterUnderTest );
125+ } catch (ConstraintViolationException e ) {
126+ var violatingFieldMessages = getViolatingFieldMessages (e .getConstraintViolations ());
127+
128+ assertThat (true )
129+ .withFailMessage (
130+ "The supplied parameter violates the validation rules. The supplied parameter is not valid: %s" ,
131+ violatingFieldMessages .collect (Collectors .joining (" | " ))
132+ )
133+ .isFalse ();
134+ } catch (Exception ignored ) {
135+ // ignore any other exceptions
136+ }
137+ } else {
138+ // Use Bean Validation to validate
139+ var violations = new HashSet <ConstraintViolation <?>>(validator .validate (parameterUnderTest ));
140+
141+ var violatingFieldMessages = getViolatingFieldMessages (violations );
142+
143+ assertThat (violations )
144+ .withFailMessage (
145+ "The supplied parameter possibly contains invalid values: %s" ,
146+ violatingFieldMessages .collect (Collectors .joining (" | " ))
147+ )
148+ .isEmpty ();
149+ }
150+ }
151+
152+ private static Stream <String > getViolatingFieldMessages (Set <ConstraintViolation <?>> violations ) {
153+ return violations
154+ .stream ()
155+ .map (violation ->
156+ "%s => %s" .formatted (
157+ violation .getPropertyPath ().toString (),
158+ violation .getMessage ()
159+ )
160+ );
161+ }
162+
111163 private static <P > void assertThatValidationIsCompliantForEachProperty (
112164 List <Rule > rules ,
113165 P parameterUnderTest ,
@@ -128,32 +180,15 @@ private static <P> void assertThatValidationIsCompliantForEachProperty(
128180 } else {
129181
130182 // Use Bean Validation to validate the copy
131- var violations = validator .validate (copy , Default .class );
132-
133- // Check if there are any violations
134- var violatingFieldMessages = violations
135- .stream ()
136- .map (violation ->
137- "%s => %s" .formatted (
138- violation .getPropertyPath ().toString (),
139- violation .getMessage ()
140- )
141- );
183+ var violations = validator .validate (copy );
142184
143185 var violatingProperties = violations .stream ().map (
144186 f -> f .getPropertyPath ().toString ()
145187 ).collect (Collectors .toSet ());
146188
147- assertThat (violatingProperties )
148- .withFailMessage (
149- "More than one property failed to validate during mutation. The supplied parameter possibly contains invalid values: %s" ,
150- violatingFieldMessages .collect (Collectors .joining (" | " ))
151- )
152- .hasSizeLessThan (2 );
153-
154189 assertThat (violatingProperties )
155190 .withFailMessage ("Validation failed for property: " + rule .getProperty () + " [" + alteredValue + "]" )
156- .hasSize ( 1 );
191+ .contains ( rule . getProperty () );
157192 }
158193 });
159194 }
0 commit comments