|
| 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