public async Task ShouldThrowValidationExceptionOnModifyWhenGuardianIdIsInvalidAndLogItAsync() { //given Guid invalidGuardianId = Guid.Empty; DateTimeOffset dateTime = GetRandomDateTime(); Guardian randomGuardian = CreateRandomGuardian(dateTime); Guardian invalidGuardian = randomGuardian; invalidGuardian.Id = invalidGuardianId; var invalidGuardianException = new InvalidGuardianException( parameterName: nameof(Guardian.Id), parameterValue: invalidGuardian.Id); var expectedGuardianValidationException = new GuardianValidationException(invalidGuardianException); //when ValueTask <Guardian> modifyGuardianTask = this.guardianService.ModifyGuardianAsync(invalidGuardian); //then await Assert.ThrowsAsync <GuardianValidationException>(() => modifyGuardianTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedGuardianValidationException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowValidationExceptionOnModifyWhenGuardianIsNullAndLogItAsync() { //given Guardian invalidGuardian = null; var nullGuardianException = new NullGuardianException(); var expectedGuardianValidationException = new GuardianValidationException(nullGuardianException); //when ValueTask <Guardian> modifyGuardianTask = this.guardianService.ModifyGuardianAsync(invalidGuardian); //then await Assert.ThrowsAsync <GuardianValidationException>(() => modifyGuardianTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedGuardianValidationException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnModifyWhenUpdatedDateIsSameAsCreatedDateAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); Guardian randomGuardian = CreateRandomGuardian(dateTime); Guardian inputGuardian = randomGuardian; var invalidGuardianInputException = new InvalidGuardianException( parameterName: nameof(Guardian.UpdatedDate), parameterValue: inputGuardian.UpdatedDate); var expectedGuardianValidationException = new GuardianValidationException(invalidGuardianInputException); // when ValueTask <Guardian> modifyGuardianTask = this.guardianService.ModifyGuardianAsync(inputGuardian); // then await Assert.ThrowsAsync <GuardianValidationException>(() => modifyGuardianTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedGuardianValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectGuardianByIdAsync(It.IsAny <Guid>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnAddWhenGuardianIsNullAndLogItAsync() { // given Guardian randomGuardian = default; Guardian nullGuardian = randomGuardian; var nullGuardianException = new NullGuardianException(); var expectedGuardianValidationException = new GuardianValidationException(nullGuardianException); // when ValueTask <Guardian> createGuardianTask = this.guardianService.CreateGuardianAsync(nullGuardian); // then await Assert.ThrowsAsync <GuardianValidationException>(() => createGuardianTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedGuardianValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertGuardianAsync(It.IsAny <Guardian>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
private GuardianValidationException CreateAndLogValidationException(Exception exception) { var GuardianValidationException = new GuardianValidationException(exception); this.loggingBroker.LogError(GuardianValidationException); return(GuardianValidationException); }
public async Task ShouldThrowValidationExceptionOnModifyIfStorageCreatedByNotSameAsCreatedByAndLogItAsync() { // given int randomNegativeMinutes = GetNegativeRandomNumber(); Guid differentId = Guid.NewGuid(); Guid invalidCreatedBy = differentId; DateTimeOffset randomDate = GetRandomDateTime(); Guardian randomGuardian = CreateRandomGuardian(randomDate); Guardian invalidGuardian = randomGuardian; invalidGuardian.CreatedDate = randomDate.AddMinutes(randomNegativeMinutes); Guardian storageGuardian = randomGuardian.DeepClone(); Guid guardianId = invalidGuardian.Id; invalidGuardian.CreatedBy = invalidCreatedBy; var invalidGuardianInputException = new InvalidGuardianException( parameterName: nameof(Guardian.CreatedBy), parameterValue: invalidGuardian.CreatedBy); var expectedGuardianValidationException = new GuardianValidationException(invalidGuardianInputException); this.storageBrokerMock.Setup(broker => broker.SelectGuardianByIdAsync(guardianId)) .ReturnsAsync(storageGuardian); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDate); // when ValueTask <Guardian> modifyGuardianTask = this.guardianService.ModifyGuardianAsync(invalidGuardian); // then await Assert.ThrowsAsync <GuardianValidationException>(() => modifyGuardianTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectGuardianByIdAsync(invalidGuardian.Id), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedGuardianValidationException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnAddWhenGuardianAlreadyExistsAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); Guardian randomGuardian = CreateRandomGuardian(dateTime); Guardian alreadyExistsGuardian = randomGuardian; alreadyExistsGuardian.UpdatedBy = alreadyExistsGuardian.CreatedBy; string randomMessage = GetRandomMessage(); string exceptionMessage = randomMessage; var duplicateKeyException = new DuplicateKeyException(exceptionMessage); var alreadyExistsGuardianException = new AlreadyExistsGuardianException(duplicateKeyException); var expectedGuardianValidationException = new GuardianValidationException(alreadyExistsGuardianException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); this.storageBrokerMock.Setup(broker => broker.InsertGuardianAsync(alreadyExistsGuardian)) .ThrowsAsync(duplicateKeyException); // when ValueTask <Guardian> createGuardianTask = this.guardianService.CreateGuardianAsync(alreadyExistsGuardian); // then await Assert.ThrowsAsync <GuardianValidationException>(() => createGuardianTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedGuardianValidationException))), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertGuardianAsync(alreadyExistsGuardian), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowValidationExceptionOnModifyIfGuardianDoesntExistAndLogItAsync() { // given int randomNegativeMinutes = GetNegativeRandomNumber(); DateTimeOffset dateTime = GetRandomDateTime(); Guardian randomGuardian = CreateRandomGuardian(dateTime); Guardian nonExistentGuardian = randomGuardian; nonExistentGuardian.CreatedDate = dateTime.AddMinutes(randomNegativeMinutes); Guardian noGuardian = null; var notFoundGuardianException = new NotFoundGuardianException(nonExistentGuardian.Id); var expectedGuardianValidationException = new GuardianValidationException(notFoundGuardianException); this.storageBrokerMock.Setup(broker => broker.SelectGuardianByIdAsync(nonExistentGuardian.Id)) .ReturnsAsync(noGuardian); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); // when ValueTask <Guardian> modifyGuardianTask = this.guardianService.ModifyGuardianAsync(nonExistentGuardian); // then await Assert.ThrowsAsync <GuardianValidationException>(() => modifyGuardianTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectGuardianByIdAsync(nonExistentGuardian.Id), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedGuardianValidationException))), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnAddWhenCreatedDateIsNotRecentAndLogItAsync( int minutes) { // given DateTimeOffset dateTime = GetRandomDateTime(); Guardian randomGuardian = CreateRandomGuardian(dateTime); Guardian inputGuardian = randomGuardian; inputGuardian.UpdatedBy = inputGuardian.CreatedBy; inputGuardian.CreatedDate = dateTime.AddMinutes(minutes); inputGuardian.UpdatedDate = inputGuardian.CreatedDate; var invalidGuardianInputException = new InvalidGuardianException( parameterName: nameof(Guardian.CreatedDate), parameterValue: inputGuardian.CreatedDate); var expectedGuardianValidationException = new GuardianValidationException(invalidGuardianInputException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); // when ValueTask <Guardian> createGuardianTask = this.guardianService.CreateGuardianAsync(inputGuardian); // then await Assert.ThrowsAsync <GuardianValidationException>(() => createGuardianTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedGuardianValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertGuardianAsync(It.IsAny <Guardian>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnDeleteWhenStorageGuardianIsNullAndLogItAsync() { // given Guid randomGuardianId = Guid.NewGuid(); Guid inputGuardianId = randomGuardianId; Guardian invalidStorageGuardian = null; var notFoundGuardianException = new NotFoundGuardianException(inputGuardianId); var expectedGuardianValidationException = new GuardianValidationException(notFoundGuardianException); this.storageBrokerMock.Setup(broker => broker.SelectGuardianByIdAsync(inputGuardianId)) .ReturnsAsync(invalidStorageGuardian); // when ValueTask <Guardian> deleteGuardianTask = this.guardianService.RemoveGuardianByIdAsync(inputGuardianId); // then await Assert.ThrowsAsync <GuardianValidationException>(() => deleteGuardianTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedGuardianValidationException))), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Never); this.storageBrokerMock.Verify(broker => broker.SelectGuardianByIdAsync(inputGuardianId), Times.Once); this.storageBrokerMock.Verify(broker => broker.DeleteGuardianAsync(It.IsAny <Guardian>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnDeleteWhenIdIsInvalidAndLogItAsync() { // given Guid randomGuardianId = default; Guid inputGuardianId = randomGuardianId; var invalidGuardianInputException = new InvalidGuardianException( parameterName: nameof(Guardian.Id), parameterValue: inputGuardianId); var expectedGuardianValidationException = new GuardianValidationException(invalidGuardianInputException); // when ValueTask <Guardian> deleteGuardianTask = this.guardianService.RemoveGuardianByIdAsync(inputGuardianId); // then await Assert.ThrowsAsync <GuardianValidationException>(() => deleteGuardianTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedGuardianValidationException))), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Never); this.storageBrokerMock.Verify(broker => broker.SelectGuardianByIdAsync(It.IsAny <Guid>()), Times.Never); this.storageBrokerMock.Verify(broker => broker.DeleteGuardianAsync(It.IsAny <Guardian>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowValidationExceptionOnRetrieveWhenStorageGuardianIsNullAndLogItAsync() { // given DateTimeOffset dateTimeOffset = GetRandomDateTime(); Guid randomGuardianId = Guid.NewGuid(); Guid inputGuardianId = randomGuardianId; Guardian nullGuardian = default; var nullGuardianException = new NotFoundGuardianException(guardianId: inputGuardianId); var expectedValidationException = new GuardianValidationException(nullGuardianException); this.storageBrokerMock.Setup(broker => broker.SelectGuardianByIdAsync(inputGuardianId)) .ReturnsAsync(nullGuardian); // when ValueTask <Guardian> retrieveGuardianTask = this.guardianService.RetrieveGuardianByIdAsync(inputGuardianId); // then await Assert.ThrowsAsync <GuardianValidationException>(() => retrieveGuardianTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectGuardianByIdAsync(inputGuardianId), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Never); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowValidationExceptionOnAddWhenGuardianFirstNameIsInvalidAndLogItAsync( string invalidGuardianFirstName) { // given DateTimeOffset dateTime = GetRandomDateTime(); Guardian randomGuardian = CreateRandomGuardian(dateTime); Guardian invalidGuardian = randomGuardian; invalidGuardian.FirstName = invalidGuardianFirstName; var invalidGuardianException = new InvalidGuardianException( parameterName: nameof(Guardian.FirstName), parameterValue: invalidGuardian.FirstName); var expectedGuardianValidationException = new GuardianValidationException(invalidGuardianException); // when ValueTask <Guardian> createGuardianTask = this.guardianService.CreateGuardianAsync(invalidGuardian); // then await Assert.ThrowsAsync <GuardianValidationException>(() => createGuardianTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedGuardianValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertGuardianAsync(It.IsAny <Guardian>()), Times.Never); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }