public async Task ShouldThrowServiceExceptionOnRetrieveAllIfServiceErrorOccursAndLogItAsync()
        {
            // given
            var serviceException = new Exception();

            var failedCourseServiceExcption =
                new FailedCourseServiceException(serviceException);

            var expectedCourseServiceException =
                new CourseServiceException(failedCourseServiceExcption);

            this.apiBrokerMock.Setup(broker =>
                                     broker.GetAllCoursesAsync())
            .ThrowsAsync(serviceException);

            // when
            ValueTask <List <Course> > retrieveAllCoursesTask =
                this.courseService.RetrieveAllCoursesAsync();

            // then
            await Assert.ThrowsAsync <CourseServiceException>(() =>
                                                              retrieveAllCoursesTask.AsTask());

            this.apiBrokerMock.Verify(broker =>
                                      broker.GetAllCoursesAsync(),
                                      Times.Once);

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

            this.apiBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
        }
Exemple #2
0
        private async ValueTask <Course> TryCatch(ReturningCourseFunction returningCourseFunction)
        {
            try
            {
                return(await returningCourseFunction());
            }
            catch (NullCourseException nullCourseException)
            {
                throw CreateAndLogValidationException(nullCourseException);
            }
            catch (InvalidCourseException invalidCourseException)
            {
                throw CreateAndLogValidationException(invalidCourseException);
            }
            catch (NotFoundCourseException nullCourseException)
            {
                throw CreateAndLogValidationException(nullCourseException);
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsCourseException =
                    new AlreadyExistsCourseException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsCourseException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedCourseException = new LockedCourseException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyException(lockedCourseException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                throw CreateAndLogDependencyException(dbUpdateException);
            }
            catch (Exception exception)
            {
                var failedCourseServiceException =
                    new FailedCourseServiceException(exception);

                throw CreateAndLogServiceException(failedCourseServiceException);
            }
        }
        public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAndLogItAsync()
        {
            // given
            int            randomNegativeNumber = GetNegativeRandomNumber();
            DateTimeOffset randomDateTime       = GetRandomDateTime();
            Course         randomCourse         = CreateRandomCourse(randomDateTime);
            Course         someCourse           = randomCourse;

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

            var failedCourseServiceException =
                new FailedCourseServiceException(serviceException);

            var expectedCourseServiceException =
                new CourseServiceException(failedCourseServiceException);

            this.dateTimeBrokerMock.Setup(broker =>
                                          broker.GetCurrentDateTime())
            .Throws(serviceException);

            // when
            ValueTask <Course> modifyCourseTask =
                this.courseService.ModifyCourseAsync(someCourse);

            // then
            await Assert.ThrowsAsync <CourseServiceException>(() =>
                                                              modifyCourseTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemple #4
0
        private IQueryable <Course> TryCatch(ReturningQueryableCourseFunction returningQueryableCourseFunction)
        {
            try
            {
                return(returningQueryableCourseFunction());
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (Exception exception)
            {
                var failedCourseServiceException =
                    new FailedCourseServiceException(exception);

                throw CreateAndLogServiceException(failedCourseServiceException);
            }
        }
        public async Task ShouldThrowServiceExceptionOnCreateWhenExceptionOccursAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime   = GetRandomDateTime();
            Course         someCourse = CreateRandomCourse(dateTime);

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

            var failedCourseServiceException =
                new FailedCourseServiceException(serviceException);

            var expectedCourseServiceException =
                new CourseServiceException(failedCourseServiceException);

            this.dateTimeBrokerMock.Setup(broker =>
                                          broker.GetCurrentDateTime())
            .Throws(serviceException);

            // when
            ValueTask <Course> createCourseTask =
                this.courseService.CreateCourseAsync(someCourse);

            // then
            await Assert.ThrowsAsync <CourseServiceException>(() =>
                                                              createCourseTask.AsTask());

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

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.InsertCourseAsync(It.IsAny <Course>()),
                                          Times.Never);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Exemple #6
0
        private async ValueTask <List <Course> > TryCatch(ReturningCoursesFunction returningCoursesFunction)
        {
            try
            {
                return(await returningCoursesFunction());
            }
            catch (HttpRequestException httpRequestException)
            {
                var failedCourseDependencyException =
                    new FailedCourseDependencyException(httpRequestException);

                throw CreateAndLogCriticalDependencyException(failedCourseDependencyException);
            }
            catch (HttpResponseUrlNotFoundException httpResponseUrlNotFoundException)
            {
                var failedCourseDependencyException =
                    new FailedCourseDependencyException(httpResponseUrlNotFoundException);

                throw CreateAndLogCriticalDependencyException(failedCourseDependencyException);
            }
            catch (HttpResponseUnauthorizedException httpResponseUnauthorizedException)
            {
                var failedCourseDependencyException =
                    new FailedCourseDependencyException(httpResponseUnauthorizedException);

                throw CreateAndLogCriticalDependencyException(failedCourseDependencyException);
            }
            catch (HttpResponseException httpResponseException)
            {
                var failedCourseDependencyException =
                    new FailedCourseDependencyException(httpResponseException);

                throw CreateAndLogDependencyException(failedCourseDependencyException);
            }
            catch (Exception serviceException)
            {
                var failedCourseServiceException =
                    new FailedCourseServiceException(serviceException);

                throw CreateAndLogServiceException(failedCourseServiceException);
            }
        }
Exemple #7
0
        public void ShouldThrowServiceExceptionOnRetrieveAllWhenExceptionOccursAndLogIt()
        {
            // given
            var serviceException = new Exception();

            var failedCourseServiceException =
                new FailedCourseServiceException(serviceException);

            var expectedCourseServiceException =
                new CourseServiceException(failedCourseServiceException);

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

            // when
            Action retrieveAllCoursesAction = () =>
                                              this.courseService.RetrieveAllCourses();

            // then
            Assert.Throws <CourseServiceException>(
                retrieveAllCoursesAction);
            this.loggingBrokerMock.Verify(broker =>
                                          broker.LogError(It.Is(SameExceptionAs(
                                                                    expectedCourseServiceException))),
                                          Times.Once);

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

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

            this.storageBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowServiceExceptionOnRetrieveByIdWhenExceptionOccursAndLogItAsync()
        {
            // given
            Guid someCourseId     = Guid.NewGuid();
            var  serviceException = new Exception();

            var failedCourseServiceException =
                new FailedCourseServiceException(serviceException);

            var expectedCourseServiceException =
                new CourseServiceException(failedCourseServiceException);

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

            // when
            ValueTask <Course> retrieveByIdCourseTask =
                this.courseService.RetrieveCourseByIdAsync(someCourseId);

            // then
            await Assert.ThrowsAsync <CourseServiceException>(() =>
                                                              retrieveByIdCourseTask.AsTask());

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

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

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