Example #1
0
        public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAndLogItAsync()
        {
            // given
            int            randomNegativeNumber = GetNegativeRandomNumber();
            DateTimeOffset randomDateTime       = GetRandomDateTime();
            StudentExam    randomStudentExam    = CreateRandomStudentExam(randomDateTime);
            StudentExam    someStudentExam      = randomStudentExam;

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

            var failedStudentExamServiceException =
                new FailedStudentExamServiceException(serviceException);

            var expectedStudentExamServiceException =
                new StudentExamServiceException(failedStudentExamServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentExamByIdAsync(someStudentExam.Id))
            .ThrowsAsync(serviceException);

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

            // when
            ValueTask <StudentExam> modifyStudentExamTask =
                this.studentExamService.ModifyStudentExamAsync(someStudentExam);

            // then
            await Assert.ThrowsAsync <StudentExamServiceException>(() =>
                                                                   modifyStudentExamTask.AsTask());

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

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime             = GetRandomDateTime();
            StudentExam    randomStudentExam    = CreateRandomStudentExam(dateTime);
            StudentExam    inputStudentGuardian = randomStudentExam;

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

            var failedStudentExamServiceException =
                new FailedStudentExamServiceException(serviceException);

            var expectedStudentExamServiceException =
                new StudentExamServiceException(failedStudentExamServiceException);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertStudentExamAsync(inputStudentGuardian))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <StudentExam> addStudentGuardianTask =
                this.studentExamService.AddStudentExamAsync(inputStudentGuardian);

            // then
            await Assert.ThrowsAsync <StudentExamServiceException>(() =>
                                                                   addStudentGuardianTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        private IQueryable <StudentExam> TryCatch(ReturningStudentExamsFunction returningStudentExamsFunction)
        {
            try
            {
                return(returningStudentExamsFunction());
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (Exception exception)
            {
                var failedStudentExamServiceException =
                    new FailedStudentExamServiceException(exception);

                throw CreateAndLogServiceException(failedStudentExamServiceException);
            }
        }
        private async ValueTask <StudentExam> TryCatch(
            ReturningStudentExamFunction returningStudentExamFunction)
        {
            try
            {
                return(await returningStudentExamFunction());
            }
            catch (NullStudentExamException nullStudentExamException)
            {
                throw CreateAndLogValidationException(nullStudentExamException);
            }
            catch (InvalidStudentExamException invalidStudentExamException)
            {
                throw CreateAndLogValidationException(invalidStudentExamException);
            }
            catch (NotFoundStudentExamException nullStudentExamException)
            {
                throw CreateAndLogValidationException(nullStudentExamException);
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedStudentExamException = new LockedStudentExamException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyException(lockedStudentExamException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                throw CreateAndLogDependencyException(dbUpdateException);
            }
            catch (Exception exception)
            {
                var failedStudentExamServiceException =
                    new FailedStudentExamServiceException(exception);

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

            var failedStudentExamServiceException =
                new FailedStudentExamServiceException(serviceException);

            var expectedStudentExamServiceException =
                new StudentExamServiceException(failedStudentExamServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentExamByIdAsync(someStudentExamId))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <StudentExam> retrieveStudentExamByIdTask =
                this.studentExamService.RetrieveStudentExamByIdAsync(someStudentExamId);

            // then
            await Assert.ThrowsAsync <StudentExamServiceException>(() =>
                                                                   retrieveStudentExamByIdTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectStudentExamByIdAsync(someStudentExamId),
                                          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 failedStudentExamServiceException =
                new FailedStudentExamServiceException(serviceException);

            var expectedStudentExamServiceException =
                new StudentExamServiceException(failedStudentExamServiceException);

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

            // when
            Action retrieveAllStudentExamsAction = () =>
                                                   this.studentExamService.RetrieveAllStudentExams();

            // then
            Assert.Throws <StudentExamServiceException>(
                retrieveAllStudentExamsAction);

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Example #7
0
        public async Task ShouldThrowServiceExceptionOnDeleteWhenExceptionOccursAndLogItAsync()
        {
            // given
            Guid randomStudentExamId = Guid.NewGuid();
            Guid inputStudentExamId  = randomStudentExamId;
            var  exception           = new Exception();

            var failedStudentExamServiceException =
                new FailedStudentExamServiceException(exception);

            var expectedStudentExamServiceException =
                new StudentExamServiceException(failedStudentExamServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentExamByIdAsync(inputStudentExamId))
            .ThrowsAsync(exception);

            // when
            ValueTask <StudentExam> deleteStudentExamTask =
                this.studentExamService.RemoveStudentExamByIdAsync(inputStudentExamId);

            // then
            await Assert.ThrowsAsync <StudentExamServiceException>(() =>
                                                                   deleteStudentExamTask.AsTask());

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

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

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