Пример #1
0
        public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAndLogItAsync()
        {
            // given
            int            randomNegativeNumber = GetNegativeRandomNumber();
            DateTimeOffset randomDateTime       = GetRandomDateTime();
            Calendar       randomCalendar       = CreateRandomCalendar(randomDateTime);
            Calendar       someCalendar         = randomCalendar;

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

            var failedCalendarServiceException = new
                                                 FailedCalendarServiceException(serviceException);

            var expectedCalendarServiceException =
                new CalendarServiceException(failedCalendarServiceException);

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

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

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Пример #2
0
        public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime       = GetRandomDateTime();
            Calendar       randomCalendar = CreateRandomCalendar(dateTime);
            Calendar       inputCalendar  = randomCalendar;

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

            var failedCalendarServiceException =
                new FailedCalendarServiceException(serviceException);

            var expectedCalendarServiceException =
                new CalendarServiceException(failedCalendarServiceException);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertCalendarAsync(inputCalendar))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <Calendar> registerCalendarTask =
                this.calendarService.AddCalendarAsync(inputCalendar);

            // then
            await Assert.ThrowsAsync <CalendarServiceException>(() =>
                                                                registerCalendarTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.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);
            }
        }
        private IQueryable <Calendar> TryCatch(ReturningCalendarsFunction returningCalendarsFunction)
        {
            try
            {
                return(returningCalendarsFunction());
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (Exception exception)
            {
                var failedCalendarServiceException =
                    new FailedCalendarServiceException(exception);

                throw CreateAndLogServiceException(failedCalendarServiceException);
            }
        }
Пример #5
0
        public async Task ShouldThrowServiceExceptionOnRetrieveWhenExceptionOccursAndLogItAsync()
        {
            // given
            Guid someCalendarId   = Guid.NewGuid();
            var  serviceException = new Exception();

            var failedCalendarServiceException =
                new FailedCalendarServiceException(serviceException);

            var expectedCalendarServiceException =
                new CalendarServiceException(failedCalendarServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCalendarByIdAsync(someCalendarId))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <Calendar> retrieveCalendarByIdTask =
                this.calendarService.RetrieveCalendarByIdAsync(someCalendarId);

            // then
            await Assert.ThrowsAsync <CalendarServiceException>(() =>
                                                                retrieveCalendarByIdTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public void ShouldThrowServiceExceptionOnRetrieveAllWhenExceptionOccursAndLogIt()
        {
            // given
            var serviceException = new Exception();

            var failedCalendarServiceException =
                new FailedCalendarServiceException(serviceException);

            var expectedCalendarServiceException =
                new CalendarServiceException(failedCalendarServiceException);

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

            // when
            Action retrieveAllCalendarAction = () =>
                                               this.calendarService.RetrieveAllCalendars();

            // then
            Assert.Throws <CalendarServiceException>(
                retrieveAllCalendarAction);

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

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

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

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