public async Task ShouldThrowValidationExceptionOnModifyIfFeeIsNullAndLogItAsync() { //given Fee invalidFee = null; var nullFeeException = new NullFeeException(); var expectedFeeValidationException = new FeeValidationException(nullFeeException); //when ValueTask <Fee> modifyFeeTask = this.feeService.ModifyFeeAsync(invalidFee); //then await Assert.ThrowsAsync <FeeValidationException>(() => modifyFeeTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedFeeValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectFeeByIdAsync(It.IsAny <Guid>()), Times.Never); this.storageBrokerMock.Verify(broker => broker.UpdateFeeAsync(It.IsAny <Fee>()), Times.Never); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnAddWhenFeeIsNullAndLogItAsync() { // given Fee randomFee = default; Fee nullFee = randomFee; var nullFeeException = new NullFeeException(); var expectedFeeValidationException = new FeeValidationException(nullFeeException); // when ValueTask <Fee> createFeeTask = this.feeService.AddFeeAsync(nullFee); // then await Assert.ThrowsAsync <FeeValidationException>(() => createFeeTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedFeeValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertFeeAsync(It.IsAny <Fee>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowValidationExceptionOnModifyIfStorageCreatedByNotSameAsCreatedByAndLogItAsync() { // given int randomNegativeMinutes = GetNegativeRandomNumber(); Guid differentId = Guid.NewGuid(); Guid invalidCreatedBy = differentId; DateTimeOffset randomDate = GetRandomDateTime(); Fee randomFee = CreateRandomFee(randomDate); randomFee.UpdatedDate = GetRandomDateTime(); Fee invalidFee = randomFee; invalidFee.CreatedDate = randomDate.AddMinutes(randomNegativeMinutes); Fee storageFee = randomFee.DeepClone(); Guid feeId = invalidFee.Id; invalidFee.CreatedBy = invalidCreatedBy; var invalidFeeException = new InvalidFeeException(); invalidFeeException.AddData( key: nameof(Fee.CreatedBy), values: $"Id is not the same as {nameof(Fee.CreatedBy)}"); var expectedFeeValidationException = new FeeValidationException(invalidFeeException); this.storageBrokerMock.Setup(broker => broker.SelectFeeByIdAsync(feeId)) .ReturnsAsync(storageFee); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(invalidFee.UpdatedDate); // when ValueTask <Fee> modifyFeeTask = this.feeService.ModifyFeeAsync(invalidFee); // then await Assert.ThrowsAsync <FeeValidationException>(() => modifyFeeTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectFeeByIdAsync(invalidFee.Id), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameValidationExceptionAs( expectedFeeValidationException))), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
private FeeValidationException CreateAndLogValidationException(Exception exception) { var feeValidationException = new FeeValidationException(exception); this.loggingBroker.LogError(feeValidationException); return(feeValidationException); }
public async Task ShouldThrowValidationExceptionOnModifyIfStorageUpdatedDateSameAsUpdatedDateAndLogItAsync() { // given int randomNegativeMinutes = GetNegativeRandomNumber(); int minutesInThePast = randomNegativeMinutes; DateTimeOffset randomDate = GetRandomDateTime(); Fee randomFee = CreateRandomFee(randomDate); randomFee.CreatedDate = randomFee.CreatedDate.AddMinutes(minutesInThePast); Fee invalidFee = randomFee; invalidFee.UpdatedDate = randomDate; Fee storageFee = randomFee.DeepClone(); Guid feeId = invalidFee.Id; var invalidFeeInputException = new InvalidFeeException( parameterName: nameof(Fee.UpdatedDate), parameterValue: invalidFee.UpdatedDate); var expectedFeeValidationException = new FeeValidationException(invalidFeeInputException); this.storageBrokerMock.Setup(broker => broker.SelectFeeByIdAsync(feeId)) .ReturnsAsync(storageFee); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDate); // when ValueTask <Fee> modifyFeeTask = this.feeService.ModifyFeeAsync(invalidFee); // then await Assert.ThrowsAsync <FeeValidationException>(() => modifyFeeTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectFeeByIdAsync(invalidFee.Id), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedFeeValidationException))), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnModifyIfUpdatedDateIsNotRecentAndLogItAsync( int minutes) { // given DateTimeOffset dateTime = GetRandomDateTime(); Fee randomFee = CreateRandomFee(dateTime); Fee inputFee = randomFee; inputFee.UpdatedBy = inputFee.CreatedBy; inputFee.UpdatedDate = dateTime.AddMinutes(minutes); var invalidFeeInputException = new InvalidFeeException(); invalidFeeInputException.AddData( key: nameof(Fee.UpdatedDate), values: "Date is not recent"); var expectedFeeValidationException = new FeeValidationException(invalidFeeInputException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); // when ValueTask <Fee> modifyFeeTask = this.feeService.ModifyFeeAsync(inputFee); // then await Assert.ThrowsAsync <FeeValidationException>(() => modifyFeeTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameValidationExceptionAs( expectedFeeValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectFeeByIdAsync(It.IsAny <Guid>()), Times.Never); this.storageBrokerMock.Verify(broker => broker.UpdateFeeAsync(It.IsAny <Fee>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowValidationExceptionOnModifyIfFeeDoesntExistAndLogItAsync() { // given int randomNegativeMinutes = GetNegativeRandomNumber(); DateTimeOffset dateTime = GetRandomDateTime(); Fee randomFee = CreateRandomFee(dateTime); Fee nonExistentFee = randomFee; nonExistentFee.CreatedDate = dateTime.AddMinutes(randomNegativeMinutes); Fee noFee = null; var notFoundFeeException = new NotFoundFeeException(nonExistentFee.Id); var expectedFeeValidationException = new FeeValidationException(notFoundFeeException); this.storageBrokerMock.Setup(broker => broker.SelectFeeByIdAsync(nonExistentFee.Id)) .ReturnsAsync(noFee); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); // when ValueTask <Fee> modifyFeeTask = this.feeService.ModifyFeeAsync(nonExistentFee); // then await Assert.ThrowsAsync <FeeValidationException>(() => modifyFeeTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectFeeByIdAsync(nonExistentFee.Id), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedFeeValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.UpdateFeeAsync(It.IsAny <Fee>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnAddIfFeeAlreadyExistsAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); Fee randomFee = CreateRandomFee(dateTime); Fee alreadyExistsFee = randomFee; alreadyExistsFee.UpdatedBy = alreadyExistsFee.CreatedBy; string randomMessage = GetRandomMessage(); string exceptionMessage = randomMessage; var duplicateKeyException = new DuplicateKeyException(exceptionMessage); var alreadyExistsFeeException = new AlreadyExistsFeeException(duplicateKeyException); var expectedFeeValidationException = new FeeValidationException(alreadyExistsFeeException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(dateTime); this.storageBrokerMock.Setup(broker => broker.InsertFeeAsync(alreadyExistsFee)) .ThrowsAsync(duplicateKeyException); // when ValueTask <Fee> addFeeTask = this.feeService.AddFeeAsync(alreadyExistsFee); // then await Assert.ThrowsAsync <FeeValidationException>(() => addFeeTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedFeeValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertFeeAsync(alreadyExistsFee), Times.Once); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnAddIfUpdatedDateIsNotSameToCreatedDateAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); Fee randomFee = CreateRandomFee(dateTime); Fee invalidFee = randomFee; invalidFee.UpdatedBy = randomFee.CreatedBy; invalidFee.UpdatedDate = GetRandomDateTime(); var invalidFeeInputException = new InvalidFeeException(); invalidFeeInputException.AddData( key: nameof(Fee.UpdatedDate), values: $"Date is not the same as {nameof(Fee.CreatedDate)}"); var expectedFeeValidationException = new FeeValidationException(invalidFeeInputException); // when ValueTask <Fee> addFeeTask = this.feeService.AddFeeAsync(invalidFee); // then await Assert.ThrowsAsync <FeeValidationException>(() => addFeeTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameValidationExceptionAs( expectedFeeValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertFeeAsync(It.IsAny <Fee>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowValidatonExceptionOnRemoveWhenStorageFeeIsInvalidAndLogItAsync() { // given DateTimeOffset dateTime = GetRandomDateTime(); Fee randomFee = CreateRandomFee(dateTime); Guid inputFeeId = randomFee.Id; Fee inputFee = randomFee; Fee nullStorageFee = null; var notFoundFeeException = new NotFoundFeeException(inputFeeId); var expectedFeeValidationException = new FeeValidationException(notFoundFeeException); this.storageBrokerMock.Setup(broker => broker.SelectFeeByIdAsync(inputFeeId)) .ReturnsAsync(nullStorageFee); // when ValueTask <Fee> actualFeeTask = this.feeService.RemoveFeeByIdAsync(inputFeeId); // then await Assert.ThrowsAsync <FeeValidationException>(() => actualFeeTask.AsTask()); this.storageBrokerMock.Verify(broker => broker.SelectFeeByIdAsync(inputFeeId), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedFeeValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.DeleteFeeAsync(It.IsAny <Fee>()), Times.Never); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowValidationExceptionOnModifyWhenFeeIdIsInvalidAndLogItAsync() { //given Guid invalidFeeId = Guid.Empty; DateTimeOffset dateTime = GetRandomDateTime(); Fee randomFee = CreateRandomFee(dateTime); Fee invalidFee = randomFee; invalidFee.Id = invalidFeeId; var invalidFeeException = new InvalidFeeException( parameterName: nameof(Fee.Id), parameterValue: invalidFee.Id); var expectedFeeValidationException = new FeeValidationException(invalidFeeException); //when ValueTask <Fee> modifyFeeTask = this.feeService.ModifyFeeAsync(invalidFee); //then await Assert.ThrowsAsync <FeeValidationException>(() => modifyFeeTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedFeeValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectFeeByIdAsync(It.IsAny <Guid>()), Times.Never); this.storageBrokerMock.Verify(broker => broker.UpdateFeeAsync(It.IsAny <Fee>()), Times.Never); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnRetrieveByIdWhenStorageFeeIsNullAndLogItAsync() { //given Guid randomFeeId = Guid.NewGuid(); Guid someFeeId = randomFeeId; Fee invalidStorageFee = null; var notFoundFeeException = new NotFoundFeeException(someFeeId); var expectedFeeValidationException = new FeeValidationException(notFoundFeeException); this.storageBrokerMock.Setup(broker => broker.SelectFeeByIdAsync(It.IsAny <Guid>())) .ReturnsAsync(invalidStorageFee); //when ValueTask <Fee> retrieveFeeByIdTask = this.feeService.RetrieveFeeByIdAsync(someFeeId); //then await Assert.ThrowsAsync <FeeValidationException>(() => retrieveFeeByIdTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs( expectedFeeValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectFeeByIdAsync(It.IsAny <Guid>()), Times.Once); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Never); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnRetrieveByIdWhenIdIsInvalidAndLogItAsync() { //given Guid randomFeeId = default; Guid inputFeeId = randomFeeId; var invalidFeeException = new InvalidFeeException(); invalidFeeException.AddData( key: nameof(Fee.Id), values: "Id is required"); var expectedFeeValidationException = new FeeValidationException(invalidFeeException); //when ValueTask <Fee> retrieveFeeByIdTask = this.feeService.RetrieveFeeByIdAsync(inputFeeId); //then await Assert.ThrowsAsync <FeeValidationException>(() => retrieveFeeByIdTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameValidationExceptionAs( expectedFeeValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectFeeByIdAsync(It.IsAny <Guid>()), Times.Never); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Never); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowValidationExceptionOnAddWhenFeeLabelIsInvalidAndLogItAsync (string invalidFeeLabel) { // given DateTimeOffset dateTime = GetRandomDateTime(); Fee randomFee = CreateRandomFee(dateTime); Fee invalidFee = randomFee; invalidFee.Label = invalidFeeLabel; var invalidFeeException = new InvalidFeeException( parameterName: nameof(Fee.Label), parameterValue: invalidFee.Label); var expectedFeeValidationException = new FeeValidationException(invalidFeeException); // when ValueTask <Fee> createFeeTask = this.feeService.AddFeeAsync(invalidFee); // then await Assert.ThrowsAsync <FeeValidationException>(() => createFeeTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedFeeValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertFeeAsync(It.IsAny <Fee>()), Times.Never); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowValidatonExceptionOnRemoveWhenIdIsInvalidAndLogItAsync() { // given Guid randomFeeId = default; Guid inputFeeId = randomFeeId; var invalidFeeInputException = new InvalidFeeException( parameterName: nameof(Fee.Id), parameterValue: inputFeeId); var expectedFeeValidationException = new FeeValidationException(invalidFeeInputException); // when ValueTask <Fee> actualFeeTask = this.feeService.RemoveFeeByIdAsync(inputFeeId); // then await Assert.ThrowsAsync <FeeValidationException>(() => actualFeeTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedFeeValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectFeeByIdAsync(It.IsAny <Guid>()), Times.Never); this.storageBrokerMock.Verify(broker => broker.DeleteFeeAsync(It.IsAny <Fee>()), Times.Never); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }
public async Task ShouldThrowValidationExceptionOnModifyIfFeeIsInvalidAndLogItAsync(string invalidText) { //given var invalidFee = new Fee { Label = invalidText }; var invalidFeeException = new InvalidFeeException(); invalidFeeException.AddData( key: nameof(Fee.Id), values: "Id is required"); invalidFeeException.AddData( key: nameof(Fee.Label), values: "Text is required"); invalidFeeException.AddData( key: nameof(Fee.CreatedBy), values: "Id is required"); invalidFeeException.AddData( key: nameof(Fee.UpdatedBy), values: "Id is required"); invalidFeeException.AddData( key: nameof(Fee.CreatedDate), values: "Date is required"); invalidFeeException.UpsertDataList( key: nameof(Fee.UpdatedDate), value: $"Date is the same as {nameof(Fee.CreatedDate)}"); invalidFeeException.UpsertDataList( key: nameof(Fee.UpdatedDate), value: "Date is required"); var expectedFeeValidationException = new FeeValidationException(invalidFeeException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(invalidFee.UpdatedDate); //when ValueTask <Fee> modifyFeeTask = this.feeService.ModifyFeeAsync(invalidFee); //then await Assert.ThrowsAsync <FeeValidationException>(() => modifyFeeTask.AsTask()); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameValidationExceptionAs( expectedFeeValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.SelectFeeByIdAsync(It.IsAny <Guid>()), Times.Never); this.storageBrokerMock.Verify(broker => broker.UpdateFeeAsync(It.IsAny <Fee>()), Times.Never); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); this.dateTimeBrokerMock.VerifyNoOtherCalls(); }