Package at.fhj.itm.beans

Source Code of at.fhj.itm.beans.LoginTest

package at.fhj.itm.beans;

import static org.easymock.EasyMock.expectLastCall;
import static org.junit.Assert.assertEquals;

import java.io.IOException;

import junit.framework.Assert;

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

import at.fhj.itm.business.ServiceAssembler;
import at.fhj.itm.business.ServiceException;
import at.fhj.itm.business.ServiceTrip;
import at.fhj.itm.business.ServiceUser;
import at.fhj.itm.model.User;
import at.fhj.itm.util.EmailUtil;
import at.fhj.itm.util.GoogleUtil;
import at.fhj.itm.util.JsfUtil;
import at.fhj.itm.util.RandomUtil;

/**
*
* This JUnit Class tests the methods of bean Login.
*
* @author Christian Taßler, Schuster Bernhard
*
*/
public class LoginTest {

  private ServiceUser mockServiceUser;
  private User mockUser;
  private JsfUtil mockJsfUtil;

  private final String pwd = "pwd";
  private final String email = "email";

  /**
   * The Bean asks the Assembler for a ServiceUser and will get a Mock
   */
  @Before
  public void setup() {
    this.mockUser = new User("", "", "", this.pwd, this.email, "", null,
        null, "");
    mockServiceUser = EasyMock.createMock(ServiceUser.class);
    mockJsfUtil = EasyMock.createMock(JsfUtil.class);

    ServiceAssembler serviceAssembler = new ServiceAssembler() {

      @Override
      public ServiceUser createServiceUser() {
        return mockServiceUser;
      }

      @Override
      public ServiceTrip createServiceTrip() {
        throw new UnsupportedOperationException("createServiceTrip()");
      }

      @Override
      public JsfUtil createJsfUtil() {
        return mockJsfUtil;
      }

      @Override
      public GoogleUtil createGoogleUtil() {
        throw new UnsupportedOperationException();
      }

      @Override
      public RandomUtil createRandomUtil() {
        throw new UnsupportedOperationException("createRandomUtil()");
      }

      @Override
      public EmailUtil createEmailUtil() {
        throw new UnsupportedOperationException("createEmailUtil()");
      }
    };
    ServiceAssembler.setServiceAssembler(serviceAssembler);
  }

  /**
   * Give back resources
   */
  @After
  public void tearDown() {
    mockServiceUser = null;
    ServiceAssembler.setServiceAssembler(null);
  }

  /**
   * Test service injection
   */
  @Test
  public void testLogin() {

    Login login = new Login();
    assertEquals(mockServiceUser, login.getServiceUser());
  }

  /**
   * Tests whether loginError is set to false.
   */
  @Test
  public void testResetLogin() {

    Login login = new Login();
    login.setLoginError(true);
    login.resetLogin();
    Assert.assertFalse(login.getLoginError());
  }

  /**
   * Test the loginButtonClicked method. It assures that this method returns
   * the correct String after login button was clicked in case of success.
   * When the login button is clicked the fields email and password are passed
   * to the Service method doLogin().
   */
  @Test
  public void testLoginButtonClickedSuccess() {
    // Configure
    EasyMock.expect(
        mockServiceUser.doLogin(mockUser.getEmail(),
            mockUser.getPassword())).andReturn(mockUser);

    EasyMock.replay(mockServiceUser);

    // Run test
    Login login = new Login();
    login.setEmail(mockUser.getEmail());
    login.setPassword(mockUser.getPassword());
    String result = login.loginButtonClicked();
    Assert.assertEquals("Login", result);

    // Verify
    EasyMock.verify(mockServiceUser);
  }

  /**
   * Test the loginButtonClicked method. It assures that this method returns
   * the correct String and that the loginError will be set to True after
   * login button was clicked, in case of an Error. When the login button is
   * clicked the fields email and password are passed to the Service method
   * doLogin().
   */
  @Test
  public void testLoginButtonClickedError() {
    // Configure
    EasyMock.expect(
        mockServiceUser.doLogin(mockUser.getEmail(),
            mockUser.getPassword())).andReturn(null);

    EasyMock.replay(mockServiceUser);

    // Run test
    Login login = new Login();
    login.setEmail(mockUser.getEmail());
    login.setPassword(mockUser.getPassword());
    String result = login.loginButtonClicked();
    Assert.assertEquals("Login failed", result);

    Assert.assertTrue(login.getLoginError());

    // Verify
    EasyMock.verify(mockServiceUser);
  }

  /**
   * Test the loginButtonClicked method. It assures that this method returns
   * the correct String after login button was clicked in case of an
   * Exception. When the login button is clicked the fields email and password
   * are passed to the Service method doLogin().
   */
  @Test
  public void testLoginButtonClickedServiceException() {
    // Configure
    EasyMock.expect(
        mockServiceUser.doLogin(mockUser.getEmail(),
            mockUser.getPassword())).andThrow(
        new ServiceException(""));

    EasyMock.replay(mockServiceUser);

    // Run Test
    Login login = new Login();
    login.setEmail(mockUser.getEmail());
    login.setPassword(mockUser.getPassword());
    String result = login.loginButtonClicked();
    Assert.assertEquals("errorDatabase", result);

    // Verify
    EasyMock.verify(mockServiceUser);
  }

  /**
   * Test the loginButtonClicked method. It assures that this method returns
   * the correct String after login button was clicked in case of that the
   * returned user is null. When the login button is clicked the fields email
   * and password are passed to the Service method doLogin().
   */
  @Test
  public void testLoginButtonClickedUserIsNull() {
    // Configure
    EasyMock.expect(
        mockServiceUser.doLogin(mockUser.getEmail(),
            mockUser.getPassword())).andReturn(null);

    EasyMock.replay(mockServiceUser);

    // Run test
    Login login = new Login();
    login.setEmail(mockUser.getEmail());
    login.setPassword(mockUser.getPassword());
    String result = login.loginButtonClicked();

    Assert.assertEquals("Login failed", result);

    // Verify
    EasyMock.verify(mockServiceUser);
  }

  /**
   * Test the loginButtonClicked and logoutButtonClicked method. It assures
   * that this method returns the correct String after login and logout button
   * was clicked. First doLogin(), and assure that True is returned and then
   * do the same with doLogout() When the login button is clicked the fields
   * email and password are passed to the Service method doLogin().
   *
   * @throws IOException
   */
  @Test
  public void testIsLoggedInAndLogedOut() throws IOException {
    // Configure
    EasyMock.expect(
        mockServiceUser.doLogin(mockUser.getEmail(),
            mockUser.getPassword())).andReturn(mockUser);
    EasyMock.expect(mockJsfUtil.getLoggedInUser()).andReturn(mockUser);
    this.mockServiceUser.doLogout(null);
    expectLastCall().times(1);
    EasyMock.expect(mockJsfUtil.getLoggedInUser()).andReturn(null);
    mockJsfUtil.redirect("./foundTrips.jsf?self=true");
    EasyMock.expectLastCall();

    EasyMock.replay(mockServiceUser);
    EasyMock.replay(mockJsfUtil);

    // Run test
    Login login = new Login();
    login.setEmail(mockUser.getEmail());
    login.setPassword(mockUser.getPassword());

    login.loginButtonClicked();
    Assert.assertTrue(login.isLoggedIn());
    login.logoutButtonClicked();
    Assert.assertFalse(login.isLoggedIn());

    // Verify
    EasyMock.verify(mockServiceUser);
    EasyMock.verify(mockJsfUtil);
  }

}
TOP

Related Classes of at.fhj.itm.beans.LoginTest

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.