Search in sources :

Example 6 with UserSession

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());
}
Also used : UserAction(org.opencastproject.usertracking.api.UserAction) UserActionList(org.opencastproject.usertracking.api.UserActionList) UserSession(org.opencastproject.usertracking.api.UserSession) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 7 with UserSession

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;
}
Also used : UserAction(org.opencastproject.usertracking.api.UserAction) UserSession(org.opencastproject.usertracking.api.UserSession)

Example 8 with UserSession

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();
        }
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) UserTrackingException(org.opencastproject.usertracking.api.UserTrackingException) UserSession(org.opencastproject.usertracking.api.UserSession) NoResultException(javax.persistence.NoResultException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ParseException(java.text.ParseException) UserTrackingException(org.opencastproject.usertracking.api.UserTrackingException) NotFoundException(org.opencastproject.util.NotFoundException)

Example 9 with UserSession

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());
}
Also used : UserAction(org.opencastproject.usertracking.api.UserAction) UserSession(org.opencastproject.usertracking.api.UserSession) Test(org.junit.Test)

Example 10 with UserSession

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;
}
Also used : UserSession(org.opencastproject.usertracking.api.UserSession)

Aggregations

UserSession (org.opencastproject.usertracking.api.UserSession)10 UserAction (org.opencastproject.usertracking.api.UserAction)6 Test (org.junit.Test)4 NoResultException (javax.persistence.NoResultException)3 UserTrackingException (org.opencastproject.usertracking.api.UserTrackingException)3 ParseException (java.text.ParseException)2 EntityManager (javax.persistence.EntityManager)2 EntityTransaction (javax.persistence.EntityTransaction)2 Query (javax.persistence.Query)2 UserActionList (org.opencastproject.usertracking.api.UserActionList)2 NotFoundException (org.opencastproject.util.NotFoundException)2 ConfigurationException (org.osgi.service.cm.ConfigurationException)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 LinkedList (java.util.LinkedList)1 PUT (javax.ws.rs.PUT)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 UserActionImpl (org.opencastproject.usertracking.impl.UserActionImpl)1