Package at.fhj.itm.business

Source Code of at.fhj.itm.business.ServiceUserLoginTest

package at.fhj.itm.business;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;

import javax.sql.DataSource;

import junit.framework.Assert;

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

import at.fhj.itm.business.ServiceUser;
import at.fhj.itm.business.ServiceUserImpl;
import at.fhj.itm.dao.DAOException;
import at.fhj.itm.dao.UserDAO;
import at.fhj.itm.model.Location;
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 following methods of ServiceUserImpl: doLogin
* doLogout recoverUserPassword setPassword
*
* @author Martin Bönsch, Christian Taßler
*
*/
public class ServiceUserLoginTest extends AbstractServiceTest {

  private UserDAO mockUserDao;
  private JsfUtil mockJsfUtil;
  private RandomUtil mockRandUtil;
  private EmailUtil mockMailUtil;
  private User mockUser;
  private Location mockLocation;

  /**
   * This method setups the mock properties for the following tests
   *
   * @throws Exception
   */
  @Before
  public void setUp() throws Exception {
        mockLocation = new Location(8605, "Kapfenberg");
    mockUser = new User("Hannes", "Lauser", "hlauser", "password", "lauser@aon.at", "+436641234567", this.mockLocation, null, "");
    mockUserDao = EasyMock.createMock(UserDAO.class);
    mockDataSource = EasyMock.createMock(DataSource.class);
    mockConnection = EasyMock.createMock(Connection.class);
    mockJsfUtil = EasyMock.createMock(JsfUtil.class);
    mockRandUtil = EasyMock.createMock(RandomUtil.class);
    mockMailUtil = EasyMock.createMock(EmailUtil.class);

  }

  /**
   * This methods runs the doLogin method of the ServiceUserImpl class. This
   * tests should run without an error.
   *
   * @throws SQLException
   */
  @Test
  public void testDoLogin() throws SQLException {
    // Setup input data:
    String email = "lauser@aon.at";
    String password = "password";

    // Setup Mocks:
    mockBegin();
    EasyMock.expect(
        mockUserDao.userValid(email, password, mockGetConnection()))
        .andReturn(mockUser);
    mockCommit();
    mockCloseConnection();

    mockBegin();
    mockUserDao.update(mockUser, mockGetConnection());
    mockCommit();
    mockCloseConnection();
    mockJsfUtil.userLoggedIn(mockUser);
    EasyMock.expectLastCall();
    replayMocks();

    // Do Test:
    ServiceUser serviceUser = new ServiceUserImpl(mockUserDao,
        mockDataSource, mockJsfUtil, mockRandUtil, mockMailUtil);
    User user = serviceUser.doLogin(email, password);

    // Verify:
    long now = System.currentTimeMillis();
    long actualTime = user.getLastLoginDate().getTime();
    // the time set was prior now
    Assert.assertTrue(actualTime <= now);
    // the time set must not be older than 5 seconds
    Assert.assertTrue(actualTime >= now - 5000);
    verifyMocks();
  }

  /**
   * This methods runs the doLogin method of the ServiceUserImpl class. In
   * this test the userValid method should return a DAOException. As a result
   * the method should log the error and throw a ServiceException.
   *
   * @throws SQLException
   */
  @Test
  public void testDoLoginDAOException() throws SQLException {
    // Setup input data:
    String email = "lauser@aon.at";
    String password = "password";

    // Setup Mocks:
    mockBegin();
    EasyMock.expect(
        mockUserDao.userValid(email, password, mockGetConnection()))
        .andThrow(new DAOException("DAOException"));
    mockCloseConnection();

    replayMocks();

    // Do Test:
    ServiceUser serviceUser = new ServiceUserImpl(mockUserDao,
        mockDataSource, mockJsfUtil, mockRandUtil, mockMailUtil);

    // Verify:
    try {
      serviceUser.doLogin(email, password);
      Assert.fail("ServiceException didn't occure");
    } catch (ServiceException e) {
    }
    verifyMocks();
  }

