/*
 * The Gemma project
 * 
 * Copyright (c) 2006 University of British Columbia
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package ubic.gemma.model.common.auditAndSecurity;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import ubic.gemma.testing.BaseTransactionalSpringContextTest;

/**
 * Tests the Contact, Person, User interitance hierarchy and the association between User and UserRole.
 * 
 * @author keshav
 * @version $Id$
 */
public class UserDaoImplTest extends BaseTransactionalSpringContextTest {

    private UserDao userDao;
    private final Log log = LogFactory.getLog( UserDaoImplTest.class );
    private User testUser;
    private UserRole ur;

    public final void testCreateUser() throws Exception {
        assert testUser != null;
        User f = ( User ) userDao.create( testUser );
        assertNotNull( f );
        assertNotNull( f.getId() );
    }

    public final void testFindByEmail() throws Exception {
        userDao.create( testUser );
        Contact u = userDao.findByEmail( "foo@bar.com" );
        assertNotNull( u );
        assertEquals( "foo@bar.com", u.getEmail() );
        assertTrue( u instanceof User );
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void onSetUpInTransaction() throws Exception {
        super.onSetUpInTransaction();

        // User Object
        testUser = User.Factory.newInstance();
        // UserRole Object
        ur = UserRole.Factory.newInstance();

        String rand = ( new Date() ).toString();

        String adminName = "admin";
        String userName = "user";
        User checkUser = userDao.findByUserName( adminName );

        if ( ( checkUser == null ) ) {
            // User Object
            testUser.setUserName( adminName );
            // UserRole Object
            ur.setUserName( adminName );
            ur.setName( adminName );
        } else {
            testUser.setUserName( rand );
            log.info( rand );
            ur.setUserName( rand );
            ur.setName( userName );
        }

        testUser.setPassword( "root" );
        testUser.setPasswordHint( "test hint" );
        testUser.setEmail( "foo@bar.com" );
        AuditTrail ad = AuditTrail.Factory.newInstance();
        ad = ( AuditTrail ) persisterHelper.persist( ad );
        testUser.setAuditTrail( ad );
        ad = ( AuditTrail ) persisterHelper.persist( ad );
        testUser.getRoles().add( ur );

        // dao.create( testUser );
    }

    /**
     * @param useDao The useDao to set.
     */
    public void setUserDao( UserDao useDao ) {
        this.userDao = useDao;
    }
}
@@ -717,16 +717,66 @@
 .Date;%0A%0A
+import org.apache.commons.lang.RandomStringUtils;%0A
 import o
@@ -1141,16 +1141,40 @@
 tTest %7B%0A
+    String email = null;
 %0A    pri
@@ -1623,32 +1623,33 @@
 te( testUser );%0A
+%0A
         Contact 
@@ -1669,37 +1669,29 @@
 indByEmail( 
-%22foo@bar.com%22
+email
  );%0A        
@@ -1732,29 +1732,21 @@
 Equals( 
-%22foo@bar.com%22
+email
 , u.getE
@@ -1947,24 +1947,93 @@
 nsaction();%0A
+        email = RandomStringUtils.randomAlphabetic( 6 ) + %22@bar.com%22;
 %0A        // 
@@ -2861,21 +2861,13 @@
 il( 
-%22foo@bar.com%22
+email
  );%0A

make sure test users have unique email addresses (well..pretty sure)