public async Task ShouldThrowDependencyOnModifyIfDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given DateTimeOffset randomDateTime = GetRandomDateTime(); DateTimeOffset dateTime = randomDateTime; Registration randomRegistration = CreateRandomRegistration(dateTime); Registration storageRegistration = randomRegistration; Registration foreignKeyConflictedRegistration = storageRegistration.DeepClone(); storageRegistration.UpdatedDate = dateTime.AddMinutes(GetNegativeRandomNumber()); var dbUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedRegistrationException = new LockedRegistrationException(dbUpdateConcurrencyException); var registrationValidationException = new RegistrationDependencyException(lockedRegistrationException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDateTime); this.storageBrokerMock.Setup(broker => broker.SelectRegistrationByIdAsync(It.IsAny <Guid>())) .ReturnsAsync(storageRegistration); this.storageBrokerMock.Setup(broker => broker.UpdateRegistrationAsync(It.IsAny <Registration>())) .ThrowsAsync(dbUpdateConcurrencyException); // when ValueTask <Registration> modifyRegistrationTask = this.registrationService.ModifyRegistrationAsync(foreignKeyConflictedRegistration); // then await Assert.ThrowsAsync <RegistrationDependencyException>(() => modifyRegistrationTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectRegistrationByIdAsync(It.IsAny <Guid>()), Times.Once); this.storageBrokerMock.Verify(broker => broker.UpdateRegistrationAsync(It.IsAny <Registration>()), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( registrationValidationException))), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
private async ValueTask <Registration> TryCatch(ReturningRegistrationFunction returningRegistrationFunction) { try { return(await returningRegistrationFunction()); } catch (NullRegistrationException nullRegistrationException) { throw CreateAndLogValidationException(nullRegistrationException); } catch (InvalidRegistrationException invalidRegistrationInputException) { throw CreateAndLogValidationException(invalidRegistrationInputException); } catch (NotFoundRegistrationException notFoundRegistrationException) { throw CreateAndLogValidationException(notFoundRegistrationException); } catch (SqlException sqlException) { throw CreateAndLogCriticalDependencyException(sqlException); } catch (DuplicateKeyException duplicateKeyException) { var alreadyExistsRegistrationException = new AlreadyExistsRegistrationException(duplicateKeyException); throw CreateAndLogValidationException(alreadyExistsRegistrationException); } catch (ForeignKeyConstraintConflictException foreignKeyConstraintConflictException) { var invalidRegistrationReferenceException = new InvalidRegistrationReferenceException(foreignKeyConstraintConflictException); throw CreateAndLogValidationException(invalidRegistrationReferenceException); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { var lockedRegistrationException = new LockedRegistrationException(dbUpdateConcurrencyException); throw CreateAndLogDependencyException(lockedRegistrationException); } catch (DbUpdateException dbUpdateException) { throw CreateAndLogDependencyException(dbUpdateException); } catch (Exception exception) { var failedRegistrationServiceException = new FailedRegistrationServiceException(exception); throw CreateAndLogServiceException(failedRegistrationServiceException); } }
public async Task ShouldThrowDependencyExceptionOnRemoveByIdIfDbConcurrencyExceptionOccursAndLogItAsync() { // given DateTimeOffset randomDateTime = GetRandomDateTime(); Registration someRegistration = CreateRandomRegistration(dateTime: randomDateTime); Registration storageRegistration = someRegistration; Guid inputRegistrationId = storageRegistration.Id; var databaseConcurrencyException = new DbUpdateConcurrencyException(); var lockedRegistrationException = new LockedRegistrationException(databaseConcurrencyException); var expectedRegistrationValidationException = new RegistrationDependencyException(lockedRegistrationException); this.storageBrokerMock.Setup(broker => broker.SelectRegistrationByIdAsync(It.IsAny <Guid>())) .ReturnsAsync(storageRegistration); this.storageBrokerMock.Setup(broker => broker.DeleteRegistrationAsync(It.IsAny <Registration>())) .ThrowsAsync(databaseConcurrencyException); // when ValueTask <Registration> removeRegistrationByIdTask = this.registrationService.RemoveRegistrationByIdAsync(inputRegistrationId); // then await Assert.ThrowsAsync <RegistrationDependencyException>(() => removeRegistrationByIdTask.AsTask()); this.storageBrokerMock.Verify(broker => broker.SelectRegistrationByIdAsync(It.IsAny <Guid>()), Times.Once); this.storageBrokerMock.Verify(broker => broker.DeleteRegistrationAsync(It.IsAny <Registration>()), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedRegistrationValidationException))), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
ShouldThrowDependencyExceptionOnRetrieveByIdWhenDbUpdateConcurrencyExceptionOccursAndLogItAsync() { // given Guid someRegistrationId = Guid.NewGuid(); var databaseUpdateConcurrencyException = new DbUpdateConcurrencyException(); var lockedRegistrationException = new LockedRegistrationException(databaseUpdateConcurrencyException); var expectedRegistrationDependencyException = new RegistrationDependencyException(lockedRegistrationException); this.storageBrokerMock.Setup(broker => broker.SelectRegistrationByIdAsync(It.IsAny <Guid>())) .ThrowsAsync(databaseUpdateConcurrencyException); // when ValueTask <Registration> retrieveByIdRegistrationTask = this.registrationService.RetrieveRegistrationByIdAsync(someRegistrationId); // then await Assert.ThrowsAsync <RegistrationDependencyException>(() => retrieveByIdRegistrationTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedRegistrationDependencyException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectRegistrationByIdAsync(It.IsAny <Guid>()), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }