Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CODE RUB: Comments Exceptions Upgrade v2.10.0 #515

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -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<Guid>()))
.ThrowsAsync(databaseUpdateConcurrencyException);

// when
ValueTask<Comment> removeCommentByIdTask =
this.commentService.RemoveCommentByIdAsync(someCommentId);

CommentDependencyValidationException actualCommentDependencyValidationException =
await Assert.ThrowsAsync<CommentDependencyValidationException>(
removeCommentByIdTask.AsTask);

// then
actualCommentDependencyValidationException.Should().BeEquivalentTo(
expectedCommentDependencyValidationException);

this.storageBrokerMock.Verify(broker =>
broker.SelectCommentByIdAsync(It.IsAny<Guid>()),
Times.Once);

this.loggingBrokerMock.Verify(broker =>
broker.LogError(It.Is(SameExceptionAs(
expectedCommentDependencyValidationException))),
Times.Once);

this.storageBrokerMock.Verify(broker =>
broker.DeleteCommentAsync(It.IsAny<Comment>()),
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<Guid>()))
.ThrowsAsync(sqlException);

// when
ValueTask<Comment> deleteCommentTask =
this.commentService.RemoveCommentByIdAsync(someCommentId);

CommentDependencyException actualCommentDependencyException =
await Assert.ThrowsAsync<CommentDependencyException>(
deleteCommentTask.AsTask);

// then
actualCommentDependencyException.Should().BeEquivalentTo(
expectedCommentDependencyException);

this.storageBrokerMock.Verify(broker =>
broker.SelectCommentByIdAsync(It.IsAny<Guid>()),
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<Guid>()))
.ThrowsAsync(serviceException);

// when
ValueTask<Comment> removeCommentByIdTask =
this.commentService.RemoveCommentByIdAsync(someCommentId);

CommentServiceException actualCommentServiceException =
await Assert.ThrowsAsync<CommentServiceException>(
removeCommentByIdTask.AsTask);

// then
actualCommentServiceException.Should().BeEquivalentTo(expectedCommentServiceException);

this.storageBrokerMock.Verify(broker =>
broker.SelectCommentByIdAsync(It.IsAny<Guid>()),
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<Guid>()))
.ThrowsAsync(databaseUpdateConcurrencyException);

// when
ValueTask<Comment> removeCommentByIdTask =
this.commentService.RemoveCommentByIdAsync(someCommentId);

CommentDependencyValidationException actualCommentDependencyValidationException =
await Assert.ThrowsAsync<CommentDependencyValidationException>(
removeCommentByIdTask.AsTask);

// then
actualCommentDependencyValidationException.Should().BeEquivalentTo(
expectedCommentDependencyValidationException);

this.storageBrokerMock.Verify(broker =>
broker.SelectCommentByIdAsync(It.IsAny<Guid>()),
Times.Once);

this.loggingBrokerMock.Verify(broker =>
broker.LogError(It.Is(SameExceptionAs(
expectedCommentDependencyValidationException))),
Times.Once);

this.storageBrokerMock.Verify(broker =>
broker.DeleteCommentAsync(It.IsAny<Comment>()),
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<Guid>()))
.ThrowsAsync(sqlException);

// when
ValueTask<Comment> deleteCommentTask =
this.commentService.RemoveCommentByIdAsync(someCommentId);

CommentDependencyException actualCommentDependencyException =
await Assert.ThrowsAsync<CommentDependencyException>(
deleteCommentTask.AsTask);

// then
actualCommentDependencyException.Should().BeEquivalentTo(
expectedCommentDependencyException);

this.storageBrokerMock.Verify(broker =>
broker.SelectCommentByIdAsync(It.IsAny<Guid>()),
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<Guid>()))
.ThrowsAsync(serviceException);

// when
ValueTask<Comment> removeCommentByIdTask =
this.commentService.RemoveCommentByIdAsync(someCommentId);

CommentServiceException actualCommentServiceException =
await Assert.ThrowsAsync<CommentServiceException>(
removeCommentByIdTask.AsTask);

// then
actualCommentServiceException.Should().BeEquivalentTo(expectedCommentServiceException);

this.storageBrokerMock.Verify(broker =>
broker.SelectCommentByIdAsync(It.IsAny<Guid>()),
Times.Once());

this.loggingBrokerMock.Verify(broker =>
broker.LogError(It.Is(SameExceptionAs(
expectedCommentServiceException))),
Times.Once);

this.storageBrokerMock.VerifyNoOtherCalls();
this.loggingBrokerMock.VerifyNoOtherCalls();
this.dateTimeBrokerMock.VerifyNoOtherCalls();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down
Loading