Package at.fhj.itm.business

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

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.After;
import org.junit.Before;
import org.junit.Test;

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.JsfUtil;
import at.fhj.itm.util.RandomUtil;

/**
* Tests for the doRegistration method of the ServiceUserImpl class.
*
* @author Hans-Joerg Tagger<br />
*         Patrick Habith
* @version 0.1 - last change Jan. 25, 2011
*/
public class ServiceUserRegTest extends AbstractServiceTest
{
    private UserDAO mockUserDao;
    private JsfUtil mockJsfUtil;
    private RandomUtil mockRandUtil;
    private EmailUtil mockMailUtil;
    private User mockUser;
    private Location mockLocation;

    /**
     * Setup mock objects for the test cases.
     *
     * @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 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();
    }

    /**
     * Test for a successful registration.
     *
     * @throws SQLException
     */
    @Test
    public void testDoRegistration() throws SQLException
    {
  // Setup Mocks:
  EasyMock.expect(mockRandUtil.getRandSessionID()).andReturn("");
  mockBegin();
  mockUserDao.update(mockUser, mockGetConnection());
  mockCommit();
  mockCloseConnection();
  EasyMock.expectLastCall();
  EasyMock.expect(
    this.mockMailUtil.sendRegConfirmation(this.mockUser.getEmail(),
      this.mockUser.getUsername())).andReturn(true);

  replayMocks();

  // Do Test:
  ServiceUser serviceUser = new ServiceUserImpl(mockUserDao,
    mockDataSource, mockJsfUtil, mockRandUtil, mockMailUtil);
  this.mockUser.setLastLoginDate(new Date());
  boolean regSuccessfull = serviceUser.doRegistration(
    mockUser.getFirstName(), mockUser.getLastName(),
    mockUser.getUsername(), mockUser.getPassword(),
    mockUser.getEmail(), mockUser.getPhone(),
    (int) mockLocation.getZip(), mockLocation.getCity());

  // Verify:
  Assert.assertTrue(regSuccessfull);
  verifyMocks();
    }

    /**
     * Test for a successful registration.
     *
     * @throws SQLException
     */
    @Test
    public void testDoRegistrationUnsuccessful() throws SQLException
    {
  // Setup Mocks:
  EasyMock.expect(mockRandUtil.getRandSessionID()).andReturn("");
  mockBegin();
  mockUserDao.update(mockUser, mockGetConnection());
  mockCommit();
  mockCloseConnection();
  EasyMock.expectLastCall().andThrow(
    new ServiceException("ServiceException"));

  replayMocks();

  // Do Test:
  ServiceUser serviceUser = new ServiceUserImpl(mockUserDao,
    mockDataSource, mockJsfUtil, mockRandUtil, mockMailUtil);
  this.mockUser.setLastLoginDate(new Date());
  boolean regSuccessfull = serviceUser.doRegistration(
    mockUser.getFirstName(), mockUser.getLastName(),
    mockUser.getUsername(), mockUser.getPassword(),
    mockUser.getEmail(), mockUser.getPhone(),
    (int) mockLocation.getZip(), mockLocation.getCity());

  // Verify:
  Assert.assertFalse(regSuccessfull);
  verifyMocks();
    }

    /**
     * Test for a username which is already used.
     *
     * @throws SQLException
     */
    @Test
    public void testCheckUsernameUsed() throws SQLException
    {
  // Setup Mocks:
  mockBegin();
  EasyMock.expect(
    mockUserDao.usernameAlreadyUsed(mockUser.getUsername(),
      mockGetConnection())).andReturn(true);
  mockCommit();
  mockCloseConnection();

  replayMocks();

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

  // Verify:
  Assert.assertTrue(usernameUsed);
  verifyMocks();
    }

    /**
     * Test for a username which is not used so far.
     *
     * @throws SQLException
     */
    @Test
    public void testCheckUsernameNotUsed() throws SQLException
    {
  // Setup Mocks:
  mockBegin();
  EasyMock.expect(
    mockUserDao.usernameAlreadyUsed(mockUser.getUsername(),
      mockGetConnection())).andReturn(false);
  mockCommit();
  mockCloseConnection();

  replayMocks();

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

  // Verify:
  Assert.assertFalse(usernameUsed);
  verifyMocks();
    }

    /**
     * Test for checkUsername throwing a ServiceException.
     *
     * @throws SQLException
     */
    @Test
    public void testCheckUsernameServiceException() throws SQLException
    {
  // Setup Mocks:
  mockBegin();
  EasyMock.expect(
    mockUserDao.usernameAlreadyUsed(mockUser.getUsername(),
      mockGetConnection())).andThrow(
    new ServiceException("ServiceException"));
  mockCloseConnection();
 
  replayMocks();

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

  try
  {
      serviceUser.checkUsername(mockUser.getUsername());
      Assert.fail("ServiceException didn't occure");
  } catch (ServiceException e)
  {
  }

  // Verify:
  verifyMocks();
    }
   
    /**
     * Test for an email which is already used.
     *
     * @throws SQLException
     */
    @Test
    public void testCheckMdailUsed() throws SQLException
    {
  // Setup Mocks:
  mockBegin();
  EasyMock.expect(
    mockUserDao.mailAlreadyUsed(mockUser.getEmail(),
      mockGetConnection())).andReturn(true);
  mockCommit();
  mockCloseConnection();

  replayMocks();

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

  // Verify:
  Assert.assertTrue(mailUsed);
  verifyMocks();
    }

    /**
     * Test for an email which is not used so far.
     *
     * @throws SQLException
     */
    @Test
    public void testCheckMailNotUsed() throws SQLException
    {
  // Setup Mocks:
  mockBegin();
  EasyMock.expect(
    mockUserDao.mailAlreadyUsed(mockUser.getEmail(),
      mockGetConnection())).andReturn(false);
  mockCommit();
  mockCloseConnection();

  replayMocks();

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

  // Verify:
  Assert.assertFalse(mailUsed);
  verifyMocks();
    }

    /**
     * Test for checkMail throwing a ServiceException.
     *
     * @throws SQLException
     */
    @Test
    public void testCheckMailServiceException() throws SQLException
    {
  // Setup Mocks:
  mockBegin();
  EasyMock.expect(
    mockUserDao.mailAlreadyUsed(mockUser.getEmail(),
      mockGetConnection())).andThrow(
    new ServiceException("ServiceException"));
  mockCloseConnection();
 
  replayMocks();

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

  try
  {
      serviceUser.checkMail(mockUser.getEmail());
      Assert.fail("ServiceException didn't occure");
  } catch (ServiceException e)
  {
  }

  // Verify:
  verifyMocks();
    }

    @After
    public void tearDown() throws Exception
    {
  mockLocation = null;
  mockUser = null;
  mockUserDao = null;
  mockDataSource = null;
  mockConnection = null;
  mockJsfUtil = null;
  mockRandUtil = null;
  mockMailUtil = null;
    }

}
TOP

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

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.