public void ShouldThrowDependencyExceptionOnRetrieveAllCalendarEntriesWhenSqlExceptionOccursAndLogIt()
        {
            // given
            var sqlException = GetSqlException();

            var expectedCalendarEntryDependencyException =
                new CalendarEntryDependencyException(sqlException);

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

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

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowDependencyExceptionOnRemoveWhenDbExceptionOccursAndLogItAsync()
        {
            // given
            Guid randomCalendarEntryId = Guid.NewGuid();
            Guid inputCalendarEntryId = randomCalendarEntryId;
            var databaseUpdateException = new DbUpdateException();

            var expectedCalendarEntryDependencyException =
                new CalendarEntryDependencyException(databaseUpdateException);

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

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

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        private CalendarEntryDependencyException CreateAndLogDependencyException(Exception exception)
        {
            var calendarEntryDependencyException = new CalendarEntryDependencyException(exception);

            this.loggingBroker.LogError(calendarEntryDependencyException);

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

            someCalendarEntry.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber);
            var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();
            var lockedCalendarEntryException       = new LockedCalendarEntryException(databaseUpdateConcurrencyException);

            var expectedCalendarEntryDependencyException =
                new CalendarEntryDependencyException(lockedCalendarEntryException);

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

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

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

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

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

            inputCalendarEntry.UpdatedBy = inputCalendarEntry.CreatedBy;
            var databaseUpdateException = new DbUpdateException();

            var expectedCalendarEntryDependencyException =
                new CalendarEntryDependencyException(databaseUpdateException);

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

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

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

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

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(
                                                                    expectedCalendarEntryDependencyException))),
                                          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 ShouldThrowDependencyExceptionOnRetrieveWhenSqlExceptionOccursAndLogItAsync()
        {
            // given
            Guid someCalendarEntryId = Guid.NewGuid();
            var  sqlException        = GetSqlException();

            var expectedCalendarEntryDependencyException =
                new CalendarEntryDependencyException(sqlException);

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

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

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

            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogCritical(It.Is(SameExceptionAs(
                                                                       expectedCalendarEntryDependencyException))),
                                          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();
        }