/** */
@Test
public void deferredSaveAndDeleteProcessedAtEndOfRequest() throws Exception {
Trivial triv = new Trivial(123L, "foo", 5);
try (Closeable root = TestObjectifyService.begin()) {
ofy().defer().save().entity(triv);
// Can load out of session
assertThat(ofy().load().entity(triv).now(), is(triv));
// But not the datastore
try {
ds().get(null, Key.create(triv).getRaw());
assert false : "Entity should not have been saved yet";
} catch (EntityNotFoundException e) {
// correct
}
}
try (Closeable root = TestObjectifyService.begin()) {
Trivial loaded = ofy().load().entity(triv).now();
assertThat(loaded, equalTo(triv));
}
try (Closeable root = TestObjectifyService.begin()) {
ofy().defer().delete().entity(triv);
// Deleted in session
assertThat(ofy().load().entity(triv).now(), nullValue());
// But not datastore
try {
ds().get(null, Key.create(triv).getRaw());
} catch (EntityNotFoundException e) {
assert false : "Entity should not have been deleted yet";
}
}
try (Closeable root = TestObjectifyService.begin()) {
Trivial loaded = ofy().load().entity(triv).now();
assertThat(loaded, nullValue());
}
}