### Question:
SpringSessionBackedSessionRegistry implements SessionRegistry { @Override public SessionInformation getSessionInformation(String sessionId) { S session = this.sessionRepository.findById(sessionId); if (session != null) { return new SpringSessionBackedSessionInformation<>(session, this.sessionRepository); } return null; } SpringSessionBackedSessionRegistry(FindByIndexNameSessionRepository<S> sessionRepository); @Override List<Object> getAllPrincipals(); @Override List<SessionInformation> getAllSessions(Object principal, boolean includeExpiredSessions); @Override SessionInformation getSessionInformation(String sessionId); @Override void refreshLastRequest(String sessionId); @Override void registerNewSession(String sessionId, Object principal); @Override void removeSessionInformation(String sessionId);  }### Answer:
@Test void sessionInformationForExistingSession() { Session session = createSession(SESSION_ID, USER_NAME, NOW); when(this.sessionRepository.findById(SESSION_ID)).thenReturn(session); SessionInformation sessionInfo = this.sessionRegistry.getSessionInformation(SESSION_ID); assertThat(sessionInfo.getSessionId()).isEqualTo(SESSION_ID); assertThat(sessionInfo.getLastRequest().toInstant().truncatedTo(ChronoUnit.MILLIS)) .isEqualTo(NOW.truncatedTo(ChronoUnit.MILLIS)); assertThat(sessionInfo.getPrincipal()).isEqualTo(USER_NAME); assertThat(sessionInfo.isExpired()).isFalse(); }
@Test void sessionInformationForExpiredSession() { Session session = createSession(SESSION_ID, USER_NAME, NOW); session.setAttribute(SpringSessionBackedSessionInformation.EXPIRED_ATTR, Boolean.TRUE); when(this.sessionRepository.findById(SESSION_ID)).thenReturn(session); SessionInformation sessionInfo = this.sessionRegistry.getSessionInformation(SESSION_ID); assertThat(sessionInfo.getSessionId()).isEqualTo(SESSION_ID); assertThat(sessionInfo.getLastRequest().toInstant().truncatedTo(ChronoUnit.MILLIS)) .isEqualTo(NOW.truncatedTo(ChronoUnit.MILLIS)); assertThat(sessionInfo.getPrincipal()).isEqualTo(USER_NAME); assertThat(sessionInfo.isExpired()).isTrue(); }
@Test void noSessionInformationForMissingSession() { assertThat(this.sessionRegistry.getSessionInformation("nonExistingSessionId")).isNull(); }
@Test void expireNow() { Session session = createSession(SESSION_ID, USER_NAME, NOW); when(this.sessionRepository.findById(SESSION_ID)).thenReturn(session); SessionInformation sessionInfo = this.sessionRegistry.getSessionInformation(SESSION_ID); assertThat(sessionInfo.isExpired()).isFalse(); sessionInfo.expireNow(); assertThat(sessionInfo.isExpired()).isTrue(); ArgumentCaptor<Session> captor = ArgumentCaptor.forClass(Session.class); verify(this.sessionRepository).save(captor.capture()); assertThat(captor.getValue().<Boolean>getAttribute(SpringSessionBackedSessionInformation.EXPIRED_ATTR)) .isEqualTo(Boolean.TRUE); }