Ejemplo n.º 1
0
      public void AddBankAccountReturnDtoWhenSaveSucceed()
      {
         //Arrange
         IBankTransferService transferService = new BankTransferService();

         var customerRepository = new StubICustomerRepository();
         customerRepository.GetGuid = (guid) =>
         {
            var customer = new Customer()
            {
               FirstName = "Jhon",
               LastName = "El rojo"
            };

            customer.ChangeCurrentIdentity(guid);

            return customer;
         };

         var bankAccountRepository = new StubIBankAccountRepository();
         bankAccountRepository.AddBankAccount = (ba) => { };
         bankAccountRepository.UnitOfWorkGet = () =>
         {
            var uow = new StubIUnitOfWork();
            uow.Commit = () => { };

            return uow;
         };

         var dto = new BankAccountDto()
         {
            CustomerId = Guid.NewGuid(),
            BankAccountNumber = "BA"
         };

         IBankAppService bankingService = new BankAppService(bankAccountRepository, customerRepository, transferService);

         //Act
         var result = bankingService.AddBankAccount(dto);

         //Assert
         Assert.IsNotNull(result);

      }
Ejemplo n.º 2
0
      public void PerformBankTransfer()
      {
         //Arrange

         //--> source bank account data
         var sourceId = new Guid("3481009C-A037-49DB-AE05-44FF6DB67DEC");
         var bankAccountNumberSource = new BankAccountNumber("4444", "5555", "3333333333", "02");
         var sourceCustomer = new Customer();
         sourceCustomer.GenerateNewIdentity();

         var source = BankAccountFactory.CreateBankAccount(sourceCustomer, bankAccountNumberSource);
         source.ChangeCurrentIdentity(sourceId);
         source.DepositMoney(1000, "initial");

         var sourceBankAccountDto = new BankAccountDto()
         {
            Id = sourceId,
            BankAccountNumber = source.Iban
         };

         //--> target bank account data
         var targetCustomer = new Customer();
         targetCustomer.GenerateNewIdentity();
         var targetId = new Guid("8A091975-F783-4730-9E03-831E9A9435C1");
         var bankAccountNumberTarget = new BankAccountNumber("1111", "2222", "3333333333", "01");
         var target = BankAccountFactory.CreateBankAccount(targetCustomer, bankAccountNumberTarget);
         target.ChangeCurrentIdentity(targetId);

         var targetBankAccountDto = new BankAccountDto()
         {
            Id = targetId,
            BankAccountNumber = target.Iban
         };

         var accounts = new List<BankAccount>()
         {
            source,
            target
         };

         var bankAccountRepository = new StubIBankAccountRepository();
         bankAccountRepository.GetGuid = (guid) => { return accounts.Where(ba => ba.Id == guid).SingleOrDefault(); };
         bankAccountRepository.UnitOfWorkGet = () =>
         {
            var unitOfWork = new StubIUnitOfWork();
            unitOfWork.Commit = () => { };

            return unitOfWork;
         };

         var customerRepository = new StubICustomerRepository();
         IBankTransferService transferService = new BankTransferService();

         IBankAppService bankingService = new BankAppService(bankAccountRepository, customerRepository, transferService);

         //Act
         bankingService.PerformBankTransfer(sourceBankAccountDto, targetBankAccountDto, 100M);

         //Assert
         Assert.AreEqual(source.Balance, 900);
         Assert.AreEqual(target.Balance, 100);
      }
Ejemplo n.º 3
0
      public void AddBankAccountReturnNullWhenCustomerIdIsEmpty()
      {
         //Arrange
         var bankAccountRepository = new StubIBankAccountRepository();
         var customerRepository = new StubICustomerRepository();
         IBankTransferService transferService = new BankTransferService();

         var dto = new BankAccountDto()
         {
            CustomerId = Guid.Empty
         };

         IBankAppService bankingService = new BankAppService(bankAccountRepository, customerRepository, transferService);

         //Act
         var result = bankingService.AddBankAccount(dto);
      }