  /**
   * This methods runs the doLogin method of the ServiceUserImpl class. In
   * this test the commit should return a SQLException. This should cause a
   * ServiceException.
   *
   * @throws SQLException
   */
  @Test
  public void testDoLoginSQLException() throws SQLException {
    // Setup input data:
    String email = "lauser@aon.at";
    String password = "password";

    // Setup Mocks:
    mockBegin();
    EasyMock.expect(
        mockUserDao.userValid(email, password, mockGetConnection()))
        .andReturn(mockUser);
    mockCommit();
    EasyMock.expectLastCall().andThrow(new SQLException());
    mockCloseConnection();

    replayMocks();

    // Do Test:
    ServiceUser serviceUser = new ServiceUserImpl(mockUserDao,
        mockDataSource, mockJsfUtil, mockRandUtil, mockMailUtil);

    // Verify:
    try {
      serviceUser.doLogin(email, password);
      Assert.fail("ServiceException didn't occure");
    } catch (ServiceException e) {
    }
    verifyMocks();
  }

  /**
   * This methods runs the doLogin method of the ServiceUserImpl class. In
   * this test no user can be found with the specified email and password
   * Strings. As a result it should return a null value.
   *
   * @throws SQLException
   */
  @Test
  public void testDoLoginWrongInput() throws SQLException {
    // Setup input data:
    String email = "lauser@aon.at";
    String password = "password";

    // Setup Mocks:
    mockBegin();
    // here the DAO should not find the user with this email and password
    EasyMock.expect(
        mockUserDao.userValid(email, password, mockGetConnection()))
        .andReturn(null);
    mockCommit();
    mockCloseConnection();

    replayMocks();

    // Do Test:
    ServiceUser serviceUser = new ServiceUserImpl(mockUserDao,
        mockDataSource, mockJsfUtil, mockRandUtil, mockMailUtil);
    User nullUser = serviceUser.doLogin(email, password);

    // Verify:
    Assert.assertNull(nullUser);
    verifyMocks();
  }

  /**
   * This methods runs the doLogin method of the ServiceUserImpl class. In
   * this test the getConnection() method should cause an SQLException, so
   * that it is unable to get a Connection. As a result this should throw an
   * ServiceException.
   *
   * @throws SQLException
   */
  @Test
  public void testDoLoginNoConnection() throws SQLException {
    // Setup input data:
    String email = "lauser@aon.at";
    String password = "password";

    // Setup Mocks:
    mockBeginNoConnection();
    replayMocks();

    // Do Test:
    ServiceUser serviceUser = new ServiceUserImpl(mockUserDao,
        mockDataSource, mockJsfUtil, mockRandUtil, mockMailUtil);

    // Verify:
    try {
      serviceUser.doLogin(email, password);
      Assert.fail("ServiceException didn't occure");
    } catch (ServiceException e) {
    }
    verifyMocks();
  }

  /**
   * This methods runs the doLogin method of the ServiceUserImpl class. In
   * this test the update command of the updateUser method should cause a
   * DAOException As a result there should be a rollback be done and the error
   * should be logged. No Exception should be thrown.
   *
   * @throws SQLException
   */
  @Test
  public void testDoLoginFailToSetLastLoginDAOException() throws SQLException {
    // Setup input data:
    String email = "lauser@aon.at";
    String password = "password";

    // Setup Mocks:
    mockBegin();
    EasyMock.expect(
        mockUserDao.userValid(email, password, mockGetConnection()))
        .andReturn(mockUser);
    mockCommit();
    mockCloseConnection();

    mockBegin();
    mockUserDao.update(mockUser, mockGetConnection());
    EasyMock.expectLastCall().andThrow(new DAOException("DAOException"));
    mockRollback();
    mockCloseConnection();
    mockJsfUtil.userLoggedIn(mockUser);
    EasyMock.expectLastCall();

    replayMocks();

    // Do Test:
    ServiceUser serviceUser = new ServiceUserImpl(mockUserDao,
        mockDataSource, mockJsfUtil, mockRandUtil, mockMailUtil);

    // Verify:
    serviceUser.doLogin(email, password);
    // if the last login date is off, just log. no critical error.

    verifyMocks();
  }

  /**
   * This methods runs the doLogin method of the ServiceUserImpl class. In
   * this test the commit of the updateUser method should cause an
   * SQLException. As a result there should be a rollback be done and the
   * error should be logged. No Exception should be thrown.
   *
   * @throws SQLException
   */
  @Test
  public void testDoLoginFailToSetLastLoginSQLException() throws SQLException {
    // Setup input data:
    String email = "lauser@aon.at";
    String password = "password";

    // Setup Mocks:
    mockBegin();
    EasyMock.expect(
        mockUserDao.userValid(email, password, mockGetConnection()))
        .andReturn(mockUser);
    mockCommit();
    mockCloseConnection();

    mockBegin();
    mockUserDao.update(mockUser, mockGetConnection());
    mockConnection.commit();
    EasyMock.expectLastCall().andThrow(new SQLException());
    mockRollback();
    mockCloseConnection();
    mockJsfUtil.userLoggedIn(mockUser);
    EasyMock.expectLastCall();

    replayMocks();

    // Do Test:
    ServiceUser serviceUser = new ServiceUserImpl(mockUserDao,
        mockDataSource, mockJsfUtil, mockRandUtil, mockMailUtil);

    // Verify:
    serviceUser.doLogin(email, password);
    verifyMocks();
  }

  /**
   * This test should test the doLogout() method of the ServiceUserImpl class.
   * The doLogout method calls the jsfUtils logout method (which would call
   * all Listeners).
   *
   * @throws SQLException
   */
  @Test
  public void testDoLogout() throws SQLException {
    // Setup Mocks:
    mockJsfUtil.userLoggedOut();
    EasyMock.expectLastCall();
    replayMocks();

    // Do Test:
    ServiceUser serviceUser = new ServiceUserImpl(mockUserDao,
        mockDataSource, mockJsfUtil, mockRandUtil, mockMailUtil);
    serviceUser.doLogout(mockUser);

    // Verify:
    verifyMocks();
  }

  /**
   * This methods runs the recoverUserPassword method of the ServiceUserImpl
   * class. In this test everything should work fine and no error should
   * accure.
   *
   * @throws SQLException
   */
  @Test
  public void testRecoverUserPassword() throws SQLException {
    // Setup input data:
    String email = "lauser@aon.at";
    String phone = "+436641234567";

    // Setup Mocks:
    mockBegin();
    EasyMock.expect(
        mockUserDao.getUserWithEmailAndPhone(email, phone,
            mockGetConnection())).andReturn(mockUser);
    mockCommit();
    mockCloseConnection();
    mockBegin();
    mockUserDao.update(mockUser, mockGetConnection());
    mockCommit();
    mockCloseConnection();
    EasyMock.expect(this.mockRandUtil.getRandSessionID()).andReturn("djfdkfjreiru");

    replayMocks();

    // Do Test:
    ServiceUser serviceUser = new ServiceUserImpl(mockUserDao,
        mockDataSource, mockJsfUtil, mockRandUtil, mockMailUtil);

    // Verify:
    Assert.assertTrue(!mockUser.getSessionID().equals(
        serviceUser.recoverUserPassword(email, phone)));
    verifyMocks();
  }

  /**
   * This methods runs the recoverUserPassword method of the ServiceUserImpl
   * class. In this test the getConnection() method should cause an
   * SQLException, so that it is unable to get a Connection. As a result this
   * should throw an ServiceException.
   *
   * @throws SQLException
   */
  @Test
  public void testRecoverUserPasswordNoConnection() throws SQLException {
    // Setup input data:
    String email = "lauser@aon.at";
    String phone = "+436641234567";

    // Setup Mocks:
    mockBeginNoConnection();

    replayMocks();

    // Do Test:
    ServiceUser serviceUser = new ServiceUserImpl(mockUserDao,
        mockDataSource, mockJsfUtil, mockRandUtil, mockMailUtil);

    // Verify:
    try {
      serviceUser.recoverUserPassword(email, phone);
      Assert.fail("ServiceException didn't occure");
    } catch (ServiceException e) {
    }
    verifyMocks();
  }

