public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAndLogItAsync()
        {
            // given
            int             randomNegativeNumber  = GetNegativeRandomNumber();
            DateTimeOffset  randomDateTime        = GetRandomDateTime();
            StudentGuardian randomStudentGuardian = CreateRandomStudentGuardian(randomDateTime);
            StudentGuardian someStudentGuardian   = randomStudentGuardian;

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

            var failedStudentGuardianServiceException =
                new FailedStudentGuardianServiceException(serviceException);

            var expectedStudentGuardianServiceException =
                new StudentGuardianServiceException(failedStudentGuardianServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentGuardianByIdAsync
                                             (someStudentGuardian.StudentId, someStudentGuardian.GuardianId))
            .ThrowsAsync(serviceException);

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

            // when
            ValueTask <StudentGuardian> modifyStudentGuardianTask =
                this.studentGuardianService.ModifyStudentGuardianAsync(someStudentGuardian);

            // then
            await Assert.ThrowsAsync <StudentGuardianServiceException>(() =>
                                                                       modifyStudentGuardianTask.AsTask());

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

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync()
        {
            // given
            DateTimeOffset  dateTime = GetRandomDateTime();
            StudentGuardian randomStudentGuardian = CreateRandomStudentGuardian(dateTime);
            StudentGuardian inputStudentGuardian  = randomStudentGuardian;

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

            var failedStudentGuardianServiceException =
                new FailedStudentGuardianServiceException(serviceException);

            var expectedStudentGuardianServiceException =
                new StudentGuardianServiceException(failedStudentGuardianServiceException);

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

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

            // when
            ValueTask <StudentGuardian> addStudentGuardianTask =
                this.studentGuardianService.AddStudentGuardianAsync(inputStudentGuardian);

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

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        private async ValueTask <StudentGuardian> TryCatch(
            ReturningStudentGuardianFunction returningStudentGuardianFunction)
        {
            try
            {
                return(await returningStudentGuardianFunction());
            }
            catch (NullStudentGuardianException nullStudentGuardianException)
            {
                throw CreateAndLogValidationException(nullStudentGuardianException);
            }
            catch (InvalidStudentGuardiantException invalidStudentGuardianInputException)
            {
                throw CreateAndLogValidationException(invalidStudentGuardianInputException);
            }
            catch (NotFoundStudentGuardianException notFoundStudentGuardianException)
            {
                throw CreateAndLogValidationException(notFoundStudentGuardianException);
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsGuardianException =
                    new AlreadyExistsStudentGuardianException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsGuardianException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedSemesterCourseException =
                    new LockedStudentGuardianException(dbUpdateConcurrencyException);

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

                throw CreateAndLogServiceException(failedStudentGuardianServiceException);
            }
        }
        private IQueryable <StudentGuardian> TryCatch(ReturningStudentGuardiansFunction returningStudentGuardiansFunction)
        {
            try
            {
                return(returningStudentGuardiansFunction());
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (Exception exception)
            {
                var failedStudentGuardianServiceException =
                    new FailedStudentGuardianServiceException(exception);

                throw CreateAndLogServiceException(failedStudentGuardianServiceException);
            }
        }
        public void ShouldThrowServiceExceptionOnRetrieveAllWhenExceptionOccursAndLogIt()
        {
            // given
            var serviceException = new Exception();

            var failedStudentGuardianServiceException =
                new FailedStudentGuardianServiceException(serviceException);

            var expectedStudentGuardianServiceException =
                new StudentGuardianServiceException(failedStudentGuardianServiceException);

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

            // when
            Action retrieveAllStudentGuardiansAction = () =>
                                                       this.studentGuardianService.RetrieveAllStudentGuardians();

            // when
            Action retrieveAllStudentGuardianAction = () =>
                                                      this.studentGuardianService.RetrieveAllStudentGuardians();

            // then
            Assert.Throws <StudentGuardianServiceException>(
                retrieveAllStudentGuardianAction);

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Example #6
0
        public async Task ShouldThrowServiceExceptionOnRetrieveWhenExceptionOccursAndLogItAsync()
        {
            // given
            Guid someStudentId    = Guid.NewGuid();
            Guid someGuardianId   = Guid.NewGuid();
            var  serviceException = new Exception();

            var failedStudentGuardianServiceException =
                new FailedStudentGuardianServiceException(serviceException);

            var expectedStudentGuardianServiceException =
                new StudentGuardianServiceException(failedStudentGuardianServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentGuardianByIdAsync(someStudentId, someGuardianId))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <StudentGuardian> retrieveStudentGuardianByIdTask =
                this.studentGuardianService.RetrieveStudentGuardianByIdAsync(someStudentId, someGuardianId);

            // then
            await Assert.ThrowsAsync <StudentGuardianServiceException>(() =>
                                                                       retrieveStudentGuardianByIdTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectStudentGuardianByIdAsync(someStudentId, someGuardianId),
                                          Times.Once);

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

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