public async void ShouldThrowValidationExceptionOnAddWhenCalendarEntryAlreadyExistsAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); CalendarEntry randomCalendarEntry = CreateRandomCalendarEntry(dateTime); CalendarEntry alreadyExistsCalendarEntry = randomCalendarEntry; alreadyExistsCalendarEntry.UpdatedBy = alreadyExistsCalendarEntry.CreatedBy; string randomMessage = GetRandomMessage(); string exceptionMessage = randomMessage; var duplicateKeyException = new DuplicateKeyException(exceptionMessage); var alreadyExistsCalendarEntryException = new AlreadyExistsCalendarEntryException(duplicateKeyException); var expectedCalendarEntryValidationException = new CalendarEntryValidationException(alreadyExistsCalendarEntryException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); this.storageBrokerMock.Setup(broker => broker.InsertCalendarEntryAsync(alreadyExistsCalendarEntry)) .ThrowsAsync(duplicateKeyException); // when ValueTask <CalendarEntry> createCalendarEntryTask = this.calendarEntryService.AddCalendarEntryAsync(alreadyExistsCalendarEntry); // then await Assert.ThrowsAsync <CalendarEntryValidationException>(() => createCalendarEntryTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedCalendarEntryValidationException))), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertCalendarEntryAsync(alreadyExistsCalendarEntry), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
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); } }