private async ValueTask <CalendarEntry> TryCatch(ReturningCalendarEntryFunction returningCalendarEntryFunction) { try { return(await returningCalendarEntryFunction()); } catch (NullCalendarEntryException nullCalendarEntryException) { throw CreateAndLogValidationException(nullCalendarEntryException); } catch (InvalidCalendarEntryException invalidCalendarEntryException) { throw CreateAndLogValidationException(invalidCalendarEntryException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (NotFoundCalendarEntryException notFoundCalendarEntryException) { throw CreateAndLogValidationException(notFoundCalendarEntryException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsCalendarEntryException = new AlreadyExistsCalendarEntryException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsCalendarEntryException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedCalendarException = new LockedCalendarEntryException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedCalendarException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { var failedCalendarEntryServiceException = new FailedCalendarEntryServiceException(exception); throw CreateAndLogServiceException(failedCalendarEntryServiceException); } }
public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given int randomNegativeNumber = GetNegativeRandomNumber(); DateTimeOffset randomDateTime = GetRandomDateTime(); CalendarEntry randomCalendarEntry = CreateRandomCalendarEntry(randomDateTime); CalendarEntry someCalendarEntry = randomCalendarEntry; someCalendarEntry.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber); var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedCalendarEntryException = new LockedCalendarEntryException(databaseUpdateConcurrencyException); var expectedCalendarEntryDependencyException = new CalendarEntryDependencyException(lockedCalendarEntryException); this.storageBrokerMock.Setup(broker => broker.SelectCalendarEntryByIdAsync(someCalendarEntry.Id)) .ThrowsAsync(databaseUpdateConcurrencyException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDateTime); // when ValueTask <CalendarEntry> modifyCalendarEntryTask = this.calendarEntryService.ModifyCalendarEntryAsync(someCalendarEntry); // then await Assert.ThrowsAsync <CalendarEntryDependencyException>(() => modifyCalendarEntryTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectCalendarEntryByIdAsync(someCalendarEntry.Id), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedCalendarEntryDependencyException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowDependencyExceptionOnRemoveWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given Guid randomCalendarEntryId = Guid.NewGuid(); Guid inputCalendarEntryId = randomCalendarEntryId; var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedCalendarEntryException = new LockedCalendarEntryException(databaseUpdateConcurrencyException); var expectedCalendarEntryDependencyException = new CalendarEntryDependencyException(lockedCalendarEntryException); this.storageBrokerMock.Setup(broker => broker.SelectCalendarEntryByIdAsync(inputCalendarEntryId)) .ThrowsAsync(databaseUpdateConcurrencyException); // when ValueTask <CalendarEntry> deleteCalendarEntryTask = this.calendarEntryService.RemoveCalendarEntryByIdAsync(inputCalendarEntryId); // then await Assert.ThrowsAsync <CalendarEntryDependencyException>(() => deleteCalendarEntryTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedCalendarEntryDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectCalendarEntryByIdAsync(inputCalendarEntryId), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }