Package org.openbankdata.bank.sveadirekt.client

Source Code of org.openbankdata.bank.sveadirekt.client.SveaDirektBankClientTest

package org.openbankdata.bank.sveadirekt.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.MockedBankResponse;
import org.openbankdata.test.util.TestUtils;

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

public class SveaDirektBankClientTest {

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

  private SveaDirektBankClient mBankClient;

  @Before
  public void setUp() {
    mBankClient = EasyMock.createMockBuilder(SveaDirektBankClient.class)
        .addMockedMethod("post")
        .addMockedMethod("get").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.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);
  }

  @Test
  public void testActivateSessionWithNonRespondingWebsite() {
    // Given
    // SveaDirekt's website is not responding
    MockedBankResponse mockedResponse = new MockedBankResponse();
    mockedResponse.code(404);
    EasyMock.expect(mBankClient.get(expectedInitialRequest())).andReturn(mockedResponse);
    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 BankResponse getValidInitialResponseMock() {
    MockedBankResponse mock = new MockedBankResponse();
    mock.code(200);
    return mock;
  }

  private BankResponse getValidLoginResponseMock() {
    MockedBankResponse mock = new MockedBankResponse();
    mock.body(TestUtils.getFileContentAsString("sveadirekt-login-successful.htm"));
    mock.code(200);
    return mock;
  }

  private BankResponse getInvalidLoginResponseMock() {
    MockedBankResponse mock = new MockedBankResponse();
    mock.body(TestUtils.getFileContentAsString("sveadirekt-login-failure.htm"));
    mock.code(200);
    return mock;
  }

  private BankRequest expectedInitialRequest() {
    return new BankRequest("https://services.sveadirekt.se/mypages/sv/").skipAuthentication(true);
  }

  private BankRequest expectedLoginRequest() {
    return new BankRequest("https://services.sveadirekt.se/mypages/sv/j_security_check")
        .addParam("j_username", USERNAME)
        .addParam("j_password", PASSWORD)
        .skipAuthentication(true);
  }
}
TOP

Related Classes of org.openbankdata.bank.sveadirekt.client.SveaDirektBankClientTest

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.