Example #1
0
        public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAndLogItAsync()
        {
            // given
            int            randomNegativeNumber = GetNegativeRandomNumber();
            DateTimeOffset randomDateTime       = GetRandomDateTime();
            SemesterCourse randomSemesterCourse = CreateRandomSemesterCourse(randomDateTime);
            SemesterCourse someSemesterCourse   = randomSemesterCourse;

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

            var failedSemesterCourseServiceException =
                new FailedSemesterCourseServiceException(serviceException);

            var expectedSemesterCourseServiceException =
                new SemesterCourseServiceException(failedSemesterCourseServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectSemesterCourseByIdAsync(someSemesterCourse.Id))
            .ThrowsAsync(serviceException);

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

            // when
            ValueTask <SemesterCourse> modifySemesterCourseTask =
                this.semesterCourseService.ModifySemesterCourseAsync(someSemesterCourse);

            // then
            await Assert.ThrowsAsync <SemesterCourseServiceException>(() =>
                                                                      modifySemesterCourseTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectSemesterCourseByIdAsync(someSemesterCourse.Id),
                                          Times.Once);

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Example #2
0
        public async Task ShouldThrowServiceExceptionOnCreateWhenExceptionOccursAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime             = GetRandomDateTime();
            SemesterCourse randomSemesterCourse = CreateRandomSemesterCourse(dateTime);
            SemesterCourse inputSemesterCourse  = randomSemesterCourse;

            inputSemesterCourse.UpdatedBy   = inputSemesterCourse.CreatedBy;
            inputSemesterCourse.UpdatedDate = inputSemesterCourse.CreatedDate;
            var serviceException = new Exception();

            var failedSemesterCourseServiceException =
                new FailedSemesterCourseServiceException(serviceException);

            var expectedSemesterCourseServiceException =
                new SemesterCourseServiceException(failedSemesterCourseServiceException);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertSemesterCourseAsync(inputSemesterCourse))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <SemesterCourse> createSemesterCourseTask =
                this.semesterCourseService.CreateSemesterCourseAsync(inputSemesterCourse);

            // then
            await Assert.ThrowsAsync <SemesterCourseServiceException>(() =>
                                                                      createSemesterCourseTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        private async ValueTask <SemesterCourse> TryCatch(
            ReturningSemesterCourseFunction returningSemesterCourseFunction)
        {
            try
            {
                return(await returningSemesterCourseFunction());
            }
            catch (NullSemesterCourseException nullSemesterCourseException)
            {
                throw CreateAndLogValidationException(nullSemesterCourseException);
            }
            catch (InvalidSemesterCourseException invalidSemesterCourseInputException)
            {
                throw CreateAndLogValidationException(invalidSemesterCourseInputException);
            }
            catch (NotFoundSemesterCourseException nullSemesterCourseException)
            {
                throw CreateAndLogValidationException(nullSemesterCourseException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsSemesterCourseException =
                    new AlreadyExistsSemesterCourseException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsSemesterCourseException);
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedSemesterCourseException = new LockedSemesterCourseException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyException(lockedSemesterCourseException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                throw CreateAndLogDependencyException(dbUpdateException);
            }
            catch (Exception exception)
            {
                var failedSemesterCourseServiceException =
                    new FailedSemesterCourseServiceException(exception);

                throw CreateAndLogServiceException(failedSemesterCourseServiceException);
            }
        }
        private IQueryable <SemesterCourse> TryCatch(ReturningSemesterCoursesFunction returningSemesterCoursesFunction)
        {
            try
            {
                return(returningSemesterCoursesFunction());
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (Exception exception)
            {
                var failedSemesterCourseServiceException =
                    new FailedSemesterCourseServiceException(exception);

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

            var failedSemesterCourseServiceException =
                new FailedSemesterCourseServiceException(serviceException);

            var expectedSemesterCourseServiceException =
                new SemesterCourseServiceException(failedSemesterCourseServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectSemesterCourseByIdAsync(someSemesterCourseId))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <SemesterCourse> retrieveSemesterCourseByIdTask =
                this.semesterCourseService.RetrieveSemesterCourseByIdAsync(someSemesterCourseId);

            // then
            await Assert.ThrowsAsync <SemesterCourseServiceException>(() =>
                                                                      retrieveSemesterCourseByIdTask.AsTask());

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

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

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

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

            var failedSemesterCourseServiceException =
                new FailedSemesterCourseServiceException(serviceException);

            var expectedSemesterCourseServiceException =
                new SemesterCourseServiceException(failedSemesterCourseServiceException);

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

            // when
            Action retrieveAllSemesterCoursesAction = () =>
                                                      this.semesterCourseService.RetrieveAllSemesterCourses();

            // then
            Assert.Throws <SemesterCourseServiceException>(
                retrieveAllSemesterCoursesAction);

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

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

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

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