public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAndLogItAsync()
        {
            // given
            int            randomNegativeNumber = GetNegativeRandomNumber();
            DateTimeOffset randomDateTime       = GetRandomDateTime();
            CalendarEntry  randomCalendarEntry  = CreateRandomCalendarEntry(randomDateTime);
            CalendarEntry  someCalendarEntry    = randomCalendarEntry;

            someCalendarEntry.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber);
            var serviceException = new Exception();

            var failedCalendarEntryServiceException =
                new FailedCalendarEntryServiceException(serviceException);

            var expectedCalendarEntryServiceException =
                new CalendarEntryServiceException(failedCalendarEntryServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCalendarEntryByIdAsync(someCalendarEntry.Id))
            .ThrowsAsync(serviceException);

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

            // when
            ValueTask <CalendarEntry> modifyCalendarEntryTask =
                this.calendarEntryService.ModifyCalendarEntryAsync(someCalendarEntry);

            // then
            await Assert.ThrowsAsync <CalendarEntryServiceException>(() =>
                                                                     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(
                                                                    expectedCalendarEntryServiceException))),
                                          Times.Once);

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime            = GetRandomDateTime();
            CalendarEntry  randomCalendarEntry = CreateRandomCalendarEntry(dateTime);
            CalendarEntry  inputCalendarEntry  = randomCalendarEntry;

            inputCalendarEntry.UpdatedBy = inputCalendarEntry.CreatedBy;
            var serviceException = new Exception();

            var failedCalendarEntryServiceException =
                new FailedCalendarEntryServiceException(serviceException);

            var expectedCalendarEntryServiceException =
                new CalendarEntryServiceException(failedCalendarEntryServiceException);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertCalendarEntryAsync(inputCalendarEntry))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <CalendarEntry> createCalendarEntryTask =
                this.calendarEntryService.AddCalendarEntryAsync(inputCalendarEntry);

            // then
            await Assert.ThrowsAsync <CalendarEntryServiceException>(() =>
                                                                     createCalendarEntryTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertCalendarEntryAsync(inputCalendarEntry),
                                          Times.Once);

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Ejemplo n.º 3
0
        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);
            }
        }
Ejemplo n.º 4
0
        private IQueryable <CalendarEntry> TryCatch(ReturningCalendarEntriesFunction returningCalendarEntriesFunction)
        {
            try
            {
                return(returningCalendarEntriesFunction());
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (Exception exception)
            {
                var failedCalendarEntryServiceException =
                    new FailedCalendarEntryServiceException(exception);

                throw CreateAndLogServiceException(failedCalendarEntryServiceException);
            }
        }
        public async Task ShouldThrowServiceExceptionOnRetrieveWhenExceptionOccursAndLogItAsync()
        {
            // given
            Guid someCalendarEntryId = Guid.NewGuid();
            var  serviceException    = new Exception();

            var failedCalendarEntryServiceException =
                new FailedCalendarEntryServiceException(serviceException);

            var expectedCalendarEntryServiceException =
                new CalendarEntryServiceException(failedCalendarEntryServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCalendarEntryByIdAsync(It.IsAny <Guid>()))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <CalendarEntry> retrieveCalendarEntryByIdTask =
                this.calendarEntryService.RetrieveCalendarEntryByIdAsync(someCalendarEntryId);

            // then
            await Assert.ThrowsAsync <CalendarEntryServiceException>(() =>
                                                                     retrieveCalendarEntryByIdTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectCalendarEntryByIdAsync(It.IsAny <Guid>()),
                                          Times.Once);

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Ejemplo n.º 6
0
        public async Task ShouldThrowServiceExceptionOnRemoveWhenExceptionOccursAndLogItAsync()
        {
            // given
            Guid randomCalendarEntryId = Guid.NewGuid();
            Guid inputCalendarEntryId  = randomCalendarEntryId;
            var  serviceException      = new Exception();

            var failedCalendarEntryServiceException =
                new FailedCalendarEntryServiceException(serviceException);

            var expectedCalendarEntryServiceException =
                new CalendarEntryServiceException(failedCalendarEntryServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCalendarEntryByIdAsync(inputCalendarEntryId))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <CalendarEntry> deleteCalendarEntryTask =
                this.calendarEntryService.RemoveCalendarEntryByIdAsync(inputCalendarEntryId);

            // then
            await Assert.ThrowsAsync <CalendarEntryServiceException>(() =>
                                                                     deleteCalendarEntryTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectCalendarEntryByIdAsync(inputCalendarEntryId),
                                          Times.Once);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Ejemplo n.º 7
0
        public void ShouldThrowServiceExceptionOnRetrieveAllCalendarEntriesWhenExceptionOccursAndLogIt()
        {
            // given
            var serviceException = new Exception();

            var failedCalendarEntryServiceException =
                new FailedCalendarEntryServiceException(serviceException);

            var expectedCalendarEntryServiceException =
                new CalendarEntryServiceException(failedCalendarEntryServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAllCalendarEntries())
            .Throws(serviceException);

            // when
            Action retrieveAllCalendarEntriesAction = () =>
                                                      this.calendarEntryService.RetrieveAllCalendarEntries();

            // then
            Assert.Throws <CalendarEntryServiceException>(
                retrieveAllCalendarEntriesAction);

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }