Esempio n. 1
0
        public async void ShouldThrowValidationExceptionOnAddWhenCategoryAlreadyExistsAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime              = GetRandomDateTime();
            Category       randomCategory        = CreateRandomCategory(dateTime);
            Category       alreadyExistsCategory = randomCategory;

            alreadyExistsCategory.UpdatedBy = alreadyExistsCategory.CreatedBy;

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

            var alreadyExistsCategoryException =
                new AlreadyExistsCategoryException(duplicateKeyException);

            var expectedCategoryValidationException =
                new CategoryValidationException(alreadyExistsCategoryException);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertCategoryAsync(alreadyExistsCategory))
            .ThrowsAsync(duplicateKeyException);

            // when
            ValueTask <Category> createCategoryTask =
                this.categoryService.AddCategoryAsync(alreadyExistsCategory);

            // then
            await Assert.ThrowsAsync <CategoryValidationException>(() =>
                                                                   createCategoryTask.AsTask());

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Esempio n. 2
0
        private async ValueTask <Category> TryCatch(
            ReturningCategoryFunction returningCategoryFunction)
        {
            try
            {
                return(await returningCategoryFunction());
            }
            catch (NullCategoryException nullCategoryException)
            {
                throw CreateAndLogValidationException(nullCategoryException);
            }
            catch (InvalidCategoryException invalidCategoryException)
            {
                throw CreateAndLogValidationException(invalidCategoryException);
            }
            catch (SqlException sqlException)
            {
                throw CreateAndLogCriticalDependencyException(sqlException);
            }
            catch (NotFoundCategoryException notFoundCategoryException)
            {
                throw CreateAndLogValidationException(notFoundCategoryException);
            }
            catch (DuplicateKeyException duplicateKeyException)
            {
                var alreadyExistsCategoryException =
                    new AlreadyExistsCategoryException(duplicateKeyException);

                throw CreateAndLogValidationException(alreadyExistsCategoryException);
            }
            catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
            {
                var lockedCategoryException = new LockedCategoryException(dbUpdateConcurrencyException);

                throw CreateAndLogDependencyException(lockedCategoryException);
            }
            catch (DbUpdateException dbUpdateException)
            {
                throw CreateAndLogDependencyException(dbUpdateException);
            }
            catch (InvalidCategoryInputException invalidCategoryInputException)
            {
                throw CreateAndLogValidationException(invalidCategoryInputException);
            }
            catch (Exception exception)
            {
                throw CreateAndLogServiceException(exception);
            }
        }