### Question:
SessionInfoFactory { public SessionInfo getSessionInfo(InternalSession internalSession, SessionID sessionID) throws SessionException { validateSession(internalSession, sessionID); return makeSessionInfo(internalSession, sessionID); }  SessionInfo getSessionInfo(InternalSession internalSession, SessionID sessionID); void validateSession(InternalSession internalSession, SessionID sid); SessionInfo makeSessionInfo(InternalSession internalSession, SessionID sid); static final String INVALID_SESSION_STATE; static final String SESSION_TIMED_OUT; }### Answer:
@Test public void shouldFailForInvalidSessions() { given(mockSession.getID()).willReturn(mockSessionID); given(mockSession.getRestrictionForToken(any(SessionID.class))).willReturn(mock(TokenRestriction.class)); given(mockSession.getState()).willReturn(INVALID); try { factory.getSessionInfo(mockSession, mockSessionID); } catch (SessionException e) { assertThat(e.getMessage()).containsIgnoringCase(getLocalisedMessage(SessionInfoFactory.INVALID_SESSION_STATE)); } }
@Test public void shouldFailForTimeoutSessions() { given(mockSession.getID()).willReturn(mockSessionID); given(mockSession.getRestrictionForToken(any(SessionID.class))).willReturn(mock(TokenRestriction.class)); given(mockSession.getState()).willReturn(VALID); given(mockSession.getState()).willReturn(INVALID); given(mockSession.getTimeLeftBeforePurge()).willReturn(1l); try { factory.getSessionInfo(mockSession, mockSessionID); } catch (SessionException e) { assertThat(e.getMessage()).containsIgnoringCase(getLocalisedMessage(SessionInfoFactory.SESSION_TIMED_OUT)); } }
@Test public void shouldGenerateSessionInfoFromInternalSession() throws SessionException { given(mockSession.getID()).willReturn(mockSessionID); given(mockSession.getRestrictionForToken(any(SessionID.class))).willReturn(mock(TokenRestriction.class)); given(mockSession.getState()).willReturn(VALID); given(mockSession.getTimeLeftBeforePurge()).willReturn(0l); SessionInfo mockSessionInfo = mock(SessionInfo.class); given(mockSession.toSessionInfo()).willReturn(mockSessionInfo); Hashtable mockHashTable = mock(Hashtable.class); given(mockSessionInfo.getProperties()).willReturn(mockHashTable); given(mockSession.getRestrictionForToken(any(SessionID.class))).willReturn(new MockTokenRestriction()); SessionInfo result = factory.getSessionInfo(mockSession, mockSessionID); assertThat(result).isNotNull(); }