public async Task ShouldThrowDependencyExceptionOnRetrieveByIdWhenDbExceptionOccursAndLogItAsync() { // given Guid someCommentId = Guid.NewGuid(); var databaseUpdateException = new DbUpdateException(); var expectedCommentDependencyException = new CommentDependencyException(databaseUpdateException); this.storageBrokerMock.Setup(broker => broker.SelectCommentByIdAsync(It.IsAny <Guid>())) .ThrowsAsync(databaseUpdateException); // when ValueTask <Comment> retrieveByIdCommentTask = this.commentService.RetrieveCommentByIdAsync(someCommentId); // then await Assert.ThrowsAsync <CommentDependencyException>(() => retrieveByIdCommentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedCommentDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectCommentByIdAsync(It.IsAny <Guid>()), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowDependencyExceptionOnRetrieveByIdWhenSqlExceptionOccursAndLogItAsync() { // given Guid randomCommentId = Guid.NewGuid(); Guid inputCommentId = randomCommentId; SqlException sqlException = GetSqlException(); var exceptionCommentDependencyException = new CommentDependencyException(sqlException); this.storageBrokerMock.Setup(broker => broker.SelectCommentByIdAsync(inputCommentId)) .ThrowsAsync(sqlException); // when ValueTask <Comment> retrieveCommentByIdTask = this.commentService.RetrieveCommentByIdAsync(inputCommentId); // then await Assert.ThrowsAsync <CommentDependencyException>(() => retrieveCommentByIdTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogCritical(It.Is(SameExceptionAs(exceptionCommentDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectCommentByIdAsync(inputCommentId), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public void ShouldThrowDependencyExceptionOnRetrieveAllCommentsWhenSqlExceptionOccursAndLogIt() { // given var sqlException = GetSqlException(); var expectedCommentDependencyException = new CommentDependencyException(sqlException); this.storageBrokerMock.Setup(broker => broker.SelectAllComments()) .Throws(sqlException); // when . then Assert.Throws <CommentDependencyException>(() => this.commentService.RetrieveAllComments()); this.storageBrokerMock.Verify(broker => broker.SelectAllComments(), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogCritical(It.Is(SameExceptionAs(expectedCommentDependencyException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
private CommentDependencyException CreateAndLogCriticalDependencyException(Exception exception) { var CommentDependencyException = new CommentDependencyException(exception); this.loggingBroker.LogCritical(CommentDependencyException); return(CommentDependencyException); }
private CommentDependencyException CreateAndLogDependencyException(Exception exception) { var commentDependencyException = new CommentDependencyException(exception); this.loggingBroker.LogError(commentDependencyException); return(commentDependencyException); }
public async Task ShouldThrowDependencyExceptionOnAddWhenDbExceptionOccursAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); Comment randomComment = CreateRandomComment(dateTime); Comment inputComment = randomComment; var databaseUpdateException = new DbUpdateException(); var expectedCommentDependencyException = new CommentDependencyException(databaseUpdateException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); this.storageBrokerMock.Setup(broker => broker.InsertCommentAsync(inputComment)) .ThrowsAsync(databaseUpdateException); // when ValueTask <Comment> createCommentTask = this.commentService.AddCommentAsync(inputComment); // then await Assert.ThrowsAsync <CommentDependencyException>(() => createCommentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedCommentDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertCommentAsync(inputComment), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given int randomNegativeNumber = GetNegativeRandomNumber(); DateTimeOffset randomDateTime = GetRandomDateTime(); Comment randomComment = CreateRandomComment(randomDateTime); Comment someComment = randomComment; someComment.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber); var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedCommentException = new LockedCommentException(databaseUpdateConcurrencyException); var expectedCommentDependencyException = new CommentDependencyException(lockedCommentException); this.storageBrokerMock.Setup(broker => broker.SelectCommentByIdAsync(someComment.Id)) .ThrowsAsync(databaseUpdateConcurrencyException); // when ValueTask <Comment> modifyCommentTask = this.commentService.ModifyCommentAsync(someComment); // then await Assert.ThrowsAsync <CommentDependencyException>(() => modifyCommentTask.AsTask()); this.storageBrokerMock.Verify(broker => broker.SelectCommentByIdAsync(someComment.Id), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedCommentDependencyException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowDependencyExceptionOnDeleteWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given Guid randomCommentId = Guid.NewGuid(); Guid inputCommentId = randomCommentId; var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedCommentException = new LockedCommentException(databaseUpdateConcurrencyException); var expectedStudentCommentException = new CommentDependencyException(lockedCommentException); this.storageBrokerMock.Setup(broker => broker.SelectCommentByIdAsync(inputCommentId)) .ThrowsAsync(databaseUpdateConcurrencyException); // when ValueTask <Comment> deleteStudentCommentTask = this.commentService.RemoveCommentByIdAsync(inputCommentId); // then await Assert.ThrowsAsync <CommentDependencyException>(() => deleteStudentCommentTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedStudentCommentException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectCommentByIdAsync(inputCommentId), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }