Skip to content

Commit d9f7548

Browse files
feat: flamingock test support DefaultExceptionValidator (#764)
1 parent 6725349 commit d9f7548

File tree

3 files changed

+114
-10
lines changed

3 files changed

+114
-10
lines changed

core/flamingock-test-support/src/main/java/io/flamingock/support/validation/impl/AuditSequenceStrictValidator.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,15 @@ public AuditSequenceStrictValidator(AuditReader auditReader, AuditEntryDefinitio
7070
public ValidationResult validate() {
7171
List<ValidationError> allErrors = new ArrayList<>();
7272

73-
int expectedSize = expectations.size();
74-
int actualSize = actualEntries.size();
75-
76-
if (expectedSize != actualSize) {
73+
if (expectations.size() != actualEntries.size()) {
7774
allErrors.add(new CountMismatchError(getExpectedChangeIds(), getActualChangeIds()));
7875
}
7976

8077
allErrors.addAll(getValidationErrors(expectations, actualEntries));
8178

82-
if (allErrors.isEmpty()) {
83-
return ValidationResult.success(VALIDATOR_NAME);
84-
}
85-
86-
return ValidationResult.failure(VALIDATOR_NAME, allErrors.toArray(new ValidationError[0]));
79+
return allErrors.isEmpty()
80+
? ValidationResult.success(VALIDATOR_NAME)
81+
: ValidationResult.failure(VALIDATOR_NAME, allErrors.toArray(new ValidationError[0]));
8782
}
8883

8984
private static List<ValidationError> getValidationErrors(List<AuditEntryExpectation> expectedEntries, List<AuditEntry> actualEntries) {

core/flamingock-test-support/src/main/java/io/flamingock/support/validation/impl/DefaultExceptionValidator.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package io.flamingock.support.validation.impl;
1717

1818
import io.flamingock.support.validation.ExceptionValidator;
19+
import io.flamingock.support.validation.error.ExceptionNotExpectedError;
20+
import io.flamingock.support.validation.error.ExceptionTypeMismatchError;
1921
import io.flamingock.support.validation.error.ValidationResult;
2022

2123
import java.util.function.Consumer;
@@ -40,7 +42,30 @@ public void setActualException(Throwable actualException) {
4042

4143
@Override
4244
public ValidationResult validate(Throwable actualException) {
43-
// TODO: Implement actual validation logic
45+
// No exception expected
46+
if (expectedExceptionClass == null) {
47+
if (actualException == null) {
48+
return ValidationResult.success(VALIDATOR_NAME);
49+
} else {
50+
return ValidationResult.failure(VALIDATOR_NAME, new ExceptionNotExpectedError(actualException));
51+
}
52+
}
53+
54+
// An exception is expected but none was thrown
55+
if (actualException == null) {
56+
return ValidationResult.failure(VALIDATOR_NAME,
57+
new ExceptionTypeMismatchError(expectedExceptionClass, null));
58+
}
59+
60+
// Type mismatch
61+
if (!expectedExceptionClass.isInstance(actualException)) {
62+
return ValidationResult.failure(VALIDATOR_NAME,
63+
new ExceptionTypeMismatchError(expectedExceptionClass, actualException.getClass()));
64+
}
65+
66+
if (expectedExceptionConsumer != null) {
67+
expectedExceptionConsumer.accept(actualException);
68+
}
4469
return ValidationResult.success(VALIDATOR_NAME);
4570
}
4671
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2025 Flamingock (https://www.flamingock.io)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.flamingock.support.validation.impl;
17+
18+
import io.flamingock.support.validation.error.ExceptionNotExpectedError;
19+
import io.flamingock.support.validation.error.ExceptionTypeMismatchError;
20+
import io.flamingock.support.validation.error.ValidationResult;
21+
import org.junit.jupiter.api.DisplayName;
22+
import org.junit.jupiter.api.Test;
23+
24+
import java.io.IOException;
25+
import java.util.concurrent.atomic.AtomicBoolean;
26+
27+
import static org.junit.jupiter.api.Assertions.*;
28+
29+
class DefaultExceptionValidatorTest {
30+
31+
@Test
32+
@DisplayName("DefaultExceptionValidatorTest: No exception expected and none thrown — should succeed")
33+
void noExceptionExpected_andNoneThrown_shouldSucceed() {
34+
DefaultExceptionValidator validator = new DefaultExceptionValidator(null, null);
35+
ValidationResult result = validator.validate(null);
36+
assertTrue(result.isSuccess());
37+
assertFalse(result.hasErrors());
38+
}
39+
40+
@Test
41+
@DisplayName("DefaultExceptionValidatorTest: No exception expected but an exception thrown — should fail with ExceptionNotExpectedError")
42+
void noExceptionExpected_butExceptionThrown_shouldFailWithNotExpected() {
43+
DefaultExceptionValidator validator = new DefaultExceptionValidator(null, null);
44+
RuntimeException ex = new RuntimeException("message");
45+
ValidationResult result = validator.validate(ex);
46+
assertTrue(result.hasErrors());
47+
assertFalse(result.isSuccess());
48+
assertEquals(1, result.getErrors().size());
49+
assertInstanceOf(ExceptionNotExpectedError.class, result.getErrors().get(0));
50+
}
51+
52+
@Test
53+
@DisplayName("DefaultExceptionValidatorTest: Exception expected but none thrown — should fail with ExceptionTypeMismatchError (null actual)")
54+
void exceptionExpected_butNoneThrown_shouldFailWithTypeMismatch_nullActual() {
55+
DefaultExceptionValidator validator = new DefaultExceptionValidator(IOException.class, null);
56+
ValidationResult result = validator.validate(null);
57+
assertTrue(result.hasErrors());
58+
assertEquals(1, result.getErrors().size());
59+
assertInstanceOf(ExceptionTypeMismatchError.class, result.getErrors().get(0));
60+
}
61+
62+
@Test
63+
@DisplayName("DefaultExceptionValidatorTest: Exception expected but wrong type thrown — should fail with ExceptionTypeMismatchError")
64+
void exceptionExpected_butWrongTypeThrown_shouldFailWithTypeMismatch() {
65+
DefaultExceptionValidator validator = new DefaultExceptionValidator(IllegalArgumentException.class, null);
66+
RuntimeException ex = new RuntimeException("message");
67+
ValidationResult result = validator.validate(ex);
68+
assertTrue(result.hasErrors());
69+
assertEquals(1, result.getErrors().size());
70+
assertInstanceOf(ExceptionTypeMismatchError.class, result.getErrors().get(0));
71+
}
72+
73+
@Test
74+
@DisplayName("DefaultExceptionValidatorTest: Exception expected and correct type thrown — should succeed and invoke consumer")
75+
void exceptionExpected_andCorrectTypeThrown_shouldSucceed_andInvokeConsumer() {
76+
AtomicBoolean consumed = new AtomicBoolean(false);
77+
DefaultExceptionValidator validator = new DefaultExceptionValidator(RuntimeException.class, e -> consumed.set(true));
78+
RuntimeException ex = new RuntimeException("message");
79+
ValidationResult result = validator.validate(ex);
80+
assertTrue(result.isSuccess());
81+
assertFalse(result.hasErrors());
82+
assertTrue(consumed.get());
83+
}
84+
}

0 commit comments

Comments
 (0)