public async void CreateCustomerCommand_EmptyName_DoesNotCreateCustomer() { // Arrange var mockCustomerEmptyFirstName = _fixture.Build <CreateCustomerCommand>() .With(p => p.FirstName, string.Empty) .Create(); var mockCustomerEmptyLastName = _fixture.Build <CreateCustomerCommand>() .Without(p => p.LastName) .Create(); // Act var createCustomerCommandHandler = new CreateCustomerCommandHandler(_petShopContext); var newCustomerId1 = await createCustomerCommandHandler.Handle(mockCustomerEmptyFirstName, CancellationToken.None); var newCustomerId2 = await createCustomerCommandHandler.Handle(mockCustomerEmptyLastName, CancellationToken.None); // Assert var numberOfCustomers = _petShopContext.Customers.Count(); Assert.Equal(0, numberOfCustomers); Assert.Equal(Guid.Empty, newCustomerId1); Assert.Equal(Guid.Empty, newCustomerId2); }
public async Task UpdateAsync() { var dataAccess = new CustomerDataAccess(this.Context); //Act var sutCreate = new CreateCustomerCommandHandler(dataAccess); var resultCreate = await sutCreate.Handle(new CreateCustomerCommand { Data = CustomerTestData.CustomerDataDTO }, CancellationToken.None); //Act var sutUpdate = new UpdateCustomerCommandHandler(dataAccess); var resultUpdate = await sutUpdate.Handle(new UpdateCustomerCommand { Id = resultCreate.Data.Id, Data = new Common.DTO.CustomerDataDTO { Phone = "0721230000" } }, CancellationToken.None); //Assert Assert.IsTrue(resultUpdate.Succeeded); }
public async void Handle_GivenValidCustomer_ShouldCreateCustomer() { // Arrange var sut = new CreateCustomerCommandHandler(_logger, _mapper, _customerRepository); var newCustomerCommandRequest = new CreateCustomerCommand { Email = "*****@*****.**", MonthlyIncome = 20000, MonthlyExpense = 200 }; // Act var result = await sut.Handle(newCustomerCommandRequest, CancellationToken.None); var customerFromDb = _customerDbContext.Customers.Find(result.Id); // Assert Assert.NotNull(result); Assert.IsType <Guid>(result.Id); Assert.Equal(newCustomerCommandRequest.Email, result.Email); Assert.Equal(newCustomerCommandRequest.MonthlyExpense, result.MonthlyExpense); Assert.Equal(newCustomerCommandRequest.MonthlyIncome, result.MonthlyIncome); Assert.NotNull(customerFromDb); Assert.Equal(result.Id, customerFromDb.Id); Assert.Equal(newCustomerCommandRequest.Email, customerFromDb.Email); Assert.Equal(newCustomerCommandRequest.MonthlyExpense, customerFromDb.MonthlyExpense); Assert.Equal(newCustomerCommandRequest.MonthlyIncome, customerFromDb.MonthlyIncome); }
public async void Handle_GivenCustomerWithInSufficientBalance_ShouldThrowBadRequest() { // Arrange // Create Customer var customerCreation = new CreateCustomerCommandHandler(_customerLogger, _mapper, _customerRepository); var newCustomerCommandRequest = new CreateCustomerCommand { Email = "*****@*****.**", MonthlyIncome = 2000, MonthlyExpense = 1200 }; var customerCreationResult = await customerCreation.Handle(newCustomerCommandRequest, CancellationToken.None); var newAccountCommandRequest = new CreateAccountCommand { Email = customerCreationResult.Email, customerId = customerCreationResult.Id }; // Act var accountCreationSUT = new CreateAccountCommandHandler(_accountLogger, _mapper, _accountRepository, _customerRepository); var accountCreationExceptionResult = await Assert.ThrowsAsync <BadRequestException>(async() => await accountCreationSUT.Handle(newAccountCommandRequest, CancellationToken.None)); var accountFromDb = _accountRepository.GetAccountByEmail(customerCreationResult.Email); // Assert Assert.Null(accountFromDb); }
public async Task Handle_GivenValidRequest_ShouldReturnCustomerId() { // Arrange var command = new CreateCustomerCommand { FirstName = "John", LastName = "Doe", Gender = "Male", PhoneNumber = "0885359164", Status = "Active", }; var sut = new CreateCustomerCommandHandler(this.deletableEntityRepository); // Act var id = await sut.Handle(command, It.IsAny <CancellationToken>()); // Assert var customer = await this.dbContext.Customers.SingleOrDefaultAsync(x => x.Id == id); customer.ShouldNotBeNull(); customer.FirstName.ShouldBe("John"); customer.LastName.ShouldBe("Doe"); customer.Gender.ShouldBe("Male"); customer.PhoneNumber.ShouldBe("0885359164"); customer.Status.ShouldBe("Active"); }
public CreateCustomerHandlerTests() { _mapper = new Mock <IMapper>(); _customer = _fixture.Create <Entity.Concrete.Customer>(); _repository = new Mock <IGenericRepository <Entity.Concrete.Customer> >(); _createCommand = _fixture.Create <CreateCustomerCommand>(); _sut = new CreateCustomerCommandHandler(_repository.Object, _mapper.Object); }
public async Task Handle_GivenNullRequest_ShouldThrowArgumentNullException() { // Arrange var sut = new CreateCustomerCommandHandler(this.deletableEntityRepository); // Act & Assert await Should.ThrowAsync <ArgumentNullException>(sut.Handle(null, It.IsAny <CancellationToken>())); }
public CreateCustomerCommandHandlerTests() { this.repositoryMock = new Mock <ICustomersRepository>(); this.identityServiceMock = new Mock <IIdentityService>(); this.handler = new CreateCustomerCommandHandler( this.repositoryMock.Object, this.identityServiceMock.Object); }
public async Task Handle_ValidCustomer_AddsCustomerToRepo() { var handler = new CreateCustomerCommandHandler(_customerRepository.Object, _unitOfWork.Object, _mapper); var result = await handler.Handle(new CreateCustomerCommand { FirstName = "Chima" }, CancellationToken.None); _customerRepository.Verify(x => x.Add(It.IsAny <Customer>()), Times.Once); result.Should().BeOfType <CustomerDto>(); }
public async Task SaveAsync() { var dataAccess = new CustomerDataAccess(this.Context); //Act var sutCreate = new CreateCustomerCommandHandler(dataAccess); var resultCreate = await sutCreate.Handle(new CreateCustomerCommand { Data = CustomerTestData.CustomerDataDTO }, CancellationToken.None); Assert.IsTrue(resultCreate.Succeeded); }
public async void CreateCustomerCommand_CreatesACustomer() { // Arrange var mockCustomer = _fixture.Build <CreateCustomerCommand>().Create(); // Act var createCustomerCommandHandler = new CreateCustomerCommandHandler(_petShopContext); var newCustomerId = await createCustomerCommandHandler.Handle(mockCustomer, CancellationToken.None); // Assert var numberOfCustomers = _petShopContext.Customers.Count(); Assert.Equal(1, numberOfCustomers); Assert.NotEqual(Guid.Empty, newCustomerId); }
public async Task It_creates_the_customer() { var dbContext = DatabaseTestHelper.GetDbContext(); var controller = new CreateCustomerCommandHandler(dbContext); var command = new CreateCustomerCommand { Name = "Piet" }; await controller.Handle(command); var customer = dbContext.Set <Customer>().Single(); customer.Name.Should().Be(command.Name); }
public async Task HandleAsync_Valid() { var applicationUserContextMock = new Mock <IApplicationUserContext>(); var customerFactoryMock = new Mock <ICustomerFactory>(); var customerRepositoryMock = new Mock <ICustomerRepository>(); var mapperMock = new Mock <IMapper>(); var command = new CreateCustomerCommand { FirstName = "Mary", LastName = "Smith", }; var id = Guid.Parse("926a4480-61f5-416a-a16f-5c722d8463f7"); var referenceNumber = new CustomerReferenceNumber(DateTime.Now, "ABC12"); var customer = new Customer(new CustomerIdentity(id), referenceNumber, "Mary", "Smith");; var expectedResponse = new CreateCustomerCommandResponse { Id = id, FirstName = "Mary", LastName = "Smith", }; customerFactoryMock .Setup(e => e.CreateCustomer("Mary", "Smith")) .Returns(customer); customerRepositoryMock .Setup(e => e.AddAsync(customer)); mapperMock .Setup(e => e.Map <Customer, CreateCustomerCommandResponse>(customer)) .Returns(expectedResponse); var handler = new CreateCustomerCommandHandler(applicationUserContextMock.Object, customerFactoryMock.Object, customerRepositoryMock.Object, mapperMock.Object); var response = await handler.HandleAsync(command); customerFactoryMock.Verify(e => e.CreateCustomer("Mary", "Smith"), Times.Once()); customerRepositoryMock.Verify(e => e.AddAsync(customer), Times.Once()); mapperMock.Verify(e => e.Map <Customer, CreateCustomerCommandResponse>(customer), Times.Once()); response.Should().BeEquivalentTo(expectedResponse); }
public async Task Should_Raise_A_CustomerCreated_Notification_Event_When_Customer_Created() { // Arrange var mediatorMock = new Mock <IMediator>(); var sut = new CreateCustomerCommandHandler(ShoppingDbContext, mediatorMock.Object); var message = new CreateCustomerCommand { Id = 1 }; // Act await sut.Handle(message, default); // Assert mediatorMock.Verify(m => m.Publish(It.Is <CustomerCreatedEvent>(cc => cc.CustomerId == message.Id), It.IsAny <CancellationToken>()), Times.Once); }
public async void ItShouldCreateCustomer() { var probe = CreateTestProbe(); var context = new Mock <ICustomerRepository>(); var provider = new Mock <ICustomerActorProvider>(); var actor = Sys.ActorOf(Props.Create <CustomersActor>(context), "customers"); var customerCommand = new CreateCustomerCommand { Id = 1, FirstName = "first", LastName = "last" }; provider.Setup(_ => _.Get()).Returns(actor); var sut = new CreateCustomerCommandHandler(provider.Object); var result = await sut.Handle(customerCommand, CancellationToken.None); result.ShouldBeOfType <MediatR.Unit>(); }
public async void Handle_GivenValidCustomer_ShouldCreateAccount() { // Arrange // Create Customer var customerCreation = new CreateCustomerCommandHandler(_customerLogger, _mapper, _customerRepository); var newCustomerCommandRequest = new CreateCustomerCommand { Email = "*****@*****.**", MonthlyIncome = 20000, MonthlyExpense = 200 }; var customerCreationResult = await customerCreation.Handle(newCustomerCommandRequest, CancellationToken.None); var newAccountCommandRequest = new CreateAccountCommand { Email = customerCreationResult.Email, customerId = customerCreationResult.Id }; // Act var accountCreationSUT = new CreateAccountCommandHandler(_accountLogger, _mapper, _accountRepository, _customerRepository); var accountCreationResult = await accountCreationSUT.Handle(newAccountCommandRequest, CancellationToken.None); var customerFromDb = _customerDbContext.Customers.Find(customerCreationResult.Id); var accountFromDb = _customerDbContext.Accounts.Find(accountCreationResult.AccountNo); // Assert Assert.NotNull(customerCreationResult); Assert.IsType <Guid>(customerCreationResult.Id); Assert.Equal(newCustomerCommandRequest.Email, customerCreationResult.Email); Assert.Equal(newCustomerCommandRequest.MonthlyExpense, customerCreationResult.MonthlyExpense); Assert.Equal(newCustomerCommandRequest.MonthlyIncome, customerCreationResult.MonthlyIncome); Assert.NotNull(customerFromDb); Assert.Equal(customerCreationResult.Id, customerFromDb.Id); Assert.Equal(newCustomerCommandRequest.Email, customerFromDb.Email); Assert.Equal(newCustomerCommandRequest.MonthlyExpense, customerFromDb.MonthlyExpense); Assert.Equal(newCustomerCommandRequest.MonthlyIncome, customerFromDb.MonthlyIncome); Assert.NotNull(accountCreationResult); Assert.Equal(customerCreationResult.Id, accountCreationResult.customerId); Assert.Equal(customerCreationResult.Email, accountCreationResult.Email); Assert.True(accountFromDb.Active); Assert.NotNull(accountFromDb); Assert.Equal(accountCreationResult.customerId, accountFromDb.CustomerId); Assert.Equal(accountCreationResult.Email, accountFromDb.Email); }
public void ShouldRegisterCustomerWhenCommandIsValid() { CreateCustomerCommand command = new CreateCustomerCommand(); command.FirstName = "Douglas"; command.LastName = "Pereira"; command.Document = "58303366343"; command.Email = "*****@*****.**"; command.Phone = "123123123"; Assert.True(command.Validate()); CreateCustomerCommandHandler handler = new CreateCustomerCommandHandler(new FakeCustomerRepository(), new FakeEmailService()); var result = handler.Handle(command); Assert.False(result == null); Assert.True(handler.Valid); }
public async Task GetAllAsync() { var dataAccess = new CustomerDataAccess(this.Context); //Act var sutCreate = new CreateCustomerCommandHandler(dataAccess); var resultCreate = await sutCreate.Handle(new CreateCustomerCommand { Data = CustomerTestData.CustomerDataDTO }, CancellationToken.None); //Act var sutGetAll = new GetCustomersQueryHandler(dataAccess); var resultGetAll = await sutGetAll.Handle(new GetCustomersQuery(), CancellationToken.None); Assert.IsTrue(resultGetAll?.Data.Count == 1); }
public async void Handle_GivenExistingCustomerEmail_ShouldThrowException() { // Arrange var sut = new CreateCustomerCommandHandler(_logger, _mapper, _customerRepository); // Create new customer var newCustomerCommandRequest = new CreateCustomerCommand { Email = "*****@*****.**", MonthlyIncome = 20000, MonthlyExpense = 200 }; var successResult = await sut.Handle(newCustomerCommandRequest, CancellationToken.None); // Act // try to create the same customer var exceptionResult = await Assert.ThrowsAsync <BadRequestException>(async() => await sut.Handle(newCustomerCommandRequest, CancellationToken.None)); Assert.Contains("exists", exceptionResult.Message); }
public async Task GetAsync() { var dataAccess = new CustomerDataAccess(this.Context); //Act var sutCreate = new CreateCustomerCommandHandler(dataAccess); var resultCreate = await sutCreate.Handle(new CreateCustomerCommand { Data = CustomerTestData.CustomerDataDTO }, CancellationToken.None); //Act var sutGet = new GetCustomerQueryHandler(dataAccess); var resultGet = await sutGet.Handle(new GetCustomerQuery { Id = resultCreate.Data.Id }, CancellationToken.None); Assert.IsTrue(resultGet?.Data != null); }
public async Task Should_Create_A_Customer() { // Arrange var mediatorMock = new Mock <IMediator>(); var sut = new CreateCustomerCommandHandler(ShoppingDbContext, mediatorMock.Object); var message = new CreateCustomerCommand { Id = 1, City = "Tehran", Country = "Iran", Phone = "232323" }; // Act await sut.Handle(message, default); //Assert var customer = ShoppingDbContext.Customers.FirstOrDefault(q => q.Id == message.Id); customer.ShouldNotBeNull(); customer.Id.ShouldBe(message.Id); customer.City.ShouldBe(message.City); customer.Country.ShouldBe(message.Country); customer.Phone.ShouldBe(message.Phone); }
public async Task DeleteAsync() { var dataAccess = new CustomerDataAccess(this.Context, Mapper()); //Act var sutCreate = new CreateCustomerCommandHandler(dataAccess); var resultCreate = await sutCreate.Handle(new CreateCustomerCommand { Data = CustomerTestData.CustomerDTO }, CancellationToken.None); //Act var sutDelete = new DeleteCustomerCommandHandler(dataAccess); var outcomeDelete = await sutDelete.Handle(new DeleteCustomerCommand { Id = resultCreate.Data.Id }, CancellationToken.None); //Assert Assert.IsTrue(outcomeDelete.Succeeded); }
public async Task CreateCustomerTaskOk() { // Arrange CreateCustomerCommandHandler commandHandler = new CreateCustomerCommandHandler(_unitOfWork, _bus, _notifications); CreateCustomerCommand command = new CreateCustomerCommand() { FirstName = "Franky", LastName = "Quintero", Telephone = "573004436932", Company = "UPC", Email = "*****@*****.**", AddressCity = "La Jagua de Ibirico", AddressZipCode = "203020", AddressLine1 = "B.Santander", AddressLine2 = "", CountryId = new Guid("32BB805F-40A4-4C37-AA96-B7945C8C385C") }; _validator.Validate(command).IsValid.ShouldBeTrue(); // Act var customerId = await commandHandler.Handle(command, CancellationToken.None); var customer = _unitOfWork.Repository.CustomerRepository.SingleOrDefault(e => e.Id == customerId); // Assert customer.ShouldNotBeNull(); customer.Id.ShouldBe(customerId); customer.FirstName.ShouldBe("Franky"); customer.LastName.ShouldBe("Quintero"); customer.Telephone.ShouldBe("573004436932"); customer.Company.ShouldBe("UPC"); customer.Email.ShouldBe("*****@*****.**"); customer.Address.City.ShouldBe("La Jagua de Ibirico"); customer.Address.ZipCode.ShouldBe("203020"); customer.Address.AddressLine1.ShouldBe("B.Santander"); customer.Address.AddressLine2.ShouldBe(""); customer.CountryId.ShouldBe(new Guid("32BB805F-40A4-4C37-AA96-B7945C8C385C")); }
public async void ListCustomersQuery_ReturnsAllCustomers() { const int numberOfCustomersToCreate = 5; // Arrange var createCustomers = _fixture.Build <CreateCustomerCommand>() .CreateMany(numberOfCustomersToCreate); var createCustomerCommandHandler = new CreateCustomerCommandHandler(_petShopContext); foreach (var customer in createCustomers) { await createCustomerCommandHandler.Handle(customer, CancellationToken.None); } // Act var mockHandler = new ListCustomersQueryHandler(_petShopContext); var mockHandlerResult = await mockHandler.Handle(new ListCustomersQuery(), CancellationToken.None); // Assert Assert.Equal(numberOfCustomersToCreate, mockHandlerResult.Count()); }
public void CreateCustomerTaskFailExceptionNotFoundCountry() { // Arrange CreateCustomerCommandHandler commandHandler = new CreateCustomerCommandHandler(_unitOfWork, _bus, _notifications); CreateCustomerCommand command = new CreateCustomerCommand() { FirstName = "Franky", LastName = "Quintero", Telephone = "573004436932", Company = "UPC", Email = "*****@*****.**", AddressCity = "La Jagua de Ibirico", AddressZipCode = "203020", AddressLine1 = "B.Santander", AddressLine2 = "", CountryId = new Guid("22BB805F-40A4-4C37-AA96-B7945C8C385C") }; _validator.Validate(command).IsValid.ShouldBeTrue(); // Act Should.Throw <NotFoundException>(async() => { var customerId = await commandHandler.Handle(command, CancellationToken.None); }); }
public CustomerController(ICustomerRepository repository, CreateCustomerCommandHandler handler) { this._repository = repository; this._handler = handler; }
public CreateCustomerCommandHandlerTests() { _customerRepository = A.Fake <ICustomerRepository>(); _testee = new CreateCustomerCommandHandler(_customerRepository); }
public CreateCustomerCommandHandlerTests() { _repository = A.Fake <IRepository <Customer> >(); _testee = new CreateCustomerCommandHandler(_repository); }