Beispiel #1
0
        public async Task GivenCustomerIsNotDeleted_WhenUpdateCustomerAsync_ThenCustomerUpdated()
        {
            // Arrange
            var customer = new Customer()
            {
                ID             = Guid.NewGuid(),
                CurrentBalance = 200,
                Email          = "*****@*****.**",
                Surname        = "Test",
                GivenNames     = "Person"
            };

            using (var ctx = _dbContextCreator.CreateDbContext())
            {
                ctx.Add(customer);
                await ctx.SaveChangesAsync();
            }

            var updatedCustomer = customer.Adapt <Customer>();

            updatedCustomer.Surname = "Updated";

            // Act
            var result = await _repo.UpdateCustomerAsync(updatedCustomer);

            //Assert
            result.Should().BeEquivalentTo(updatedCustomer);
            using (var ctx = _dbContextCreator.CreateDbContext())
            {
                var customerInDb = await ctx.Customer.FirstOrDefaultAsync();

                customerInDb.Should().BeEquivalentTo(updatedCustomer);
            }
        }
Beispiel #2
0
        public async Task GivenCustomerHasEnoughBalance_WhenCreatePayment_ThenPaymentCreatedAndBalanceAdjusted()
        {
            // Arrange
            var customer = new Customer()
            {
                CurrentBalance = _request.Payment.Amount
            };

            _customerRepo.Setup(c => c.GetCustomerAsync(It.IsAny <Guid>())).ReturnsAsync(customer);

            var expectedPayment = _request.Payment.Adapt <Payment>();

            expectedPayment.RequestedDateUtc = _utcNow;
            expectedPayment.PaymentStatus    = PaymentStatus.Pending;

            var expectedCustomer = customer.Adapt <Customer>();

            expectedCustomer.CurrentBalance = customer.CurrentBalance - expectedPayment.Amount;

            // Act
            var result = await _handler.Handle(_request, new CancellationToken());

            // Assert
            result.Should().BeEquivalentTo(expectedPayment);
            _customerRepo.Verify(c => c.GetCustomerAsync(It.Is <Guid>(id => id == _request.Payment.CustomerID)));
            _customerRepo.Verify(c => c.UpdateCustomerAsync(It.Is <Customer>(cust => cust.IsEquivalentTo(expectedCustomer))));
            _paymentRepo.Verify(p => p.CreatePaymentAsync(It.Is <Payment>(payment => payment.IsEquivalentTo(expectedPayment))));
        }
Beispiel #3
0
        public async Task GivenCustomerIsDeleted_WhenUpdateCustomerAsync_ThenThrowException()
        {
            // Arrange
            var customer = new Customer()
            {
                ID             = Guid.NewGuid(),
                CurrentBalance = 200,
                Email          = "*****@*****.**",
                Surname        = "Test",
                GivenNames     = "Person",
                IsDeleted      = true
            };

            using (var ctx = _dbContextCreator.CreateDbContext())
            {
                ctx.Add(customer);
                await ctx.SaveChangesAsync();
            }

            var updatedCustomer = customer.Adapt <Customer>();

            updatedCustomer.Surname = "Updated";

            // Act + Assert
            await Assert.ThrowsAsync <UnableToUpdateDeletedRecordsException>(() => _repo.UpdateCustomerAsync(updatedCustomer));
        }
Beispiel #4
0
        public async Task GivenPaymentIsClosed_WhenProcessPayment_ThenPaymentIsClosedAndCustomerBalanceReadjusted()
        {
            // Arrange
            _request.Payment = _request.Payment.Adapt <Payment>();           // create a copy so it won't affect mock setup for payment repo
            _request.Payment.PaymentStatus = PaymentStatus.Closed;

            var customer = new Customer()
            {
                CurrentBalance = 0
            };

            _customerRepo.Setup(c => c.GetCustomerAsync(It.IsAny <Guid>())).ReturnsAsync(customer);

            var expectedPayment = _request.Payment.Adapt <Payment>();

            expectedPayment.ProcessedDateUtc = _utcNow;

            var expectedCustomer = customer.Adapt <Customer>();

            expectedCustomer.CurrentBalance = customer.CurrentBalance + expectedPayment.Amount;

            // Act
            var result = await _handler.Handle(_request, new CancellationToken());

            // Assert
            result.Should().BeEquivalentTo(expectedPayment);
            _paymentRepo.Verify(p => p.UpdatePaymentAsync(It.Is <Payment>(payment => payment.IsEquivalentTo(expectedPayment))));
            _customerRepo.Verify(c => c.UpdateCustomerAsync(It.Is <Customer>(cust => cust.IsEquivalentTo(expectedCustomer))));
        }
        protected override CustomerDto SetupDeepMapper(Customer customer)
        {
            TypeAdapterConfig <Customer, CustomerDto> .NewConfig()
            .Map(dest => dest.Addresses, src => src.Addresses ?? new List <Address>())
            .Map(dest => dest.AddressesArray, src => src.AddressesArray ?? new Address[0])
            .Compile();

            return(customer.Adapt <Customer, CustomerDto>());
        }
