DatastoreSession extends StandardSession { public void restoreFromEntities(Key sessionKey, Iterable<Entity> entities) throws ClassNotFoundException, IOException { Entity metadataEntity = null; List<Entity> attributeEntities = new LinkedList<>(); for (Entity entity : entities) { if (entity.getKey().equals(sessionKey)) { metadataEntity = entity; } else { attributeEntities.add(entity); } } if (metadataEntity == null) { throw new IOException("The serialized session is missing the metadata entity"); } restoreMetadataFromEntity(metadataEntity); restoreAttributesFromEntity(attributeEntities); setId(sessionKey.getName()); initialAttributes.addAll(Collections.list(getAttributeNames())); } DatastoreSession(Manager manager); void restoreFromEntities(Key sessionKey, Iterable<Entity> entities); List<Entity> saveToEntities(Key sessionKey, KeyFactory attributeKeyFactory); Set<String> getSuppressedAttributes(); @Override Object getAttribute(String name); @Override void setAttribute(String name, Object value, boolean notify);  }
@Test public void testMetadataDeserialization() throws Exception { Entity metadata = Entity.newBuilder(sessionKey) .set(SessionMetadata.MAX_INACTIVE_INTERVAL, 0) .set(SessionMetadata.CREATION_TIME, 1) .set(SessionMetadata.LAST_ACCESSED_TIME, 2) .set(SessionMetadata.IS_NEW, true) .set(SessionMetadata.IS_VALID, true) .set(SessionMetadata.THIS_ACCESSED_TIME, 3) .build(); DatastoreSession session = new DatastoreSession(sessionManager); session.restoreFromEntities(sessionKey, Collections.singleton(metadata)); assertEquals(session.getMaxInactiveInterval(), 0); assertEquals(session.getCreationTime(), 1); assertEquals(session.getLastAccessedTime(), 2); assertEquals(session.isNew(), true); assertEquals(session.isValid(), true); assertEquals(session.getThisAccessedTime(), 3); }
@Test(expected = IOException.class) public void testSerializationWithoutMetadata() throws Exception { DatastoreSession session = new DatastoreSession(sessionManager); Entity attribute = Entity.newBuilder(new KeyFactory("project") .setKind("kind") .newKey("456")) .build(); session.restoreFromEntities(sessionKey, Collections.singleton(attribute)); }