use of org.opencastproject.usertracking.api.UserSession in project opencast by opencast.
the class UserActionListImplTest method testListAddition.
/**
* Tests that adding events to a UserActionList works using the list add method
*/
@Test
public void testListAddition() {
UserSession us1 = new UserSessionImpl();
us1.setSessionId("4");
us1.setUserId("testing user");
us1.setUserIp("127.0.0.1");
UserSession us2 = new UserSessionImpl();
us2.setSessionId("6");
us2.setUserId("testing user 2");
us2.setUserIp("127.0.0.5");
UserAction ua1 = new UserActionImpl();
ua1.setId(4L);
ua1.setInpoint(90);
ua1.setIsPlaying(true);
ua1.setMediapackageId("false");
ua1.setOutpoint(100);
ua1.setSession(us1);
ua1.setType("test");
UserAction ua2 = new UserActionImpl();
ua2.setId(6L);
ua2.setInpoint(5);
ua2.setIsPlaying(false);
ua2.setMediapackageId("other");
ua2.setOutpoint(20);
ua2.setSession(us2);
ua2.setType("other test");
List<UserAction> list = new LinkedList<UserAction>();
list.add(ua1);
list.add(ua2);
UserActionList ual = new UserActionListImpl();
ual.add(list);
ual.setTotal(2);
Assert.assertEquals(2, ual.getTotal());
Assert.assertEquals(2, ual.getUserActions().size());
Assert.assertEquals(new Long(4), ual.getUserActions().get(0).getId());
Assert.assertEquals(90, ual.getUserActions().get(0).getInpoint());
Assert.assertEquals(true, ual.getUserActions().get(0).getIsPlaying());
Assert.assertEquals("false", ual.getUserActions().get(0).getMediapackageId());
Assert.assertEquals(100, ual.getUserActions().get(0).getOutpoint());
Assert.assertEquals("4", ual.getUserActions().get(0).getSession().getSessionId());
Assert.assertEquals("test", ual.getUserActions().get(0).getType());
Assert.assertEquals("testing user", ual.getUserActions().get(0).getSession().getUserId());
Assert.assertEquals("127.0.0.1", ual.getUserActions().get(0).getSession().getUserIp());
Assert.assertEquals(new Long(6), ual.getUserActions().get(1).getId());
Assert.assertEquals(5, ual.getUserActions().get(1).getInpoint());
Assert.assertEquals(false, ual.getUserActions().get(1).getIsPlaying());
Assert.assertEquals("other", ual.getUserActions().get(1).getMediapackageId());
Assert.assertEquals(20, ual.getUserActions().get(1).getOutpoint());
Assert.assertEquals("6", ual.getUserActions().get(1).getSession().getSessionId());
Assert.assertEquals("other test", ual.getUserActions().get(1).getType());
Assert.assertEquals("testing user 2", ual.getUserActions().get(1).getSession().getUserId());
Assert.assertEquals("127.0.0.5", ual.getUserActions().get(1).getSession().getUserIp());
Assert.assertEquals(15, ual.getUserActions().get(1).getLength());
}
use of org.opencastproject.usertracking.api.UserSession in project opencast by opencast.
the class UserTrackingServiceImplTest method createAndVerifyUserAction.
/**
* Creates and verifies a user action with an arbitrary date
* @throws Exception
*/
private UserAction createAndVerifyUserAction(String type, String sessionId, String mediapackageId, String userId, String userIp, int inpoint, int outpoint, Date createdDate) throws Exception {
UserSession userSession = createUserSession(sessionId, userId, userIp);
UserAction userAction = createUserAction(type, mediapackageId, inpoint, outpoint, createdDate, userSession);
if (UserTrackingServiceImpl.FOOTPRINT_KEY.equals(type)) {
userAction = service.addUserFootprint(userAction, userSession);
} else {
userAction = service.addUserTrackingEvent(userAction, userSession);
}
Long id = userAction.getId();
// Ensure that, once persisted, the user action has an ID
Assert.assertNotNull(id);
// Sanity checks
UserActionImpl fromDb = (UserActionImpl) service.getUserAction(id);
Assert.assertNotNull(fromDb);
Assert.assertNotNull(fromDb.getCreated());
Assert.assertEquals(id, fromDb.getId());
Assert.assertEquals(userAction.getMediapackageId(), fromDb.getMediapackageId());
Assert.assertEquals(userAction.getSession().getSessionId(), fromDb.getSession().getSessionId());
Assert.assertEquals(userAction.getType(), fromDb.getType());
Assert.assertEquals(userAction.getSession().getUserId(), fromDb.getSession().getUserId());
Assert.assertEquals(userAction.getSession().getUserIp(), fromDb.getSession().getUserIp());
Assert.assertEquals(userAction.getInpoint(), fromDb.getInpoint());
Assert.assertEquals(userAction.getOutpoint(), fromDb.getOutpoint());
Assert.assertEquals(userAction.getIsPlaying(), fromDb.getIsPlaying());
Assert.assertEquals(userAction.getSession().getUserIp(), fromDb.getSession().getUserIp());
return userAction;
}
use of org.opencastproject.usertracking.api.UserSession in project opencast by opencast.
the class UserTrackingServiceImpl method addUserTrackingEvent.
public UserAction addUserTrackingEvent(UserAction a, UserSession session) throws UserTrackingException {
EntityManager em = null;
EntityTransaction tx = null;
if (!logIp)
session.setUserIp("-omitted-");
if (!logUser)
session.setUserId("-omitted-");
if (!logSession)
session.setSessionId("-omitted-");
try {
em = emf.createEntityManager();
tx = em.getTransaction();
tx.begin();
UserSession userSession = populateSession(em, session);
a.setSession(userSession);
em.persist(a);
tx.commit();
return a;
} catch (Exception e) {
if (tx.isActive()) {
tx.rollback();
}
throw new UserTrackingException(e);
} finally {
if (em != null && em.isOpen()) {
em.close();
}
}
}
use of org.opencastproject.usertracking.api.UserSession in project opencast by opencast.
the class UserActionImplTest method testForwardLength.
/**
* Ensures a positive length user action calculates its length correctly.
*/
@Test
public void testForwardLength() {
UserSession us = new UserSessionImpl();
us.setSessionId("4");
us.setUserId("testing user");
us.setUserIp("127.0.0.1");
UserAction ua = new UserActionImpl();
ua.setId(4L);
ua.setInpoint(90);
ua.setIsPlaying(true);
ua.setMediapackageId("false");
ua.setOutpoint(100);
ua.setSession(us);
ua.setType("test");
Assert.assertEquals(new Long(4), ua.getId());
Assert.assertEquals(90, ua.getInpoint());
Assert.assertEquals(true, ua.getIsPlaying());
Assert.assertEquals("false", ua.getMediapackageId());
Assert.assertEquals(100, ua.getOutpoint());
Assert.assertEquals("4", ua.getSession().getSessionId());
Assert.assertEquals("test", ua.getType());
Assert.assertEquals("testing user", ua.getSession().getUserId());
Assert.assertEquals("127.0.0.1", ua.getSession().getUserIp());
Assert.assertEquals(10, ua.getLength());
}
use of org.opencastproject.usertracking.api.UserSession in project opencast by opencast.
the class UserTrackingServiceImplTest method createUserSession.
/**
* Creates a user session
* @throws Exception
*/
private UserSession createUserSession(String sessionId, String userId, String userIp) {
UserSession userSession = new UserSessionImpl();
userSession.setSessionId(sessionId);
userSession.setUserId(userId);
userSession.setUserIp(userIp);
return userSession;
}
Aggregations