Esempio n. 1
0
        public void ShouldThrowDependencyExceptionOnRetrieveAllCategoriesWhenSqlExceptionOccursAndLogIt()
        {
            // given
            var sqlException = GetSqlException();

            var expectedCategoryDependencyException =
                new CategoryDependencyException(sqlException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectAllCategories())
            .Throws(sqlException);

            // when . then
            Assert.Throws <CategoryDependencyException>(() =>
                                                        this.categoryService.RetrieveAllCategories());

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

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowDependencyExceptionOnRetrieveByIdWhenDbExceptionOccursAndLogItAsync()
        {
            // given
            Guid someCategoryId          = Guid.NewGuid();
            var  databaseUpdateException = new DbUpdateException();

            var expectedCategoryDependencyException =
                new CategoryDependencyException(databaseUpdateException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCategoryByIdAsync(It.IsAny <Guid>()))
            .ThrowsAsync(databaseUpdateException);

            // when
            ValueTask <Category> retrieveByIdCategoryTask =
                this.categoryService.RetrieveCategoryByIdAsync(someCategoryId);

            // then
            await Assert.ThrowsAsync <CategoryDependencyException>(() =>
                                                                   retrieveByIdCategoryTask.AsTask());

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
        public async Task ShouldThrowDependencyExceptionOnRetrieveByIdWhenSqlExceptionOccursAndLogItAsync()
        {
            // given
            Guid         randomCategoryId = Guid.NewGuid();
            Guid         inputCategoryId  = randomCategoryId;
            SqlException sqlException     = GetSqlException();

            var exceptionCategoryDependencyException =
                new CategoryDependencyException(sqlException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCategoryByIdAsync(inputCategoryId))
            .ThrowsAsync(sqlException);

            // when
            ValueTask <Category> retrieveCategoryByIdTask =
                this.categoryService.RetrieveCategoryByIdAsync(inputCategoryId);

            // then
            await Assert.ThrowsAsync <CategoryDependencyException>(() =>
                                                                   retrieveCategoryByIdTask.AsTask());

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Esempio n. 4
0
        private CategoryDependencyException CreateAndLogDependencyException(Exception exception)
        {
            var categoryDependencyException = new CategoryDependencyException(exception);

            this.loggingBroker.LogError(categoryDependencyException);

            return(categoryDependencyException);
        }
        public async Task ShouldThrowDependencyExceptionOnAddWhenDbExceptionOccursAndLogItAsync()
        {
            // given
            DateTimeOffset dateTime       = GetRandomDateTime();
            Category       randomCategory = CreateRandomCategory(dateTime);
            Category       inputCategory  = randomCategory;

            inputCategory.UpdatedBy = inputCategory.CreatedBy;
            var databaseUpdateException = new DbUpdateException();

            var expectedCategoryDependencyException =
                new CategoryDependencyException(databaseUpdateException);

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

            this.storageBrokerMock.Setup(broker =>
                                         broker.InsertCategoryAsync(inputCategory))
            .ThrowsAsync(databaseUpdateException);

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

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

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

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

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

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }
Esempio n. 6
0
        public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            int            randomNegativeNumber = GetNegativeRandomNumber();
            DateTimeOffset randomDateTime       = GetRandomDateTime();
            Category       randomCategory       = CreateRandomCategory(randomDateTime);
            Category       someCategory         = randomCategory;

            someCategory.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber);
            var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();
            var lockedCategoryException            = new LockedCategoryException(databaseUpdateConcurrencyException);

            var expectedCategoryDependencyException =
                new CategoryDependencyException(lockedCategoryException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCategoryByIdAsync(someCategory.Id))
            .ThrowsAsync(databaseUpdateConcurrencyException);

            // when
            ValueTask <Category> modifyCategoryTask =
                this.categoryService.ModifyCategoryAsync(someCategory);

            // then
            await Assert.ThrowsAsync <CategoryDependencyException>(() =>
                                                                   modifyCategoryTask.AsTask());

            this.storageBrokerMock.Verify(broker =>
                                          broker.SelectCategoryByIdAsync(someCategory.Id),
                                          Times.Once);

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

            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
            this.dateTimeBrokerMock.VerifyNoOtherCalls();
        }
Esempio n. 7
0
        public async Task ShouldThrowDependencyExceptionOnDeleteWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync()
        {
            // given
            Guid randomCategoryId = Guid.NewGuid();
            Guid inputCategoryId  = randomCategoryId;
            var  databaseUpdateConcurrencyException = new DbUpdateConcurrencyException();

            var lockedCategoryException =
                new LockedCategoryException(databaseUpdateConcurrencyException);

            var expectedStudentCategoryException =
                new CategoryDependencyException(lockedCategoryException);

            this.storageBrokerMock.Setup(broker =>
                                         broker.SelectCategoryByIdAsync(inputCategoryId))
            .ThrowsAsync(databaseUpdateConcurrencyException);

            // when
            ValueTask <Category> deleteStudentCategoryTask =
                this.categoryService.RemoveCategoryByIdAsync(inputCategoryId);

            // then
            await Assert.ThrowsAsync <CategoryDependencyException>(() => deleteStudentCategoryTask.AsTask());

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

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

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