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); } }
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(); }
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(); }
ShouldThrowDependencyExceptionOnRetrieveByIdWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given Guid someCategoryId = Guid.NewGuid(); var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedCategoryException = new LockedCategoryException(databaseUpdateConcurrencyException); var expectedCategoryDependencyException = new CategoryDependencyException(lockedCategoryException); this.storageBrokerMock.Setup(broker => broker.SelectCategoryByIdAsync(It.IsAny <Guid>())) .ThrowsAsync(databaseUpdateConcurrencyException); // 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(); }