  /**
   * This methods runs the recoverUserPassword method of the ServiceUserImpl
   * class. In this test the DAO should not be able to find a user with this
   * email and phone. As a result the method should return a null value.
   *
   * @throws SQLException
   */
  @Test
  public void testRecoverUserPasswordNoUserWithEmailAndPhone()
      throws SQLException {
    // Setup input data:
    String email = "lauser@aon.at";
    String phone = "+436641234567";

    // Setup Mocks:
    mockBegin();
    EasyMock.expect(
        mockUserDao.getUserWithEmailAndPhone(email, phone,
            mockGetConnection())).andReturn(null);
    mockCommit();
    mockCloseConnection();

    replayMocks();

    // Do Test:
    ServiceUser serviceUser = new ServiceUserImpl(mockUserDao,
        mockDataSource, mockJsfUtil, mockRandUtil, mockMailUtil);

    // Verify:
    Assert.assertNull(serviceUser.recoverUserPassword(email, phone));
    verifyMocks();
  }

  /**
   * This methods runs the recoverUserPassword method of the ServiceUserImpl
   * class. In this test the getUserWithEmailAndPhone method should return a
   * DAOException. As a result the method should log the error and throw a
   * ServiceException.
   *
   * @throws SQLException
   */
  @Test
  public void testRecoverUserPasswordDAOException() throws SQLException {
    // Setup input data:
    String email = "lauser@aon.at";
    String phone = "+436641234567";

    // Setup Mocks:
    mockBegin();
    EasyMock.expect(
        mockUserDao.getUserWithEmailAndPhone(email, phone,
            mockGetConnection())).andThrow(
        new DAOException("DAOException"));
    mockCloseConnection();

    replayMocks();

    // Do Test:
    ServiceUser serviceUser = new ServiceUserImpl(mockUserDao,
        mockDataSource, mockJsfUtil, mockRandUtil, mockMailUtil);

    // Verify:

    try {
      serviceUser.recoverUserPassword(email, phone);
      Assert.fail("ServiceException didn't occure");
    } catch (ServiceException e) {
    }
    verifyMocks();
  }

  /**
   * This methods runs the recoverUserPassword method of the ServiceUserImpl
   * class. In this test the commit should return a SQLException. This should
   * throw a Service Exception.
   *
   * @throws SQLException
   */
  @Test
  public void testRecoverUserPasswordSQLException() throws SQLException {
    // Setup input data:
    String email = "lauser@aon.at";
    String phone = "+436641234567";

    // Setup Mocks:
    mockBegin();
    EasyMock.expect(
        mockUserDao.getUserWithEmailAndPhone(email, phone,
            mockGetConnection())).andReturn(mockUser);
    mockCommit();
    EasyMock.expectLastCall().andThrow(new SQLException());
    mockCloseConnection();

    replayMocks();

    // Do Test:
    ServiceUser serviceUser = new ServiceUserImpl(mockUserDao,
        mockDataSource, mockJsfUtil, mockRandUtil, mockMailUtil);

    // Verify:

    try {
      serviceUser.recoverUserPassword(email, phone);
      Assert.fail("ServiceException didn't occure");
    } catch (ServiceException e) {
    }
    verifyMocks();
  }

