Package org.openbankdata.bank.americanexpress.client

Source Code of org.openbankdata.bank.americanexpress.client.AmericanExpressBankClientTest

package org.openbankdata.bank.americanexpress.client;

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

import org.openbankdata.core.client.BankRequest;
import org.openbankdata.core.client.BankResponse;
import org.openbankdata.core.client.Cache;
import org.openbankdata.core.client.MockedBankResponse;

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

public class AmericanExpressBankClientTest {

  private Cache mockedCache = new Cache();

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

  private static final String USERNAME = "username";
  private static final String PASSWORD = "password";

  private AmericanExpressBankClient mBankClient;

  @Before
  public void setUp() {
    mBankClient = EasyMock.createMockBuilder(AmericanExpressBankClient.class)
        .addMockedMethod("post")
        .addMockedMethod("get")
        .addMockedMethod("getCache").createMock();
  }

  @After
  public void tearDown() {
    EasyMock.reset(mBankClient);
  }

  @Test
  public void testActivateSessionWithValidCredentials() {
    // Setup mocks
    EasyMock.expect(mBankClient.get(expectedInitialRequest())).andReturn(
        getValidInitialResponseMock());
    EasyMock.expect(mBankClient.post(expectedLoginRequest()))
        .andReturn(getValidLoginResponseMock());
    EasyMock.expect(mBankClient.getCache()).andReturn(mockedCache);
    EasyMock.replay(mBankClient);

    // Given
    // A correct username and password.
    mBankClient.setCredentials(USERNAME, PASSWORD);

    // When
    // Activating the session
    boolean actual = mBankClient.activateSession();

    // Then
    assertTrue("The session should have been activated.", actual);
    assertTrue("The response should have been added to the cache for the list accounts URI.",
        mockedCache.exists(new BankRequest(ACCOUNT_URI)));
  }

  @Test
  public void testActivateSessionWithNonRespondingWebsite() {
    // Given
    // AmericanExpress' website is not responding
    MockedBankResponse mockedBankResponse = new MockedBankResponse();
    mockedBankResponse.code(401);
    EasyMock.expect(mBankClient.get(expectedInitialRequest())).andReturn(mockedBankResponse);
    EasyMock.replay(mBankClient);

    // When
    // Activating the session
    boolean actual = mBankClient.activateSession();

    // Then
    assertFalse("The session should NOT have been activated", actual);
  }

  @Test
  public void testActivateSessionWithInvalidCredentials() {
    // Setup mocks
    EasyMock.expect(mBankClient.get(expectedInitialRequest())).andReturn(
        getValidInitialResponseMock());
    EasyMock.expect(mBankClient.post(expectedLoginRequest()))
        .andReturn(getInvalidLoginResponseMock());
    EasyMock.replay(mBankClient);

    // Given
    // Invalid credentials
    mBankClient.setCredentials(USERNAME, PASSWORD);
    // When
    // Activating the session
    boolean actual = mBankClient.activateSession();

    // Then
    assertFalse("The session should NOT have been activated", actual);
  }

  private BankRequest expectedInitialRequest() {
    return new BankRequest("https://www.americanexpress.com/home/se/home_c.shtml")
        .skipAuthentication(true);
  }

  private BankResponse getValidInitialResponseMock() {
    MockedBankResponse response = new MockedBankResponse();
    response.code(200);
    return response;
  }

  private BankRequest expectedLoginRequest() {
    return new BankRequest("https://global.americanexpress.com/myca/logon/emea/action")
        .skipAuthentication(true)
        .addParam("request_type", "LogLogonHandler")
        .addParam("Face", "sv_SE")
        .addParam(
            "DestPage",
            "https://global.americanexpress.com/myca/intl/acctsumm/emea/accountSummary.do" +
                "?request_type=&Face=sv_SE")
        .addParam("Logon", "Continue...")
        .addParam("UserID", USERNAME)
        .addParam("Password", PASSWORD);
  }

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

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

}
TOP

Related Classes of org.openbankdata.bank.americanexpress.client.AmericanExpressBankClientTest

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.