|
| 1 | +package org.lfenergy.shapeshifter.core.service.validation; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 5 | +import static org.mockito.BDDMockito.given; |
| 6 | +import static org.mockito.Mockito.doCallRealMethod; |
| 7 | +import static org.mockito.Mockito.never; |
| 8 | +import static org.mockito.Mockito.verify; |
| 9 | + |
| 10 | +import org.junit.jupiter.api.BeforeEach; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 13 | +import org.lfenergy.shapeshifter.api.FlexOffer; |
| 14 | +import org.lfenergy.shapeshifter.api.USEFRoleType; |
| 15 | +import org.lfenergy.shapeshifter.core.model.UftpMessage; |
| 16 | +import org.lfenergy.shapeshifter.core.model.UftpParticipant; |
| 17 | +import org.lfenergy.shapeshifter.core.service.validation.model.ValidationResult; |
| 18 | +import org.mockito.Mock; |
| 19 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 20 | + |
| 21 | +@ExtendWith(MockitoExtension.class) |
| 22 | +class UftpValidatorTest { |
| 23 | + |
| 24 | + private static final String REJECTION_REASON = "Invalid message"; |
| 25 | + |
| 26 | + @Mock |
| 27 | + private UftpValidator<FlexOffer> validator; |
| 28 | + |
| 29 | + private final UftpMessage<FlexOffer> uftpMessage = UftpMessage.createOutgoing( |
| 30 | + new UftpParticipant("sender", USEFRoleType.DSO), |
| 31 | + new FlexOffer() |
| 32 | + ); |
| 33 | + |
| 34 | + @BeforeEach |
| 35 | + void useDefaultValidateImplementation() { |
| 36 | + doCallRealMethod().when(validator).validate(uftpMessage); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void validate_validMessage_returnsOk() { |
| 41 | + given(validator.isValid(uftpMessage)).willReturn(true); |
| 42 | + |
| 43 | + var result = validator.validate(uftpMessage); |
| 44 | + |
| 45 | + assertThat(result).isEqualTo(ValidationResult.ok()); |
| 46 | + verify(validator).isValid(uftpMessage); |
| 47 | + verify(validator, never()).getReason(); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void validate_invalidMessage_returnsRejectionWithReason() { |
| 52 | + given(validator.isValid(uftpMessage)).willReturn(false); |
| 53 | + given(validator.getReason()).willReturn(REJECTION_REASON); |
| 54 | + |
| 55 | + var result = validator.validate(uftpMessage); |
| 56 | + |
| 57 | + assertThat(result).isEqualTo(ValidationResult.rejection(REJECTION_REASON)); |
| 58 | + verify(validator).isValid(uftpMessage); |
| 59 | + verify(validator).getReason(); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void validate_invalidMessageWithBlankReason_throwsIllegalArgumentException() { |
| 64 | + given(validator.isValid(uftpMessage)).willReturn(false); |
| 65 | + given(validator.getReason()).willReturn(" "); |
| 66 | + |
| 67 | + assertThatThrownBy(() -> validator.validate(uftpMessage)) |
| 68 | + .isInstanceOf(IllegalArgumentException.class) |
| 69 | + .hasMessage("Rejection reason cannot be blank."); |
| 70 | + |
| 71 | + verify(validator).isValid(uftpMessage); |
| 72 | + verify(validator).getReason(); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void validate_invalidMessageWithNullReason_throwsIllegalArgumentException() { |
| 77 | + given(validator.isValid(uftpMessage)).willReturn(false); |
| 78 | + given(validator.getReason()).willReturn(null); |
| 79 | + |
| 80 | + assertThatThrownBy(() -> validator.validate(uftpMessage)) |
| 81 | + .isInstanceOf(IllegalArgumentException.class) |
| 82 | + .hasMessage("Rejection reason cannot be blank."); |
| 83 | + |
| 84 | + verify(validator).isValid(uftpMessage); |
| 85 | + verify(validator).getReason(); |
| 86 | + } |
| 87 | +} |
0 commit comments