diff --git a/CHANGELOG.md b/CHANGELOG.md index ab49d84..bf39051 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## 3.7.0 +### Added +- `UftpValidator#validate` returns a `ValidationResult`, so a validator can return a rejection reason with details. The default implementation uses `isValid` and `getReason`, so existing validators keep working. + ## 3.5.0 ### Changed - #214 Trim attribute values diff --git a/core/src/main/java/org/lfenergy/shapeshifter/core/service/validation/UftpUserDefinedValidator.java b/core/src/main/java/org/lfenergy/shapeshifter/core/service/validation/UftpUserDefinedValidator.java index 4ce6951..2aa5645 100644 --- a/core/src/main/java/org/lfenergy/shapeshifter/core/service/validation/UftpUserDefinedValidator.java +++ b/core/src/main/java/org/lfenergy/shapeshifter/core/service/validation/UftpUserDefinedValidator.java @@ -14,13 +14,13 @@ * public class MyCustomValidator implements UftpUserDefinedValidator<FlexRequest> { * * @Override - * public boolean appliesTo(Class<? extends FlexRequest> clazz) { + * public boolean appliesTo(Class<? extends PayloadMessageType> clazz) { * // here you can filter on the message type that you want to validate on * return clazz.equals(FlexRequest.class); * } * * @Override - * public boolean valid(UftpParticipant sender, FlexRequest flexRequest) { + * public boolean isValid(UftpMessage<FlexRequest> uftpMessage) { * // implement your validation logic here; return true if valid, false otherwise * } * @@ -32,6 +32,17 @@ * } * * + *

