### Question:
SessionAdapter implements TokenAdapter<InternalSession> { public Token toToken(InternalSession session) { String tokenId = tokenIdFactory.toSessionTokenId(session); Token token = new Token(tokenId, TokenType.SESSION); String userId = config.getUserId(session); token.setUserId(userId); long unixTimeMillis = session.getExpirationTime(TimeUnit.MILLISECONDS) + config.getSessionExpiryGracePeriod(TimeUnit.MILLISECONDS); Calendar expiryTimeStamp = TimeUtils.fromUnixTime(unixTimeMillis, TimeUnit.MILLISECONDS); token.setExpiryTimestamp(expiryTimeStamp); token.setAttribute(SessionTokenField.SESSION_ID.getField(), session.getID().toString()); String jsonBlob = serialisation.serialise(session); blobUtils.setBlobFromString(token, jsonBlob); String latestAccessTime = filterLatestAccessTime(token); if (latestAccessTime != null) { token.setAttribute(SessionTokenField.LATEST_ACCESS_TIME.getField(), latestAccessTime); } token.setAttribute(SessionTokenField.SESSION_HANDLE.getField(), session.getSessionHandle()); return token; } @Inject  SessionAdapter(TokenIdFactory tokenIdFactory, CoreTokenConfig config,
                          JSONSerialisation serialisation, TokenBlobUtils blobUtils); Token toToken(InternalSession session); InternalSession fromToken(Token token); int findIndexOfValidField(String blob); String filterLatestAccessTime(Token token);  }### Answer:
@Test public void shouldAssignAttributeFromSessionLatestAccessTime() { long timestamp = 12345l; InternalSession mockSession = mock(InternalSession.class); SessionID mockSessionID = mock(SessionID.class); String sessionHandle = SessionID.SHANDLE_SCHEME_PREFIX + "ferret"; given(mockSessionID.toString()).willReturn("badger"); given(jsonSerialisation.deserialise(anyString(), any(Class.class))).willReturn(mockSession); given(mockSession.getExpirationTime()).willReturn(timestamp); given(mockSession.getID()).willReturn(mockSessionID); given(mockSession.getSessionHandle()).willReturn(sessionHandle); given(tokenIdFactory.toSessionTokenId(eq(mockSession))).willReturn("badger"); given(jsonSerialisation.serialise(any())).willReturn(""); Token token = adapter.toToken(mockSession); assertThat(token.getExpiryTimestamp()).isNotNull(); }
@Test public void shouldAssignSessionID() { long timestamp = 12345l; InternalSession mockSession = mock(InternalSession.class); SessionID mockSessionID = mock(SessionID.class); String sessionId = "badger"; String sessionHandle = SessionID.SHANDLE_SCHEME_PREFIX + "ferret"; given(mockSessionID.toString()).willReturn(sessionId); given(jsonSerialisation.deserialise(anyString(), any(Class.class))).willReturn(mockSession); given(mockSession.getExpirationTime()).willReturn(timestamp); given(mockSession.getID()).willReturn(mockSessionID); given(mockSession.getSessionHandle()).willReturn(sessionHandle); given(tokenIdFactory.toSessionTokenId(eq(mockSession))).willReturn(sessionId); given(jsonSerialisation.serialise(any())).willReturn(""); Token token = adapter.toToken(mockSession); assertThat(token.<String>getValue(SessionTokenField.SESSION_ID.getField())).isEqualTo(sessionId); }