  /**
   * This methods runs the recoverUserPassword method of the ServiceUserImpl
   * class. In this test the update command of the updateUser method should
   * cause a DAOException As a result there should be a rollback be done and a
   * ServiceException be thrown.
   *
   * @throws SQLException
   */
  @Test
  public void testRecoverUserPasswordUpdateUserDAOException()
      throws SQLException {
    // Setup input data:
    String email = "lauser@aon.at";
    String phone = "+436641234567";

    // Setup Mocks:
    mockBegin();
    EasyMock.expect(
        mockUserDao.getUserWithEmailAndPhone(email, phone,
            mockGetConnection())).andReturn(mockUser);
    mockCommit();
    mockCloseConnection();
    mockBegin();
    mockUserDao.update(mockUser, mockGetConnection());
    EasyMock.expectLastCall().andThrow(new DAOException("DAOException"));
    mockRollback();
    mockCloseConnection();
    EasyMock.expect(this.mockRandUtil.getRandSessionID()).andReturn("djfdkfjreiru");
   
    replayMocks();

    // Do Test:
    ServiceUser serviceUser = new ServiceUserImpl(mockUserDao,
        mockDataSource, mockJsfUtil, mockRandUtil, mockMailUtil);

    // Verify:

    try {
      serviceUser.recoverUserPassword(email, phone);
      Assert.fail("ServiceException didn't occure");
    } catch (ServiceException e) {
    }
    verifyMocks();
  }

  /**
   * This methods runs the recoverUserPassword method of the ServiceUserImpl
   * class. In this test the commit command of the updateUser method should
   * cause a SQLException As a result there should be a rollback be done and a
   * ServiceException be thrown.
   *
   * @throws SQLException
   */
  @Test
  public void testRecoverUserPasswordUpdateUserSQLException()
      throws SQLException {
    // Setup input data:
    String email = "lauser@aon.at";
    String phone = "+436641234567";

    // Setup Mocks:
    mockBegin();
    EasyMock.expect(
        mockUserDao.getUserWithEmailAndPhone(email, phone,
            mockGetConnection())).andReturn(mockUser);
    mockCommit();
    mockCloseConnection();
    mockBegin();
    mockUserDao.update(mockUser, mockGetConnection());
    mockCommit();
    EasyMock.expectLastCall().andThrow(new SQLException());
    mockRollback();
    mockCloseConnection();
    EasyMock.expect(this.mockRandUtil.getRandSessionID()).andReturn("djfdkfjreiru");
   
    replayMocks();

    // Do Test:
    ServiceUser serviceUser = new ServiceUserImpl(mockUserDao,
        mockDataSource, mockJsfUtil, mockRandUtil, mockMailUtil);

    // Verify:

    try {
      serviceUser.recoverUserPassword(email, phone);
      Assert.fail("ServiceException didn't occure");
    } catch (ServiceException e) {
    }
    verifyMocks();
  }

  /**
   * This methods runs the setPassword method of the ServiceUserImpl class. In
   * this test everything should work fine and no error should occur.
   *
   * @throws SQLException
   */
  @Test
  public void testSetPassword() throws SQLException {
    // Setup input data:
    String newPassword = "newPassword";
    String sessionId = "sessionId";

    // Setup Mocks:
    mockBegin();
    EasyMock.expect(
        mockUserDao.getUserForSessionId(sessionId, mockGetConnection()))
        .andReturn(mockUser);
    mockCommit();
    mockCloseConnection();
    mockBegin();
    mockUserDao.update(mockUser, mockGetConnection());
    mockCommit();
    mockCloseConnection();
    EasyMock.expect(this.mockRandUtil.getRandSessionID()).andReturn("djfdkfjreiru");
   
    replayMocks();

    // Do Test:
    ServiceUser serviceUser = new ServiceUserImpl(mockUserDao,
        mockDataSource, mockJsfUtil, mockRandUtil, mockMailUtil);

    // Verify:
    Assert.assertTrue(serviceUser.setPassword(newPassword, sessionId));
    verifyMocks();
  }

  /**
   * This methods runs the setPassword method of the ServiceUserImpl class. In
   * this test the getConnection() method should cause an SQLException, so
   * that it is unable to get a Connection. As a result this should throw an
   * ServiceException.
   *
   * @throws SQLException
   */
  @Test
  public void testSetPasswordNoConnection() throws SQLException {
    // Setup input data:
    String newPassword = "newPassword";
    String sessionId = "sessionId";

    // Setup Mocks:
    mockBeginNoConnection();

    replayMocks();

    // Do Test:
    ServiceUser serviceUser = new ServiceUserImpl(mockUserDao,
        mockDataSource, mockJsfUtil, mockRandUtil, mockMailUtil);

    // Verify:
    try {
      serviceUser.setPassword(newPassword, sessionId);
      Assert.fail("ServiceException didn't occure");
    } catch (ServiceException e) {
    }
    verifyMocks();

  }

