Package org.openbankdata.bank.americanexpress.service

Source Code of org.openbankdata.bank.americanexpress.service.AmericanExpressAccountServiceTest

package org.openbankdata.bank.americanexpress.service;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.openbankdata.bank.americanexpress.client.AmericanExpressBankClient;
import org.openbankdata.core.Account;
import org.openbankdata.core.AccountType;
import org.openbankdata.core.client.BankRequest;
import org.openbankdata.core.client.BankResponse;
import org.openbankdata.core.client.MockedBankResponse;

import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;

import java.math.BigDecimal;
import java.util.Currency;
import java.util.List;

public class AmericanExpressAccountServiceTest {

  private AmericanExpressAccountService mAccountService;

  private AmericanExpressBankClient mockedBankClient;

  private static final String ACCOUNT_URI =
      "https://global.americanexpress.com/myca/intl/acctsumm/emea/accountSummary.do?request_type=&Face=sv_SE";

  @Before
  public void setUp() {
    mockedBankClient = EasyMock.createMock(AmericanExpressBankClient.class);
    mAccountService = new AmericanExpressAccountService(mockedBankClient);
  }

  @Test
  public void testGetAccounts() {
    // Given
    // One account exists at the website
    EasyMock.expect(mockedBankClient.get(new BankRequest(ACCOUNT_URI))).andReturn(
        mockedAccountResponse());
    EasyMock.replay(mockedBankClient);

    // When
    // Getting the list of accounts
    List<Account> actual = mAccountService.getAccounts();

    // Then
    assertEquals("Only one account should be returned", 1, actual.size());
    assertEquals("The returned account should have correct data.", expectedAccount(), actual.get(0));

  }

  @Test
  public void testGetAccountsWithZeroAccountsAvailable() {
    // Given
    // No accounts exists
    EasyMock.expect(mockedBankClient.get(new BankRequest(ACCOUNT_URI))).andReturn(
        emptyAccountsResponse());
    EasyMock.replay(mockedBankClient);
    // When
    // Getting the list of accounts
    List<Account> actual = mAccountService.getAccounts();

    // Then
    assertTrue("The returned list should be empty", actual.isEmpty());
  }

  private Account expectedAccount() {
    Account account = new Account();
    account.setAccountNumber("XXX-12345");
    account.setCurrency(Currency.getInstance("SEK"));
    account.setName("American Express Corporate Card");
    account.setId("0");
    account.setBalance(BigDecimal.valueOf(1234.56));
    account.setAvailable(BigDecimal.valueOf(1234.56));
    account.setAccountType(AccountType.LIABILITY);
    return account;
  }

  private BankResponse mockedAccountResponse() {
    MockedBankResponse response = new MockedBankResponse("americanexpress-login-successful.htm");
    response.code(200);
    return response;
  }

  private BankResponse emptyAccountsResponse() {
    MockedBankResponse response = new MockedBankResponse();
    response.code(200);
    response.body("");
    return response;
  }
}
TOP

Related Classes of org.openbankdata.bank.americanexpress.service.AmericanExpressAccountServiceTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.