Пример #1
0
        public async void ShouldThrowValidationExceptionOnAddWhenTagAlreadyExistsAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime         = GetRandomDateTime();
            Tag            randomTag        = CreateRandomTag(dateTime);
            Tag            alreadyExistsTag = randomTag;

            alreadyExistsTag.UpdatedBy = alreadyExistsTag.CreatedBy;

            string randomMessage         = GetRandomMessage();
            string exceptionMessage      = randomMessage;
            var    duplicateKeyException = new DuplicateKeyException(exceptionMessage);

            var alreadyExistsTagException =
                new AlreadyExistsTagException(duplicateKeyException);

            var expectedTagValidationException =
                new TagValidationException(alreadyExistsTagException);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertTagAsync(alreadyExistsTag))
            .ThrowsAsync(duplicateKeyException);

            // when
            ValueTask <Tag> createTagTask =
                this.tagService.AddTagAsync(alreadyExistsTag);

            // then
            await Assert.ThrowsAsync <TagValidationException>(() =>
                                                              createTagTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Пример #2
0
        private async ValueTask <Tag> TryCatch(
            ReturningTagFunction returningTagFunction)
        {
            try
            {
                return(await returningTagFunction());
            }
            catch (NullTagException nullTagException)
            {
                throw CreateAndLogValidationException(nullTagException);
            }
            catch (InvalidTagException invalidTagException)
            {
                throw CreateAndLogValidationException(invalidTagException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsTagException =
                    new AlreadyExistsTagException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsTagException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedTagException = new LockedTagException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyException(lockedTagException);
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                throw CreateAndLogDependencyException(dbUpdateException);
            }
            catch (InvalidTagInputException invalidTagInputException)
            {
                throw CreateAndLogValidationException(invalidTagInputException);
            }
            catch (NotFoundTagException notFoundTagException)
            {
                throw CreateAndLogValidationException(notFoundTagException);
            }
            catch (Exception exception)
            {
                throw CreateAndLogServiceException(exception);
            }
        }