DatastoreStore extends StoreBase { @Override public Session load(String id) throws ClassNotFoundException, IOException { log.debug("Session " + id + " requested"); TraceContext context = startSpan("Loading session"); Key sessionKey = newKey(id); DatastoreSession session = deserializeSession(sessionKey); endSpan(context); log.debug("Session " + id + " loaded"); return session; }  @Override int getSize(); @Override String[] keys(); @Override Session load(String id); @Override void remove(String id); @Override void clear(); @Override void save(Session session); @Override void processExpires(); void setNamespace(String namespace); void setSessionKind(String sessionKind); void setTraceRequest(boolean traceRequest);  }
@Test public void testEmptySessionLoading() throws Exception { DatastoreSession session = new DatastoreSession(manager); session.setValid(true); session.setId(keyId); Entity sessionEntity = session.saveMetadataToEntity(key); when(datastore.<Entity>run(any())).thenReturn( new IteratorQueryResults<>(Collections.singleton(sessionEntity).iterator())); Session loadedSession = store.load(keyId); verify(datastore).run(any()); verify(manager).createEmptySession(); assertEquals(loadedSession.getId(), keyId); }
@Test public void testLoadNonExistentSession() throws Exception { when(datastore.run(any())).thenReturn(new IteratorQueryResults<>(Collections.emptyIterator())); Session session = store.load("456"); verify(manager, never()).createEmptySession(); assertNull(session); }
@Test public void testDecomposedSessionLoad() throws Exception { DatastoreSession session = new DatastoreSession(manager); session.setValid(true); session.setId(keyId); session.setAttribute("count", 2); session.setAttribute("map", Collections.singletonMap("key", "value")); KeyFactory attributeKeyFactory = datastore.newKeyFactory() .setKind("kind") .addAncestor(PathElement.of("kind", key.getName())); List<Entity> entities = session.saveToEntities(key, attributeKeyFactory); QueryResults<Entity> queryResults = new IteratorQueryResults<>(entities.iterator()); when(datastore.<Entity>run(any())).thenReturn(queryResults); Session restored = store.load(keyId); assertEquals(keyId, restored.getId()); assertEquals(2, restored.getSession().getAttribute("count")); assertEquals("value", ((Map<String, String>)session.getSession().getAttribute("map")).get("key")); }