public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAndLogItAsync() { // given int randomNegativeNumber = GetNegativeRandomNumber(); DateTimeOffset randomDateTime = GetRandomDateTime(); StudentExamFee someStudentExamFee = CreateRandomStudentExamFee(randomDateTime); someStudentExamFee.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber); var serviceException = new Exception(); var failedStudentExamFeeServiceException = new FailedStudentExamFeeServiceException(serviceException); var expectedStudentExamFeeServiceException = new StudentExamFeeServiceException(failedStudentExamFeeServiceException); this.storageBrokerMock.Setup(broker => broker.SelectStudentExamFeeByIdsAsync( It.IsAny <Guid>(), It.IsAny <Guid>())) .ThrowsAsync(serviceException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDateTime); // when ValueTask <StudentExamFee> modifyStudentExamFeeTask = this.studentExamFeeService.ModifyStudentExamFeeAsync(someStudentExamFee); // then await Assert.ThrowsAsync <StudentExamFeeServiceException>(() => modifyStudentExamFeeTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectStudentExamFeeByIdsAsync( It.IsAny <Guid>(), It.IsAny <Guid>()), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedStudentExamFeeServiceException))), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowServiceExceptionOnCreateWhenExceptionOccursAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); StudentExamFee someStudentExamFee = CreateRandomStudentExamFee(dateTime); someStudentExamFee.UpdatedBy = someStudentExamFee.CreatedBy; someStudentExamFee.UpdatedDate = someStudentExamFee.CreatedDate; var serviceException = new Exception(); var failedStudentExamFeeServiceException = new FailedStudentExamFeeServiceException(serviceException); var expectedStudentExamFeeServiceException = new StudentExamFeeServiceException(failedStudentExamFeeServiceException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); this.storageBrokerMock.Setup(broker => broker.InsertStudentExamFeeAsync(It.IsAny <StudentExamFee>())) .ThrowsAsync(serviceException); // when ValueTask <StudentExamFee> createStudentExamFeeTask = this.studentExamFeeService.AddStudentExamFeeAsync(someStudentExamFee); // then await Assert.ThrowsAsync <StudentExamFeeServiceException>(() => createStudentExamFeeTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedStudentExamFeeServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertStudentExamFeeAsync(It.IsAny <StudentExamFee>()), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
private async ValueTask <StudentExamFee> TryCatch( ReturningStudentExamFeeFunction returningStudentExamFeeFunction) { try { return(await returningStudentExamFeeFunction()); } catch (NullStudentExamFeeException nullStudentExamFeeException) { throw CreateAndLogValidationException(nullStudentExamFeeException); } catch (NotFoundStudentExamFeeException notFoundStudentExamFeeException) { throw CreateAndLogValidationException(notFoundStudentExamFeeException); } catch (InvalidStudentExamFeeException invalidStudentExamFeeInputException) { throw CreateAndLogValidationException(invalidStudentExamFeeInputException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsStudentExamFeeException = new AlreadyExistsStudentExamFeeException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsStudentExamFeeException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedStudentExamFeeException = new LockedStudentExamFeeException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedStudentExamFeeException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { var failedStudentExamFeeServiceException = new FailedStudentExamFeeServiceException(exception); throw CreateAndLogServiceException(failedStudentExamFeeServiceException); } }
public async Task ShouldThrowServiceExceptionOnRemoveWhenExceptionOccursAndLogItAsync() { // given Guid someStudentId = Guid.NewGuid(); Guid someExamFeeId = Guid.NewGuid(); var serviceException = new Exception(); var failedStudentExamFeeServiceException = new FailedStudentExamFeeServiceException(serviceException); var expectedStudentExamFeeServiceException = new StudentExamFeeServiceException(failedStudentExamFeeServiceException); this.storageBrokerMock.Setup(broker => broker.SelectStudentExamFeeByIdsAsync( It.IsAny <Guid>(), It.IsAny <Guid>())) .Throws(serviceException); // when ValueTask <StudentExamFee> removeStudentExamFeeTask = this.studentExamFeeService.RemoveStudentExamFeeByIdAsync(someStudentId, someExamFeeId); // then await Assert.ThrowsAsync <StudentExamFeeServiceException>(() => removeStudentExamFeeTask.AsTask()); this.storageBrokerMock.Verify(broker => broker.SelectStudentExamFeeByIdsAsync( It.IsAny <Guid>(), It.IsAny <Guid>()), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedStudentExamFeeServiceException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.DeleteStudentExamFeeAsync(It.IsAny <StudentExamFee>()), Times.Never); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
private IQueryable <StudentExamFee> TryCatch( ReturningStudentExamFeesFunction returningStudentExamFeesFunction) { try { return(returningStudentExamFeesFunction()); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (Exception exception) { var failedStudentExamFeeServiceException = new FailedStudentExamFeeServiceException(exception); throw CreateAndLogServiceException(failedStudentExamFeeServiceException); } }
public void ShouldThrowServiceExceptionOnRetrieveAllStudentExamFeesWhenExceptionOccursAndLogIt() { // given var serviceException = new Exception(); var failedStudentExamFeeServiceException = new FailedStudentExamFeeServiceException(serviceException); var expectedStudentExamFeeServiceException = new StudentExamFeeServiceException(failedStudentExamFeeServiceException); this.storageBrokerMock.Setup(broker => broker.SelectAllStudentExamFees()) .Throws(serviceException); // when Action retrieveAllStudentExamFeesAction = () => this.studentExamFeeService.RetrieveAllStudentExamFees(); // then Assert.Throws <StudentExamFeeServiceException>( retrieveAllStudentExamFeesAction); this.storageBrokerMock.Verify(broker => broker.SelectAllStudentExamFees(), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedStudentExamFeeServiceException))), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }