/// <summary> /// Tries to block provided amount plus withdrawal fee. Operator cards cannot be used for this operation /// </summary> /// <param name="cardNumber">Card number</param> /// <param name="amount">Amount to block</param> /// <returns>Operation Id</returns> public Guid BlockAmount(string cardNumber, int amount) { if (IsOperatorCard(cardNumber)) { throw new NotAuthorizedOperatorException(); } var balanceInfo = GetCardBalanceInfo(cardNumber); var fee = CalculateWithdrawalFee(amount); var totalAmount = amount + fee; if (balanceInfo.EffectiveBalance < totalAmount) { throw new InsufficientFundsException(); } balanceInfo.BlockFunds(totalAmount); return(_historyManager.AddAtOperation(cardNumber, amount, fee)); }
public void TestBlockAmount() { // Arrange const string cardNumber = "5372100906627713"; const int amount = 33; const int amount2 = 100; const int amount3 = 10000; const decimal fee = 0.33M; const decimal fee2 = 1M; const decimal fee3 = 100M; var operationId = Guid.NewGuid(); var operationId2 = Guid.NewGuid(); var operationId3 = Guid.NewGuid(); _historyManager.AddAtOperation(cardNumber, amount, fee).Returns(operationId); _historyManager.AddAtOperation(cardNumber, amount2, fee2).Returns(operationId2); _historyManager.AddAtOperation(cardNumber, amount3, fee3).Returns(operationId3); var initBalance = _hostProcessorServiceMock.GetCardBalance(cardNumber); var expectedBalance = initBalance - amount - fee - amount2 - fee2 - amount3 - fee3; // Act var result = _hostProcessorServiceMock.BlockAmount(cardNumber, amount); var result2 = _hostProcessorServiceMock.BlockAmount(cardNumber, amount2); var result3 = _hostProcessorServiceMock.BlockAmount(cardNumber, amount3); // Assert Assert.Equal(operationId, result); Assert.Equal(operationId2, result2); Assert.Equal(operationId3, result3); Received.InOrder(() => { _historyManager.AddAtOperation(Arg.Is(cardNumber), Arg.Is((decimal)amount), Arg.Is(fee)); _historyManager.AddAtOperation(Arg.Is(cardNumber), Arg.Is((decimal)amount2), Arg.Is(fee2)); _historyManager.AddAtOperation(Arg.Is(cardNumber), Arg.Is((decimal)amount3), Arg.Is(fee3)); }); var resultBalance = _hostProcessorServiceMock.GetCardBalance(cardNumber); Assert.Equal(expectedBalance, resultBalance); }