public async Task ShouldThrowDependencyExceptionOnRetrieveWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given Guid randomUserId = Guid.NewGuid(); Guid inputUserId = randomUserId; var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedUserException = new LockedUserException(databaseUpdateConcurrencyException); var expectedUserDependencyException = new UserDependencyException(lockedUserException); this.userManagementBrokerMock.Setup(broker => broker.SelectUserByIdAsync(inputUserId)) .ThrowsAsync(databaseUpdateConcurrencyException); // when ValueTask <User> retrieveUserTask = this.userService.RetrieveUserByIdAsync(inputUserId); // then await Assert.ThrowsAsync <UserDependencyException>(() => retrieveUserTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedUserDependencyException))), Times.Once); this.userManagementBrokerMock.Verify(broker => broker.SelectUserByIdAsync(inputUserId), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.userManagementBrokerMock.VerifyNoOtherCalls(); }
private async ValueTask <User> TryCatch(ReturningUserFunction returningUserFunction) { try { return(await returningUserFunction()); } catch (NullUserException nullUserException) { throw CreateAndLogValidationException(nullUserException); } catch (InvalidUserException invalidUserException) { throw CreateAndLogValidationException(invalidUserException); } catch (NotFoundUserException nullUserException) { throw CreateAndLogValidationException(nullUserException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsUserException = new AlreadyExistsUserException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsUserException); } catch (SqlException sqlException) { var failedUserStorageException = new FailedUserStorageException(sqlException); throw CreateAndLogCriticalDependencyException(failedUserStorageException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedUserException = new LockedUserException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedUserException); } catch (DbUpdateException dbUpdateException) { var failedUserStorageException = new FailedUserStorageException(dbUpdateException); throw CreateAndLogDependencyException(failedUserStorageException); } catch (Exception exception) { var failedUserServiceException = new FailedUserServiceException(exception); throw CreateAndLogServiceException(failedUserServiceException); } }
public async Task ShouldThrowDependencyExceptionOnModifyIfDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given int randomNegativeNumber = GetNegativeRandomNumber(); DateTimeOffset randomDateTime = GetRandomDateTime(); User randomUser = CreateRandomUser(randomDateTime); User someUser = randomUser; someUser.CreatedDate = randomDateTime.AddMinutes(randomNegativeNumber); var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedUserException = new LockedUserException(databaseUpdateConcurrencyException); var expectedUserDependencyException = new UserDependencyException(lockedUserException); this.userManagementBrokerMock.Setup(broker => broker.SelectUserByIdAsync(It.IsAny <Guid>())) .ThrowsAsync(databaseUpdateConcurrencyException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDateTime); // when ValueTask <User> modifyUserTask = this.userService.ModifyUserAsync(someUser); // then await Assert.ThrowsAsync <UserDependencyException>(() => modifyUserTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.userManagementBrokerMock.Verify(broker => broker.SelectUserByIdAsync(It.IsAny <Guid>()), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedUserDependencyException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.userManagementBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }