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