public async Task <UpdateCustomerCommandResponse> Handle(UpdateCustomerCommand request, CancellationToken cancellationToken) { var response = new UpdateCustomerCommandResponse(); var validator = new UpdateCustomerCommandValidator(); var result = await validator.ValidateAsync(request); if (result.Errors.Count > 0) { response.Success = false; response.ValidationErrors = new List <string>(); foreach (var error in result.Errors) { response.ValidationErrors.Add(error.ErrorMessage); } } if (result.Errors.Count > 0) { throw new ValidationException(result); } if (response.Success) { var customer = _mapper.Map <Customer>(request); await _customerRepository.UpdateAsync(customer); response.Customer = _mapper.Map <UpdateCustomerCommandViewModel>(customer); } return(response); }
public UpdateCustomerHandlerTest(QueryTestFixture fixture) { _unitOfWork = fixture.UnitOfWork; _bus = fixture.Bus; _notifications = fixture.Notifications; _validator = new UpdateCustomerCommandValidator(); }
public void TestValidate_CustomerNotFound_ValidationError( [Frozen] Mock <IRepository <Entities.Customer> > customerRepoMock, UpdateCustomerCommandValidator sut, UpdateCustomerCommand command, string accountNumber, List <CustomerAddressDto> addresses, string contactType, List <PersonPhoneDto> phoneNumbers ) { //Arrange var customerDto = new StoreCustomerDto { AccountNumber = accountNumber, Addresses = addresses, Contacts = new List <StoreCustomerContactDto> { new StoreCustomerContactDto { ContactType = contactType, ContactPerson = new PersonDto { EmailAddresses = new List <PersonEmailAddressDto> { new PersonEmailAddressDto { EmailAddress = EmailAddress.Create("*****@*****.**").Value } }, PhoneNumbers = phoneNumbers } } } }; command.Customer = customerDto; command.Customer.AccountNumber = command.Customer.AccountNumber.Substring(0, 10); customerRepoMock.Setup(x => x.GetBySpecAsync( It.IsAny <GetCustomerSpecification>(), It.IsAny <CancellationToken>() )) .ReturnsAsync((Entities.StoreCustomer)null); //Act var result = sut.TestValidate(command); //Assert result.ShouldHaveValidationErrorFor(command => command.Customer.AccountNumber) .WithErrorMessage("Customer does not exist"); }
public void ItShouldFailIfCustomerDoesNotExist() { Customer customer = null; var readStore = new Mock <ICustomerRepository>(); readStore.Setup(_ => _.GetCustomerById(It.IsAny <int>())).ReturnsAsync(customer); var sut = new UpdateCustomerCommandValidator(readStore.Object); var result = sut.Validate(new UpdateCustomerCommand { Id = 1 }); result.IsValid.ShouldBe(false); result.Errors[0].ErrorMessage.ShouldBe("Customer must exist to update"); }
public void TestValidate_WithAccountNumberTooLong_ValidationError( UpdateCustomerCommandValidator sut, UpdateCustomerCommand command ) { //Arrange command.Customer.AccountNumber = "AW000000011"; //Act var result = sut.TestValidate(command); //Assert result.ShouldHaveValidationErrorFor(command => command.Customer.AccountNumber) .WithErrorMessage("Account number must not exceed 10 characters"); }
public void TestValidate_WithEmptyAccountNumber_ValidationError( UpdateCustomerCommandValidator sut, UpdateCustomerCommand command ) { //Arrange command.Customer.AccountNumber = null; //Act var result = sut.TestValidate(command); //Assert result.ShouldHaveValidationErrorFor(command => command.Customer.AccountNumber) .WithErrorMessage("Account number is required"); }
public void ItShouldDeleteIfExistingCustomer() { var customer = new Customer { CustomerId = 1 }; var readStore = new Mock <ICustomerRepository>(); readStore.Setup(_ => _.GetCustomerById(It.IsAny <int>())).ReturnsAsync(customer); var sut = new UpdateCustomerCommandValidator(readStore.Object); var result = sut.Validate(new UpdateCustomerCommand { Id = 1 }); result.IsValid.ShouldBe(true); }
public async Task ValidateAsync_InvalidCommand_ValidationResultIsNotValid() { // Arrange var validator = new UpdateCustomerCommandValidator(); var command = new UpdateCustomerCommand { PhoneNumber = 123456789 }; // Act var result = await validator.ValidateAsync(command); // Assert result.Should().NotBeNull(); result.IsValid.Should().BeFalse(); result.Errors.Should().NotBeEmpty(); }
public async Task ValidateAsync_ValidCommand_ValidationResultIsValid() { // Arrange var validator = new UpdateCustomerCommandValidator(); var command = new UpdateCustomerCommand { Id = Guid.Parse("2DEE6F31-67F4-43E1-A944-9672B03B257C"), PhoneNumber = 123456789, Website = "https://www.example.com" }; // Act var result = await validator.ValidateAsync(command); // Assert result.Should().NotBeNull(); result.IsValid.Should().BeTrue(); result.Errors.Should().BeEmpty(); }
public void TestValidate_ValidCommand_NoValidationError( [Frozen] Mock <IRepository <Entities.Customer> > customerRepoMock, Entities.StoreCustomer customer, UpdateCustomerCommandValidator sut, UpdateCustomerCommand command ) { //Arrange command.Customer.AccountNumber = "1"; customerRepoMock.Setup(x => x.GetBySpecAsync( It.IsAny <GetCustomerSpecification>(), It.IsAny <CancellationToken>() )) .ReturnsAsync(customer); //Act var result = sut.TestValidate(command); //Assert result.ShouldNotHaveValidationErrorFor(command => command.Customer); result.ShouldNotHaveValidationErrorFor(command => command.Customer.AccountNumber); }
/// <summary> /// Returns true if ... is valid. /// </summary> /// <returns><c>true</c> if this instance is valid; otherwise, <c>false</c>.</returns> /// <exception cref="System.NotImplementedException"></exception> public override bool IsValid() { ValidationResult = new UpdateCustomerCommandValidator().Validate(this); return(ValidationResult.IsValid); }