/* ===================================================================
 * UserDaoTest.java
 * 
 * Created Sep 29, 2004 1:22:20 PM
 * 
 * Copyright (c) 2004 Matt Magoffin (spamsqr@msqr.us)
 * 
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License as 
 * published by the Free Software Foundation; either version 2 of 
 * the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License 
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 
 * 02111-1307 USA
 * ===================================================================
 * $Id: UserDaoTest.java 59 2009-05-11 09:16:24Z msqr $
 * ===================================================================
 */

package magoffin.matt.ieat.dao;

import magoffin.matt.ieat.dao.UserDao.UserIndexCallbackData;
import magoffin.matt.ieat.domain.User;

import org.apache.log4j.Logger;

/**
 * Test case for the UserDao.
 * 
 * <p>These tests can only work when the test data is loaded in the database!</p>
 * 
 * @author Matt Magoffin (spamsqr@msqr.us)
 * @version $Revision: 59 $ $Date: 2009-05-11 21:16:24 +1200 (Mon, 11 May 2009) $
 */
public class UserDaoTest extends AbstractDaoTest {
	
	private static final Logger LOG = Logger.getLogger(UserDaoTest.class);
	
	/** The UserDao to test. */
	protected UserDao userDao;
	
	/**
	 * Test clean user registrations.
	 */
	public void testCleanStaleUserRegistrations() {
		userDao.removeStaleUserRegistrations();
	}
	
	/**
	 * Test create a new user.
	 */
	public void testCreateAndDeleteNewUser() {
		User u = getDummyUser();
		User newUser = userDao.get(userDao.store(u));
		
		assertNotNull("Returned user should not be null on create",newUser);
		assertNotNull("New user should have user ID set", newUser.getUserId());
		
		// verify can get user from DAO
		newUser = userDao.get(newUser.getUserId());
		assertNotNull("New user should be available from DAO", newUser);
		assertTrue("New user's login should be same as dummy user's", u.getLogin().equals(newUser.getLogin()));
		
		// now clean up and remove test user
		userDao.delete(userDao.get(newUser.getUserId()));
		
		// verify not in DAO anymore
		newUser = userDao.getUser(u.getLogin());
		assertNull("After delete user should not be returned from DAO", newUser);
	}
	
	/**
	 * Test get a user by login.
	 */
	public void testGetUserByLogin() {
		// first throw user into DB
		User u = getDummyUser();
		userDao.store(u);
		
		// test that find by login works
		u = userDao.getUser(TEST_USER_LOGIN);
		assertNotNull("User should not be null for login '" +TEST_USER_LOGIN +"'", u);
		assertTrue("User should have login of '" +TEST_USER_LOGIN +"'", TEST_USER_LOGIN.equals(u.getLogin()));
	}

	/**
	 * Test able to index.
	 */
	public void testIndex() {
		userDao.index(new UserDao.UserIndexCallback() {
			public void handle(UserIndexCallbackData data) {
				if ( LOG.isDebugEnabled() ) {
					LOG.debug("Got user index callback data: " +data);
				}
				assertNotNull("User callback data should not be null", data);
				assertNotNull("User callback data 'name' should not be null", data.getName());
				assertNotNull("User callback data 'userId' should not be null", data.getUserId());
				assertNotNull("User callback data 'email' should not be null", data.getEmail());
				assertNotNull("User callback data 'createdDate' should not be null", data.getCreatedDate());
			}
		});
	}
	
}