To return a rejection reason with details, override {@link UftpValidator#validate} instead:

+ * + *
+ *
+ *   @Override
+ *   public ValidationResult validate(UftpMessage<FlexRequest> uftpMessage) {
+ *     // return ValidationResult.ok() if valid
+ *     return ValidationResult.rejection("My custom validation failed: ISP 12 exceeds contract capacity");
+ *   }
+ * 
+ * * @param The type of UFTP payload message that can be validated by this class */ public interface UftpUserDefinedValidator extends UftpValidator { diff --git a/core/src/main/java/org/lfenergy/shapeshifter/core/service/validation/UftpValidationService.java b/core/src/main/java/org/lfenergy/shapeshifter/core/service/validation/UftpValidationService.java index 7fbf841..934575a 100644 --- a/core/src/main/java/org/lfenergy/shapeshifter/core/service/validation/UftpValidationService.java +++ b/core/src/main/java/org/lfenergy/shapeshifter/core/service/validation/UftpValidationService.java @@ -47,8 +47,8 @@ public ValidationResult validate(UftpMessage uftpM @SuppressWarnings("unchecked") // There is an unchecked cast to type T. However, this has already been verified by the appliesTo method of the validator. private ValidationResult validate(UftpValidator validator, UftpMessage uftpMessage) { - if (validator.appliesTo(uftpMessage.payloadMessage().getClass()) && !validator.isValid((UftpMessage) uftpMessage)) { - return ValidationResult.rejection(validator.getReason()); + if (validator.appliesTo(uftpMessage.payloadMessage().getClass())) { + return validator.validate((UftpMessage) uftpMessage); } return ValidationResult.ok(); } diff --git a/core/src/main/java/org/lfenergy/shapeshifter/core/service/validation/UftpValidator.java b/core/src/main/java/org/lfenergy/shapeshifter/core/service/validation/UftpValidator.java index 92d1ea5..bcdfcf7 100644 --- a/core/src/main/java/org/lfenergy/shapeshifter/core/service/validation/UftpValidator.java +++ b/core/src/main/java/org/lfenergy/shapeshifter/core/service/validation/UftpValidator.java @@ -6,6 +6,7 @@ import org.lfenergy.shapeshifter.api.PayloadMessageType; import org.lfenergy.shapeshifter.core.model.UftpMessage; +import org.lfenergy.shapeshifter.core.service.validation.model.ValidationResult; /** * Validator that validates incoming (and outgoing) UFTP payload messages. @@ -19,6 +20,17 @@ */ public interface UftpValidator { + /** + * Validates a UFTP message with the specified payload message type and returns a {@link ValidationResult} object that + * indicates whether the validation was successful or not. + * + * @param uftpMessage The UFTP message to validate. + * @return A {@link ValidationResult} object indicating the result of the validation. + */ + default ValidationResult validate(UftpMessage uftpMessage) { + return isValid(uftpMessage) ? ValidationResult.ok() : ValidationResult.rejection(getReason()); + } + /** * Checks if this validator can validate the specified UFTP payload message type. *

This is needed because the generic type parameter at class level is not available at runtime.

diff --git a/core/src/test/java/org/lfenergy/shapeshifter/core/service/validation/UftpValidationServiceTest.java b/core/src/test/java/org/lfenergy/shapeshifter/core/service/validation/UftpValidationServiceTest.java index 7a6244f..00de62d 100644 --- a/core/src/test/java/org/lfenergy/shapeshifter/core/service/validation/UftpValidationServiceTest.java +++ b/core/src/test/java/org/lfenergy/shapeshifter/core/service/validation/UftpValidationServiceTest.java @@ -17,6 +17,7 @@ import org.lfenergy.shapeshifter.api.FlexOfferResponse; import org.lfenergy.shapeshifter.core.model.UftpMessageFixture; import org.lfenergy.shapeshifter.core.model.UftpParticipant; +import org.lfenergy.shapeshifter.core.service.validation.model.ValidationResult; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @@ -90,11 +91,11 @@ void validate_multiple_apply_all_ok() { var uftpMessage = UftpMessageFixture.createOutgoing(sender, flexOffer); - given(baseValidation1.isValid(uftpMessage)).willReturn(true); - given(baseValidation2.isValid(uftpMessage)).willReturn(true); - given(messageValidation1.isValid(uftpMessage)).willReturn(true); - given(messageValidation2.isValid(uftpMessage)).willReturn(true); - given(userDefinedValidation1.isValid(uftpMessage)).willReturn(true); + given(baseValidation1.validate(uftpMessage)).willReturn(ValidationResult.ok()); + given(baseValidation2.validate(uftpMessage)).willReturn(ValidationResult.ok()); + given(messageValidation1.validate(uftpMessage)).willReturn(ValidationResult.ok()); + given(messageValidation2.validate(uftpMessage)).willReturn(ValidationResult.ok()); + given(userDefinedValidation1.validate(uftpMessage)).willReturn(ValidationResult.ok()); var result = testSubject.validate(uftpMessage); @@ -113,7 +114,7 @@ void validate_one_applies_is_ok() { var uftpMessage = UftpMessageFixture.createOutgoing(sender, flexOfferResponse); - given(userDefinedValidation2.isValid(uftpMessage)).willReturn(true); + given(userDefinedValidation2.validate(uftpMessage)).willReturn(ValidationResult.ok()); var result = testSubject.validate(uftpMessage); @@ -142,8 +143,7 @@ void validate_multiple_apply_first_fails() { var uftpMessage = UftpMessageFixture.createOutgoing(sender, flexOfferResponse); - given(userDefinedValidation2.isValid(uftpMessage)).willReturn(false); - given(userDefinedValidation2.getReason()).willReturn("userDefinedValidation2"); + given(userDefinedValidation2.validate(uftpMessage)).willReturn(ValidationResult.rejection("userDefinedValidation2")); var result = testSubject.validate(uftpMessage); @@ -160,10 +160,9 @@ void validate_multiple_apply_third_fails() { var uftpMessage = UftpMessageFixture.createOutgoing(sender, flexOffer); - given(baseValidation1.isValid(uftpMessage)).willReturn(true); - given(baseValidation2.isValid(uftpMessage)).willReturn(true); - given(messageValidation1.isValid(uftpMessage)).willReturn(false); - given(messageValidation1.getReason()).willReturn("messageValidation1"); + given(baseValidation1.validate(uftpMessage)).willReturn(ValidationResult.ok()); + given(baseValidation2.validate(uftpMessage)).willReturn(ValidationResult.ok()); + given(messageValidation1.validate(uftpMessage)).willReturn(ValidationResult.rejection("messageValidation1")); var result = testSubject.validate(uftpMessage); @@ -182,12 +181,11 @@ void validate_multiple_apply_fifth_fails() { var uftpMessage = UftpMessageFixture.createOutgoing(sender, flexOffer); - given(baseValidation1.isValid(uftpMessage)).willReturn(true); - given(baseValidation2.isValid(uftpMessage)).willReturn(true); - given(messageValidation1.isValid(uftpMessage)).willReturn(true); - given(messageValidation2.isValid(uftpMessage)).willReturn(true); - given(userDefinedValidation1.isValid(uftpMessage)).willReturn(false); - given(userDefinedValidation1.getReason()).willReturn("userDefinedValidation1"); + given(baseValidation1.validate(uftpMessage)).willReturn(ValidationResult.ok()); + given(baseValidation2.validate(uftpMessage)).willReturn(ValidationResult.ok()); + given(messageValidation1.validate(uftpMessage)).willReturn(ValidationResult.ok()); + given(messageValidation2.validate(uftpMessage)).willReturn(ValidationResult.ok()); + given(userDefinedValidation1.validate(uftpMessage)).willReturn(ValidationResult.rejection("userDefinedValidation1")); var result = testSubject.validate(uftpMessage); @@ -201,8 +199,7 @@ void validate_one_applies_fails() { var uftpMessage = UftpMessageFixture.createOutgoing(sender, flexOfferResponse); - given(userDefinedValidation2.isValid(uftpMessage)).willReturn(false); - given(userDefinedValidation2.getReason()).willReturn("userDefinedValidation2"); + given(userDefinedValidation2.validate(uftpMessage)).willReturn(ValidationResult.rejection("userDefinedValidation2")); var result = testSubject.validate(uftpMessage); diff --git a/core/src/test/java/org/lfenergy/shapeshifter/core/service/validation/UftpValidatorTest.java b/core/src/test/java/org/lfenergy/shapeshifter/core/service/validation/UftpValidatorTest.java new file mode 100644 index 0000000..b0a7b3d --- /dev/null +++ b/core/src/test/java/org/lfenergy/shapeshifter/core/service/validation/UftpValidatorTest.java @@ -0,0 +1,87 @@ +package org.lfenergy.shapeshifter.core.service.validation; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.lfenergy.shapeshifter.api.FlexOffer; +import org.lfenergy.shapeshifter.api.USEFRoleType; +import org.lfenergy.shapeshifter.core.model.UftpMessage; +import org.lfenergy.shapeshifter.core.model.UftpParticipant; +import org.lfenergy.shapeshifter.core.service.validation.model.ValidationResult; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class UftpValidatorTest { + + private static final String REJECTION_REASON = "Invalid message"; + + @Mock + private UftpValidator validator; + + private final UftpMessage uftpMessage = UftpMessage.createOutgoing( + new UftpParticipant("sender", USEFRoleType.DSO), + new FlexOffer() + ); + + @BeforeEach + void useDefaultValidateImplementation() { + doCallRealMethod().when(validator).validate(uftpMessage); + } + + @Test + void validate_validMessage_returnsOk() { + given(validator.isValid(uftpMessage)).willReturn(true); + + var result = validator.validate(uftpMessage); + + assertThat(result).isEqualTo(ValidationResult.ok()); + verify(validator).isValid(uftpMessage); + verify(validator, never()).getReason(); + } + + @Test + void validate_invalidMessage_returnsRejectionWithReason() { + given(validator.isValid(uftpMessage)).willReturn(false); + given(validator.getReason()).willReturn(REJECTION_REASON); + + var result = validator.validate(uftpMessage); + + assertThat(result).isEqualTo(ValidationResult.rejection(REJECTION_REASON)); + verify(validator).isValid(uftpMessage); + verify(validator).getReason(); + } + + @Test + void validate_invalidMessageWithBlankReason_throwsIllegalArgumentException() { + given(validator.isValid(uftpMessage)).willReturn(false); + given(validator.getReason()).willReturn(" "); + + assertThatThrownBy(() -> validator.validate(uftpMessage)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Rejection reason cannot be blank."); + + verify(validator).isValid(uftpMessage); + verify(validator).getReason(); + } + + @Test + void validate_invalidMessageWithNullReason_throwsIllegalArgumentException() { + given(validator.isValid(uftpMessage)).willReturn(false); + given(validator.getReason()).willReturn(null); + + assertThatThrownBy(() -> validator.validate(uftpMessage)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Rejection reason cannot be blank."); + + verify(validator).isValid(uftpMessage); + verify(validator).getReason(); + } +}