From 5b7283b1a90b09f6d74a03053bcd85f681a52484 Mon Sep 17 00:00:00 2001 From: Greg Hays Date: Mon, 14 Aug 2023 16:58:06 -0700 Subject: [PATCH 1/3] CODE RUB: WIP Closes: #505 --- .../CommentServiceTests.Exceptions.Add.cs | 508 +++++++++-------- .../CommentServiceTests.Exceptions.Modify.cs | 530 +++++++++--------- .../AlreadyExistsCommentException.cs | 10 +- .../Exceptions/CommentDependencyException.cs | 14 +- .../CommentDependencyValidationException.cs | 10 +- .../Exceptions/CommentServiceException.cs | 11 +- .../Exceptions/CommentValidationException.cs | 13 +- .../FailedCommentServiceException.cs | 18 +- .../FailedCommentStorageException.cs | 18 +- .../Exceptions/InvalidCommentException.cs | 19 +- .../InvalidCommentReferenceException.cs | 15 +- .../Exceptions/LockedCommentException.cs | 17 +- .../Exceptions/NotFoundCommentException.cs | 18 +- .../Exceptions/NullCommentException.cs | 18 +- 14 files changed, 649 insertions(+), 570 deletions(-) diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Exceptions.Add.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Exceptions.Add.cs index 7fe966c7..38ec67cc 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Exceptions.Add.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Exceptions.Add.cs @@ -16,247 +16,267 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Comments { - public partial class CommentServiceTests - { - [Fact] - public async Task ShouldThrowCriticalDependencyExceptionOnAddIfSqlErrorOccursAndLogItAsync() - { - // given - Comment someComment = CreateRandomComment(); - SqlException sqlException = GetSqlException(); - - var failedCommentStorageException = - new FailedCommentStorageException(sqlException); - - var expectedCommentDependencyException = - new CommentDependencyException(failedCommentStorageException); - - this.dateTimeBrokerMock.Setup(broker => - broker.GetCurrentDateTimeOffset()) - .Throws(sqlException); - - // when - ValueTask addCommentTask = - this.commentService.AddCommentAsync(someComment); - - CommentDependencyException actualCommentDependencyException = - await Assert.ThrowsAsync( - addCommentTask.AsTask); - - // then - actualCommentDependencyException.Should().BeEquivalentTo( - expectedCommentDependencyException); - - this.dateTimeBrokerMock.Verify(broker => - broker.GetCurrentDateTimeOffset(), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.InsertCommentAsync(It.IsAny()), - Times.Never); - - this.loggingBrokerMock.Verify(broker => - broker.LogCritical(It.Is(SameExceptionAs( - expectedCommentDependencyException))), - Times.Once); - - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - this.storageBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - } - - [Fact] - public async Task ShouldThrowDependencyValidationExceptionOnAddIfCommentAlreadyExsitsAndLogItAsync() - { - // given - Comment randomComment = CreateRandomComment(); - Comment alreadyExistsComment = randomComment; - string randomMessage = GetRandomMessage(); - - var duplicateKeyException = - new DuplicateKeyException(randomMessage); - - var alreadyExistsCommentException = - new AlreadyExistsCommentException(duplicateKeyException); - - var expectedCommentDependencyValidationException = - new CommentDependencyValidationException(alreadyExistsCommentException); - - this.dateTimeBrokerMock.Setup(broker => - broker.GetCurrentDateTimeOffset()) - .Throws(duplicateKeyException); - - // when - ValueTask addCommentTask = - this.commentService.AddCommentAsync(alreadyExistsComment); - - CommentDependencyValidationException actualCommentDependencyValidationException = - await Assert.ThrowsAsync( - addCommentTask.AsTask); - - // then - actualCommentDependencyValidationException.Should().BeEquivalentTo( - expectedCommentDependencyValidationException); - - this.dateTimeBrokerMock.Verify(broker => - broker.GetCurrentDateTimeOffset(), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.InsertCommentAsync(It.IsAny()), - Times.Never); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedCommentDependencyValidationException))), - Times.Once); - - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - this.storageBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - } - - [Fact] - public async Task ShouldThrowDependencyExceptionOnAddIfDatabaseUpdateErrorOccursAndLogItAsync() - { - // given - Comment someComment = CreateRandomComment(); - - var databaseUpdateException = - new DbUpdateException(); - - var failedCommentStorageException = - new FailedCommentStorageException(databaseUpdateException); - - var expectedCommentDependencyException = - new CommentDependencyException(failedCommentStorageException); - - this.dateTimeBrokerMock.Setup(broker => - broker.GetCurrentDateTimeOffset()) - .Throws(databaseUpdateException); - - // when - ValueTask addCommentTask = - this.commentService.AddCommentAsync(someComment); - - CommentDependencyException actualCommentDependencyException = - await Assert.ThrowsAsync( - addCommentTask.AsTask); - - // then - actualCommentDependencyException.Should().BeEquivalentTo( - expectedCommentDependencyException); - - this.dateTimeBrokerMock.Verify(broker => - broker.GetCurrentDateTimeOffset(), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.InsertCommentAsync(It.IsAny()), - Times.Never); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedCommentDependencyException))), - Times.Once); - - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - this.storageBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - } - - [Fact] - public async Task ShouldThrowServiceExceptionOnAddIfDatabaseUpdateErrorOccursAndLogItAsync() - { - // given - Comment someComment = CreateRandomComment(); - var serviceException = new Exception(); - - var failedCommentServiceException = - new FailedCommentServiceException(serviceException); - - var expectedCommentServiceException = - new CommentServiceException(failedCommentServiceException); - - this.dateTimeBrokerMock.Setup(broker => - broker.GetCurrentDateTimeOffset()) - .Throws(serviceException); - - // when - ValueTask addCommentTask = - this.commentService.AddCommentAsync(someComment); - - CommentServiceException actualCommentServiceException = - await Assert.ThrowsAsync( - addCommentTask.AsTask); - - //then - actualCommentServiceException.Should().BeEquivalentTo( - expectedCommentServiceException); - - this.dateTimeBrokerMock.Verify(broker => - broker.GetCurrentDateTimeOffset(), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.InsertCommentAsync(It.IsAny()), - Times.Never); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedCommentServiceException))), - Times.Once); - - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - this.storageBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - } - - [Fact] - public async void ShouldThrowValidationExceptionOnAddIfReferenceErrorOccursAndLogItAsync() - { - // given - Comment someComment = CreateRandomComment(); - string randomMessage = GetRandomMessage(); - string exceptionMessage = randomMessage; - - var foreignKeyConstraintConflictException = - new ForeignKeyConstraintConflictException(exceptionMessage); - - var invalidCommentReferenceException = - new InvalidCommentReferenceException(foreignKeyConstraintConflictException); - - var expectedCommentValidationException = - new CommentDependencyValidationException(invalidCommentReferenceException); - - this.dateTimeBrokerMock.Setup(broker => - broker.GetCurrentDateTimeOffset()) - .Throws(foreignKeyConstraintConflictException); - - // when - ValueTask addCommentTask = - this.commentService.AddCommentAsync(someComment); - - CommentDependencyValidationException actualCommentDependencyValidationException = - await Assert.ThrowsAsync( - addCommentTask.AsTask); - - // then - actualCommentDependencyValidationException.Should().BeEquivalentTo( - expectedCommentValidationException); - - this.dateTimeBrokerMock.Verify(broker => - broker.GetCurrentDateTimeOffset(), - Times.Once()); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedCommentValidationException))), - Times.Once); - - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - this.storageBrokerMock.VerifyNoOtherCalls(); - } - } -} + public partial class CommentServiceTests + { + [Fact] + private async Task ShouldThrowCriticalDependencyExceptionOnAddIfSqlErrorOccursAndLogItAsync() + { + // given + Comment someComment = CreateRandomComment(); + SqlException sqlException = GetSqlException(); + + var failedCommentStorageException = + new FailedCommentStorageException( + message: "Failed comment storage error occurred, contact support.", + innerException: sqlException); + + var expectedCommentDependencyException = + new CommentDependencyException( + message: "Comment dependency error occurred, contact support.", + innerException: failedCommentStorageException); + + this.dateTimeBrokerMock.Setup(broker => + broker.GetCurrentDateTimeOffset()) + .Throws(sqlException); + + // when + ValueTask addCommentTask = + this.commentService.AddCommentAsync(someComment); + + CommentDependencyException actualCommentDependencyException = + await Assert.ThrowsAsync( + addCommentTask.AsTask); + + // then + actualCommentDependencyException.Should().BeEquivalentTo( + expectedCommentDependencyException); + + this.dateTimeBrokerMock.Verify(broker => + broker.GetCurrentDateTimeOffset(), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.InsertCommentAsync(It.IsAny()), + Times.Never); + + this.loggingBrokerMock.Verify(broker => + broker.LogCritical(It.Is(SameExceptionAs( + expectedCommentDependencyException))), + Times.Once); + + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + this.storageBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + } + + [Fact] + private async Task ShouldThrowDependencyValidationExceptionOnAddIfCommentAlreadyExsitsAndLogItAsync() + { + // given + Comment randomComment = CreateRandomComment(); + Comment alreadyExistsComment = randomComment; + string randomMessage = GetRandomMessage(); + + var duplicateKeyException = + new DuplicateKeyException(randomMessage); + + var alreadyExistsCommentException = + new AlreadyExistsCommentException( + message: "Comment with the same id already exists, please correct.", + innerException: duplicateKeyException); + + var expectedCommentDependencyValidationException = + new CommentDependencyValidationException( + message: "Comment dependency validation occurred, please try again.", + innerException: alreadyExistsCommentException); + + this.dateTimeBrokerMock.Setup(broker => + broker.GetCurrentDateTimeOffset()) + .Throws(duplicateKeyException); + + // when + ValueTask addCommentTask = + this.commentService.AddCommentAsync(alreadyExistsComment); + + CommentDependencyValidationException actualCommentDependencyValidationException = + await Assert.ThrowsAsync( + addCommentTask.AsTask); + + // then + actualCommentDependencyValidationException.Should().BeEquivalentTo( + expectedCommentDependencyValidationException); + + this.dateTimeBrokerMock.Verify(broker => + broker.GetCurrentDateTimeOffset(), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.InsertCommentAsync(It.IsAny()), + Times.Never); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedCommentDependencyValidationException))), + Times.Once); + + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + this.storageBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + } + + [Fact] + private async Task ShouldThrowDependencyExceptionOnAddIfDatabaseUpdateErrorOccursAndLogItAsync() + { + // given + Comment someComment = CreateRandomComment(); + + var databaseUpdateException = + new DbUpdateException(); + + var failedCommentStorageException = + new FailedCommentStorageException( + message: "Failed comment storage error occurred, contact support.", + innerException: databaseUpdateException); + + var expectedCommentDependencyException = + new CommentDependencyException( + message: "Comment dependency error occurred, contact support.", + innerException: failedCommentStorageException); + + this.dateTimeBrokerMock.Setup(broker => + broker.GetCurrentDateTimeOffset()) + .Throws(databaseUpdateException); + + // when + ValueTask addCommentTask = + this.commentService.AddCommentAsync(someComment); + + CommentDependencyException actualCommentDependencyException = + await Assert.ThrowsAsync( + addCommentTask.AsTask); + + // then + actualCommentDependencyException.Should().BeEquivalentTo( + expectedCommentDependencyException); + + this.dateTimeBrokerMock.Verify(broker => + broker.GetCurrentDateTimeOffset(), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.InsertCommentAsync(It.IsAny()), + Times.Never); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedCommentDependencyException))), + Times.Once); + + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + this.storageBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + } + + [Fact] + private async Task ShouldThrowServiceExceptionOnAddIfDatabaseUpdateErrorOccursAndLogItAsync() + { + // given + Comment someComment = CreateRandomComment(); + var serviceException = new Exception(); + + var failedCommentServiceException = + new FailedCommentServiceException( + message: "Failed comment service occurred, please contact support", + innerException: serviceException); + + var expectedCommentServiceException = + new CommentServiceException( + message: "Comment service error occurred, contact support.", + innerException: failedCommentServiceException); + + this.dateTimeBrokerMock.Setup(broker => + broker.GetCurrentDateTimeOffset()) + .Throws(serviceException); + + // when + ValueTask addCommentTask = + this.commentService.AddCommentAsync(someComment); + + CommentServiceException actualCommentServiceException = + await Assert.ThrowsAsync( + addCommentTask.AsTask); + + //then + actualCommentServiceException.Should().BeEquivalentTo( + expectedCommentServiceException); + + this.dateTimeBrokerMock.Verify(broker => + broker.GetCurrentDateTimeOffset(), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.InsertCommentAsync(It.IsAny()), + Times.Never); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedCommentServiceException))), + Times.Once); + + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + this.storageBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + } + + [Fact] + private async void ShouldThrowValidationExceptionOnAddIfReferenceErrorOccursAndLogItAsync() + { + // given + Comment someComment = CreateRandomComment(); + string randomMessage = GetRandomMessage(); + string exceptionMessage = randomMessage; + + var foreignKeyConstraintConflictException = + new ForeignKeyConstraintConflictException(exceptionMessage); + + var invalidCommentReferenceException = + new InvalidCommentReferenceException( + message: "Invalid comment reference error occurred.", + innerException: foreignKeyConstraintConflictException); + + var expectedCommentValidationException = + new CommentDependencyValidationException( + message: "Comment dependency validation occurred, please try again.", + innerException: invalidCommentReferenceException); + + this.dateTimeBrokerMock.Setup(broker => + broker.GetCurrentDateTimeOffset()) + .Throws(foreignKeyConstraintConflictException); + + // when + ValueTask addCommentTask = + this.commentService.AddCommentAsync(someComment); + + CommentDependencyValidationException actualCommentDependencyValidationException = + await Assert.ThrowsAsync( + addCommentTask.AsTask); + + // then + actualCommentDependencyValidationException.Should().BeEquivalentTo( + expectedCommentValidationException); + + this.dateTimeBrokerMock.Verify(broker => + broker.GetCurrentDateTimeOffset(), + Times.Once()); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedCommentValidationException))), + Times.Once); + + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + this.storageBrokerMock.VerifyNoOtherCalls(); + } + } +} \ No newline at end of file diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Exceptions.Modify.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Exceptions.Modify.cs index 455938b4..ede4cdc3 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Exceptions.Modify.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Exceptions.Modify.cs @@ -16,265 +16,273 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Comments { - public partial class CommentServiceTests - { - [Fact] - public async Task ShouldThrowCriticalDependencyExceptionOnModifyIfSqlErrorOccursAndLogItAsync() - { - // given - Comment randomComment = CreateRandomComment(); - SqlException sqlException = GetSqlException(); - - var failedCommentStorageException = - new FailedCommentStorageException(sqlException); - - var expectedCommentDependencyException = - new CommentDependencyException(failedCommentStorageException); - - this.dateTimeBrokerMock.Setup(broker => - broker.GetCurrentDateTimeOffset()) - .Throws(sqlException); - - // when - ValueTask addCommentTask = - this.commentService.ModifyCommentAsync(randomComment); - - CommentDependencyException acutalCommentDependencyException = - await Assert.ThrowsAsync( - addCommentTask.AsTask); - - // then - acutalCommentDependencyException.Should().BeEquivalentTo( - expectedCommentDependencyException); - - this.dateTimeBrokerMock.Verify(broker => - broker.GetCurrentDateTimeOffset(), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.SelectCommentByIdAsync(randomComment.Id), - Times.Never); - - this.loggingBrokerMock.Verify(broker => - broker.LogCritical(It.Is(SameExceptionAs( - expectedCommentDependencyException))), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.UpdateCommentAsync(randomComment), - Times.Never); - - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - this.storageBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - } - - [Fact] - public async void ShouldThrowValidationExceptionOnModifyIfReferenceErrorOccursAndLogItAsync() - { - // given - Comment foreignKeyConflictedComment = CreateRandomComment(); - string randomMessage = GetRandomMessage(); - string exceptionMessage = randomMessage; - - var foreignKeyConstraintConflictException = - new ForeignKeyConstraintConflictException(exceptionMessage); - - var invalidCommentReferenceException = - new InvalidCommentReferenceException(foreignKeyConstraintConflictException); - - var expectedCommentDependencyValidationException = - new CommentDependencyValidationException(invalidCommentReferenceException); - - this.dateTimeBrokerMock.Setup(broker => - broker.GetCurrentDateTimeOffset()) - .Throws(foreignKeyConstraintConflictException); - - // when - ValueTask modifyCommentTask = - this.commentService.ModifyCommentAsync(foreignKeyConflictedComment); - - CommentDependencyValidationException actualCommentDependencyValidationException = - await Assert.ThrowsAsync( - modifyCommentTask.AsTask); - - // then - actualCommentDependencyValidationException.Should().BeEquivalentTo( - expectedCommentDependencyValidationException); - - this.dateTimeBrokerMock.Verify(broker => - broker.GetCurrentDateTimeOffset(), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.SelectCommentByIdAsync(foreignKeyConflictedComment.Id), - Times.Never); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedCommentDependencyValidationException))), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.UpdateCommentAsync(foreignKeyConflictedComment), - Times.Never); - - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - this.storageBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - } - - [Fact] - public async Task ShouldThrowDependencyExceptionOnModifyIfDatabaseUpdateExceptionOccursAndLogItAsync() - { - // given - Comment randomComment = CreateRandomComment(); - var databaseUpdateException = new DbUpdateException(); - - var failedCommentException = - new FailedCommentStorageException(databaseUpdateException); - - var expectedCommentDependencyException = - new CommentDependencyException(failedCommentException); - - this.dateTimeBrokerMock.Setup(broker => - broker.GetCurrentDateTimeOffset()) - .Throws(databaseUpdateException); - - //when - ValueTask modifyCommentTask = - this.commentService.ModifyCommentAsync(randomComment); - - CommentDependencyException actualCommentDependencyException = - await Assert.ThrowsAsync( - modifyCommentTask.AsTask); - - // then - actualCommentDependencyException.Should().BeEquivalentTo( - expectedCommentDependencyException); - - this.dateTimeBrokerMock.Verify(broker => - broker.GetCurrentDateTimeOffset(), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.SelectCommentByIdAsync(randomComment.Id), - Times.Never); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedCommentDependencyException))), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.UpdateCommentAsync(randomComment), - Times.Never); - - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - this.storageBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - } - - [Fact] - public async Task ShouldThrowDependencyValidationExceptionOnModifyIfDatabaseUpdateConcurrencyErrorOccursAndLogItAsync() - { - // given - Comment randomComment = CreateRandomComment(); - var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); - - var lockedCommentException = - new LockedCommentException(databaseUpdateConcurrencyException); - - var expectedCommentDependencyValidationException = - new CommentDependencyValidationException(lockedCommentException); - - this.dateTimeBrokerMock.Setup(broker => - broker.GetCurrentDateTimeOffset()) - .Throws(databaseUpdateConcurrencyException); - - // when - ValueTask modifyCommentTask = - this.commentService.ModifyCommentAsync(randomComment); - - CommentDependencyValidationException actualCommentDependencyValidationException = - await Assert.ThrowsAsync( - modifyCommentTask.AsTask); - - // then - actualCommentDependencyValidationException.Should().BeEquivalentTo( - expectedCommentDependencyValidationException); - - this.dateTimeBrokerMock.Verify(broker => - broker.GetCurrentDateTimeOffset(), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.SelectCommentByIdAsync(randomComment.Id), - Times.Never); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedCommentDependencyValidationException))), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.UpdateCommentAsync(randomComment), - Times.Never); - - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - this.storageBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - } - - [Fact] - public async Task ShouldThrowServiceExceptionOnModifyIfServiceErrorOccursAndLogItAsync() - { - // given - Comment randomComment = CreateRandomComment(); - var serviceException = new Exception(); - - var failedCommentException = - new FailedCommentServiceException(serviceException); - - var expectedCommentServiceException = - new CommentServiceException(failedCommentException); - - this.dateTimeBrokerMock.Setup(broker => - broker.GetCurrentDateTimeOffset()) - .Throws(serviceException); - - // when - ValueTask modifyCommentTask = - this.commentService.ModifyCommentAsync(randomComment); - - CommentServiceException actualCommentServiceException = - await Assert.ThrowsAsync( - modifyCommentTask.AsTask); - - // then - actualCommentServiceException.Should().BeEquivalentTo( - expectedCommentServiceException); - - this.dateTimeBrokerMock.Verify(broker => - broker.GetCurrentDateTimeOffset(), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.SelectCommentByIdAsync(randomComment.Id), - Times.Never); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedCommentServiceException))), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.UpdateCommentAsync(randomComment), - Times.Never); - - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - this.storageBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - } - } + public partial class CommentServiceTests + { + [Fact] + private async Task ShouldThrowCriticalDependencyExceptionOnModifyIfSqlErrorOccursAndLogItAsync() + { + // given + Comment randomComment = CreateRandomComment(); + SqlException sqlException = GetSqlException(); + + var failedCommentStorageException = + new FailedCommentStorageException( + message: "Failed comment storage error occurred, contact support.", + innerException: sqlException); + + var expectedCommentDependencyException = + new CommentDependencyException( + message: "Comment dependency error occurred, contact support.", + innerException: failedCommentStorageException); + + this.dateTimeBrokerMock.Setup(broker => + broker.GetCurrentDateTimeOffset()) + .Throws(sqlException); + + // when + ValueTask addCommentTask = + this.commentService.ModifyCommentAsync(randomComment); + + CommentDependencyException acutalCommentDependencyException = + await Assert.ThrowsAsync( + addCommentTask.AsTask); + + // then + acutalCommentDependencyException.Should().BeEquivalentTo( + expectedCommentDependencyException); + + this.dateTimeBrokerMock.Verify(broker => + broker.GetCurrentDateTimeOffset(), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.SelectCommentByIdAsync(randomComment.Id), + Times.Never); + + this.loggingBrokerMock.Verify(broker => + broker.LogCritical(It.Is(SameExceptionAs( + expectedCommentDependencyException))), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.UpdateCommentAsync(randomComment), + Times.Never); + + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + this.storageBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + } + + [Fact] + private async void ShouldThrowValidationExceptionOnModifyIfReferenceErrorOccursAndLogItAsync() + { + // given + Comment foreignKeyConflictedComment = CreateRandomComment(); + string randomMessage = GetRandomMessage(); + string exceptionMessage = randomMessage; + + var foreignKeyConstraintConflictException = + new ForeignKeyConstraintConflictException( + + exceptionMessage); + + var invalidCommentReferenceException = + new InvalidCommentReferenceException( + + foreignKeyConstraintConflictException); + + var expectedCommentDependencyValidationException = + new CommentDependencyValidationException(invalidCommentReferenceException); + + this.dateTimeBrokerMock.Setup(broker => + broker.GetCurrentDateTimeOffset()) + .Throws(foreignKeyConstraintConflictException); + + // when + ValueTask modifyCommentTask = + this.commentService.ModifyCommentAsync(foreignKeyConflictedComment); + + CommentDependencyValidationException actualCommentDependencyValidationException = + await Assert.ThrowsAsync( + modifyCommentTask.AsTask); + + // then + actualCommentDependencyValidationException.Should().BeEquivalentTo( + expectedCommentDependencyValidationException); + + this.dateTimeBrokerMock.Verify(broker => + broker.GetCurrentDateTimeOffset(), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.SelectCommentByIdAsync(foreignKeyConflictedComment.Id), + Times.Never); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedCommentDependencyValidationException))), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.UpdateCommentAsync(foreignKeyConflictedComment), + Times.Never); + + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + this.storageBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + } + + [Fact] + private async Task ShouldThrowDependencyExceptionOnModifyIfDatabaseUpdateExceptionOccursAndLogItAsync() + { + // given + Comment randomComment = CreateRandomComment(); + var databaseUpdateException = new DbUpdateException(); + + var failedCommentException = + new FailedCommentStorageException(databaseUpdateException); + + var expectedCommentDependencyException = + new CommentDependencyException(failedCommentException); + + this.dateTimeBrokerMock.Setup(broker => + broker.GetCurrentDateTimeOffset()) + .Throws(databaseUpdateException); + + //when + ValueTask modifyCommentTask = + this.commentService.ModifyCommentAsync(randomComment); + + CommentDependencyException actualCommentDependencyException = + await Assert.ThrowsAsync( + modifyCommentTask.AsTask); + + // then + actualCommentDependencyException.Should().BeEquivalentTo( + expectedCommentDependencyException); + + this.dateTimeBrokerMock.Verify(broker => + broker.GetCurrentDateTimeOffset(), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.SelectCommentByIdAsync(randomComment.Id), + Times.Never); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedCommentDependencyException))), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.UpdateCommentAsync(randomComment), + Times.Never); + + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + this.storageBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + } + + [Fact] + private async Task ShouldThrowDependencyValidationExceptionOnModifyIfDatabaseUpdateConcurrencyErrorOccursAndLogItAsync() + { + // given + Comment randomComment = CreateRandomComment(); + var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); + + var lockedCommentException = + new LockedCommentException(databaseUpdateConcurrencyException); + + var expectedCommentDependencyValidationException = + new CommentDependencyValidationException(lockedCommentException); + + this.dateTimeBrokerMock.Setup(broker => + broker.GetCurrentDateTimeOffset()) + .Throws(databaseUpdateConcurrencyException); + + // when + ValueTask modifyCommentTask = + this.commentService.ModifyCommentAsync(randomComment); + + CommentDependencyValidationException actualCommentDependencyValidationException = + await Assert.ThrowsAsync( + modifyCommentTask.AsTask); + + // then + actualCommentDependencyValidationException.Should().BeEquivalentTo( + expectedCommentDependencyValidationException); + + this.dateTimeBrokerMock.Verify(broker => + broker.GetCurrentDateTimeOffset(), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.SelectCommentByIdAsync(randomComment.Id), + Times.Never); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedCommentDependencyValidationException))), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.UpdateCommentAsync(randomComment), + Times.Never); + + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + this.storageBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + } + + [Fact] + private async Task ShouldThrowServiceExceptionOnModifyIfServiceErrorOccursAndLogItAsync() + { + // given + Comment randomComment = CreateRandomComment(); + var serviceException = new Exception(); + + var failedCommentException = + new FailedCommentServiceException(serviceException); + + var expectedCommentServiceException = + new CommentServiceException(failedCommentException); + + this.dateTimeBrokerMock.Setup(broker => + broker.GetCurrentDateTimeOffset()) + .Throws(serviceException); + + // when + ValueTask modifyCommentTask = + this.commentService.ModifyCommentAsync(randomComment); + + CommentServiceException actualCommentServiceException = + await Assert.ThrowsAsync( + modifyCommentTask.AsTask); + + // then + actualCommentServiceException.Should().BeEquivalentTo( + expectedCommentServiceException); + + this.dateTimeBrokerMock.Verify(broker => + broker.GetCurrentDateTimeOffset(), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.SelectCommentByIdAsync(randomComment.Id), + Times.Never); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedCommentServiceException))), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.UpdateCommentAsync(randomComment), + Times.Never); + + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + this.storageBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + } + } } \ No newline at end of file diff --git a/Taarafo.Core/Models/Comments/Exceptions/AlreadyExistsCommentException.cs b/Taarafo.Core/Models/Comments/Exceptions/AlreadyExistsCommentException.cs index 22d3fc8d..f594a812 100644 --- a/Taarafo.Core/Models/Comments/Exceptions/AlreadyExistsCommentException.cs +++ b/Taarafo.Core/Models/Comments/Exceptions/AlreadyExistsCommentException.cs @@ -11,7 +11,11 @@ namespace Taarafo.Core.Models.Comments.Exceptions public class AlreadyExistsCommentException : Xeption { public AlreadyExistsCommentException(Exception innerException) - : base(message: "Comment with the same id already exists.", innerException) - { } + : base( + message: "Comment with the same id already exists, please correct.", + innerException: innerException) { } + + public AlreadyExistsCommentException(string message, Exception innerException) + : base(message, innerException) { } } -} +} \ No newline at end of file diff --git a/Taarafo.Core/Models/Comments/Exceptions/CommentDependencyException.cs b/Taarafo.Core/Models/Comments/Exceptions/CommentDependencyException.cs index 52a867ef..0a25d552 100644 --- a/Taarafo.Core/Models/Comments/Exceptions/CommentDependencyException.cs +++ b/Taarafo.Core/Models/Comments/Exceptions/CommentDependencyException.cs @@ -10,8 +10,12 @@ namespace Taarafo.Core.Models.Comments.Exceptions { public class CommentDependencyException : Xeption { - public CommentDependencyException(Exception innerException) : - base(message: "Comment dependency error occurred, contact support.", innerException) - { } - } -} + public CommentDependencyException(Exception innerException) + : base( + message: "Comment dependency error occurred, contact support.", + innerException: innerException) { } + + public CommentDependencyException(string message, Exception innerException) + : base(message, innerException) { } + } +} \ No newline at end of file diff --git a/Taarafo.Core/Models/Comments/Exceptions/CommentDependencyValidationException.cs b/Taarafo.Core/Models/Comments/Exceptions/CommentDependencyValidationException.cs index 8b9e1c6b..877240bb 100644 --- a/Taarafo.Core/Models/Comments/Exceptions/CommentDependencyValidationException.cs +++ b/Taarafo.Core/Models/Comments/Exceptions/CommentDependencyValidationException.cs @@ -10,7 +10,11 @@ namespace Taarafo.Core.Models.Comments.Exceptions public class CommentDependencyValidationException : Xeption { public CommentDependencyValidationException(Xeption innerException) - : base(message: "Comment dependency validation occurred, please try again.", innerException) - { } + : base( + message: "Comment dependency validation occurred, please try again.", + innerException: innerException) { } + + public CommentDependencyValidationException(string message, Xeption innerException) + : base(message, innerException) { } } -} +} \ No newline at end of file diff --git a/Taarafo.Core/Models/Comments/Exceptions/CommentServiceException.cs b/Taarafo.Core/Models/Comments/Exceptions/CommentServiceException.cs index ec282b5b..1bf86454 100644 --- a/Taarafo.Core/Models/Comments/Exceptions/CommentServiceException.cs +++ b/Taarafo.Core/Models/Comments/Exceptions/CommentServiceException.cs @@ -11,6 +11,11 @@ namespace Taarafo.Core.Models.Comments.Exceptions public class CommentServiceException : Xeption { public CommentServiceException(Exception innerException) - : base(message: "Comment service error occurred, contact support.", innerException) { } - } -} + : base( + message: "Comment service error occurred, contact support.", + innerException: innerException) { } + + public CommentServiceException(string message, Exception innerException) + : base(message, innerException) { } + } +} \ No newline at end of file diff --git a/Taarafo.Core/Models/Comments/Exceptions/CommentValidationException.cs b/Taarafo.Core/Models/Comments/Exceptions/CommentValidationException.cs index a6e6f575..fbfab181 100644 --- a/Taarafo.Core/Models/Comments/Exceptions/CommentValidationException.cs +++ b/Taarafo.Core/Models/Comments/Exceptions/CommentValidationException.cs @@ -10,8 +10,11 @@ namespace Taarafo.Core.Models.Comments.Exceptions public class CommentValidationException : Xeption { public CommentValidationException(Xeption innerException) - : base(message: "Comment validation errors occurred, please try again.", - innerException) - { } - } -} + : base( + message: "Comment validation errors occurred, please try again.", + innerException: innerException) { } + + public CommentValidationException(string message, Xeption innerException) + : base(message, innerException) { } + } +} \ No newline at end of file diff --git a/Taarafo.Core/Models/Comments/Exceptions/FailedCommentServiceException.cs b/Taarafo.Core/Models/Comments/Exceptions/FailedCommentServiceException.cs index 68fc5763..5fd64d86 100644 --- a/Taarafo.Core/Models/Comments/Exceptions/FailedCommentServiceException.cs +++ b/Taarafo.Core/Models/Comments/Exceptions/FailedCommentServiceException.cs @@ -8,10 +8,14 @@ namespace Taarafo.Core.Models.Comments.Exceptions { - public class FailedCommentServiceException : Xeption - { - public FailedCommentServiceException(Exception innerException) - : base(message: "Failed comment service occurred, please contact support", innerException) - { } - } -} + public class FailedCommentServiceException : Xeption + { + public FailedCommentServiceException(Exception innerException) + : base( + message: "Failed comment service occurred, please contact support", + innerException: innerException) { } + + public FailedCommentServiceException(string message, Exception innerException) + : base(message, innerException) { } + } +} \ No newline at end of file diff --git a/Taarafo.Core/Models/Comments/Exceptions/FailedCommentStorageException.cs b/Taarafo.Core/Models/Comments/Exceptions/FailedCommentStorageException.cs index 0607ce65..b517aa2c 100644 --- a/Taarafo.Core/Models/Comments/Exceptions/FailedCommentStorageException.cs +++ b/Taarafo.Core/Models/Comments/Exceptions/FailedCommentStorageException.cs @@ -8,10 +8,14 @@ namespace Taarafo.Core.Models.Comments.Exceptions { - public class FailedCommentStorageException : Xeption - { - public FailedCommentStorageException(Exception innerException) - : base(message: "Failed comment storage error occurred, contact support.", innerException) - { } - } -} + public class FailedCommentStorageException : Xeption + { + public FailedCommentStorageException(Exception innerException) + : base( + message: "Failed comment storage error occurred, contact support.", + innerException: innerException) { } + + public FailedCommentStorageException(string message, Exception innerException) + : base(message, innerException) { } + } +} \ No newline at end of file diff --git a/Taarafo.Core/Models/Comments/Exceptions/InvalidCommentException.cs b/Taarafo.Core/Models/Comments/Exceptions/InvalidCommentException.cs index 8aee12f1..bc7012fb 100644 --- a/Taarafo.Core/Models/Comments/Exceptions/InvalidCommentException.cs +++ b/Taarafo.Core/Models/Comments/Exceptions/InvalidCommentException.cs @@ -3,14 +3,19 @@ // FREE TO USE TO CONNECT THE WORLD // --------------------------------------------------------------- +using System; using Xeptions; namespace Taarafo.Core.Models.Comments.Exceptions { - public class InvalidCommentException : Xeption - { - public InvalidCommentException() - : base(message: "Invalid comment. Please correct the errors and try again.") - { } - } -} + public class InvalidCommentException : Xeption + { + public InvalidCommentException() + : base( + message: "Invalid comment. Please correct the errors and try again.") + { } + + public InvalidCommentException(string message, Exception innerException) + : base(message, innerException) { } + } +} \ No newline at end of file diff --git a/Taarafo.Core/Models/Comments/Exceptions/InvalidCommentReferenceException.cs b/Taarafo.Core/Models/Comments/Exceptions/InvalidCommentReferenceException.cs index bbabd467..b3b53eb5 100644 --- a/Taarafo.Core/Models/Comments/Exceptions/InvalidCommentReferenceException.cs +++ b/Taarafo.Core/Models/Comments/Exceptions/InvalidCommentReferenceException.cs @@ -8,9 +8,14 @@ namespace Taarafo.Core.Models.Comments.Exceptions { - public class InvalidCommentReferenceException : Xeption - { - public InvalidCommentReferenceException(Exception innerException) - : base(message: "Invalid comment reference error occurred.", innerException) { } - } + public class InvalidCommentReferenceException : Xeption + { + public InvalidCommentReferenceException(Exception innerException) + : base( + message: "Invalid comment reference error occurred.", + innerException: innerException) { } + + public InvalidCommentReferenceException(string message, Exception innerException) + : base(message, innerException) { } + } } \ No newline at end of file diff --git a/Taarafo.Core/Models/Comments/Exceptions/LockedCommentException.cs b/Taarafo.Core/Models/Comments/Exceptions/LockedCommentException.cs index 067c2735..234797c2 100644 --- a/Taarafo.Core/Models/Comments/Exceptions/LockedCommentException.cs +++ b/Taarafo.Core/Models/Comments/Exceptions/LockedCommentException.cs @@ -8,9 +8,14 @@ namespace Taarafo.Core.Models.Comments.Exceptions { - public class LockedCommentException : Xeption - { - public LockedCommentException(Exception innerException) - : base(message: "Locked comment record exception, please try again later", innerException) { } - } -} + public class LockedCommentException : Xeption + { + public LockedCommentException(Exception innerException) + : base( + message: "Locked comment record exception, please try again later", + innerException: innerException) { } + + public LockedCommentException(string message, Exception innerException) + : base(message, innerException) { } + } +} \ No newline at end of file diff --git a/Taarafo.Core/Models/Comments/Exceptions/NotFoundCommentException.cs b/Taarafo.Core/Models/Comments/Exceptions/NotFoundCommentException.cs index ef766d37..b6cc48f4 100644 --- a/Taarafo.Core/Models/Comments/Exceptions/NotFoundCommentException.cs +++ b/Taarafo.Core/Models/Comments/Exceptions/NotFoundCommentException.cs @@ -8,10 +8,14 @@ namespace Taarafo.Core.Models.Comments.Exceptions { - public class NotFoundCommentException : Xeption - { - public NotFoundCommentException(Guid commentId) - : base(message: $"Couldn't find comment with id: {commentId}.") - { } - } -} + public class NotFoundCommentException : Xeption + { + public NotFoundCommentException(Guid commentId) + : base( + message: $"Comment with id: {commentId} not found, please contact support.") + { } + + public NotFoundCommentException(string message, Exception innerException) + : base(message, innerException) { } + } +} \ No newline at end of file diff --git a/Taarafo.Core/Models/Comments/Exceptions/NullCommentException.cs b/Taarafo.Core/Models/Comments/Exceptions/NullCommentException.cs index 332aab1d..2678cc7b 100644 --- a/Taarafo.Core/Models/Comments/Exceptions/NullCommentException.cs +++ b/Taarafo.Core/Models/Comments/Exceptions/NullCommentException.cs @@ -3,14 +3,18 @@ // FREE TO USE TO CONNECT THE WORLD // --------------------------------------------------------------- +using System; using Xeptions; namespace Taarafo.Core.Models.Comments.Exceptions { - public class NullCommentException : Xeption - { - public NullCommentException() - : base(message: "Comment is null.") - { } - } -} + public class NullCommentException : Xeption + { + public NullCommentException() + : base(message: "Comment is null, please correct.") + { } + + public NullCommentException(string message, Exception innerException) + : base(message, innerException) { } + } +} \ No newline at end of file From c385b799b2573421d17586a786eb702e15aea93d Mon Sep 17 00:00:00 2001 From: Greg Hays Date: Tue, 15 Aug 2023 13:26:43 -0700 Subject: [PATCH 2/3] CODE RUB: Comments Exceptions Upgrade v2.10.0 --- .../CommentServiceTests.Exceptions.Modify.cs | 84 +- ...mmentServiceTests.Exceptions.RemoveById.cs | 284 +++--- ...mentServiceTests.Exceptions.RetrieveAll.cs | 20 +- ...entServiceTests.Exceptions.RetrieveById.cs | 186 ++-- .../Comments/CommentServiceTests.Logic.Add.cs | 86 +- .../CommentServiceTests.Logic.Modify.cs | 86 +- .../CommentServiceTests.Logic.RemoveById.cs | 86 +- .../CommentServiceTests.Logic.RetrieveAll.cs | 50 +- .../CommentServiceTests.Logic.RetrieveById.cs | 52 +- .../CommentServiceTests.Validation.Modify.cs | 854 ++++++++++-------- .../CommentServiceTests.Validations.Add.cs | 449 ++++----- ...mentServiceTests.Validations.RemoveById.cs | 186 ++-- ...ntServiceTests.Validations.RetrieveById.cs | 180 ++-- .../Comments/CommentServiceTests.cs | 5 + Taarafo.Core/Models/Comments/Comment.cs | 1 - .../Exceptions/CommentDependencyException.cs | 7 +- .../Exceptions/CommentServiceException.cs | 15 +- .../Exceptions/CommentValidationException.cs | 4 +- .../ForeignKeyCommentReferenceException.cs | 21 + .../Comments/CommentService.Exceptions.cs | 4 +- .../Comments/CommentService.Validations.cs | 4 +- .../Foundations/Comments/CommentService.cs | 138 +-- .../Foundations/Comments/ICommentService.cs | 2 +- 23 files changed, 1453 insertions(+), 1351 deletions(-) create mode 100644 Taarafo.Core/Models/Comments/Exceptions/ForeignKeyCommentReferenceException.cs diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Exceptions.Modify.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Exceptions.Modify.cs index ede4cdc3..4c935be1 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Exceptions.Modify.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Exceptions.Modify.cs @@ -5,7 +5,6 @@ using System; using System.Threading.Tasks; -using EFxceptions.Models.Exceptions; using FluentAssertions; using Microsoft.Data.SqlClient; using Microsoft.EntityFrameworkCore; @@ -73,65 +72,6 @@ await Assert.ThrowsAsync( this.loggingBrokerMock.VerifyNoOtherCalls(); } - [Fact] - private async void ShouldThrowValidationExceptionOnModifyIfReferenceErrorOccursAndLogItAsync() - { - // given - Comment foreignKeyConflictedComment = CreateRandomComment(); - string randomMessage = GetRandomMessage(); - string exceptionMessage = randomMessage; - - var foreignKeyConstraintConflictException = - new ForeignKeyConstraintConflictException( - - exceptionMessage); - - var invalidCommentReferenceException = - new InvalidCommentReferenceException( - - foreignKeyConstraintConflictException); - - var expectedCommentDependencyValidationException = - new CommentDependencyValidationException(invalidCommentReferenceException); - - this.dateTimeBrokerMock.Setup(broker => - broker.GetCurrentDateTimeOffset()) - .Throws(foreignKeyConstraintConflictException); - - // when - ValueTask modifyCommentTask = - this.commentService.ModifyCommentAsync(foreignKeyConflictedComment); - - CommentDependencyValidationException actualCommentDependencyValidationException = - await Assert.ThrowsAsync( - modifyCommentTask.AsTask); - - // then - actualCommentDependencyValidationException.Should().BeEquivalentTo( - expectedCommentDependencyValidationException); - - this.dateTimeBrokerMock.Verify(broker => - broker.GetCurrentDateTimeOffset(), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.SelectCommentByIdAsync(foreignKeyConflictedComment.Id), - Times.Never); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedCommentDependencyValidationException))), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.UpdateCommentAsync(foreignKeyConflictedComment), - Times.Never); - - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - this.storageBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - } - [Fact] private async Task ShouldThrowDependencyExceptionOnModifyIfDatabaseUpdateExceptionOccursAndLogItAsync() { @@ -140,10 +80,14 @@ private async Task ShouldThrowDependencyExceptionOnModifyIfDatabaseUpdateExcepti var databaseUpdateException = new DbUpdateException(); var failedCommentException = - new FailedCommentStorageException(databaseUpdateException); + new FailedCommentStorageException( + message: "Failed comment storage error occurred, contact support.", + innerException: databaseUpdateException); var expectedCommentDependencyException = - new CommentDependencyException(failedCommentException); + new CommentDependencyException( + message: "Comment dependency error occurred, contact support.", + innerException: failedCommentException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTimeOffset()) @@ -191,10 +135,14 @@ private async Task ShouldThrowDependencyValidationExceptionOnModifyIfDatabaseUpd var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedCommentException = - new LockedCommentException(databaseUpdateConcurrencyException); + new LockedCommentException( + message: "Locked comment record exception, please try again later", + innerException: databaseUpdateConcurrencyException); var expectedCommentDependencyValidationException = - new CommentDependencyValidationException(lockedCommentException); + new CommentDependencyValidationException( + message: "Comment dependency validation occurred, please try again.", + innerException: lockedCommentException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTimeOffset()) @@ -242,10 +190,14 @@ private async Task ShouldThrowServiceExceptionOnModifyIfServiceErrorOccursAndLog var serviceException = new Exception(); var failedCommentException = - new FailedCommentServiceException(serviceException); + new FailedCommentServiceException( + message: "Failed comment service occurred, please contact support", + innerException: serviceException); var expectedCommentServiceException = - new CommentServiceException(failedCommentException); + new CommentServiceException( + message: "Comment service error occurred, contact support.", + innerException: failedCommentException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTimeOffset()) diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Exceptions.RemoveById.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Exceptions.RemoveById.cs index 9c8fc811..6d99eea5 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Exceptions.RemoveById.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Exceptions.RemoveById.cs @@ -15,140 +15,152 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Comments { - public partial class CommentServiceTests - { - [Fact] - public async Task ShouldThrowDependencyValidationOnRemoveIfDatabaseUpdateConcurrencyErrorOccursAndLogItAsync() - { - // given - Guid someCommentId = Guid.NewGuid(); - - var databaseUpdateConcurrencyException = - new DbUpdateConcurrencyException(); - - var lockedCommentException = - new LockedCommentException(databaseUpdateConcurrencyException); - - var expectedCommentDependencyValidationException = - new CommentDependencyValidationException(lockedCommentException); - - this.storageBrokerMock.Setup(broker => - broker.SelectCommentByIdAsync(It.IsAny())) - .ThrowsAsync(databaseUpdateConcurrencyException); - - // when - ValueTask removeCommentByIdTask = - this.commentService.RemoveCommentByIdAsync(someCommentId); - - CommentDependencyValidationException actualCommentDependencyValidationException = - await Assert.ThrowsAsync( - removeCommentByIdTask.AsTask); - - // then - actualCommentDependencyValidationException.Should().BeEquivalentTo( - expectedCommentDependencyValidationException); - - this.storageBrokerMock.Verify(broker => - broker.SelectCommentByIdAsync(It.IsAny()), - Times.Once); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedCommentDependencyValidationException))), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.DeleteCommentAsync(It.IsAny()), - Times.Never); - - this.storageBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - } - - [Fact] - public async Task ShouldThrowDependencyExceptionOnDeleteWhenSqlExceptionOccursAndLogItAsync() - { - // given - Guid someCommentId = Guid.NewGuid(); - SqlException sqlException = GetSqlException(); - - var failedCommentStorageException = - new FailedCommentStorageException(sqlException); - - var expectedCommentDependencyException = - new CommentDependencyException(failedCommentStorageException); - - this.storageBrokerMock.Setup(broker => - broker.SelectCommentByIdAsync(It.IsAny())) - .ThrowsAsync(sqlException); - - // when - ValueTask deleteCommentTask = - this.commentService.RemoveCommentByIdAsync(someCommentId); - - CommentDependencyException actualCommentDependencyException = - await Assert.ThrowsAsync( - deleteCommentTask.AsTask); - - // then - actualCommentDependencyException.Should().BeEquivalentTo( - expectedCommentDependencyException); - - this.storageBrokerMock.Verify(broker => - broker.SelectCommentByIdAsync(It.IsAny()), - Times.Once); - - this.loggingBrokerMock.Verify(broker => - broker.LogCritical(It.Is(SameExceptionAs( - expectedCommentDependencyException))), - Times.Once); - - this.storageBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - } - - [Fact] - public async Task ShouldThrowServiceExceptionOnRemoveIfExceptionOccursAndLogItAsync() - { - // given - Guid someCommentId = Guid.NewGuid(); - var serviceException = new Exception(); - - var failedCommentServiceException = - new FailedCommentServiceException(serviceException); - - var expectedCommentServiceException = - new CommentServiceException(failedCommentServiceException); - - this.storageBrokerMock.Setup(broker => - broker.SelectCommentByIdAsync(It.IsAny())) - .ThrowsAsync(serviceException); - - // when - ValueTask removeCommentByIdTask = - this.commentService.RemoveCommentByIdAsync(someCommentId); - - CommentServiceException actualCommentServiceException = - await Assert.ThrowsAsync( - removeCommentByIdTask.AsTask); - - // then - actualCommentServiceException.Should().BeEquivalentTo(expectedCommentServiceException); - - this.storageBrokerMock.Verify(broker => - broker.SelectCommentByIdAsync(It.IsAny()), - Times.Once()); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedCommentServiceException))), - Times.Once); - - this.storageBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - } - } + public partial class CommentServiceTests + { + [Fact] + private async Task ShouldThrowDependencyValidationOnRemoveIfDatabaseUpdateConcurrencyErrorOccursAndLogItAsync() + { + // given + Guid someCommentId = Guid.NewGuid(); + + var databaseUpdateConcurrencyException = + new DbUpdateConcurrencyException(); + + var lockedCommentException = + new LockedCommentException( + message: "Locked comment record exception, please try again later", + innerException: databaseUpdateConcurrencyException); + + var expectedCommentDependencyValidationException = + new CommentDependencyValidationException( + message: "Comment dependency validation occurred, please try again.", + innerException: lockedCommentException); + + this.storageBrokerMock.Setup(broker => + broker.SelectCommentByIdAsync(It.IsAny())) + .ThrowsAsync(databaseUpdateConcurrencyException); + + // when + ValueTask removeCommentByIdTask = + this.commentService.RemoveCommentByIdAsync(someCommentId); + + CommentDependencyValidationException actualCommentDependencyValidationException = + await Assert.ThrowsAsync( + removeCommentByIdTask.AsTask); + + // then + actualCommentDependencyValidationException.Should().BeEquivalentTo( + expectedCommentDependencyValidationException); + + this.storageBrokerMock.Verify(broker => + broker.SelectCommentByIdAsync(It.IsAny()), + Times.Once); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedCommentDependencyValidationException))), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.DeleteCommentAsync(It.IsAny()), + Times.Never); + + this.storageBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + } + + [Fact] + private async Task ShouldThrowDependencyExceptionOnDeleteWhenSqlExceptionOccursAndLogItAsync() + { + // given + Guid someCommentId = Guid.NewGuid(); + SqlException sqlException = GetSqlException(); + + var failedCommentStorageException = + new FailedCommentStorageException( + message: "Failed comment storage error occurred, contact support.", + innerException: sqlException); + + var expectedCommentDependencyException = + new CommentDependencyException( + message: "Comment dependency error occurred, contact support.", + innerException: failedCommentStorageException); + + this.storageBrokerMock.Setup(broker => + broker.SelectCommentByIdAsync(It.IsAny())) + .ThrowsAsync(sqlException); + + // when + ValueTask deleteCommentTask = + this.commentService.RemoveCommentByIdAsync(someCommentId); + + CommentDependencyException actualCommentDependencyException = + await Assert.ThrowsAsync( + deleteCommentTask.AsTask); + + // then + actualCommentDependencyException.Should().BeEquivalentTo( + expectedCommentDependencyException); + + this.storageBrokerMock.Verify(broker => + broker.SelectCommentByIdAsync(It.IsAny()), + Times.Once); + + this.loggingBrokerMock.Verify(broker => + broker.LogCritical(It.Is(SameExceptionAs( + expectedCommentDependencyException))), + Times.Once); + + this.storageBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + } + + [Fact] + private async Task ShouldThrowServiceExceptionOnRemoveIfExceptionOccursAndLogItAsync() + { + // given + Guid someCommentId = Guid.NewGuid(); + var serviceException = new Exception(); + + var failedCommentServiceException = + new FailedCommentServiceException( + message: "Failed comment service occurred, please contact support", + innerException: serviceException); + + var expectedCommentServiceException = + new CommentServiceException( + message: "Comment service error occurred, contact support.", + innerException: failedCommentServiceException); + + this.storageBrokerMock.Setup(broker => + broker.SelectCommentByIdAsync(It.IsAny())) + .ThrowsAsync(serviceException); + + // when + ValueTask removeCommentByIdTask = + this.commentService.RemoveCommentByIdAsync(someCommentId); + + CommentServiceException actualCommentServiceException = + await Assert.ThrowsAsync( + removeCommentByIdTask.AsTask); + + // then + actualCommentServiceException.Should().BeEquivalentTo(expectedCommentServiceException); + + this.storageBrokerMock.Verify(broker => + broker.SelectCommentByIdAsync(It.IsAny()), + Times.Once()); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedCommentServiceException))), + Times.Once); + + this.storageBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + } + } } \ No newline at end of file diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Exceptions.RetrieveAll.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Exceptions.RetrieveAll.cs index 52891a97..a71e6972 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Exceptions.RetrieveAll.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Exceptions.RetrieveAll.cs @@ -15,16 +15,20 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Comments public partial class CommentServiceTests { [Fact] - public void ShouldThrowCriticalDependencyExceptionOnRetrieveAllWhenSqlExceptionOccursAndLogIt() + private void ShouldThrowCriticalDependencyExceptionOnRetrieveAllWhenSqlExceptionOccursAndLogIt() { // given SqlException sqlException = GetSqlException(); var failedStorageException = - new FailedCommentStorageException(sqlException); + new FailedCommentStorageException( + message: "Failed comment storage error occurred, contact support.", + innerException: sqlException); var expectedCommentDependencyException = - new CommentDependencyException(failedStorageException); + new CommentDependencyException( + message: "Comment dependency error occurred, contact support.", + innerException: failedStorageException); this.storageBrokerMock.Setup(broker => broker.SelectAllComments()) @@ -57,17 +61,21 @@ public void ShouldThrowCriticalDependencyExceptionOnRetrieveAllWhenSqlExceptionO } [Fact] - public void ShouldThrowServiceExceptionOnRetrieveAllIfServiceErrorOccursAndLogItAsync() + private void ShouldThrowServiceExceptionOnRetrieveAllIfServiceErrorOccursAndLogItAsync() { // given string exceptionMessage = GetRandomMessage(); var serviceException = new Exception(exceptionMessage); var failedCommentServiceException = - new FailedCommentServiceException(serviceException); + new FailedCommentServiceException( + message: "Failed comment service occurred, please contact support", + innerException: serviceException); var expectedCommentServiceException = - new CommentServiceException(failedCommentServiceException); + new CommentServiceException( + message: "Comment service error occurred, contact support.", + innerException: failedCommentServiceException); this.storageBrokerMock.Setup(broker => broker.SelectAllComments()) diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Exceptions.RetrieveById.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Exceptions.RetrieveById.cs index 55dbf188..c68a3b27 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Exceptions.RetrieveById.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Exceptions.RetrieveById.cs @@ -14,92 +14,100 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Comments { - public partial class CommentServiceTests - { - [Fact] - public async Task ShouldThrowCriticalDependencyExceptionOnRetrieveByIdIfSqlErrorOccursAndLogItAsync() - { - // given - Guid someId = Guid.NewGuid(); - SqlException sqlException = GetSqlException(); - - var failedCommentStorageException = - new FailedCommentStorageException(sqlException); - - var expectedCommentDependencyException = - new CommentDependencyException(failedCommentStorageException); - - this.storageBrokerMock.Setup(broker => - broker.SelectCommentByIdAsync(It.IsAny())) - .ThrowsAsync(sqlException); - - // when - ValueTask retrieveCommentByIdTask = - this.commentService.RetrieveCommentByIdAsync(someId); - - CommentDependencyException actualCommentDependencyException = - await Assert.ThrowsAsync( - retrieveCommentByIdTask.AsTask); - - // then - actualCommentDependencyException.Should().BeEquivalentTo( - expectedCommentDependencyException); - - this.storageBrokerMock.Verify(broker => - broker.SelectCommentByIdAsync(It.IsAny()), - Times.Once); - - this.loggingBrokerMock.Verify(broker => - broker.LogCritical(It.Is(SameExceptionAs( - expectedCommentDependencyException))), - Times.Once); - - this.storageBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - } - - [Fact] - public async Task ShouldThrowServiceExceptionOnRetrieveByIdIfServiceErrorOccursAndLogItAsync() - { - // given - Guid someId = Guid.NewGuid(); - var serviceException = new Exception(); - - var failedCommentServiceException = - new FailedCommentServiceException(serviceException); - - var expectedCommentServiceException = - new CommentServiceException(failedCommentServiceException); - - this.storageBrokerMock.Setup(broker => - broker.SelectCommentByIdAsync(It.IsAny())) - .ThrowsAsync(serviceException); - - // when - ValueTask retrieveCommentByIdTask = - this.commentService.RetrieveCommentByIdAsync(someId); - - CommentServiceException actualCommentServiceException = - await Assert.ThrowsAsync( - retrieveCommentByIdTask.AsTask); - - // then - actualCommentServiceException.Should().BeEquivalentTo( - expectedCommentServiceException); - - this.storageBrokerMock.Verify(broker => - broker.SelectCommentByIdAsync(It.IsAny()), - Times.Once); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedCommentServiceException))), - Times.Once); - - this.storageBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - } - } -} + public partial class CommentServiceTests + { + [Fact] + private async Task ShouldThrowCriticalDependencyExceptionOnRetrieveByIdIfSqlErrorOccursAndLogItAsync() + { + // given + Guid someId = Guid.NewGuid(); + SqlException sqlException = GetSqlException(); + + var failedCommentStorageException = + new FailedCommentStorageException( + message: "Failed comment storage error occurred, contact support.", + innerException: sqlException); + + var expectedCommentDependencyException = + new CommentDependencyException( + message: "Comment dependency error occurred, contact support.", + innerException: failedCommentStorageException); + + this.storageBrokerMock.Setup(broker => + broker.SelectCommentByIdAsync(It.IsAny())) + .ThrowsAsync(sqlException); + + // when + ValueTask retrieveCommentByIdTask = + this.commentService.RetrieveCommentByIdAsync(someId); + + CommentDependencyException actualCommentDependencyException = + await Assert.ThrowsAsync( + retrieveCommentByIdTask.AsTask); + + // then + actualCommentDependencyException.Should().BeEquivalentTo( + expectedCommentDependencyException); + + this.storageBrokerMock.Verify(broker => + broker.SelectCommentByIdAsync(It.IsAny()), + Times.Once); + + this.loggingBrokerMock.Verify(broker => + broker.LogCritical(It.Is(SameExceptionAs( + expectedCommentDependencyException))), + Times.Once); + + this.storageBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + } + + [Fact] + private async Task ShouldThrowServiceExceptionOnRetrieveByIdIfServiceErrorOccursAndLogItAsync() + { + // given + Guid someId = Guid.NewGuid(); + var serviceException = new Exception(); + + var failedCommentServiceException = + new FailedCommentServiceException( + message: "Failed comment service occurred, please contact support", + innerException: serviceException); + + var expectedCommentServiceException = + new CommentServiceException( + message: "Comment service error occurred, contact support.", + innerException: failedCommentServiceException); + + this.storageBrokerMock.Setup(broker => + broker.SelectCommentByIdAsync(It.IsAny())) + .ThrowsAsync(serviceException); + + // when + ValueTask retrieveCommentByIdTask = + this.commentService.RetrieveCommentByIdAsync(someId); + + CommentServiceException actualCommentServiceException = + await Assert.ThrowsAsync( + retrieveCommentByIdTask.AsTask); + + // then + actualCommentServiceException.Should().BeEquivalentTo( + expectedCommentServiceException); + + this.storageBrokerMock.Verify(broker => + broker.SelectCommentByIdAsync(It.IsAny()), + Times.Once); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedCommentServiceException))), + Times.Once); + + this.storageBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + } + } +} \ No newline at end of file diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Logic.Add.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Logic.Add.cs index 4c803ab8..ef84b4b3 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Logic.Add.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Logic.Add.cs @@ -13,46 +13,46 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Comments { - public partial class CommentServiceTests - { - [Fact] - public async Task ShouldAddCommentAsync() - { - // given - DateTimeOffset randomDateTime = - GetRandomDateTimeOffset(); - - Comment randomComment = CreateRandomComment(randomDateTime); - Comment inputComment = randomComment; - Comment storageComment = inputComment; - Comment expectedComment = storageComment.DeepClone(); - - this.dateTimeBrokerMock.Setup(broker => - broker.GetCurrentDateTimeOffset()) - .Returns(randomDateTime); - - this.storageBrokerMock.Setup(broker => - broker.InsertCommentAsync(inputComment)) - .ReturnsAsync(storageComment); - - // when - Comment actualComment = await this.commentService - .AddCommentAsync(inputComment); - - // then - actualComment.Should().BeEquivalentTo(expectedComment); - - this.dateTimeBrokerMock.Verify(broker => - broker.GetCurrentDateTimeOffset(), - Times.Once()); - - this.storageBrokerMock.Verify(broker => - broker.InsertCommentAsync(inputComment), - Times.Once); - - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - this.storageBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - } - } -} + public partial class CommentServiceTests + { + [Fact] + private async Task ShouldAddCommentAsync() + { + // given + DateTimeOffset randomDateTime = + GetRandomDateTimeOffset(); + + Comment randomComment = CreateRandomComment(randomDateTime); + Comment inputComment = randomComment; + Comment storageComment = inputComment; + Comment expectedComment = storageComment.DeepClone(); + + this.dateTimeBrokerMock.Setup(broker => + broker.GetCurrentDateTimeOffset()) + .Returns(randomDateTime); + + this.storageBrokerMock.Setup(broker => + broker.InsertCommentAsync(inputComment)) + .ReturnsAsync(storageComment); + + // when + Comment actualComment = await this.commentService + .AddCommentAsync(inputComment); + + // then + actualComment.Should().BeEquivalentTo(expectedComment); + + this.dateTimeBrokerMock.Verify(broker => + broker.GetCurrentDateTimeOffset(), + Times.Once()); + + this.storageBrokerMock.Verify(broker => + broker.InsertCommentAsync(inputComment), + Times.Once); + + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + this.storageBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + } + } +} \ No newline at end of file diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Logic.Modify.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Logic.Modify.cs index e2caa071..811213d8 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Logic.Modify.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Logic.Modify.cs @@ -13,55 +13,55 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Comments { - public partial class CommentServiceTests - { - [Fact] - public async Task ShouldModifyCommentAsync() - { - // given - int minuteInPast = GetRandomNegativeNumber(); - DateTimeOffset randomDate = GetRandomDateTimeOffset(); - Comment randomComment = CreateRandomModifyComment(randomDate.AddMinutes(minuteInPast)); - Comment inputComment = randomComment.DeepClone(); - inputComment.UpdatedDate = randomDate; - Comment storageComment = randomComment; - Comment updatedComment = inputComment; - Comment expectedComment = updatedComment.DeepClone(); + public partial class CommentServiceTests + { + [Fact] + private async Task ShouldModifyCommentAsync() + { + // given + int minuteInPast = GetRandomNegativeNumber(); + DateTimeOffset randomDate = GetRandomDateTimeOffset(); + Comment randomComment = CreateRandomModifyComment(randomDate.AddMinutes(minuteInPast)); + Comment inputComment = randomComment.DeepClone(); + inputComment.UpdatedDate = randomDate; + Comment storageComment = randomComment; + Comment updatedComment = inputComment; + Comment expectedComment = updatedComment.DeepClone(); - this.dateTimeBrokerMock.Setup(broker => - broker.GetCurrentDateTimeOffset()) - .Returns(randomDate); + this.dateTimeBrokerMock.Setup(broker => + broker.GetCurrentDateTimeOffset()) + .Returns(randomDate); - this.storageBrokerMock.Setup(broker => - broker.SelectCommentByIdAsync(inputComment.Id)) - .ReturnsAsync(storageComment); + this.storageBrokerMock.Setup(broker => + broker.SelectCommentByIdAsync(inputComment.Id)) + .ReturnsAsync(storageComment); - this.storageBrokerMock.Setup(broker => - broker.UpdateCommentAsync(inputComment)) - .ReturnsAsync(updatedComment); + this.storageBrokerMock.Setup(broker => + broker.UpdateCommentAsync(inputComment)) + .ReturnsAsync(updatedComment); - // when - Comment actualComment = - await this.commentService.ModifyCommentAsync(inputComment); + // when + Comment actualComment = + await this.commentService.ModifyCommentAsync(inputComment); - // then - actualComment.Should().BeEquivalentTo(expectedComment); + // then + actualComment.Should().BeEquivalentTo(expectedComment); - this.dateTimeBrokerMock.Verify(broker => - broker.GetCurrentDateTimeOffset(), - Times.Once); + this.dateTimeBrokerMock.Verify(broker => + broker.GetCurrentDateTimeOffset(), + Times.Once); - this.storageBrokerMock.Verify(broker => - broker.SelectCommentByIdAsync(inputComment.Id), - Times.Once); + this.storageBrokerMock.Verify(broker => + broker.SelectCommentByIdAsync(inputComment.Id), + Times.Once); - this.storageBrokerMock.Verify(broker => - broker.UpdateCommentAsync(inputComment), - Times.Once); + this.storageBrokerMock.Verify(broker => + broker.UpdateCommentAsync(inputComment), + Times.Once); - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - this.storageBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - } - } -} + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + this.storageBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + } + } +} \ No newline at end of file diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Logic.RemoveById.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Logic.RemoveById.cs index 0a41a8f7..daff5018 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Logic.RemoveById.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Logic.RemoveById.cs @@ -13,46 +13,46 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Comments { - public partial class CommentServiceTests - { - [Fact] - public async Task ShouldRemoveCommentByIdAsync() - { - // given - Guid randomId = Guid.NewGuid(); - Guid inputCommentId = randomId; - Comment randomComment = CreateRandomComment(); - Comment storageComment = randomComment; - Comment expectedInputComment = storageComment; - Comment deletedComment = expectedInputComment; - Comment expectedComment = deletedComment.DeepClone(); - - this.storageBrokerMock.Setup(broker => - broker.SelectCommentByIdAsync(inputCommentId)) - .ReturnsAsync(storageComment); - - this.storageBrokerMock.Setup(broker => - broker.DeleteCommentAsync(expectedInputComment)) - .ReturnsAsync(deletedComment); - - // when - Comment actualComment = await this.commentService - .RemoveCommentByIdAsync(inputCommentId); - - // then - actualComment.Should().BeEquivalentTo(expectedComment); - - this.storageBrokerMock.Verify(broker => - broker.SelectCommentByIdAsync(inputCommentId), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.DeleteCommentAsync(expectedInputComment), - Times.Once); - - this.storageBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - } - } -} + public partial class CommentServiceTests + { + [Fact] + private async Task ShouldRemoveCommentByIdAsync() + { + // given + Guid randomId = Guid.NewGuid(); + Guid inputCommentId = randomId; + Comment randomComment = CreateRandomComment(); + Comment storageComment = randomComment; + Comment expectedInputComment = storageComment; + Comment deletedComment = expectedInputComment; + Comment expectedComment = deletedComment.DeepClone(); + + this.storageBrokerMock.Setup(broker => + broker.SelectCommentByIdAsync(inputCommentId)) + .ReturnsAsync(storageComment); + + this.storageBrokerMock.Setup(broker => + broker.DeleteCommentAsync(expectedInputComment)) + .ReturnsAsync(deletedComment); + + // when + Comment actualComment = await this.commentService + .RemoveCommentByIdAsync(inputCommentId); + + // then + actualComment.Should().BeEquivalentTo(expectedComment); + + this.storageBrokerMock.Verify(broker => + broker.SelectCommentByIdAsync(inputCommentId), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.DeleteCommentAsync(expectedInputComment), + Times.Once); + + this.storageBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + } + } +} \ No newline at end of file diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Logic.RetrieveAll.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Logic.RetrieveAll.cs index f5f8e4dc..fa94185a 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Logic.RetrieveAll.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Logic.RetrieveAll.cs @@ -11,33 +11,33 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Comments { - public partial class CommentServiceTests - { - [Fact] - public void ShouldReturnComments() - { - // given - IQueryable randomComments = CreateRandomComments(); - IQueryable storageComments = randomComments; - IQueryable expectedComments = storageComments; + public partial class CommentServiceTests + { + [Fact] + private void ShouldReturnComments() + { + // given + IQueryable randomComments = CreateRandomComments(); + IQueryable storageComments = randomComments; + IQueryable expectedComments = storageComments; - this.storageBrokerMock.Setup(broker => - broker.SelectAllComments()) - .Returns(storageComments); + this.storageBrokerMock.Setup(broker => + broker.SelectAllComments()) + .Returns(storageComments); - // when - IQueryable actualComments = - this.commentService.RetrieveAllComments(); + // when + IQueryable actualComments = + this.commentService.RetrieveAllComments(); - // then - actualComments.Should().BeEquivalentTo(expectedComments); + // then + actualComments.Should().BeEquivalentTo(expectedComments); - this.storageBrokerMock.Verify(broker => - broker.SelectAllComments(), - Times.Once); + this.storageBrokerMock.Verify(broker => + broker.SelectAllComments(), + Times.Once); - this.storageBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - } - } -} + this.storageBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + } + } +} \ No newline at end of file diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Logic.RetrieveById.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Logic.RetrieveById.cs index 412f0e40..167235e7 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Logic.RetrieveById.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Logic.RetrieveById.cs @@ -12,34 +12,34 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Comments { - public partial class CommentServiceTests - { - [Fact] - public async Task ShouldRetrieveCommentByIdAsync() - { - // given - Comment randomComment = CreateRandomComment(); - Comment storageComment = randomComment; - Comment expectedComment = storageComment.DeepClone(); + public partial class CommentServiceTests + { + [Fact] + private async Task ShouldRetrieveCommentByIdAsync() + { + // given + Comment randomComment = CreateRandomComment(); + Comment storageComment = randomComment; + Comment expectedComment = storageComment.DeepClone(); - this.storageBrokerMock.Setup(broker => - broker.SelectCommentByIdAsync(randomComment.Id)) - .ReturnsAsync(storageComment); + this.storageBrokerMock.Setup(broker => + broker.SelectCommentByIdAsync(randomComment.Id)) + .ReturnsAsync(storageComment); - // when - Comment actualComment = - await this.commentService.RetrieveCommentByIdAsync(randomComment.Id); + // when + Comment actualComment = + await this.commentService.RetrieveCommentByIdAsync(randomComment.Id); - // then - actualComment.Should().BeEquivalentTo(expectedComment); + // then + actualComment.Should().BeEquivalentTo(expectedComment); - this.storageBrokerMock.Verify(broker => - broker.SelectCommentByIdAsync(randomComment.Id), - Times.Once); + this.storageBrokerMock.Verify(broker => + broker.SelectCommentByIdAsync(randomComment.Id), + Times.Once); - this.storageBrokerMock.VerifyNoOtherCalls(); - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - } - } -} + this.storageBrokerMock.VerifyNoOtherCalls(); + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + } + } +} \ No newline at end of file diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Validation.Modify.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Validation.Modify.cs index 7831520e..3aebb472 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Validation.Modify.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Validation.Modify.cs @@ -5,6 +5,7 @@ using System; using System.Threading.Tasks; +using EFxceptions.Models.Exceptions; using FluentAssertions; using Force.DeepCloner; using Moq; @@ -14,391 +15,468 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Comments { - public partial class CommentServiceTests - { - [Fact] - public async Task ShouldThrowValidationExceptionOnModifyIfCommentIsNullAndLogItAsync() - { - // given - Comment nullComment = null; - var nullCommentException = new NullCommentException(); - - var expectedCommentValidationException = - new CommentValidationException(nullCommentException); - - // when - ValueTask modifyCommentTask = - this.commentService.ModifyCommentAsync(nullComment); - - CommentValidationException actualCommentValidationException = - await Assert.ThrowsAsync( - modifyCommentTask.AsTask); - - // then - actualCommentValidationException.Should().BeEquivalentTo( - expectedCommentValidationException); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedCommentValidationException))), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.UpdateCommentAsync(It.IsAny()), - Times.Never); - - this.loggingBrokerMock.VerifyNoOtherCalls(); - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - this.storageBrokerMock.VerifyNoOtherCalls(); - } - - [Theory] - [InlineData(null)] - [InlineData("")] - [InlineData(" ")] - public async Task ShouldThrowValidationExceptionOnModifyIfCommentIsInvalidAndLogItAsync(string invalidText) - { - // given - var invalidComment = new Comment - { - Content = invalidText, - }; - - var invalidCommentException = new InvalidCommentException(); - - invalidCommentException.AddData( - key: nameof(Comment.Id), - values: "Id is required"); - - invalidCommentException.AddData( - key: nameof(Comment.Content), - values: "Text is required"); - - invalidCommentException.AddData( - key: nameof(Comment.CreatedDate), - values: "Date is required"); - - invalidCommentException.AddData( - key: nameof(Comment.UpdatedDate), - values: - new[] { - "Date is required", - $"Date is the same as {nameof(Comment.CreatedDate)}" - }); - - invalidCommentException.AddData( - key: nameof(Comment.PostId), - values: "Id is required"); - - var expectedCommentValidationException = - new CommentValidationException(invalidCommentException); - - // when - ValueTask modifyCommentTask = - this.commentService.ModifyCommentAsync(invalidComment); - - CommentValidationException actualCommentValidationException = - await Assert.ThrowsAsync( - modifyCommentTask.AsTask); - - // then - actualCommentValidationException.Should().BeEquivalentTo( - expectedCommentValidationException); - - this.dateTimeBrokerMock.Verify(broker => - broker.GetCurrentDateTimeOffset(), - Times.Once); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedCommentValidationException))), - Times.Once()); - - this.storageBrokerMock.Verify(broker => - broker.UpdateCommentAsync(It.IsAny()), - Times.Never); - - this.loggingBrokerMock.VerifyNoOtherCalls(); - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - this.storageBrokerMock.VerifyNoOtherCalls(); - } - - [Fact] - public async Task ShouldThrowValidationExceptionOnModifyIfUpdatedDateIsSameAsCreatedDateAndLogItAsync() - { - // given - DateTimeOffset randomDateTime = GetRandomDateTimeOffset(); - Comment randomComment = CreateRandomComment(randomDateTime); - Comment invalidComment = randomComment; - var invalidCommentException = new InvalidCommentException(); - - invalidCommentException.AddData( - key: nameof(Comment.UpdatedDate), - values: $"Date is the same as {nameof(Comment.CreatedDate)}"); - - var expectedCommentValidationException = - new CommentValidationException(invalidCommentException); - - this.dateTimeBrokerMock.Setup(broker => - broker.GetCurrentDateTimeOffset()) - .Returns(randomDateTime); - - // when - ValueTask modifyCommentTask = - this.commentService.ModifyCommentAsync(invalidComment); - - CommentValidationException actualCommentValidationException = - await Assert.ThrowsAsync( - modifyCommentTask.AsTask); - - // then - actualCommentValidationException.Should().BeEquivalentTo( - expectedCommentValidationException); - - this.dateTimeBrokerMock.Verify(broker => - broker.GetCurrentDateTimeOffset(), - Times.Once); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedCommentValidationException))), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.SelectCommentByIdAsync(invalidComment.Id), - Times.Never); - - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - this.storageBrokerMock.VerifyNoOtherCalls(); - } - - [Theory] - [MemberData(nameof(InvalidMinuteCases))] - public async Task ShouldThrowValidationExceptionOnModifyIfUpdatedDateIsNotRecentAndLogItAsync(int minutes) - { - // given - DateTimeOffset dateTime = GetRandomDateTimeOffset(); - Comment randomComment = CreateRandomComment(dateTime); - randomComment.UpdatedDate = dateTime.AddMinutes(minutes); - - var invalidCommentException = - new InvalidCommentException(); - - invalidCommentException.AddData( - key: nameof(Comment.UpdatedDate), - values: "Date is not recent"); - - var expectedCommentValidationException = - new CommentValidationException(invalidCommentException); - - this.dateTimeBrokerMock.Setup(broker => - broker.GetCurrentDateTimeOffset()) - .Returns(dateTime); - - // when - ValueTask modifyCommentTask = - this.commentService.ModifyCommentAsync(randomComment); - - CommentValidationException actualCommentValidationException = - await Assert.ThrowsAsync( - modifyCommentTask.AsTask); - - // then - actualCommentValidationException.Should().BeEquivalentTo( - expectedCommentValidationException); - - this.dateTimeBrokerMock.Verify(broker => - broker.GetCurrentDateTimeOffset(), - Times.Once); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedCommentValidationException))), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.SelectCommentByIdAsync(It.IsAny()), - Times.Never); - - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - this.storageBrokerMock.VerifyNoOtherCalls(); - } - - [Fact] - public async Task ShouldThrowValidationExceptionOnModifyIfCommentDoesNotExistAndLogItAsync() - { - // given - int randomNegativeMinutes = GetRandomNegativeNumber(); - DateTimeOffset dateTime = GetRandomDateTimeOffset(); - Comment randomComment = CreateRandomComment(dateTime); - Comment nonExistComment = randomComment; - nonExistComment.CreatedDate = dateTime.AddMinutes(randomNegativeMinutes); - Comment nullComment = null; - - var notFoundCommentException = - new NotFoundCommentException(nonExistComment.Id); - - var expectedCommentValidationException = - new CommentValidationException(notFoundCommentException); - - this.storageBrokerMock.Setup(broker => - broker.SelectCommentByIdAsync(nonExistComment.Id)) - .ReturnsAsync(nullComment); - - this.dateTimeBrokerMock.Setup(broker => - broker.GetCurrentDateTimeOffset()) - .Returns(dateTime); - - // when - ValueTask modifyCommentTask = - this.commentService.ModifyCommentAsync(nonExistComment); - - CommentValidationException actualCommentValidationException = - await Assert.ThrowsAsync( - modifyCommentTask.AsTask); - - // then - actualCommentValidationException.Should().BeEquivalentTo( - expectedCommentValidationException); - - this.storageBrokerMock.Verify(broker => - broker.SelectCommentByIdAsync(nonExistComment.Id), - Times.Once); - - this.dateTimeBrokerMock.Verify(broker => - broker.GetCurrentDateTimeOffset(), - Times.Once); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedCommentValidationException))), - Times.Once); - - this.storageBrokerMock.VerifyNoOtherCalls(); - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - } - - [Fact] - public async Task ShouldThrowValidationExceptionOnModifyIfStorageCreatedDateNotSameAsCreatedDateAndLogItAsync() - { - // given - int randomNumber = GetRandomNumber(); - int randomMinutes = randomNumber; - DateTimeOffset randomDate = GetRandomDateTimeOffset(); - Comment randomComment = CreateRandomModifyComment(randomDate); - Comment invalidComment = randomComment.DeepClone(); - Comment storageComment = randomComment.DeepClone(); - storageComment.CreatedDate = storageComment.CreatedDate.AddMinutes(randomMinutes); - storageComment.UpdatedDate = storageComment.UpdatedDate.AddMinutes(randomMinutes); - Guid commentId = invalidComment.Id; - - var invalidCommentException = - new InvalidCommentException(); - - invalidCommentException.AddData( - key: nameof(Comment.CreatedDate), - values: $"Date is not the same as {nameof(Comment.CreatedDate)}"); - - var expectedCommentValidationException = - new CommentValidationException(invalidCommentException); - - this.storageBrokerMock.Setup(broker => - broker.SelectCommentByIdAsync(commentId)) - .ReturnsAsync(storageComment); - - this.dateTimeBrokerMock.Setup(broker => - broker.GetCurrentDateTimeOffset()) - .Returns(randomDate); - - // when - ValueTask modifyCommentTask = - this.commentService.ModifyCommentAsync(invalidComment); - - CommentValidationException actualCommentValidationException = - await Assert.ThrowsAsync(() => - modifyCommentTask.AsTask()); - - // then - actualCommentValidationException.Should() - .BeEquivalentTo(expectedCommentValidationException); - - this.storageBrokerMock.Verify(broker => - broker.SelectCommentByIdAsync(invalidComment.Id), - Times.Once); - - this.dateTimeBrokerMock.Verify(broker => - broker.GetCurrentDateTimeOffset(), - Times.Once); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedCommentValidationException))), - Times.Once); - - this.storageBrokerMock.VerifyNoOtherCalls(); - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - } - - [Fact] - public async Task ShouldThrowValidationExceptionOnModifyIfStorageUpdatedDateSameAsUpdatedDateAndLogItAsync() - { - // given - DateTimeOffset randomDateTime = GetRandomDateTimeOffset(); - Comment randomComment = CreateRandomModifyComment(randomDateTime); - Comment invalidComment = randomComment; - - Comment storageComment = randomComment.DeepClone(); - invalidComment.UpdatedDate = storageComment.UpdatedDate; - - var invalidCommentException = new InvalidCommentException(); - - invalidCommentException.AddData( - key: nameof(Comment.UpdatedDate), - values: $"Date is the same as {nameof(Comment.UpdatedDate)}"); - - var expectedCommentValidationException = - new CommentValidationException(invalidCommentException); - - this.storageBrokerMock.Setup(broker => - broker.SelectCommentByIdAsync(invalidComment.Id)) - .ReturnsAsync(storageComment); - - this.dateTimeBrokerMock.Setup(broker => - broker.GetCurrentDateTimeOffset()) - .Returns(randomDateTime); - - // when - ValueTask modifyCommentTask = - this.commentService.ModifyCommentAsync(invalidComment); - - CommentValidationException actualCommentValidationException = - await Assert.ThrowsAsync( - modifyCommentTask.AsTask); - - // then - actualCommentValidationException.Should().BeEquivalentTo( - expectedCommentValidationException); - - this.dateTimeBrokerMock.Verify(broker => - broker.GetCurrentDateTimeOffset(), - Times.Once); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedCommentValidationException))), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.SelectCommentByIdAsync(invalidComment.Id), - Times.Once); - - this.storageBrokerMock.VerifyNoOtherCalls(); - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - } - } -} + public partial class CommentServiceTests + { + [Fact] + private async Task ShouldThrowValidationExceptionOnModifyIfCommentIsNullAndLogItAsync() + { + // given + Comment nullComment = null; + var nullCommentException = new NullCommentException(); + + var expectedCommentValidationException = + new CommentValidationException( + message: "Comment validation errors occurred, please try again.", + innerException: nullCommentException); + + // when + ValueTask modifyCommentTask = + this.commentService.ModifyCommentAsync(nullComment); + + CommentValidationException actualCommentValidationException = + await Assert.ThrowsAsync( + modifyCommentTask.AsTask); + + // then + actualCommentValidationException.Should().BeEquivalentTo( + expectedCommentValidationException); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedCommentValidationException))), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.UpdateCommentAsync(It.IsAny()), + Times.Never); + + this.loggingBrokerMock.VerifyNoOtherCalls(); + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + this.storageBrokerMock.VerifyNoOtherCalls(); + } + + [Theory] + [InlineData(null)] + [InlineData("")] + [InlineData(" ")] + private async Task ShouldThrowValidationExceptionOnModifyIfCommentIsInvalidAndLogItAsync(string invalidText) + { + // given + var invalidComment = new Comment + { + Content = invalidText, + }; + + var invalidCommentException = new InvalidCommentException(); + + invalidCommentException.AddData( + key: nameof(Comment.Id), + values: "Id is required"); + + invalidCommentException.AddData( + key: nameof(Comment.Content), + values: "Text is required"); + + invalidCommentException.AddData( + key: nameof(Comment.CreatedDate), + values: "Date is required"); + + invalidCommentException.AddData( + key: nameof(Comment.UpdatedDate), + values: + new[] { + "Date is required", + $"Date is the same as {nameof(Comment.CreatedDate)}" + }); + + invalidCommentException.AddData( + key: nameof(Comment.PostId), + values: "Id is required"); + + var expectedCommentValidationException = + new CommentValidationException( + message: "Comment validation errors occurred, please try again.", + innerException: invalidCommentException); + + // when + ValueTask modifyCommentTask = + this.commentService.ModifyCommentAsync(invalidComment); + + CommentValidationException actualCommentValidationException = + await Assert.ThrowsAsync( + modifyCommentTask.AsTask); + + // then + actualCommentValidationException.Should().BeEquivalentTo( + expectedCommentValidationException); + + this.dateTimeBrokerMock.Verify(broker => + broker.GetCurrentDateTimeOffset(), + Times.Once); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedCommentValidationException))), + Times.Once()); + + this.storageBrokerMock.Verify(broker => + broker.UpdateCommentAsync(It.IsAny()), + Times.Never); + + this.loggingBrokerMock.VerifyNoOtherCalls(); + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + this.storageBrokerMock.VerifyNoOtherCalls(); + } + + [Fact] + private async Task ShouldThrowValidationExceptionOnModifyIfUpdatedDateIsSameAsCreatedDateAndLogItAsync() + { + // given + DateTimeOffset randomDateTime = GetRandomDateTimeOffset(); + Comment randomComment = CreateRandomComment(randomDateTime); + Comment invalidComment = randomComment; + var invalidCommentException = new InvalidCommentException(); + + invalidCommentException.AddData( + key: nameof(Comment.UpdatedDate), + values: $"Date is the same as {nameof(Comment.CreatedDate)}"); + + var expectedCommentValidationException = + new CommentValidationException( + message: "Comment validation errors occurred, please try again.", + innerException: invalidCommentException); + + this.dateTimeBrokerMock.Setup(broker => + broker.GetCurrentDateTimeOffset()) + .Returns(randomDateTime); + + // when + ValueTask modifyCommentTask = + this.commentService.ModifyCommentAsync(invalidComment); + + CommentValidationException actualCommentValidationException = + await Assert.ThrowsAsync( + modifyCommentTask.AsTask); + + // then + actualCommentValidationException.Should().BeEquivalentTo( + expectedCommentValidationException); + + this.dateTimeBrokerMock.Verify(broker => + broker.GetCurrentDateTimeOffset(), + Times.Once); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedCommentValidationException))), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.SelectCommentByIdAsync(invalidComment.Id), + Times.Never); + + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + this.storageBrokerMock.VerifyNoOtherCalls(); + } + + [Theory] + [MemberData(nameof(InvalidMinuteCases))] + private async Task ShouldThrowValidationExceptionOnModifyIfUpdatedDateIsNotRecentAndLogItAsync(int minutes) + { + // given + DateTimeOffset dateTime = GetRandomDateTimeOffset(); + Comment randomComment = CreateRandomComment(dateTime); + randomComment.UpdatedDate = dateTime.AddMinutes(minutes); + + var invalidCommentException = + new InvalidCommentException(); + + invalidCommentException.AddData( + key: nameof(Comment.UpdatedDate), + values: "Date is not recent"); + + var expectedCommentValidationException = + new CommentValidationException( + message: "Comment validation errors occurred, please try again.", + innerException: invalidCommentException); + + this.dateTimeBrokerMock.Setup(broker => + broker.GetCurrentDateTimeOffset()) + .Returns(dateTime); + + // when + ValueTask modifyCommentTask = + this.commentService.ModifyCommentAsync(randomComment); + + CommentValidationException actualCommentValidationException = + await Assert.ThrowsAsync( + modifyCommentTask.AsTask); + + // then + actualCommentValidationException.Should().BeEquivalentTo( + expectedCommentValidationException); + + this.dateTimeBrokerMock.Verify(broker => + broker.GetCurrentDateTimeOffset(), + Times.Once); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedCommentValidationException))), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.SelectCommentByIdAsync(It.IsAny()), + Times.Never); + + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + this.storageBrokerMock.VerifyNoOtherCalls(); + } + + [Fact] + private async Task ShouldThrowValidationExceptionOnModifyIfCommentDoesNotExistAndLogItAsync() + { + // given + int randomNegativeMinutes = GetRandomNegativeNumber(); + DateTimeOffset dateTime = GetRandomDateTimeOffset(); + Comment randomComment = CreateRandomComment(dateTime); + Comment nonExistComment = randomComment; + nonExistComment.CreatedDate = dateTime.AddMinutes(randomNegativeMinutes); + Comment nullComment = null; + var invalidCommentException = new InvalidOperationException(); + + var notFoundCommentException = + new NotFoundCommentException( + message: $"Comment with id: {nonExistComment.Id} not found, please contact support.", + innerException: invalidCommentException); + + var expectedCommentValidationException = + new CommentValidationException( + message: "Comment validation errors occurred, please try again.", + innerException: notFoundCommentException); + + this.storageBrokerMock.Setup(broker => + broker.SelectCommentByIdAsync(nonExistComment.Id)) + .ReturnsAsync(nullComment); + + this.dateTimeBrokerMock.Setup(broker => + broker.GetCurrentDateTimeOffset()) + .Returns(dateTime); + + // when + ValueTask modifyCommentTask = + this.commentService.ModifyCommentAsync(nonExistComment); + + CommentValidationException actualCommentValidationException = + await Assert.ThrowsAsync( + modifyCommentTask.AsTask); + + // then + actualCommentValidationException.Should().BeEquivalentTo( + expectedCommentValidationException); + + this.storageBrokerMock.Verify(broker => + broker.SelectCommentByIdAsync(nonExistComment.Id), + Times.Once); + + this.dateTimeBrokerMock.Verify(broker => + broker.GetCurrentDateTimeOffset(), + Times.Once); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedCommentValidationException))), + Times.Once); + + this.storageBrokerMock.VerifyNoOtherCalls(); + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + } + + [Fact] + private async Task ShouldThrowValidationExceptionOnModifyIfStorageCreatedDateNotSameAsCreatedDateAndLogItAsync() + { + // given + int randomNumber = GetRandomNumber(); + int randomMinutes = randomNumber; + DateTimeOffset randomDate = GetRandomDateTimeOffset(); + Comment randomComment = CreateRandomModifyComment(randomDate); + Comment invalidComment = randomComment.DeepClone(); + Comment storageComment = randomComment.DeepClone(); + storageComment.CreatedDate = storageComment.CreatedDate.AddMinutes(randomMinutes); + storageComment.UpdatedDate = storageComment.UpdatedDate.AddMinutes(randomMinutes); + Guid commentId = invalidComment.Id; + + var invalidCommentException = + new InvalidCommentException(); + + invalidCommentException.AddData( + key: nameof(Comment.CreatedDate), + values: $"Date is not the same as {nameof(Comment.CreatedDate)}"); + + var expectedCommentValidationException = + new CommentValidationException( + message: "Comment validation errors occurred, please try again.", + innerException: invalidCommentException); + + this.storageBrokerMock.Setup(broker => + broker.SelectCommentByIdAsync(commentId)) + .ReturnsAsync(storageComment); + + this.dateTimeBrokerMock.Setup(broker => + broker.GetCurrentDateTimeOffset()) + .Returns(randomDate); + + // when + ValueTask modifyCommentTask = + this.commentService.ModifyCommentAsync(invalidComment); + + CommentValidationException actualCommentValidationException = + await Assert.ThrowsAsync(() => + modifyCommentTask.AsTask()); + + // then + actualCommentValidationException.Should() + .BeEquivalentTo(expectedCommentValidationException); + + this.storageBrokerMock.Verify(broker => + broker.SelectCommentByIdAsync(invalidComment.Id), + Times.Once); + + this.dateTimeBrokerMock.Verify(broker => + broker.GetCurrentDateTimeOffset(), + Times.Once); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedCommentValidationException))), + Times.Once); + + this.storageBrokerMock.VerifyNoOtherCalls(); + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + } + + [Fact] + private async Task ShouldThrowValidationExceptionOnModifyIfStorageUpdatedDateSameAsUpdatedDateAndLogItAsync() + { + // given + DateTimeOffset randomDateTime = GetRandomDateTimeOffset(); + Comment randomComment = CreateRandomModifyComment(randomDateTime); + Comment invalidComment = randomComment; + + Comment storageComment = randomComment.DeepClone(); + invalidComment.UpdatedDate = storageComment.UpdatedDate; + + var invalidCommentException = new InvalidCommentException(); + + invalidCommentException.AddData( + key: nameof(Comment.UpdatedDate), + values: $"Date is the same as {nameof(Comment.UpdatedDate)}"); + + var expectedCommentValidationException = + new CommentValidationException( + message: "Comment validation errors occurred, please try again.", + innerException: invalidCommentException); + + this.storageBrokerMock.Setup(broker => + broker.SelectCommentByIdAsync(invalidComment.Id)) + .ReturnsAsync(storageComment); + + this.dateTimeBrokerMock.Setup(broker => + broker.GetCurrentDateTimeOffset()) + .Returns(randomDateTime); + + // when + ValueTask modifyCommentTask = + this.commentService.ModifyCommentAsync(invalidComment); + + CommentValidationException actualCommentValidationException = + await Assert.ThrowsAsync( + modifyCommentTask.AsTask); + + // then + actualCommentValidationException.Should().BeEquivalentTo( + expectedCommentValidationException); + + this.dateTimeBrokerMock.Verify(broker => + broker.GetCurrentDateTimeOffset(), + Times.Once); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedCommentValidationException))), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.SelectCommentByIdAsync(invalidComment.Id), + Times.Once); + + this.storageBrokerMock.VerifyNoOtherCalls(); + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + } + + [Fact] + private async void ShouldThrowValidationExceptionOnModifyIfForeignKeyReferenceErrorOccursAndLogItAsync() + { + // given + Comment foreignKeyConflictedComment = CreateRandomComment(); + ForeignKeyConstraintConflictException foreignKeyException = GetForeignKeyException(); + + var foreignKeyConstraintConflictException = + new ForeignKeyCommentReferenceException( + message: "ForeignKey exception has occurred, contact support.", + innerException: foreignKeyException); + + var invalidCommentReferenceException = + new InvalidCommentReferenceException( + message: "Invalid comment reference error occurred.", + innerException: foreignKeyConstraintConflictException); + + var expectedCommentDependencyValidationException = + new CommentDependencyValidationException( + message: "Comment dependency validation occurred, please try again.", + innerException: invalidCommentReferenceException); + + this.dateTimeBrokerMock.Setup(broker => + broker.GetCurrentDateTimeOffset()) + .Throws(foreignKeyConstraintConflictException); + + // when + ValueTask modifyCommentTask = + this.commentService.ModifyCommentAsync(foreignKeyConflictedComment); + + CommentDependencyValidationException actualCommentDependencyValidationException = + await Assert.ThrowsAsync( + modifyCommentTask.AsTask); + + // then + actualCommentDependencyValidationException.Should().BeEquivalentTo( + expectedCommentDependencyValidationException); + + this.dateTimeBrokerMock.Verify(broker => + broker.GetCurrentDateTimeOffset(), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.SelectCommentByIdAsync(foreignKeyConflictedComment.Id), + Times.Never); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedCommentDependencyValidationException))), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.UpdateCommentAsync(foreignKeyConflictedComment), + Times.Never); + + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + this.storageBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + } + } +} \ No newline at end of file diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Validations.Add.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Validations.Add.cs index 3402329e..b1ce1af6 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Validations.Add.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Validations.Add.cs @@ -13,224 +13,231 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Comments { - public partial class CommentServiceTests - { - [Fact] - public async Task ShouldThrowValidationExceptionOnAddIfCommentIsNullAndLogItAsync() - { - // given - Comment nullComment = null; - - var nullCommentException = - new NullCommentException(); - - var expectedCommentValidationException = - new CommentValidationException(nullCommentException); - - // when - ValueTask addCommentTask = - this.commentService.AddCommentAsync(nullComment); - - CommentValidationException actualCommentValidationException = - await Assert.ThrowsAsync( - addCommentTask.AsTask); - - // then - actualCommentValidationException.Should().BeEquivalentTo( - expectedCommentValidationException); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedCommentValidationException))), - Times.Once); - - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - this.storageBrokerMock.VerifyNoOtherCalls(); - } - - [Theory] - [InlineData(null)] - [InlineData("")] - [InlineData(" ")] - public async Task ShouldThrowValidationExceptionOnAddIfCommentIsInvalidAndLogItAsync( - string invalidText) - { - // given - var invalidComment = new Comment - { - Content = invalidText - }; - - var invalidCommentException = - new InvalidCommentException(); - - invalidCommentException.AddData( - key: nameof(Comment.Id), - values: "Id is required"); - - invalidCommentException.AddData( - key: nameof(Comment.Content), - values: "Text is required"); - - invalidCommentException.AddData( - key: nameof(Comment.CreatedDate), - values: "Date is required"); - - invalidCommentException.AddData( - key: nameof(Comment.UpdatedDate), - values: "Date is required"); - - invalidCommentException.AddData( - key: nameof(Comment.PostId), - values: "Id is required"); - - var expectedCommentValidationException = - new CommentValidationException(invalidCommentException); - - // when - ValueTask addCommentTask = - this.commentService.AddCommentAsync(invalidComment); - - CommentValidationException actualCommentValidationException = - await Assert.ThrowsAsync( - addCommentTask.AsTask); - - // then - actualCommentValidationException.Should().BeEquivalentTo( - expectedCommentValidationException); - - this.dateTimeBrokerMock.Verify(broker => - broker.GetCurrentDateTimeOffset(), - Times.Once()); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedCommentValidationException))), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.InsertCommentAsync(It.IsAny()), - Times.Never); - - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - this.storageBrokerMock.VerifyNoOtherCalls(); - } - - [Fact] - public async Task ShouldThrowValidationExceptionOnAddIfCreateAndUpdateDatesIsNotSameAndLogItAsync() - { - // given - int randomNumber = GetRandomNumber(); - DateTimeOffset randomDateTimeOffset = GetRandomDateTimeOffset(); - Comment randomComment = CreateRandomComment(randomDateTimeOffset); - Comment invalidComment = randomComment; - - invalidComment.UpdatedDate = - invalidComment.CreatedDate.AddDays(randomNumber); - - var invalidCommentException = - new InvalidCommentException(); - - invalidCommentException.AddData( - key: nameof(Comment.UpdatedDate), - values: $"Date is not the same as {nameof(Comment.CreatedDate)}"); - - var expectedCommentValidationException = - new CommentValidationException(invalidCommentException); - - this.dateTimeBrokerMock.Setup(broker => - broker.GetCurrentDateTimeOffset()) - .Returns(randomDateTimeOffset); - - // when - ValueTask addCommentTask = - this.commentService.AddCommentAsync(invalidComment); - - CommentValidationException actualCommentValidationException = - await Assert.ThrowsAsync( - addCommentTask.AsTask); - - // then - actualCommentValidationException.Should().BeEquivalentTo(expectedCommentValidationException); - - this.dateTimeBrokerMock.Verify(broker => - broker.GetCurrentDateTimeOffset(), - Times.Once()); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedCommentValidationException))), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.InsertCommentAsync(It.IsAny()), - Times.Never); - - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - this.storageBrokerMock.VerifyNoOtherCalls(); - } - - [Theory] - [MemberData(nameof(MinutesBeforeOrAfter))] - public async Task ShouldThrowValidationExceptionOnAddIfCreatedDateIsNotRecentAndLogItAsync( - int minutesBeforeOrAfter) - { - // given - DateTimeOffset randomDateTime = - GetRandomDateTimeOffset(); - - DateTimeOffset invalidDateTime = - randomDateTime.AddMinutes(minutesBeforeOrAfter); - - Comment randomComment = CreateRandomComment(invalidDateTime); - Comment invalidComment = randomComment; - - var invalidCommentException = - new InvalidCommentException(); - - invalidCommentException.AddData( - key: nameof(Comment.CreatedDate), - values: "Date is not recent"); - - var expectedCommentValidationException = - new CommentValidationException(invalidCommentException); - - this.dateTimeBrokerMock.Setup(broker => - broker.GetCurrentDateTimeOffset()) - .Returns(randomDateTime); - - // when - ValueTask addCommentTask = - this.commentService.AddCommentAsync(invalidComment); - - CommentValidationException actualCommentValidationException = - await Assert.ThrowsAsync( - addCommentTask.AsTask); - - // then - actualCommentValidationException.Should().BeEquivalentTo( - expectedCommentValidationException); - - this.dateTimeBrokerMock.Verify(broker => - broker.GetCurrentDateTimeOffset(), - Times.Once()); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedCommentValidationException))), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.InsertCommentAsync(It.IsAny()), - Times.Never); - - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - this.storageBrokerMock.VerifyNoOtherCalls(); - } - - } -} + public partial class CommentServiceTests + { + [Fact] + private async Task ShouldThrowValidationExceptionOnAddIfCommentIsNullAndLogItAsync() + { + // given + Comment nullComment = null; + + var nullCommentException = + new NullCommentException(); + + var expectedCommentValidationException = + new CommentValidationException( + message: "Comment validation errors occurred, please try again.", + innerException: nullCommentException); + + // when + ValueTask addCommentTask = + this.commentService.AddCommentAsync(nullComment); + + CommentValidationException actualCommentValidationException = + await Assert.ThrowsAsync( + addCommentTask.AsTask); + + // then + actualCommentValidationException.Should().BeEquivalentTo( + expectedCommentValidationException); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedCommentValidationException))), + Times.Once); + + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + this.storageBrokerMock.VerifyNoOtherCalls(); + } + + [Theory] + [InlineData(null)] + [InlineData("")] + [InlineData(" ")] + private async Task ShouldThrowValidationExceptionOnAddIfCommentIsInvalidAndLogItAsync( + string invalidText) + { + // given + var invalidComment = new Comment + { + Content = invalidText + }; + + var invalidCommentException = + new InvalidCommentException(); + + invalidCommentException.AddData( + key: nameof(Comment.Id), + values: "Id is required"); + + invalidCommentException.AddData( + key: nameof(Comment.Content), + values: "Text is required"); + + invalidCommentException.AddData( + key: nameof(Comment.CreatedDate), + values: "Date is required"); + + invalidCommentException.AddData( + key: nameof(Comment.UpdatedDate), + values: "Date is required"); + + invalidCommentException.AddData( + key: nameof(Comment.PostId), + values: "Id is required"); + + var expectedCommentValidationException = + new CommentValidationException( + message: "Comment validation errors occurred, please try again.", + innerException: invalidCommentException); + + // when + ValueTask addCommentTask = + this.commentService.AddCommentAsync(invalidComment); + + CommentValidationException actualCommentValidationException = + await Assert.ThrowsAsync( + addCommentTask.AsTask); + + // then + actualCommentValidationException.Should().BeEquivalentTo( + expectedCommentValidationException); + + this.dateTimeBrokerMock.Verify(broker => + broker.GetCurrentDateTimeOffset(), + Times.Once()); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedCommentValidationException))), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.InsertCommentAsync(It.IsAny()), + Times.Never); + + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + this.storageBrokerMock.VerifyNoOtherCalls(); + } + + [Fact] + private async Task ShouldThrowValidationExceptionOnAddIfCreateAndUpdateDatesIsNotSameAndLogItAsync() + { + // given + int randomNumber = GetRandomNumber(); + DateTimeOffset randomDateTimeOffset = GetRandomDateTimeOffset(); + Comment randomComment = CreateRandomComment(randomDateTimeOffset); + Comment invalidComment = randomComment; + + invalidComment.UpdatedDate = + invalidComment.CreatedDate.AddDays(randomNumber); + + var invalidCommentException = + new InvalidCommentException(); + + invalidCommentException.AddData( + key: nameof(Comment.UpdatedDate), + values: $"Date is not the same as {nameof(Comment.CreatedDate)}"); + + var expectedCommentValidationException = + new CommentValidationException( + message: "Comment validation errors occurred, please try again.", + innerException: invalidCommentException); + + this.dateTimeBrokerMock.Setup(broker => + broker.GetCurrentDateTimeOffset()) + .Returns(randomDateTimeOffset); + + // when + ValueTask addCommentTask = + this.commentService.AddCommentAsync(invalidComment); + + CommentValidationException actualCommentValidationException = + await Assert.ThrowsAsync( + addCommentTask.AsTask); + + // then + actualCommentValidationException.Should().BeEquivalentTo(expectedCommentValidationException); + + this.dateTimeBrokerMock.Verify(broker => + broker.GetCurrentDateTimeOffset(), + Times.Once()); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedCommentValidationException))), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.InsertCommentAsync(It.IsAny()), + Times.Never); + + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + this.storageBrokerMock.VerifyNoOtherCalls(); + } + + [Theory] + [MemberData(nameof(MinutesBeforeOrAfter))] + private async Task ShouldThrowValidationExceptionOnAddIfCreatedDateIsNotRecentAndLogItAsync( + int minutesBeforeOrAfter) + { + // given + DateTimeOffset randomDateTime = + GetRandomDateTimeOffset(); + + DateTimeOffset invalidDateTime = + randomDateTime.AddMinutes(minutesBeforeOrAfter); + + Comment randomComment = CreateRandomComment(invalidDateTime); + Comment invalidComment = randomComment; + + var invalidCommentException = + new InvalidCommentException(); + + invalidCommentException.AddData( + key: nameof(Comment.CreatedDate), + values: "Date is not recent"); + + var expectedCommentValidationException = + new CommentValidationException( + message: "Comment validation errors occurred, please try again.", + innerException: invalidCommentException); + + this.dateTimeBrokerMock.Setup(broker => + broker.GetCurrentDateTimeOffset()) + .Returns(randomDateTime); + + // when + ValueTask addCommentTask = + this.commentService.AddCommentAsync(invalidComment); + + CommentValidationException actualCommentValidationException = + await Assert.ThrowsAsync( + addCommentTask.AsTask); + + // then + actualCommentValidationException.Should().BeEquivalentTo( + expectedCommentValidationException); + + this.dateTimeBrokerMock.Verify(broker => + broker.GetCurrentDateTimeOffset(), + Times.Once()); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedCommentValidationException))), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.InsertCommentAsync(It.IsAny()), + Times.Never); + + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + this.storageBrokerMock.VerifyNoOtherCalls(); + } + } +} \ No newline at end of file diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Validations.RemoveById.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Validations.RemoveById.cs index 0d728ffe..504a7ce2 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Validations.RemoveById.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Validations.RemoveById.cs @@ -13,95 +13,99 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Comments { - public partial class CommentServiceTests - { - [Fact] - public async Task ShouldThrowValidationExceptionOnRemoveIfIdIsInvalidAndLogItAsync() - { - // given - Guid invalidCommentId = Guid.Empty; - - var invalidCommentException = - new InvalidCommentException(); - - invalidCommentException.AddData( - key: nameof(Comment.Id), - values: "Id is required"); - - var expectedCommentValidationException = - new CommentValidationException(invalidCommentException); - - // when - ValueTask removeCommentByIdTask = - this.commentService.RemoveCommentByIdAsync(invalidCommentId); - - CommentValidationException actualCommentValidationException = - await Assert.ThrowsAsync( - removeCommentByIdTask.AsTask); - - // then - actualCommentValidationException.Should().BeEquivalentTo( - expectedCommentValidationException); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedCommentValidationException))), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.DeleteCommentAsync(It.IsAny()), - Times.Never); - - this.loggingBrokerMock.VerifyNoOtherCalls(); - this.storageBrokerMock.VerifyNoOtherCalls(); - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - } - - [Fact] - public async Task ShouldThrowNotFoundExceptionOnRemoveIfCommentIsNotFoundAndLogItAsync() - { - // given - Guid inputCommentId = Guid.NewGuid(); - Comment noComment = null; - - var notFoundCommentException = - new NotFoundCommentException(inputCommentId); - - var expectedCommentValidationException = - new CommentValidationException(notFoundCommentException); - - this.storageBrokerMock.Setup(broker => - broker.SelectCommentByIdAsync(It.IsAny())) - .ReturnsAsync(noComment); - - // when - ValueTask removeCommentByIdTask = - this.commentService.RemoveCommentByIdAsync(inputCommentId); - - CommentValidationException actualCommentValidationException = - await Assert.ThrowsAsync( - removeCommentByIdTask.AsTask); - - // then - actualCommentValidationException.Should().BeEquivalentTo( - expectedCommentValidationException); - - this.storageBrokerMock.Verify(broker => - broker.SelectCommentByIdAsync(It.IsAny()), - Times.Once); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedCommentValidationException))), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.DeleteCommentAsync(It.IsAny()), - Times.Never); - - this.storageBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - } - } + public partial class CommentServiceTests + { + [Fact] + private async Task ShouldThrowValidationExceptionOnRemoveIfIdIsInvalidAndLogItAsync() + { + // given + Guid invalidCommentId = Guid.Empty; + + var invalidCommentException = + new InvalidCommentException(); + + invalidCommentException.AddData( + key: nameof(Comment.Id), + values: "Id is required"); + + var expectedCommentValidationException = + new CommentValidationException( + message: "Comment validation errors occurred, please try again.", + innerException: invalidCommentException); + + // when + ValueTask removeCommentByIdTask = + this.commentService.RemoveCommentByIdAsync(invalidCommentId); + + CommentValidationException actualCommentValidationException = + await Assert.ThrowsAsync( + removeCommentByIdTask.AsTask); + + // then + actualCommentValidationException.Should().BeEquivalentTo( + expectedCommentValidationException); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedCommentValidationException))), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.DeleteCommentAsync(It.IsAny()), + Times.Never); + + this.loggingBrokerMock.VerifyNoOtherCalls(); + this.storageBrokerMock.VerifyNoOtherCalls(); + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + } + + [Fact] + public async Task ShouldThrowNotFoundExceptionOnRemoveIfCommentIsNotFoundAndLogItAsync() + { + // given + Guid inputCommentId = Guid.NewGuid(); + Comment noComment = null; + + var notFoundCommentException = + new NotFoundCommentException(inputCommentId); + + var expectedCommentValidationException = + new CommentValidationException( + message: "Comment validation errors occurred, please try again.", + innerException: notFoundCommentException); + + this.storageBrokerMock.Setup(broker => + broker.SelectCommentByIdAsync(It.IsAny())) + .ReturnsAsync(noComment); + + // when + ValueTask removeCommentByIdTask = + this.commentService.RemoveCommentByIdAsync(inputCommentId); + + CommentValidationException actualCommentValidationException = + await Assert.ThrowsAsync( + removeCommentByIdTask.AsTask); + + // then + actualCommentValidationException.Should().BeEquivalentTo( + expectedCommentValidationException); + + this.storageBrokerMock.Verify(broker => + broker.SelectCommentByIdAsync(It.IsAny()), + Times.Once); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedCommentValidationException))), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.DeleteCommentAsync(It.IsAny()), + Times.Never); + + this.storageBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + } + } } diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Validations.RetrieveById.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Validations.RetrieveById.cs index 27a266be..3e9affc4 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Validations.RetrieveById.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Validations.RetrieveById.cs @@ -13,91 +13,95 @@ namespace Taarafo.Core.Tests.Unit.Services.Foundations.Comments { - public partial class CommentServiceTests - { - [Fact] - public async Task ShouldThrowValidationExceptionOnRetrieveByIdIfIdIsInvalidAndLogItAsync() - { - // given - var invalidCommentId = Guid.Empty; - - var invalidCommentException = - new InvalidCommentException(); - - invalidCommentException.AddData( - key: nameof(Comment.Id), - values: "Id is required"); - - var expectedCommentValidationException = - new CommentValidationException(invalidCommentException); - - // when - ValueTask retrieveCommentByIdTask = - this.commentService.RetrieveCommentByIdAsync(invalidCommentId); - - CommentValidationException actualCommentValidationException = - await Assert.ThrowsAsync( - retrieveCommentByIdTask.AsTask); - - // then - actualCommentValidationException.Should().BeEquivalentTo( - expectedCommentValidationException); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedCommentValidationException))), - Times.Once); - - this.storageBrokerMock.Verify(broker => - broker.SelectCommentByIdAsync(It.IsAny()), - Times.Never); - - this.loggingBrokerMock.VerifyNoOtherCalls(); - this.storageBrokerMock.VerifyNoOtherCalls(); - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - } - - [Fact] - public async Task ShouldThrowNotFoundExceptionOnRetrieveByIdIfCommentIsNotFoundAndLogItAsync() - { - //given - Guid someCommentId = Guid.NewGuid(); - Comment noComment = null; - - var notFoundCommentException = - new NotFoundCommentException(someCommentId); - - var expectedCommentValidationException = - new CommentValidationException(notFoundCommentException); - - this.storageBrokerMock.Setup(broker => - broker.SelectCommentByIdAsync(It.IsAny())) - .ReturnsAsync(noComment); - - //when - ValueTask retrieveCommentByIdTask = - this.commentService.RetrieveCommentByIdAsync(someCommentId); - - CommentValidationException actualCommentValidationException = - await Assert.ThrowsAsync( - retrieveCommentByIdTask.AsTask); - - // then - actualCommentValidationException.Should().BeEquivalentTo( - expectedCommentValidationException); - - this.storageBrokerMock.Verify(broker => - broker.SelectCommentByIdAsync(It.IsAny()), - Times.Once()); - - this.loggingBrokerMock.Verify(broker => - broker.LogError(It.Is(SameExceptionAs( - expectedCommentValidationException))), - Times.Once); - - this.storageBrokerMock.VerifyNoOtherCalls(); - this.loggingBrokerMock.VerifyNoOtherCalls(); - this.dateTimeBrokerMock.VerifyNoOtherCalls(); - } - } -} + public partial class CommentServiceTests + { + [Fact] + private async Task ShouldThrowValidationExceptionOnRetrieveByIdIfIdIsInvalidAndLogItAsync() + { + // given + var invalidCommentId = Guid.Empty; + + var invalidCommentException = + new InvalidCommentException(); + + invalidCommentException.AddData( + key: nameof(Comment.Id), + values: "Id is required"); + + var expectedCommentValidationException = + new CommentValidationException( + message: "Comment validation errors occurred, please try again.", + innerException: invalidCommentException); + + // when + ValueTask retrieveCommentByIdTask = + this.commentService.RetrieveCommentByIdAsync(invalidCommentId); + + CommentValidationException actualCommentValidationException = + await Assert.ThrowsAsync( + retrieveCommentByIdTask.AsTask); + + // then + actualCommentValidationException.Should().BeEquivalentTo( + expectedCommentValidationException); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedCommentValidationException))), + Times.Once); + + this.storageBrokerMock.Verify(broker => + broker.SelectCommentByIdAsync(It.IsAny()), + Times.Never); + + this.loggingBrokerMock.VerifyNoOtherCalls(); + this.storageBrokerMock.VerifyNoOtherCalls(); + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + } + + [Fact] + private async Task ShouldThrowNotFoundExceptionOnRetrieveByIdIfCommentIsNotFoundAndLogItAsync() + { + //given + Guid someCommentId = Guid.NewGuid(); + Comment noComment = null; + + var notFoundCommentException = + new NotFoundCommentException(someCommentId); + + var expectedCommentValidationException = + new CommentValidationException( + message: "Comment validation errors occurred, please try again.", + innerException: notFoundCommentException); + + this.storageBrokerMock.Setup(broker => + broker.SelectCommentByIdAsync(It.IsAny())) + .ReturnsAsync(noComment); + + //when + ValueTask retrieveCommentByIdTask = + this.commentService.RetrieveCommentByIdAsync(someCommentId); + + CommentValidationException actualCommentValidationException = + await Assert.ThrowsAsync( + retrieveCommentByIdTask.AsTask); + + // then + actualCommentValidationException.Should().BeEquivalentTo( + expectedCommentValidationException); + + this.storageBrokerMock.Verify(broker => + broker.SelectCommentByIdAsync(It.IsAny()), + Times.Once()); + + this.loggingBrokerMock.Verify(broker => + broker.LogError(It.Is(SameExceptionAs( + expectedCommentValidationException))), + Times.Once); + + this.storageBrokerMock.VerifyNoOtherCalls(); + this.loggingBrokerMock.VerifyNoOtherCalls(); + this.dateTimeBrokerMock.VerifyNoOtherCalls(); + } + } +} \ No newline at end of file diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.cs index 4a550cbb..19e61729 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.cs @@ -8,6 +8,7 @@ using System.Linq; using System.Linq.Expressions; using System.Runtime.Serialization; +using EFxceptions.Models.Exceptions; using Microsoft.Data.SqlClient; using Moq; using Taarafo.Core.Brokers.DateTimes; @@ -67,6 +68,10 @@ private static string GetRandomMessage() => private static SqlException GetSqlException() => (SqlException)FormatterServices.GetUninitializedObject(typeof(SqlException)); + private static ForeignKeyConstraintConflictException GetForeignKeyException() => + (ForeignKeyConstraintConflictException)FormatterServices + .GetUninitializedObject(typeof(ForeignKeyConstraintConflictException)); + private static int GetRandomNumber() => new IntRange(min: 2, max: 10).GetValue(); diff --git a/Taarafo.Core/Models/Comments/Comment.cs b/Taarafo.Core/Models/Comments/Comment.cs index e6c2432a..cd9b20c8 100644 --- a/Taarafo.Core/Models/Comments/Comment.cs +++ b/Taarafo.Core/Models/Comments/Comment.cs @@ -14,7 +14,6 @@ public class Comment public string Content { get; set; } public DateTimeOffset CreatedDate { get; set; } public DateTimeOffset UpdatedDate { get; set; } - public Guid PostId { get; set; } public Post Post { get; set; } } diff --git a/Taarafo.Core/Models/Comments/Exceptions/CommentDependencyException.cs b/Taarafo.Core/Models/Comments/Exceptions/CommentDependencyException.cs index 0a25d552..d2497d6c 100644 --- a/Taarafo.Core/Models/Comments/Exceptions/CommentDependencyException.cs +++ b/Taarafo.Core/Models/Comments/Exceptions/CommentDependencyException.cs @@ -13,9 +13,10 @@ public class CommentDependencyException : Xeption public CommentDependencyException(Exception innerException) : base( message: "Comment dependency error occurred, contact support.", - innerException: innerException) { } + innerException: innerException) + { } - public CommentDependencyException(string message, Exception innerException) + public CommentDependencyException(string message, Exception innerException) : base(message, innerException) { } - } + } } \ No newline at end of file diff --git a/Taarafo.Core/Models/Comments/Exceptions/CommentServiceException.cs b/Taarafo.Core/Models/Comments/Exceptions/CommentServiceException.cs index 1bf86454..3a00bf3f 100644 --- a/Taarafo.Core/Models/Comments/Exceptions/CommentServiceException.cs +++ b/Taarafo.Core/Models/Comments/Exceptions/CommentServiceException.cs @@ -8,14 +8,15 @@ namespace Taarafo.Core.Models.Comments.Exceptions { - public class CommentServiceException : Xeption - { - public CommentServiceException(Exception innerException) - : base( - message: "Comment service error occurred, contact support.", - innerException: innerException) { } + public class CommentServiceException : Xeption + { + public CommentServiceException(Exception innerException) + : base( + message: "Comment service error occurred, contact support.", + innerException: innerException) + { } public CommentServiceException(string message, Exception innerException) - : base(message, innerException) { } + : base(message, innerException) { } } } \ No newline at end of file diff --git a/Taarafo.Core/Models/Comments/Exceptions/CommentValidationException.cs b/Taarafo.Core/Models/Comments/Exceptions/CommentValidationException.cs index fbfab181..02435ae6 100644 --- a/Taarafo.Core/Models/Comments/Exceptions/CommentValidationException.cs +++ b/Taarafo.Core/Models/Comments/Exceptions/CommentValidationException.cs @@ -14,7 +14,7 @@ public CommentValidationException(Xeption innerException) message: "Comment validation errors occurred, please try again.", innerException: innerException) { } - public CommentValidationException(string message, Xeption innerException) + public CommentValidationException(string message, Xeption innerException) : base(message, innerException) { } - } + } } \ No newline at end of file diff --git a/Taarafo.Core/Models/Comments/Exceptions/ForeignKeyCommentReferenceException.cs b/Taarafo.Core/Models/Comments/Exceptions/ForeignKeyCommentReferenceException.cs new file mode 100644 index 00000000..00e968d4 --- /dev/null +++ b/Taarafo.Core/Models/Comments/Exceptions/ForeignKeyCommentReferenceException.cs @@ -0,0 +1,21 @@ +// --------------------------------------------------------------- +// Copyright (c) Coalition of the Good-Hearted Engineers +// FREE TO USE TO CONNECT THE WORLD +// --------------------------------------------------------------- + +using System; +using Xeptions; + +namespace Taarafo.Core.Models.Comments.Exceptions +{ + public class ForeignKeyCommentReferenceException : Xeption + { + public ForeignKeyCommentReferenceException(Exception innerException) + : base( + message: "ForeignKey exception has occurred, contact support.", + innerException: innerException) { } + + public ForeignKeyCommentReferenceException(string message, Exception innerException) + : base(message, innerException) { } + } + } \ No newline at end of file diff --git a/Taarafo.Core/Services/Foundations/Comments/CommentService.Exceptions.cs b/Taarafo.Core/Services/Foundations/Comments/CommentService.Exceptions.cs index 22077008..73e51d45 100644 --- a/Taarafo.Core/Services/Foundations/Comments/CommentService.Exceptions.cs +++ b/Taarafo.Core/Services/Foundations/Comments/CommentService.Exceptions.cs @@ -52,10 +52,10 @@ private async ValueTask TryCatch(ReturningCommentFunction returningComm throw CreateAndLogDependencyValidationException(alreadyExistsCommentException); } - catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException) + catch (ForeignKeyCommentReferenceException foreignKeyCommentReferenceException) { var invalidCommentReferenceException = - new InvalidCommentReferenceException(foreignKeyConstraintConflictException); + new InvalidCommentReferenceException(foreignKeyCommentReferenceException); throw CreateAndLogDependencyValidationException(invalidCommentReferenceException); } diff --git a/Taarafo.Core/Services/Foundations/Comments/CommentService.Validations.cs b/Taarafo.Core/Services/Foundations/Comments/CommentService.Validations.cs index 22265565..d8030eed 100644 --- a/Taarafo.Core/Services/Foundations/Comments/CommentService.Validations.cs +++ b/Taarafo.Core/Services/Foundations/Comments/CommentService.Validations.cs @@ -85,6 +85,7 @@ private static void ValidateCommentIsNotNull(Comment comment) private static dynamic IsNotSame( DateTimeOffset firstDate, DateTimeOffset secondDate, + string secondDateName) => new { Condition = firstDate != secondDate, @@ -94,6 +95,7 @@ private static dynamic IsNotSame( private static dynamic IsSame( DateTimeOffset firstDate, DateTimeOffset secondDate, + string secondDateName) => new { Condition = firstDate == secondDate, @@ -156,4 +158,4 @@ private static void Validate(params (dynamic Rule, string Parameter)[] validatio invalidCommentException.ThrowIfContainsErrors(); } } -} +} \ No newline at end of file diff --git a/Taarafo.Core/Services/Foundations/Comments/CommentService.cs b/Taarafo.Core/Services/Foundations/Comments/CommentService.cs index 898438bf..ec456b65 100644 --- a/Taarafo.Core/Services/Foundations/Comments/CommentService.cs +++ b/Taarafo.Core/Services/Foundations/Comments/CommentService.cs @@ -13,72 +13,72 @@ namespace Taarafo.Core.Services.Foundations.Comments { - public partial class CommentService : ICommentService - { - private readonly IStorageBroker storageBroker; - private readonly IDateTimeBroker dateTimeBroker; - private readonly ILoggingBroker loggingBroker; - - public CommentService( - IStorageBroker storageBroker, - IDateTimeBroker dateTimeBroker, - ILoggingBroker loggingBroker) - { - this.storageBroker = storageBroker; - this.dateTimeBroker = dateTimeBroker; - this.loggingBroker = loggingBroker; - } - - public ValueTask AddCommentAsync(Comment comment) => - TryCatch(async () => - { - ValidateCommentOnAdd(comment); - - return await this.storageBroker.InsertCommentAsync(comment); - }); - - public ValueTask RetrieveCommentByIdAsync(Guid commentId) => - TryCatch(async () => - { - ValidateCommentId(commentId); - - Comment maybeComment = await this.storageBroker - .SelectCommentByIdAsync(commentId); - - ValidateStorageComment(maybeComment, commentId); - - return maybeComment; - }); - - public IQueryable RetrieveAllComments() => - TryCatch(() => this.storageBroker.SelectAllComments()); - - public ValueTask ModifyCommentAsync(Comment comment) => - TryCatch(async () => - { - ValidateCommentOnModify(comment); - - Comment maybeComment = - await this.storageBroker.SelectCommentByIdAsync(comment.Id); - - ValidateStorageComment(maybeComment, comment.Id); - ValidateAginstStorageCommentOnModify(inputComment: comment, storageComment: maybeComment); - - return await this.storageBroker.UpdateCommentAsync(comment); - }); - - public ValueTask RemoveCommentByIdAsync(Guid commentId) => - TryCatch(async () => - { - ValidateCommentId(commentId); - - Comment maybeComment = await this.storageBroker - .SelectCommentByIdAsync(commentId); - - ValidateStorageComment(maybeComment, commentId); - - return await this.storageBroker - .DeleteCommentAsync(maybeComment); - }); - } -} + public partial class CommentService : ICommentService + { + private readonly IStorageBroker storageBroker; + private readonly IDateTimeBroker dateTimeBroker; + private readonly ILoggingBroker loggingBroker; + + public CommentService( + IStorageBroker storageBroker, + IDateTimeBroker dateTimeBroker, + ILoggingBroker loggingBroker) + { + this.storageBroker = storageBroker; + this.dateTimeBroker = dateTimeBroker; + this.loggingBroker = loggingBroker; + } + + public ValueTask AddCommentAsync(Comment comment) => + TryCatch(async () => + { + ValidateCommentOnAdd(comment); + + return await this.storageBroker.InsertCommentAsync(comment); + }); + + public ValueTask RetrieveCommentByIdAsync(Guid commentId) => + TryCatch(async () => + { + ValidateCommentId(commentId); + + Comment maybeComment = await this.storageBroker + .SelectCommentByIdAsync(commentId); + + ValidateStorageComment(maybeComment, commentId); + + return maybeComment; + }); + + public IQueryable RetrieveAllComments() => + TryCatch(() => this.storageBroker.SelectAllComments()); + + public ValueTask ModifyCommentAsync(Comment comment) => + TryCatch(async () => + { + ValidateCommentOnModify(comment); + + Comment maybeComment = + await this.storageBroker.SelectCommentByIdAsync(comment.Id); + + ValidateStorageComment(maybeComment, comment.Id); + ValidateAginstStorageCommentOnModify(inputComment: comment, storageComment: maybeComment); + + return await this.storageBroker.UpdateCommentAsync(comment); + }); + + public ValueTask RemoveCommentByIdAsync(Guid commentId) => + TryCatch(async () => + { + ValidateCommentId(commentId); + + Comment maybeComment = await this.storageBroker + .SelectCommentByIdAsync(commentId); + + ValidateStorageComment(maybeComment, commentId); + + return await this.storageBroker + .DeleteCommentAsync(maybeComment); + }); + } +} \ No newline at end of file diff --git a/Taarafo.Core/Services/Foundations/Comments/ICommentService.cs b/Taarafo.Core/Services/Foundations/Comments/ICommentService.cs index b9549a9f..071ccdad 100644 --- a/Taarafo.Core/Services/Foundations/Comments/ICommentService.cs +++ b/Taarafo.Core/Services/Foundations/Comments/ICommentService.cs @@ -18,4 +18,4 @@ public interface ICommentService ValueTask ModifyCommentAsync(Comment comment); ValueTask RemoveCommentByIdAsync(Guid commentId); } -} +} \ No newline at end of file From fce1d728616f6643813cc44ea1a6d36f41e9cd46 Mon Sep 17 00:00:00 2001 From: Greg Hays Date: Tue, 15 Aug 2023 14:03:14 -0700 Subject: [PATCH 3/3] CODE RUB: Fix Failing Tests --- .../CommentServiceTests.Exceptions.Add.cs | 5 +- .../CommentServiceTests.Validation.Modify.cs | 4 +- .../Comments/CommentService.Exceptions.cs | 290 +++++++++--------- 3 files changed, 148 insertions(+), 151 deletions(-) diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Exceptions.Add.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Exceptions.Add.cs index 38ec67cc..01e85b6d 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Exceptions.Add.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Exceptions.Add.cs @@ -233,11 +233,10 @@ private async void ShouldThrowValidationExceptionOnAddIfReferenceErrorOccursAndL { // given Comment someComment = CreateRandomComment(); - string randomMessage = GetRandomMessage(); - string exceptionMessage = randomMessage; + ForeignKeyConstraintConflictException foreignKeyException = GetForeignKeyException(); var foreignKeyConstraintConflictException = - new ForeignKeyConstraintConflictException(exceptionMessage); + new ForeignKeyCommentReferenceException(foreignKeyException); var invalidCommentReferenceException = new InvalidCommentReferenceException( diff --git a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Validation.Modify.cs b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Validation.Modify.cs index 3aebb472..7917afe1 100644 --- a/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Validation.Modify.cs +++ b/Taarafo.Core.Tests.Unit/Services/Foundations/Comments/CommentServiceTests.Validation.Modify.cs @@ -427,9 +427,7 @@ private async void ShouldThrowValidationExceptionOnModifyIfForeignKeyReferenceEr ForeignKeyConstraintConflictException foreignKeyException = GetForeignKeyException(); var foreignKeyConstraintConflictException = - new ForeignKeyCommentReferenceException( - message: "ForeignKey exception has occurred, contact support.", - innerException: foreignKeyException); + new ForeignKeyCommentReferenceException(foreignKeyException); var invalidCommentReferenceException = new InvalidCommentReferenceException( diff --git a/Taarafo.Core/Services/Foundations/Comments/CommentService.Exceptions.cs b/Taarafo.Core/Services/Foundations/Comments/CommentService.Exceptions.cs index 73e51d45..38a928cd 100644 --- a/Taarafo.Core/Services/Foundations/Comments/CommentService.Exceptions.cs +++ b/Taarafo.Core/Services/Foundations/Comments/CommentService.Exceptions.cs @@ -15,149 +15,149 @@ namespace Taarafo.Core.Services.Foundations.Comments { - public partial class CommentService - { - private delegate ValueTask ReturningCommentFunction(); - private delegate IQueryable ReturningCommentsFunction(); - - private async ValueTask TryCatch(ReturningCommentFunction returningCommentFunction) - { - try - { - return await returningCommentFunction(); - } - catch (NullCommentException nullCommentException) - { - throw CreateAndLogValidationException(nullCommentException); - } - catch (InvalidCommentException invalidCommentException) - { - throw CreateAndLogValidationException(invalidCommentException); - } - catch (SqlException sqlException) - { - var failedCommentStorageException = - new FailedCommentStorageException(sqlException); - - throw CreateAndLogCriticalDependencyException(failedCommentStorageException); - } - catch (NotFoundCommentException notFoundCommentException) - { - throw CreateAndLogValidationException(notFoundCommentException); - } - catch (DuplicateKeyException duplicateKeyException) - { - var alreadyExistsCommentException = - new AlreadyExistsCommentException(duplicateKeyException); - - throw CreateAndLogDependencyValidationException(alreadyExistsCommentException); - } - catch (ForeignKeyCommentReferenceException foreignKeyCommentReferenceException) - { - var invalidCommentReferenceException = - new InvalidCommentReferenceException(foreignKeyCommentReferenceException); - - throw CreateAndLogDependencyValidationException(invalidCommentReferenceException); - } - catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) - { - var lockedCommentException = new LockedCommentException(dbUpdateConcurrencyException); - - throw CreateAndLogDependencyValidationException(lockedCommentException); - } - catch (DbUpdateException databaseUpdateException) - { - var failedCommentStorageException = - new FailedCommentStorageException(databaseUpdateException); - - throw CreateAndLogDependecyException(failedCommentStorageException); - } - catch (Exception exception) - { - var failedCommentServiceException = - new FailedCommentServiceException(exception); - - throw CreateAndLogServiceException(failedCommentServiceException); - } - } - - private IQueryable TryCatch(ReturningCommentsFunction returningCommentsFunction) - { - try - { - return returningCommentsFunction(); - } - catch (SqlException sqlException) - { - var failedCommentStorageException = - new FailedCommentStorageException(sqlException); - throw CreateAndLogCriticalDependencyException(failedCommentStorageException); - } - catch (Exception exception) - { - var failedCommentServiceException = - new FailedCommentServiceException(exception); - - throw CreateAndLogServiceException(failedCommentServiceException); - } - } - - private CommentValidationException CreateAndLogValidationException( - Xeption exception) - { - var commentValidationException = - new CommentValidationException(exception); - - this.loggingBroker.LogError(commentValidationException); - - return commentValidationException; - } - - private CommentDependencyException CreateAndLogCriticalDependencyException( - Xeption exception) - { - var commentDependencyException = new CommentDependencyException(exception); - this.loggingBroker.LogCritical(commentDependencyException); - - return commentDependencyException; - } - - private CommentDependencyValidationException CreateAndLogDependencyValidationException( - Xeption exception) - { - var commentDependencyValidationException = - new CommentDependencyValidationException(exception); - - this.loggingBroker.LogError(commentDependencyValidationException); - - return commentDependencyValidationException; - } - - private CommentDependencyException CreateAndLogDependecyException( - Xeption exception) - { - var commentDependencyException = new CommentDependencyException(exception); - this.loggingBroker.LogError(commentDependencyException); - - return commentDependencyException; - } - - private CommentServiceException CreateAndLogServiceException( - Xeption exception) - { - var commentServiceException = new CommentServiceException(exception); - this.loggingBroker.LogError(commentServiceException); - - return commentServiceException; - } - - private CommentServiceException CreateAndLogServiceException( - Exception exception) - { - var commentServiceException = new CommentServiceException(exception); - this.loggingBroker.LogError(commentServiceException); - - return commentServiceException; - } - } + public partial class CommentService + { + private delegate ValueTask ReturningCommentFunction(); + private delegate IQueryable ReturningCommentsFunction(); + + private async ValueTask TryCatch(ReturningCommentFunction returningCommentFunction) + { + try + { + return await returningCommentFunction(); + } + catch (NullCommentException nullCommentException) + { + throw CreateAndLogValidationException(nullCommentException); + } + catch (InvalidCommentException invalidCommentException) + { + throw CreateAndLogValidationException(invalidCommentException); + } + catch (SqlException sqlException) + { + var failedCommentStorageException = + new FailedCommentStorageException(sqlException); + + throw CreateAndLogCriticalDependencyException(failedCommentStorageException); + } + catch (NotFoundCommentException notFoundCommentException) + { + throw CreateAndLogValidationException(notFoundCommentException); + } + catch (DuplicateKeyException duplicateKeyException) + { + var alreadyExistsCommentException = + new AlreadyExistsCommentException(duplicateKeyException); + + throw CreateAndLogDependencyValidationException(alreadyExistsCommentException); + } + catch (ForeignKeyCommentReferenceException foreignKeyCommentReferenceException) + { + var invalidCommentReferenceException = + new InvalidCommentReferenceException(foreignKeyCommentReferenceException); + + throw CreateAndLogDependencyValidationException(invalidCommentReferenceException); + } + catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) + { + var lockedCommentException = new LockedCommentException(dbUpdateConcurrencyException); + + throw CreateAndLogDependencyValidationException(lockedCommentException); + } + catch (DbUpdateException databaseUpdateException) + { + var failedCommentStorageException = + new FailedCommentStorageException(databaseUpdateException); + + throw CreateAndLogDependecyException(failedCommentStorageException); + } + catch (Exception exception) + { + var failedCommentServiceException = + new FailedCommentServiceException(exception); + + throw CreateAndLogServiceException(failedCommentServiceException); + } + } + + private IQueryable TryCatch(ReturningCommentsFunction returningCommentsFunction) + { + try + { + return returningCommentsFunction(); + } + catch (SqlException sqlException) + { + var failedCommentStorageException = + new FailedCommentStorageException(sqlException); + throw CreateAndLogCriticalDependencyException(failedCommentStorageException); + } + catch (Exception exception) + { + var failedCommentServiceException = + new FailedCommentServiceException(exception); + + throw CreateAndLogServiceException(failedCommentServiceException); + } + } + + private CommentValidationException CreateAndLogValidationException( + Xeption exception) + { + var commentValidationException = + new CommentValidationException(exception); + + this.loggingBroker.LogError(commentValidationException); + + return commentValidationException; + } + + private CommentDependencyException CreateAndLogCriticalDependencyException( + Xeption exception) + { + var commentDependencyException = new CommentDependencyException(exception); + this.loggingBroker.LogCritical(commentDependencyException); + + return commentDependencyException; + } + + private CommentDependencyValidationException CreateAndLogDependencyValidationException( + Xeption exception) + { + var commentDependencyValidationException = + new CommentDependencyValidationException(exception); + + this.loggingBroker.LogError(commentDependencyValidationException); + + return commentDependencyValidationException; + } + + private CommentDependencyException CreateAndLogDependecyException( + Xeption exception) + { + var commentDependencyException = new CommentDependencyException(exception); + this.loggingBroker.LogError(commentDependencyException); + + return commentDependencyException; + } + + private CommentServiceException CreateAndLogServiceException( + Xeption exception) + { + var commentServiceException = new CommentServiceException(exception); + this.loggingBroker.LogError(commentServiceException); + + return commentServiceException; + } + + private CommentServiceException CreateAndLogServiceException( + Exception exception) + { + var commentServiceException = new CommentServiceException(exception); + this.loggingBroker.LogError(commentServiceException); + + return commentServiceException; + } + } }