  /**
   * This methods runs the setPassword method of the ServiceUserImpl class. In
   * this test the DAO should not be able to find a user with this sessionID.
   * As a result the method should return a null value.
   *
   * @throws SQLException
   */
  @Test
  public void testSetPasswordNoUserForSessionId() throws SQLException {
    // Setup input data:
    String newPassword = "newPassword";
    String sessionId = "sessionId";

    // Setup Mocks:
    mockBegin();
    EasyMock.expect(
        mockUserDao.getUserForSessionId(sessionId, mockGetConnection()))
        .andReturn(null);
    mockCommit();
    mockCloseConnection();

    replayMocks();

    // Do Test:
    ServiceUser serviceUser = new ServiceUserImpl(mockUserDao,
        mockDataSource, mockJsfUtil, mockRandUtil, mockMailUtil);

    // Verify:
    Assert.assertFalse(serviceUser.setPassword(newPassword, sessionId));
    verifyMocks();
  }

  /**
   * This methods runs the setPassword method of the ServiceUserImpl class. In
   * this test the lastLoginDate isn't null so an error should occur and throw
   * a ServiceException.
   *
   * @throws SQLException
   */
  @Test
  public void testSetPasswordUserNotPrepared() throws SQLException {

    // Setup input data:
    String newPassword = "newPassword";
    String sessionId = "sessionId";

    // Setup Mocks:
    mockBegin();
    EasyMock.expect(
        mockUserDao.getUserForSessionId(sessionId, mockGetConnection()))
        .andReturn(mockUser);
    mockCommit();
    mockCloseConnection();
    // date must be null, to be sure the recovering of the password is
    // authorized. So it shouldnt work here!
    mockUser.setLastLoginDate(new Date());

    replayMocks();

    // Do Test:
    ServiceUser serviceUser = new ServiceUserImpl(mockUserDao,
        mockDataSource, mockJsfUtil, mockRandUtil, mockMailUtil);

    // Verify:
    Assert.assertFalse(serviceUser.setPassword(newPassword, sessionId));
    verifyMocks();
  }

  /**
   * This methods runs the setPassword method of the ServiceUserImpl class. In
   * this test the getUserWithEmailAndPhone method should return a
   * DAOException. As a result the method should log the error and throw a
   * ServiceException.
   *
   * @throws SQLException
   */
  @Test
  public void testSetPasswordDAOException() throws SQLException {
    // Setup input data:
    String newPassword = "newPassword";
    String sessionId = "sessionId";

    // Setup Mocks:
    mockBegin();
    EasyMock.expect(
        mockUserDao.getUserForSessionId(sessionId, mockGetConnection()))
        .andThrow(new DAOException("DAOException"));
    mockCloseConnection();

    replayMocks();

    // Do Test:
    ServiceUser serviceUser = new ServiceUserImpl(mockUserDao,
        mockDataSource, mockJsfUtil, mockRandUtil, mockMailUtil);

    // Verify:
    try {
      serviceUser.setPassword(newPassword, sessionId);
      Assert.fail("ServiceException didn't occure");
    } catch (ServiceException e) {
    }
    verifyMocks();
  }

  /**
   * This methods runs the setPassword method of the ServiceUserImpl class. In
   * this test the commit should return a SQLException. This should throw a
   * ServiceException
   *
   * @throws SQLException
   */
  @Test
  public void testSetPasswordSQLException() throws SQLException {
    // Setup input data:
    String newPassword = "newPassword";
    String sessionId = "sessionId";

    // Setup Mocks:
    mockBegin();
    EasyMock.expect(
        mockUserDao.getUserForSessionId(sessionId, mockGetConnection()))
        .andReturn(mockUser);
    mockCommit();
    EasyMock.expectLastCall().andThrow(new SQLException());
    mockCloseConnection();

    replayMocks();

    // Do Test:
    ServiceUser serviceUser = new ServiceUserImpl(mockUserDao,
        mockDataSource, mockJsfUtil, mockRandUtil, mockMailUtil);

    // Verify:
    try {
      serviceUser.setPassword(newPassword, sessionId);
      Assert.fail("ServiceException didn't occure");
    } catch (ServiceException e) {
    }
    verifyMocks();
  }

