public async Task ShouldThrowDependencyExceptionOnDeleteWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            Guid randomCalendarId = Guid.NewGuid();
            Guid inputCalendarId  = randomCalendarId;
            var  databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();
            var  lockedCalendarException            = new LockedCalendarException(databaseUpdateConcurrencyException);

            var expectedCalendarDependencyException =
                new CalendarDependencyException(lockedCalendarException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCalendarByIdAsync(inputCalendarId))
            .ThrowsAsync(databaseUpdateConcurrencyException);

            // when
            ValueTask <Calendar> deleteCalendarTask =
                this.calendarService.RemoveCalendarByIdAsync(inputCalendarId);

            // then
            await Assert.ThrowsAsync <CalendarDependencyException>(() =>
                                                                   deleteCalendarTask.AsTask());

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(expectedCalendarDependencyException))),
                                          Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectCalendarByIdAsync(inputCalendarId),
                                          Times.Once);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Esempio n. 2
0
        public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            int            randomNegativeNumber = GetNegativeRandomNumber();
            DateTimeOffset randomDateTime       = GetRandomDateTime();
            Calendar       randomCalendar       = CreateRandomCalendar(randomDateTime);
            Calendar       someCalendar         = randomCalendar;

            someCalendar.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber);
            var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();
            var lockedCalendarException            = new LockedCalendarException(databaseUpdateConcurrencyException);

            var expectedCalendarDependencyException =
                new CalendarDependencyException(lockedCalendarException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCalendarByIdAsync(someCalendar.Id))
            .ThrowsAsync(databaseUpdateConcurrencyException);

            this.dateTimeBrokerMock.Setup(broker =>
                                          broker.GetCurrentDateTime())
            .Returns(randomDateTime);

            // when
            ValueTask <Calendar> modifyCalendarTask =
                this.calendarService.ModifyCalendarAsync(someCalendar);

            // then
            await Assert.ThrowsAsync <CalendarDependencyException>(() =>
                                                                   modifyCalendarTask.AsTask());

            this.dateTimeBrokerMock.Verify(broker =>
                                           broker.GetCurrentDateTime(),
                                           Times.Once);

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectCalendarByIdAsync(someCalendar.Id),
                                          Times.Once);

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(
                                                                    expectedCalendarDependencyException))),
                                          Times.Once);

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        private async ValueTask <Calendar> TryCatch(ReturningCalendarFunction returningCalendarFunction)
        {
            try
            {
                return(await returningCalendarFunction());
            }
            catch (NullCalendarException nullCalendarException)
            {
                throw CreateAndLogValidationException(nullCalendarException);
            }
            catch (InvalidCalendarException invalidCalendarException)
            {
                throw CreateAndLogValidationException(invalidCalendarException);
            }
            catch (NotFoundCalendarException nullCalendarException)
            {
                throw CreateAndLogValidationException(nullCalendarException);
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsCalendarException =
                    new AlreadyExistsCalendarException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsCalendarException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedCalendarException = new LockedCalendarException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyException(lockedCalendarException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                throw CreateAndLogDependencyException(dbUpdateException);
            }
            catch (Exception exception)
            {
                var failedCalendarServiceException =
                    new FailedCalendarServiceException(exception);

                throw CreateAndLogServiceException(failedCalendarServiceException);
            }
        }