}
@Test
public void should_batch_several_entities_async() throws Exception {
CompleteBean bean = CompleteBeanTestBuilder.builder().randomId().name("name").buid();
Tweet tweet1 = TweetTestBuilder.tweet().randomId().content("tweet1").buid();
Tweet tweet2 = TweetTestBuilder.tweet().randomId().content("tweet2").buid();
final CountDownLatch latch = new CountDownLatch(2);
final AtomicReference<Object> successSpy = new AtomicReference<>();
final AtomicReference<Throwable> exceptionSpy = new AtomicReference<>();
FutureCallback<Object> successCallBack = new FutureCallback<Object>() {
@Override
public void onSuccess(Object result) {
successSpy.getAndSet(result);
latch.countDown();
}
@Override
public void onFailure(Throwable t) {
latch.countDown();
}
};
FutureCallback<Object> errorCallBack = new FutureCallback<Object>() {
@Override
public void onSuccess(Object result) {
latch.countDown();
}
@Override
public void onFailure(Throwable t) {
exceptionSpy.getAndSet(t);
latch.countDown();
}
};
// Start batch
AsyncBatch batch = manager.createBatch();
batch.startBatch();
batch.insert(bean);
batch.insert(tweet1);
batch.insert(tweet2);
batch.insert(user);
CompleteBean foundBean = manager.find(CompleteBean.class, bean.getId()).getImmediately();
Tweet foundTweet1 = manager.find(Tweet.class, tweet1.getId()).getImmediately();
Tweet foundTweet2 = manager.find(Tweet.class, tweet2.getId()).getImmediately();
User foundUser = manager.find(User.class, user.getId()).getImmediately();
assertThat(foundBean).isNull();
assertThat(foundTweet1).isNull();
assertThat(foundTweet2).isNull();
assertThat(foundUser).isNull();
// Flush
batch.endBatch(successCallBack, errorCallBack);
latch.await();
final ResultSet resultSet = manager.getNativeSession().execute("SELECT id,favoriteTweets,followers,friends,age_in_years,name,welcomeTweet,label,preferences FROM CompleteBean WHERE id=:id", bean.getId());
assertThat(resultSet.all()).hasSize(1);
foundBean = manager.find(CompleteBean.class, bean.getId()).getImmediately();
foundTweet1 = manager.find(Tweet.class, tweet1.getId()).getImmediately();
foundTweet2 = manager.find(Tweet.class, tweet2.getId()).getImmediately();
foundUser = manager.find(User.class, user.getId()).getImmediately();
assertThat(foundBean.getName()).isEqualTo("name");
assertThat(foundTweet1.getContent()).isEqualTo("tweet1");
assertThat(foundTweet2.getContent()).isEqualTo("tweet2");
assertThat(foundUser.getFirstname()).isEqualTo("fn");
assertThat(foundUser.getLastname()).isEqualTo("ln");
assertThatBatchContextHasBeenReset(batch);
assertThat(successSpy.get()).isNotNull().isSameAs(Empty.INSTANCE);