public void ShouldThrowServiceExceptionOnRetrieveAllWhenExceptionOccursAndLogIt()
        {
            // given
            var serviceException = new Exception();

            var failedTeacherContactServiceException =
                new FailedTeacherContactServiceException(serviceException);

            var expectedTeacherContactServiceException =
                new TeacherContactServiceException(failedTeacherContactServiceException);

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

            // when
            Action retrieveAllTeacherContactsAction = () =>
                                                      this.teacherContactService.RetrieveAllTeacherContacts();

            // then
            Assert.Throws <TeacherContactServiceException>(
                retrieveAllTeacherContactsAction);

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        private async ValueTask <TeacherContact> TryCatch(
            ReturningTeacherContactFunction returningTeacherContactFunction)
        {
            try
            {
                return(await returningTeacherContactFunction());
            }
            catch (NullTeacherContactException nullTeacherContactException)
            {
                throw CreateAndLogValidationException(nullTeacherContactException);
            }
            catch (InvalidTeacherContactException invalidTeacherContactException)
            {
                throw CreateAndLogValidationException(invalidTeacherContactException);
            }
            catch (NotFoundTeacherContactException notFoundTeacherContactException)
            {
                throw CreateAndLogValidationException(notFoundTeacherContactException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsTeacherContactException =
                    new AlreadyExistsTeacherContactException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsTeacherContactException);
            }
            catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException)
            {
                var invalidTeacherContactReferenceException =
                    new InvalidTeacherContactReferenceException(foreignKeyConstraintConflictException);

                throw CreateAndLogValidationException(invalidTeacherContactReferenceException);
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedTeacherContactException =
                    new LockedTeacherContactException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyException(lockedTeacherContactException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                throw CreateAndLogDependencyException(dbUpdateException);
            }
            catch (Exception exception)
            {
                var failedTeacherContactServiceException =
                    new FailedTeacherContactServiceException(exception);

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

            var failedTeacherContactServiceException =
                new FailedTeacherContactServiceException(serviceException);

            var expectedTeacherContactException =
                new TeacherContactServiceException(
                    failedTeacherContactServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectTeacherContactByIdAsync(someTeacherId, someContactId))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <TeacherContact> removeTeacherContactTask =
                this.teacherContactService.RemoveTeacherContactByIdAsync(
                    someTeacherId,
                    someContactId);

            // then
            await Assert.ThrowsAsync <TeacherContactServiceException>(() =>
                                                                      removeTeacherContactTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectTeacherContactByIdAsync(someTeacherId, someContactId),
                                          Times.Once);

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        private IQueryable <TeacherContact> TryCatch(
            ReturningTeacherContactsFunction returningTeacherContactsFunction)
        {
            try
            {
                return(returningTeacherContactsFunction());
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (Exception exception)
            {
                var failedTeacherContactServiceException =
                    new FailedTeacherContactServiceException(exception);

                throw CreateAndLogServiceException(failedTeacherContactServiceException);
            }
        }
        public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync()
        {
            // given
            TeacherContact randomTeacherContact = CreateRandomTeacherContact();
            TeacherContact inputTeacherContact  = randomTeacherContact;
            var            serviceException     = new Exception();

            var failedTeacherContactServiceException =
                new FailedTeacherContactServiceException(serviceException);

            var expectedTeacherContactServiceException =
                new TeacherContactServiceException(failedTeacherContactServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertTeacherContactAsync(It.IsAny <TeacherContact>()))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <TeacherContact> addTeacherContactTask =
                this.teacherContactService.AddTeacherContactAsync(inputTeacherContact);

            // then
            await Assert.ThrowsAsync <TeacherContactServiceException>(() =>
                                                                      addTeacherContactTask.AsTask());

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

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

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