-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding validation and simple logging
- Loading branch information
Iestyn Jones
committed
May 10, 2023
1 parent
cf12697
commit e1d0126
Showing
17 changed files
with
374 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/Tests/UL.Core.Tests/Validators/FizzBuzzRequestValidatorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
// <copyright file="FizzBuzzRequestValidatorTests.cs" company="Bugail Consulting Ltd"> | ||
// Copyright 2023 (c) Bugail Consulting Ltd. All rights reserved. | ||
// </copyright> | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
|
||
namespace UL.Core.Tests.Validators | ||
{ | ||
using Bogus; | ||
using FluentValidation.TestHelper; | ||
using NUnit.Framework; | ||
using UL.Core.Requests; | ||
using UL.Core.Validators; | ||
|
||
public class FizzBuzzRequestValidatorTests | ||
{ | ||
private FizzBuzzRequestValidator validator; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
validator = new FizzBuzzRequestValidator(); | ||
} | ||
|
||
[TestCase("")] | ||
[TestCase(null)] | ||
[TestCase("12x")] | ||
public void Validate_InvalidStart_HasErrors(string value) | ||
{ | ||
// Arrange | ||
var model = new FizzBuzzRequest(value, string.Empty); | ||
|
||
// Act | ||
var result = validator.TestValidate(model); | ||
|
||
// Assert | ||
result.ShouldHaveValidationErrorFor(person => person.Start); | ||
} | ||
|
||
[TestCase("")] | ||
[TestCase(null)] | ||
[TestCase("12x")] | ||
[TestCase("0")] | ||
public void Validate_InvalidEnd_HasErrors(string value) | ||
{ | ||
// Arrange | ||
var model = new FizzBuzzRequest(string.Empty, value); | ||
|
||
// Act | ||
var result = validator.TestValidate(model); | ||
|
||
// Assert | ||
result.ShouldHaveValidationErrorFor(person => person.End); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
// <copyright file="FizzBuzzRequest.cs" company="Bugail Consulting Ltd"> | ||
// Copyright 2023 (c) Bugail Consulting Ltd. All rights reserved. | ||
// </copyright> | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
|
||
namespace UL.Core.Requests | ||
{ | ||
/// <summary> | ||
/// The fizzbuzz request. | ||
/// </summary> | ||
public class FizzBuzzRequest | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="FizzBuzzRequest"/> class. | ||
/// </summary> | ||
/// <param name="start">The start value.</param> | ||
/// <param name="end">The end value.</param> | ||
public FizzBuzzRequest(string start, string end) | ||
{ | ||
this.Start = start; | ||
this.End = end; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the start value. | ||
/// </summary> | ||
public string Start { get; } | ||
|
||
/// <summary> | ||
/// Gets the end value. | ||
/// </summary> | ||
public string End { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
// <copyright file="FizzBuzzRequestValidator.cs" company="Bugail Consulting Ltd"> | ||
// Copyright 2023 (c) Bugail Consulting Ltd. All rights reserved. | ||
// </copyright> | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
|
||
namespace UL.Core.Validators | ||
{ | ||
using FluentValidation; | ||
using UL.Core.Extensions; | ||
using UL.Core.Requests; | ||
|
||
/// <summary> | ||
/// The fizz buzz validator. | ||
/// </summary> | ||
public class FizzBuzzRequestValidator : AbstractValidator<FizzBuzzRequest> | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="FizzBuzzRequestValidator"/> class. | ||
/// </summary> | ||
public FizzBuzzRequestValidator() | ||
{ | ||
this.RuleFor(x => x.Start) | ||
.NotEmpty() | ||
.NotNull() | ||
.Must(x => x.IsNumeric()).WithMessage("Start must be a valid number"); | ||
|
||
this.RuleFor(x => x.End) | ||
.NotEmpty() | ||
.NotNull() | ||
.Must(x => x.IsNumeric()).WithMessage("End must be a valid number") | ||
.Custom((x, context) => | ||
{ | ||
if (!int.TryParse(x, out int value) || value <= 0) | ||
{ | ||
context.AddFailure($"End must be greater than 0."); | ||
} | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.