private async ValueTask <Contact> TryCatch(ReturningContactFunction returningContactFunction)
        {
            try
            {
                return(await returningContactFunction());
            }
            catch (NullContactException nullContactException)
            {
                throw CreateAndLogValidationException(nullContactException);
            }
            catch (NotFoundContactException notFoundContactException)
            {
                throw CreateAndLogValidationException(notFoundContactException);
            }
            catch (InvalidContactException invalidContactException)
            {
                throw CreateAndLogValidationException(invalidContactException);
            }
            catch (SqlException sqlException)
            {
                var failedContactStorageException =
                    new FailedContactStorageException(sqlException);

                throw CreateAndLogCriticalDependencyException(failedContactStorageException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsContactException =
                    new AlreadyExistsContactException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsContactException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedContactException =
                    new LockedContactException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyException(lockedContactException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                throw CreateAndLogDependencyException(dbUpdateException);
            }
            catch (Exception exception)
            {
                var failedContactServiceException =
                    new FailedContactServiceException(exception);

                throw CreateAndLogServiceException(failedContactServiceException);
            }
        }
        public async Task ShouldThrowServiceExceptionOnModifyIfServiceExceptionOccursAndLogItAsync()
        {
            // given
            int            randomNegativeNumber = GetNegativeRandomNumber();
            DateTimeOffset randomDateTime       = GetRandomDateTime();
            Contact        randomContact        = CreateRandomContact(randomDateTime);
            Contact        someContact          = randomContact;

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

            var failedContactServiceException =
                new FailedContactServiceException(serviceException);

            var expectedContactServiceException =
                new ContactServiceException(failedContactServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectContactByIdAsync(someContact.Id))
            .ThrowsAsync(serviceException);

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

            // when
            ValueTask <Contact> modifyContactTask =
                this.contactService.ModifyContactAsync(someContact);

            // then
            await Assert.ThrowsAsync <ContactServiceException>(() =>
                                                               modifyContactTask.AsTask());

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

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectContactByIdAsync(someContact.Id),
                                          Times.Once);

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Пример #3
0
        public async Task ShouldThrowServiceExceptionOnAddWhenExceptionOccursAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime      = GetRandomDateTime();
            Contact        randomContact = CreateRandomContact(dateTime);
            Contact        inputContact  = randomContact;

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

            var failedContactServiceException =
                new FailedContactServiceException(serviceException);

            var expectedContactServiceException =
                new ContactServiceException(failedContactServiceException);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertContactAsync(inputContact))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <Contact> registerContactTask =
                this.contactService.AddContactAsync(inputContact);

            // then
            await Assert.ThrowsAsync <ContactServiceException>(() =>
                                                               registerContactTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        private IQueryable <Contact> TryCatch(ReturningQueryableContactFunction returningQueryableContactFunction)
        {
            try
            {
                return(returningQueryableContactFunction());
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (Exception exception)
            {
                var failedContactServiceException =
                    new FailedContactServiceException(exception);

                throw CreateAndLogServiceException(failedContactServiceException);
            }
        }
        public async Task ShouldThrowServiceExceptionOnDeleteWhenExceptionOccursAndLogItAsync()
        {
            // given
            Guid randomContactId  = Guid.NewGuid();
            Guid inputContactId   = randomContactId;
            var  serviceException = new Exception();

            var failedContactServiceException =
                new FailedContactServiceException(serviceException);

            var expectedContactServiceException =
                new ContactServiceException(failedContactServiceException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectContactByIdAsync(inputContactId))
            .ThrowsAsync(serviceException);

            // when
            ValueTask <Contact> deleteContactTask =
                this.contactService.RemoveContactByIdAsync(inputContactId);

            // then
            await Assert.ThrowsAsync <ContactServiceException>(() =>
                                                               deleteContactTask.AsTask());

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

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

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

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

            var failedContactServiceException =
                new FailedContactServiceException(serviceException);

            var expectedContactServiceException =
                new ContactServiceException(failedContactServiceException);

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

            // when
            Action retrieveAllContactsAction = () =>
                                               this.contactService.RetrieveAllContacts();

            // then
            Assert.Throws <ContactServiceException>(
                retrieveAllContactsAction);

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Пример #7
0
        public async Task ShouldThrowServiceExceptionOnRetrieveWhenExceptionOccursAndLogIt()
        {
            // given
            var serviceException = new Exception();

            var failedContactServiceException =
                new FailedContactServiceException(serviceException);

            var expectedContactServiceException =
                new ContactServiceException(failedContactServiceException);

            var guid = Guid.NewGuid();

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

            // when
            ValueTask <Contact> retrieveTask = this.contactService.RetrieveContactByIdAsync(guid);

            // then
            await Assert.ThrowsAsync <ContactServiceException>(() => retrieveTask.AsTask());

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

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

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

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