SessionManager implements Closeable { public RepositoryBackedSession getSession(RequestWithSession request, boolean create, SessionTracking.IdAndSource forceId) { SessionTracking.IdAndSource id = retrieveId(request, forceId); RepositoryBackedSession session = null; if (id != null && id.id != null) { putIdInLoggingMdc(id.id); request.setRequestedSessionId(id.id, id.cookie); session = fetchSession(id.id, true); if (!request.isRepositoryChecked()) { logger.info("Session with sessionId: '{}' but it was not in repository!", id); request.repositoryChecked(); } } else { putIdInLoggingMdc(null); request.setRequestedSessionId(null, tracking.isCookieTracking()); } if (session == null && create) { if (forceId == null) { id = new SessionTracking.IdAndSource(tracking.newId(), tracking.isCookieTracking()); } else { id = forceId; } putIdInLoggingMdc(id.id); logger.info("Creating new session with sessionId: '{}'", id); session = newSession(id.id); } if (session != null) { session.checkUsedAndLock(); } return session; } SessionManager(ExecutorFacade executors, SessionFactory factory, SessionRepository repository,
      SessionTracking tracking, SessionNotifier notifier, SessionConfiguration configuration, ClassLoader classLoader); RepositoryBackedSession fetchSession(String sessionId, boolean updateTimestamp); RepositoryBackedSession getSession(RequestWithSession request, boolean create,
      SessionTracking.IdAndSource forceId); void propagateSession(RequestWithSession request, ResponseWithSessionId response); void delete(String sessionId, boolean expired); void requestFinished(); Future<?> submit(String timer, Runnable task); ScheduledFuture<?> schedule(String timer, Runnable task, long period); void deleteAsync(final String sessionId, final boolean expired); SessionRepository getRepository(); SessionNotifier getNotifier(); void invokeCommit(RepositoryBackedSession session); SessionConfiguration getConfiguration(); ClassLoader getSessionClassLoader(); SerializerDeserializer getSerializerDeserializer(); void invalidationConflict(RepositoryBackedSession session, boolean onExpiry); MetricRegistry getMetrics(); String encodeUrl(RequestWithSession request, String url); void reset(); @Override void close(); void switchSessionId(RepositoryBackedSession session); void switchSessionId(RepositoryBackedSession session, String forceId); @Override String toString(); void remove(SessionData sessionData); boolean isConnected();  }
@Test public void testGetSessionNoSessionId() { RequestWithSession request = mock(RequestWithSession.class); RepositoryBackedSession retrievedSession = sessionManager.getSession(request, false, null); verify(request).setRequestedSessionId(null, false); assertNull("Session id shouldn't be in logging MDC", MDC.get(configuration.getLoggingMdcKey())); assertNull(retrievedSession); }
@Test public void testGetSessionNoSessionIdCreate() { RequestWithSession request = mock(RequestWithSession.class); when(tracking.newId()).thenReturn("new-id"); RepositoryBackedSession session = mock(RepositoryBackedSession.class); when(session.getId()).thenReturn("new-id"); when(factory.build(any(SessionData.class))).thenReturn(session); RepositoryBackedSession retrievedSession = sessionManager.getSession(request, true, null); verify(request).setRequestedSessionId(null, false); verify(tracking).newId(); assertEquals("Session id in Logging MDC is wrong", "new-id", MDC.get(configuration.getLoggingMdcKey())); assertSame(session, retrievedSession); assertEquals(1, metrics.meter(MetricRegistry.name(SessionManager.SESSIONS_METRIC_PREFIX, "created")).getCount()); }
@Test public void testGetSessionExpiredValid() { SessionData sessionData = new SessionData("2", now(), 10); RepositoryBackedSession session = mock(RepositoryBackedSession.class); when(session.getId()).thenReturn("2"); when(session.isExpired()).thenReturn(true); when(session.isValid()).thenReturn(true); when(repository.getSessionData("2")).thenReturn(sessionData); when(factory.build(sessionData)).thenReturn(session); RequestWithSession request = mock(RequestWithSession.class); when(request.getRequestedSessionId()).thenReturn("2"); when(request.isIdRetrieved()).thenReturn(true); RepositoryBackedSession retrievedSession = sessionManager.getSession(request, false, null); verify(session).doInvalidate(true); assertNull(retrievedSession); }
@Test public void testGetSessionExpiredInvalid() { SessionData sessionData = new SessionData("2", now(), 10); RepositoryBackedSession session = mock(RepositoryBackedSession.class); when(session.getId()).thenReturn("2"); when(session.isExpired()).thenReturn(true); when(session.isValid()).thenReturn(false); when(repository.getSessionData("2")).thenReturn(sessionData); when(factory.build(sessionData)).thenReturn(session); RequestWithSession request = mock(RequestWithSession.class); when(request.getRequestedSessionId()).thenReturn("2"); when(request.isIdRetrieved()).thenReturn(true); RepositoryBackedSession retrievedSession = sessionManager.getSession(request, false, null); verify(session, never()).doInvalidate(true); assertNull(retrievedSession); }