public void ShouldThrowServiceExceptionOnRetrieveAllCalendarEntriesWhenExceptionOccursAndLogIt()
        {
            // given
            var exception = new Exception();

            var expectedCalendarEntryServiceException =
                new CalendarEntryServiceException(exception);

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

            // when . then
            Assert.Throws <CalendarEntryServiceException>(() =>
                                                          this.calendarEntryService.RetrieveAllCalendarEntries());

            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();
        }
        public async Task ShouldThrowServiceExceptionOnRemoveWhenExceptionOccursAndLogItAsync()
        {
            // given
            Guid randomCalendarEntryId = Guid.NewGuid();
            Guid inputCalendarEntryId = randomCalendarEntryId;
            var exception = new Exception();

            var expectedCalendarEntryServiceException =
                new CalendarEntryServiceException(exception);

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

            // 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();
        }
        private CalendarEntryServiceException CreateAndLogServiceException(Exception exception)
        {
            var calendarEntryServiceException = new CalendarEntryServiceException(exception);

            this.loggingBroker.LogError(calendarEntryServiceException);

            return(calendarEntryServiceException);
        }
        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();
        }
        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();
        }