/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* This software is protected by the OLAT software license.<br>
* Use is subject to license terms.<br>
* See LICENSE.TXT in this distribution for details.
* <p>
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.<br>
* All rights reserved.
* <p>
*/
package org.olat.instantMessaging;
import java.util.List;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.olat.basesecurity.Authentication;
import org.olat.basesecurity.ManagerFactory;
import org.olat.core.commons.persistence.DB;
import org.olat.core.commons.persistence.DBFactory;
import org.olat.core.logging.Tracing;
import org.olat.core.test.OlatTestCase;
import org.olat.core.util.coordinate.CoordinatorManager;
import org.olat.instantMessaging.ui.ConnectedUsersListEntry;
import org.olat.test.JunitTestHelper;
/**
* Description:<br>
* IM junit tests
*
* <P>
* Initial Date: Nov 16, 2006 <br>
* @author guido
*/
public class IMUnitTest extends OlatTestCase {
String testUserA = "anIdentity1";
String testUserB = "anIdentity2";
String testUserC = "testuser@trashymail.com";
public IMUnitTest(String arg0) {
super(arg0);
}
/**
* @see junit.framework.TestCase#setUp()
*/
public void setUp() throws Exception {
super.setUp();
DBFactory.getJunitInstance().clearDatabase();
JunitTestHelper.createAndPersistIdentityAsUser(testUserA);
JunitTestHelper.createAndPersistIdentityAsUser(testUserB);
JunitTestHelper.createAndPersistIdentityAsUser(testUserC);
DBFactory.getInstance().closeSession();
}
/**
* TearDown is called after each test
*/
public void tearDown() {
try {
DB db = DBFactory.getInstance();
db.closeSession();
} catch (Exception e) {
Tracing.logError("Exception in tearDown(): " + e, IMUnitTest.class);
}
}
public void testIMStuff() {
if(InstantMessagingModule.isEnabled()) {
try {
String groupIdPlain = "junittestgroup-12343w5234";
String groupName = "junittestgroup";
InstantMessaging im = InstantMessagingModule.getAdapter();
String groupId = im.getNameHelper().getGroupnameForOlatInstance(groupIdPlain);
//first delete possible accounts and groups on the IM server
InstantMessagingModule.getAdapter().deleteAccount(testUserA);
InstantMessagingModule.getAdapter().deleteAccount(testUserB);
InstantMessagingModule.getAdapter().deleteAccount(testUserC);
im.deleteRosterGroup(groupId);
Authentication authC = ManagerFactory.getManager().findAuthenticationByAuthusername(testUserC, ClientManager.PROVIDER_INSTANT_MESSAGING);
if(authC != null) ManagerFactory.getManager().deleteAuthentication(authC);
DBFactory.getInstance().intermediateCommit();
InstantMessagingClient imClientC = InstantMessagingModule.getAdapter().getClientManager().getInstantMessagingClient(testUserC);
//wait some time as connection process is in background thread
Thread.sleep(3000);
assertTrue(imClientC.isConnected());
imClientC.closeConnection(true);
assertTrue(InstantMessagingModule.getAdapter().deleteAccount(testUserC));
//delete IM passwords, otherwise accounts don't get created
Authentication authA = ManagerFactory.getManager().findAuthenticationByAuthusername(testUserA, ClientManager.PROVIDER_INSTANT_MESSAGING);
Authentication authB = ManagerFactory.getManager().findAuthenticationByAuthusername(testUserB, ClientManager.PROVIDER_INSTANT_MESSAGING);
if(authA != null) ManagerFactory.getManager().deleteAuthentication(authA);
if(authB != null) ManagerFactory.getManager().deleteAuthentication(authB);
//get the IM client, it connects automatically to the server (creates an account on the im server)
InstantMessagingClient imClientA = InstantMessagingModule.getAdapter().getClientManager().getInstantMessagingClient(testUserA);
assertNotNull(imClientA);
InstantMessagingClient imClientB = InstantMessagingModule.getAdapter().getClientManager().getInstantMessagingClient(testUserB);
assertNotNull(imClientB);
Thread.sleep(1000);
assertEquals(true, imClientA.isConnected());
int groupCountA = imClientA.getRoster().getGroupCount();
assertEquals(true, imClientB.isConnected());
assertTrue(im.countConnectedUsers() >= 2); //there is may be as well an admin user connected
//add user to roster
im.addUserToFriendsRoster(testUserA, groupId, groupName, testUserB);
Thread.sleep(1000);
assertEquals(1, imClientA.getRoster().getGroup(groupName).getEntryCount());
Thread.sleep(1000);
im.renameRosterGroup(groupId, groupName+"ABC");
Thread.sleep(1000);
assertEquals(1, imClientA.getRoster().getGroup(groupName+"ABC").getEntryCount());
Thread.sleep(1000);
im.removeUserFromFriendsRoster(groupId, testUserB);
Thread.sleep(1000);
im.deleteRosterGroup(groupId);
Thread.sleep(1000);
assertEquals(groupCountA, imClientA.getRoster().getGroupCount());
//localy we do not have all information we need
//todo, add dummy values locally as we do not have authUserSessions
if (CoordinatorManager.getCoordinator().isClusterMode()) {
List<ConnectedUsersListEntry> l = im.getAllConnectedUsers(null);
ConnectedUsersListEntry entry = l.get(1);
assertNotNull(entry);
}
im.getClientManager().destroyInstantMessagingClient(testUserA);
im.getClientManager().destroyInstantMessagingClient(testUserB);
//delete the accounts with random passwords and recreate the default ones
assertTrue(InstantMessagingModule.getAdapter().deleteAccount(testUserA));
assertTrue(InstantMessagingModule.getAdapter().deleteAccount(testUserB));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static Test suite() throws Exception {
return new TestSuite(IMUnitTest.class);
}
}