  /**
   * This methods runs the setPassword method of the ServiceUserImpl class. In
   * this test the update command of the updateUser method should cause a
   * DAOException As a result there should be a rollback be done and return a
   * false value.
   *
   * @throws SQLException
   */
  @Test
  public void testSetPasswordUpdateUserDAOException() throws SQLException {
    // Setup input data:
    String newPassword = "newPassword";
    String sessionId = "sessionId";

    // Setup Mocks:
    mockBegin();
    EasyMock.expect(
        mockUserDao.getUserForSessionId(sessionId, mockGetConnection()))
        .andReturn(mockUser);
    mockCommit();
    mockCloseConnection();
    mockBegin();
    mockUserDao.update(mockUser, mockGetConnection());
    EasyMock.expectLastCall().andThrow(new DAOException("DAOException"));
    mockRollback();
    mockCloseConnection();
    EasyMock.expect(this.mockRandUtil.getRandSessionID()).andReturn("djfdkfjreiru");
   
    replayMocks();

    // Do Test:
    ServiceUser serviceUser = new ServiceUserImpl(mockUserDao,
        mockDataSource, mockJsfUtil, mockRandUtil, mockMailUtil);

    // Verify:
    try {
      serviceUser.setPassword(newPassword, sessionId);
      Assert.fail("ServiceException didn't occure");
    } catch (ServiceException e) {
    }
    verifyMocks();
  }

  /**
   * This methods runs the setPassword method of the ServiceUserImpl class. In
   * this test the commit command of the updateUser method should cause a
   * SQLException. As a result there should a rollback be done and return a
   * false value.
   *
   * @throws SQLException
   */
  @Test
  public void testSetPasswordUpdateUserSQLException() throws SQLException {
    // Setup input data:
    String newPassword = "newPassword";
    String sessionId = "sessionId";

    // Setup Mocks:
    mockBegin();
    EasyMock.expect(
        mockUserDao.getUserForSessionId(sessionId, mockGetConnection()))
        .andReturn(mockUser);
    mockCommit();
    mockCloseConnection();
    mockBegin();
    mockUserDao.update(mockUser, mockGetConnection());
    mockCommit();
    EasyMock.expectLastCall().andThrow(new SQLException());
    mockRollback();
    mockCloseConnection();
    EasyMock.expect(this.mockRandUtil.getRandSessionID()).andReturn("djfdkfjreiru");

    replayMocks();

    // Do Test:
    ServiceUser serviceUser = new ServiceUserImpl(mockUserDao,
        mockDataSource, mockJsfUtil, mockRandUtil, mockMailUtil);

    // Verify:
    try {
      serviceUser.setPassword(newPassword, sessionId);
      Assert.fail("ServiceException didn't occure");
    } catch (ServiceException e) {
    }
    verifyMocks();
  }

  /**
   * This method replays all mocks
   *
   * @throws SQLException
   */
  protected void replayMocks() throws SQLException {
    EasyMock.replay(mockUserDao);
    EasyMock.replay(mockJsfUtil);
    EasyMock.replay(mockRandUtil);
    EasyMock.replay(mockMailUtil);
    super.replayMocks();
  }

  /**
   * This method verifies all mocks.
   *
   * @throws SQLException
   */
  protected void verifyMocks() throws SQLException {
    EasyMock.verify(mockUserDao);
    EasyMock.verify(mockJsfUtil);
    EasyMock.verify(mockRandUtil);
    EasyMock.verify(mockMailUtil);
    super.verifyMocks();
  }
}
TOP

Related Classes of at.fhj.itm.business.ServiceUserLoginTest

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.