Ejemplo n.º 4
0
      public void AddBankAccountThrowInvalidOperationExceptionWhenCustomerNotExist()
      {
         //Arrange
         var bankAccountRepository = new StubIBankAccountRepository();
         var customerRepository = new StubICustomerRepository();
         customerRepository.GetGuid = (guid) => { return null; };

         IBankTransferService transferService = new BankTransferService();

         var dto = new BankAccountDto()
         {
            CustomerId = Guid.NewGuid()
         };

         IBankAppService bankingService = new BankAppService(bankAccountRepository, customerRepository, transferService);

         //Act
         bankingService.AddBankAccount(dto);
      }
 /// <summary>
 ///    <see
 ///       cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IMainBoundedContextService" />
 /// </summary>
 /// <param name="newBankAccount">
 ///    <see
 ///       cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IMainBoundedContextService" />
 /// </param>
 /// <returns>
 ///    <see
 ///       cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IMainBoundedContextService" />
 /// </returns>
 public BankAccountDto AddNewBankAccount(BankAccountDto newBankAccount)
 {
    return _bankAppService.AddBankAccount(newBankAccount);
 }
 /// <summary>
 ///    <see
 ///       cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IMainBoundedContextService" />
 /// </summary>
 /// <param name="from">
 ///    <see
 ///       cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IMainBoundedContextService" />
 /// </param>
 /// <param name="to">
 ///    <see
 ///       cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IMainBoundedContextService" />
 /// </param>
 /// <param name="amount">
 ///    <see
 ///       cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IMainBoundedContextService" />
 /// </param>
 public void PerformTransfer(BankAccountDto from, BankAccountDto to, decimal amount)
 {
    _bankAppService.PerformBankTransfer(from, to, amount);
 }
Ejemplo n.º 7
0
      /// <summary>
      ///    <see cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IBankAppService" />
      /// </summary>
      /// <param name="bankAccountDto">
      ///    <see cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IBankAppService" />
      /// </param>
      /// <returns>
      ///    <see cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IBankAppService" />
      /// </returns>
      public BankAccountDto AddBankAccount(BankAccountDto bankAccountDto)
      {
         if (bankAccountDto == null || bankAccountDto.CustomerId == Guid.Empty) {
            throw new ArgumentException(Messages.warning_CannotAddNullBankAccountOrInvalidCustomer);
         }

         //check if exists the customer for this bank account
         var associatedCustomer = _customerRepository.Get(bankAccountDto.CustomerId);

         if (associatedCustomer != null) // if the customer exist
         {
            //Create a new bank account  number
            var accountNumber = CalculateNewBankAccountNumber();

            //Create account from factory 
            var account = BankAccountFactory.CreateBankAccount(associatedCustomer, accountNumber);

            //save bank account
            SaveBankAccount(account);

            return account.ProjectedAs<BankAccountDto>();
         }
         else //the customer for this bank account not exist, cannot create a new bank account
         {
            throw new InvalidOperationException(Messages.warning_CannotCreateBankAccountForNonExistingCustomer);
         }

      }
Ejemplo n.º 8
0
 private bool BankAccountHasIdentity(BankAccountDto bankAccountDto)
 {
    //return true is bank account dto has identity
    return (bankAccountDto != null && bankAccountDto.Id != Guid.Empty);
 }
Ejemplo n.º 9
0
      /// <summary>
      ///    <see cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IBankAppService" />
      /// </summary>
      /// <param name="fromAccount">
      ///    <see cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IBankAppService" />
      /// </param>
      /// <param name="toAccount">
      ///    <see cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IBankAppService" />
      /// </param>
      /// <param name="amount">
      ///    <see cref="Microsoft.Samples.NLayerApp.Application.MainBoundedContext.BankingModule.Services.IBankAppService" />
      /// </param>
      public void PerformBankTransfer(BankAccountDto fromAccount, BankAccountDto toAccount, decimal amount)
      {
         //Application-Logic Process: 
         // 1º Get Accounts objects from Repositories
         // 2º Start Transaction
         // 3º Call PerformTransfer method in Domain Service
         // 4º If no exceptions, commit the unit of work and complete transaction

         if (BankAccountHasIdentity(fromAccount) && BankAccountHasIdentity(toAccount))
         {
            var source = _bankAccountRepository.Get(fromAccount.Id);
            var target = _bankAccountRepository.Get(toAccount.Id);

            if (source != null & target != null) // if all accounts exist
            {
               using (var scope = new TransactionScope())
               {
                  //perform transfer
                  _transferService.PerformTransfer(amount, source, target);

                  //comit unit of work
                  _bankAccountRepository.UnitOfWork.Commit();

                  //complete transaction
                  scope.Complete();
               }
            }
            else
            {
               LoggerFactory.CreateLog().LogError(Messages.error_CannotPerformTransferInvalidAccounts);
            }
         }
         else
         {
            LoggerFactory.CreateLog().LogError(Messages.error_CannotPerformTransferInvalidAccounts);
         }

      }