|
| 1 | +using System.Text.RegularExpressions; |
| 2 | +using ServiceLayer.Mesh.FileTypes.NbssAppointmentEvents.Validation; |
| 3 | + |
| 4 | +namespace ServiceLayer.Mesh.Tests.FileTypes.NbssAppointmentEvents.Validation; |
| 5 | + |
| 6 | +public partial class HeaderFieldRegexValidatorTests |
| 7 | +{ |
| 8 | + private const string FieldName = "TestField"; |
| 9 | + private const string MissingCode = "ERR001"; |
| 10 | + private const string InvalidFormatCode = "ERR002"; |
| 11 | + private readonly Regex _pattern = TestRegex(); |
| 12 | + |
| 13 | + [Fact] |
| 14 | + public void Validate_NullValue_ShouldReturnMissingError() |
| 15 | + { |
| 16 | + // Arrange |
| 17 | + var file = TestDataBuilder.BuildValidParsedFile(); |
| 18 | + file.FileHeader!.ExtractId = null; |
| 19 | + |
| 20 | + var validator = new HeaderFieldRegexValidator(x => x.ExtractId, FieldName, _pattern, MissingCode, InvalidFormatCode); |
| 21 | + |
| 22 | + // Act |
| 23 | + var errors = validator.Validate(file).ToList(); |
| 24 | + |
| 25 | + // Assert |
| 26 | + errors.ShouldContainValidationError(FieldName, $"{FieldName} is missing", MissingCode, ValidationErrorScope.Header); |
| 27 | + } |
| 28 | + |
| 29 | + [Theory] |
| 30 | + [InlineData("")] |
| 31 | + [InlineData("invalid")] |
| 32 | + public void Validate_ValueNotMatchingPattern_ShouldReturnInvalidFormatError(string invalidValue) |
| 33 | + { |
| 34 | + // Arrange |
| 35 | + var file = TestDataBuilder.BuildValidParsedFile(); |
| 36 | + file.FileHeader!.ExtractId = invalidValue; |
| 37 | + |
| 38 | + var validator = new HeaderFieldRegexValidator(x => x.ExtractId, FieldName, _pattern, MissingCode, InvalidFormatCode); |
| 39 | + |
| 40 | + // Act |
| 41 | + var errors = validator.Validate(file).ToList(); |
| 42 | + |
| 43 | + // Assert |
| 44 | + errors.ShouldContainValidationError(FieldName, $"{FieldName} is in an invalid format", InvalidFormatCode,ValidationErrorScope.Header); |
| 45 | + } |
| 46 | + |
| 47 | + [Theory] |
| 48 | + [InlineData("AB12")] |
| 49 | + [InlineData("CD34")] |
| 50 | + public void Validate_ValueMatchingPattern_ShouldReturnNoErrors(string validValue) |
| 51 | + { |
| 52 | + // Arrange |
| 53 | + var file = TestDataBuilder.BuildValidParsedFile(); |
| 54 | + file.FileHeader!.ExtractId = validValue; |
| 55 | + |
| 56 | + var validator = new HeaderFieldRegexValidator(x => x.ExtractId, FieldName, _pattern, MissingCode, InvalidFormatCode); |
| 57 | + |
| 58 | + // Act |
| 59 | + var errors = validator.Validate(file).ToList(); |
| 60 | + |
| 61 | + // Assert |
| 62 | + Assert.Empty(errors); |
| 63 | + } |
| 64 | + |
| 65 | + [GeneratedRegex(@"^[A-Z]{2}\d{2}$", RegexOptions.Compiled)] |
| 66 | + private static partial Regex TestRegex(); |
| 67 | +} |
0 commit comments