public void ShouldThrowDependencyExceptionOnRetrieveAllWhenSqlExceptionOccursAndLogIt() { // given var sqlException = GetSqlException(); var expectedGuardianDependencyException = new GuardianDependencyException(sqlException); this.storageBrokerMock.Setup(broker => broker.SelectAllGuardians()) .Throws(sqlException); // when . then Assert.Throws <GuardianDependencyException>(() => this.guardianService.RetrieveAllGuardians()); this.loggingBrokerMock.Verify(broker => broker.LogCritical(It.Is(SameExceptionAs(expectedGuardianDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectAllGuardians(), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
private GuardianDependencyException CreateAndLogCriticalDependencyException(Exception exception) { var guardianDependencyException = new GuardianDependencyException(exception); this.loggingBroker.LogCritical(guardianDependencyException); return(guardianDependencyException); }
public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given int randomNegativeNumber = GetNegativeRandomNumber(); DateTimeOffset randomDateTime = GetRandomDateTime(); Guardian randomGuardian = CreateRandomGuardian(randomDateTime); Guardian someGuardian = randomGuardian; someGuardian.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber); var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedGuardianException = new LockedGuardianException(databaseUpdateConcurrencyException); var expectedGuardianDependencyException = new GuardianDependencyException(lockedGuardianException); this.storageBrokerMock.Setup(broker => broker.SelectGuardianByIdAsync(someGuardian.Id)) .ThrowsAsync(databaseUpdateConcurrencyException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDateTime); // when ValueTask <Guardian> modifyGuardianTask = this.guardianService.ModifyGuardianAsync(someGuardian); // then await Assert.ThrowsAsync <GuardianDependencyException>(() => modifyGuardianTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectGuardianByIdAsync(someGuardian.Id), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedGuardianDependencyException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowDependencyExceptionOnAddWhenDbExceptionOccursAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); Guardian randomGuardian = CreateRandomGuardian(dateTime); Guardian inputGuardian = randomGuardian; inputGuardian.UpdatedBy = inputGuardian.CreatedBy; var databaseUpdateException = new DbUpdateException(); var expectedGuardianDependencyException = new GuardianDependencyException(databaseUpdateException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); this.storageBrokerMock.Setup(broker => broker.InsertGuardianAsync(inputGuardian)) .ThrowsAsync(databaseUpdateException); // when ValueTask <Guardian> createGuardianTask = this.guardianService.CreateGuardianAsync(inputGuardian); // then await Assert.ThrowsAsync <GuardianDependencyException>(() => createGuardianTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedGuardianDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertGuardianAsync(inputGuardian), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowDependencyExceptionOnDeleteWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given Guid randomGuardianId = Guid.NewGuid(); Guid inputGuardianId = randomGuardianId; var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedGuardianException = new LockedGuardianException(databaseUpdateConcurrencyException); var expectedGuardianDependencyException = new GuardianDependencyException(lockedGuardianException); this.storageBrokerMock.Setup(broker => broker.SelectGuardianByIdAsync(inputGuardianId)) .ThrowsAsync(databaseUpdateConcurrencyException); // when ValueTask <Guardian> deleteGuardianTask = this.guardianService.RemoveGuardianByIdAsync(inputGuardianId); // then await Assert.ThrowsAsync <GuardianDependencyException>(() => deleteGuardianTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedGuardianDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectGuardianByIdAsync(inputGuardianId), Times.Once); this.storageBrokerMock.Verify(broker => broker.DeleteGuardianAsync(It.IsAny <Guardian>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowDependencyExceptionOnRetrieveWhenDbExceptionOccursAndLogItAsync() { // given Guid someGuardianId = Guid.NewGuid(); var databaseUpdateException = new DbUpdateException(); var expectedGuardianDependencyException = new GuardianDependencyException(databaseUpdateException); this.storageBrokerMock.Setup(broker => broker.SelectGuardianByIdAsync(someGuardianId)) .ThrowsAsync(databaseUpdateException); // when ValueTask <Guardian> retrieveGuardianTask = this.guardianService.RetrieveGuardianByIdAsync(someGuardianId); // then await Assert.ThrowsAsync <GuardianDependencyException>(() => retrieveGuardianTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedGuardianDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectGuardianByIdAsync(someGuardianId), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }