Example #1
0
        public void ShouldThrowServiceExceptionOnRetrieveAllWhenExceptionOccursAndLogIt()
        {
            // given
            var serviceException = new Exception();

            var failedStudentContactServiceException =
                new FailedStudentContactServiceException(serviceException);

            var expectedStudentContactServiceException =
                new StudentContactServiceException(failedStudentContactServiceException);

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

            // when
            Action retrieveAllStudentContactAction = () =>
                                                     this.studentContactService.RetrieveAllStudentContacts();

            // then
            Assert.Throws <StudentContactServiceException>(
                retrieveAllStudentContactAction);

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Example #2
0
        private async ValueTask <StudentContact> TryCatch(
            ReturningStudentContactFunction returningStudentContactFunction)
        {
            try
            {
                return(await returningStudentContactFunction());
            }
            catch (NullStudentContactException nullStudentContactException)
            {
                throw CreateAndLogValidationException(nullStudentContactException);
            }
            catch (InvalidStudentContactException invalidStudentContactException)
            {
                throw CreateAndLogValidationException(invalidStudentContactException);
            }
            catch (NotFoundStudentContactException notFoundStudentContactException)
            {
                throw CreateAndLogValidationException(notFoundStudentContactException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsStudentContactException =
                    new AlreadyExistsStudentContactException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsStudentContactException);
            }
            catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException)
            {
                var invalidStudentContactReferenceException =
                    new InvalidStudentContactReferenceException(foreignKeyConstraintConflictException);

                throw CreateAndLogValidationException(invalidStudentContactReferenceException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedStudentContactException =
                    new LockedStudentContactException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyException(lockedStudentContactException);
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                throw CreateAndLogDependencyException(dbUpdateException);
            }
            catch (Exception exception)
            {
                var failedStudentContactServiceException =
                    new FailedStudentContactServiceException(exception);

                throw CreateAndLogServiceException(failedStudentContactServiceException);
            }
        }
Example #3
0
        public async Task ShouldThrowServiceExceptionOnRemoveWhenExceptionOccursAndLogItAsync()
        {
            // given
            var  randomContactId  = Guid.NewGuid();
            var  randomStudentId  = Guid.NewGuid();
            Guid someContactId    = randomContactId;
            Guid someStudentId    = randomStudentId;
            var  serviceException = new Exception();

            var failedStudentContactServiceException =
                new FailedStudentContactServiceException(serviceException);

            var expectedStudentContactServiceException =
                new StudentContactServiceException(failedStudentContactServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectStudentContactByIdAsync(someStudentId, someContactId))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <StudentContact> removeStudentContactTask =
                this.studentContactService.RemoveStudentContactByIdAsync(
                    someStudentId,
                    someContactId);

            // then
            await Assert.ThrowsAsync <StudentContactServiceException>(() =>
                                                                      removeStudentContactTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectStudentContactByIdAsync(someStudentId, someContactId),
                                          Times.Once);

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Example #4
0
        private IQueryable <StudentContact> TryCatch(
            ReturningStudentContactsFunction returningStudentContactsFunction)
        {
            try
            {
                return(returningStudentContactsFunction());
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (Exception exception)
            {
                var failedStudentContactServiceException =
                    new FailedStudentContactServiceException(exception);

                throw CreateAndLogServiceException(failedStudentContactServiceException);
            }
        }
Example #5
0
        public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync()
        {
            // given
            StudentContact randomStudentContact = CreateRandomStudentContact();
            StudentContact inputStudentContact  = randomStudentContact;
            var            serviceException     = new Exception();

            var failedStudentContactServiceException =
                new FailedStudentContactServiceException(serviceException);

            var expectedStudentContactServiceException =
                new StudentContactServiceException(failedStudentContactServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertStudentContactAsync(inputStudentContact))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <StudentContact> addStudentContactTask =
                this.studentContactService.AddStudentContactAsync(inputStudentContact);

            // then
            await Assert.ThrowsAsync <StudentContactServiceException>(() =>
                                                                      addStudentContactTask.AsTask());

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

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

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