### Question:
CTSOperations implements SessionOperations { public SessionInfo refresh(Session session, boolean reset) throws SessionException { if (reset) { return remote.refresh(session, reset); } else { SessionID sessionID = session.getID(); try { InternalSession internalSession = readToken(sessionID); return sessionInfoFactory.getSessionInfo(internalSession, sessionID); } catch (ReadFailedSessionException e) { return remote.refresh(session, reset); } } } @Inject  CTSOperations(CTSPersistentStore cts, SessionAdapter adapter,
                         TokenIdFactory idFactory, SessionInfoFactory sessionInfoFactory,
                         SessionService sessionService, RemoteOperations remote,
                         @Named(SessionConstants.SESSION_DEBUG) Debug debug); SessionInfo refresh(Session session, boolean reset); void logout(Session session); @Override void destroy(Session requester, Session session); void setProperty(Session session, String name, String value);  }### Answer:
@Test public void shouldReadTokenFromCTS() throws CoreTokenException, SessionException { Token mockToken = mock(Token.class); given(mockCTS.read(anyString())).willReturn(mockToken); InternalSession mockInternalSession = mock(InternalSession.class); given(mockAdapter.fromToken(eq(mockToken))).willReturn(mockInternalSession); SessionInfo mockSessionInfo = mock(SessionInfo.class); given(mockInfoFactory.getSessionInfo(eq(mockInternalSession), any(SessionID.class))).willReturn(mockSessionInfo); SessionInfo result = ctsOperations.refresh(mockSession, false); assertThat(result).isEqualTo(mockSessionInfo); }
@Test public void shouldReadTokenFromRemoteWhenCTSFails() throws CoreTokenException, SessionException { given(mockCTS.read(anyString())).willThrow(new ReadFailedException("id", new IOException())); SessionInfo mockSessionInfo = mock(SessionInfo.class); given(mockRemote.refresh(mockSession, false)).willReturn(mockSessionInfo); SessionInfo result = ctsOperations.refresh(mockSession, false); assertThat(result).isEqualTo(mockSessionInfo); }
@Test public void shouldResetLastAccessTimeRemotely() throws CoreTokenException, SessionException { Token mockToken = mock(Token.class); given(mockCTS.read(anyString())).willReturn(mockToken); InternalSession mockInternalSession = mock(InternalSession.class); given(mockAdapter.fromToken(eq(mockToken))).willReturn(mockInternalSession); ctsOperations.refresh(mockSession, true); verify(mockRemote).refresh(mockSession, true); }
@Test (expectedExceptions = SessionException.class) public void shouldThrowExceptionOnReadError() throws CoreTokenException, SessionException { given(mockCTS.read(anyString())).willThrow(new CoreTokenException("")); ctsOperations.refresh(mockSession, false); }