Skip to content

Commit 1b4541a

Browse files
committed
feat: flamingock test support DefaultExceptionValidator
1 parent 4d89588 commit 1b4541a

File tree

2 files changed

+110
-16
lines changed

2 files changed

+110
-16
lines changed

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

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
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-
*/
161
package io.flamingock.support.validation.impl;
172

183
import io.flamingock.support.validation.ExceptionValidator;
4+
import io.flamingock.support.validation.error.ExceptionNotExpectedError;
5+
import io.flamingock.support.validation.error.ExceptionTypeMismatchError;
196
import io.flamingock.support.validation.error.ValidationResult;
207

218
import java.util.function.Consumer;
@@ -40,7 +27,30 @@ public void setActualException(Throwable actualException) {
4027

4128
@Override
4229
public ValidationResult validate(Throwable actualException) {
43-
// TODO: Implement actual validation logic
30+
// No exception expected
31+
if (expectedExceptionClass == null) {
32+
if (actualException == null) {
33+
return ValidationResult.success(VALIDATOR_NAME);
34+
} else {
35+
return ValidationResult.failure(VALIDATOR_NAME, new ExceptionNotExpectedError(actualException));
36+
}
37+
}
38+
39+
// An exception is expected but none was thrown
40+
if (actualException == null) {
41+
return ValidationResult.failure(VALIDATOR_NAME,
42+
new ExceptionTypeMismatchError(expectedExceptionClass, null));
43+
}
44+
45+
// Type mismatch
46+
if (!expectedExceptionClass.isInstance(actualException)) {
47+
return ValidationResult.failure(VALIDATOR_NAME,
48+
new ExceptionTypeMismatchError(expectedExceptionClass, actualException.getClass()));
49+
}
50+
51+
if (expectedExceptionConsumer != null) {
52+
expectedExceptionConsumer.accept(actualException);
53+
}
4454
return ValidationResult.success(VALIDATOR_NAME);
4555
}
4656
}
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)