Beispiel #6
0
        public void Mapster()
        {
            Customer _nc = GetNewCustomer();

            for (int i = 0; i < ItCount; i++)
            {
                CustomerViewItem customerViewItem = _nc.Adapt <CustomerViewItem>();
            }
        }
Beispiel #7
0
        public async Task <ActionResult <CustomerDto> > AddCustomer([FromBody] Customer customer)
        {
            await _repository.AddCustomerAsync(customer);

            await _repository.CommitAsync();

            return(CreatedAtAction(nameof(GetCustomerDetail),
                                   new { id = customer.PersonId },
                                   customer.Adapt <CustomerDto> ()));
        }
        public async Task <bool> UpdateAsync(Customer item, CancellationToken cancellationToken = default)
        {
            using (var context = new BookLibraryContext(this._optionsBuilder.Options))
            {
                var customer = await context.Customers.SingleOrDefaultAsync(o => o.Id == item.Id, cancellationToken);

                if (customer == null)
                {
                    return(false);
                }

                item.Adapt(customer);

                await context.SaveChangesAsync(cancellationToken);

                return(true);
            }
        }
        public void Basic_Poco_Is_Mapped_With_New_Config()
        {
            var config = new TypeAdapterConfig();

            config.NewConfig <Customer, CustomerDTO>()
            .Map(dest => dest.Address_Country, src => "TestAddressCountry");

            var customer = new Customer
            {
                Id      = 12345,
                Name    = "TestName",
                Surname = "TestSurname"
            };

            var customerDto = customer.Adapt <CustomerDTO>(config);

            customerDto.Id.ShouldEqual(12345);
            customerDto.Name.ShouldEqual("TestName");
            customerDto.Address_Country.ShouldEqual("TestAddressCountry");
        }
Beispiel #10
0
        public async Task GivenCustomerExists_WhenTopUpCustomerBalancet_ThenBalanceAdjusted()
        {
            // Arrange
            var customer = new Customer()
            {
                CurrentBalance = 0
            };

            _customerRepo.Setup(c => c.GetCustomerAsync(It.IsAny <Guid>())).ReturnsAsync(customer);

            var expectedCustomer = customer.Adapt <Customer>();

            expectedCustomer.CurrentBalance = customer.CurrentBalance + _request.TopUpCustomerBalanceDto.TopUpAmount;

            // Act
            await _handler.Handle(_request, new CancellationToken());

            // Assert
            _customerRepo.Verify(c => c.GetCustomerAsync(It.Is <Guid>(id => id == _request.TopUpCustomerBalanceDto.CustomerID)));
            _customerRepo.Verify(c => c.UpdateCustomerAsync(It.Is <Customer>(cust => cust.IsEquivalentTo(expectedCustomer))));
        }
Beispiel #11
0
        public ActionResult GetCustomerByUserId(string Id)
        {
            var AccountId = _accountService.GetAccounts(a => a.User_Id.Equals(Id)).ToList();

            if (!AccountId.Any())
            {
                CreateAccount(Id);
                //get created account
                var      accountCreated = _accountService.GetAccounts(t => t.User_Id.Equals(Id)).ToList();
                Customer customer       = CreateCustomer(accountCreated);
                return(Ok(customer.Adapt <CustomerVM>()));
            }
            var result = _customerService.GetCustomers(c => c.AccountId == AccountId[0].Id).ToList();

            if (!result.Any())
            {
                Customer customer = CreateCustomer(AccountId);
                return(Ok(customer.Adapt <CustomerVM>()));
            }
            return(Ok(result[0].Adapt <CustomerVM>()));
        }
        public void NewConfig_Overwrites_Config()
        {
            var config = new TypeAdapterConfig();

            config.NewConfig <Customer, CustomerDTO>()
            .Map(dest => dest.Name, src => src.Name + "_Enhanced");

            config.NewConfig <Customer, CustomerDTO>()
            .Map(dest => dest.Address_Country, src => "TestAddressCountry");

            var customer = new Customer
            {
                Id      = 12345,
                Name    = "TestName",
                Surname = "TestSurname"
            };

            var customerDto = customer.Adapt <CustomerDTO>(config);

            customerDto.Id.ShouldEqual(12345);
            customerDto.Name.ShouldEqual("TestName");
            customerDto.Address_Country.ShouldEqual("TestAddressCountry");
        }
 public void SaveCustomer(Customer customer) => OrderManager.SaveCustomer(customer.Adapt <CustomerDTO>());
Beispiel #14
0
 public static void ConfigureMapster(Customer customerInstance)
 {
     customerInstance.Adapt <Customer, CustomerDTO>();    //exercise
 }
Beispiel #15
0
 protected override CustomerDto Map(Customer customer)
 {
     return(customer.Adapt <Customer, CustomerDto>());
 }
 protected override CustomerDto Map(Customer customer)
 => customer.Adapt <Customer, CustomerDto>();