Esempio n. 1
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);
            }
        }
Esempio n. 2
0
        public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            int            randomNegativeNumber = GetNegativeRandomNumber();
            DateTimeOffset randomDateTime       = GetRandomDateTime();
            Tag            randomTag            = CreateRandomTag(randomDateTime);
            Tag            someTag = randomTag;

            someTag.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber);
            var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();
            var lockedTagException = new LockedTagException(databaseUpdateConcurrencyException);

            var expectedTagDependencyException =
                new TagDependencyException(lockedTagException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectTagByIdAsync(someTag.Id))
            .ThrowsAsync(databaseUpdateConcurrencyException);

            // when
            ValueTask <Tag> modifyTagTask =
                this.tagService.ModifyTagAsync(someTag);

            // then
            await Assert.ThrowsAsync <TagDependencyException>(() =>
                                                              modifyTagTask.AsTask());

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectTagByIdAsync(someTag.Id),
                                          Times.Once);

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Esempio n. 3
0
        ShouldThrowDependencyExceptionOnRetrieveByIdWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            Guid someTagId = Guid.NewGuid();
            var  databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();

            var lockedTagException =
                new LockedTagException(databaseUpdateConcurrencyException);

            var expectedTagDependencyException =
                new TagDependencyException(lockedTagException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectTagByIdAsync(It.IsAny <Guid>()))
            .ThrowsAsync(databaseUpdateConcurrencyException);

            // when
            ValueTask <Tag> retrieveByIdTagTask =
                this.tagService.RetrieveTagByIdAsync(someTagId);

            // then
            await Assert.ThrowsAsync <TagDependencyException>(() =>
                                                              retrieveByIdTagTask.AsTask());

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

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

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