}
@Test
public void should_return_entities_for_typed_query_async() throws Exception {
CompleteBean paul = builder().randomId().name("Paul").age(35L)
.addFriends("foo", "bar").addFollowers("George", "Jack").addPreference(1, "FR")
.addPreference(2, "Paris").addPreference(3, "75014").buid();
CompleteBean john = builder().randomId().name("John").age(34L)
.addFriends("qux", "twix").addFollowers("Isaac", "Lara").addPreference(1, "US")
.addPreference(2, "NewYork").buid();
manager.insert(paul);
manager.insert(john);
final CountDownLatch latch = new CountDownLatch(2);
final AtomicReference<Object> successSpy1 = new AtomicReference<>();
final AtomicReference<Object> successSpy2 = new AtomicReference<>();
FutureCallback<Object> successCallBack1 = new FutureCallback<Object>() {
@Override
public void onSuccess(Object result) {
successSpy1.getAndSet(result);
latch.countDown();
}
@Override
public void onFailure(Throwable t) {
latch.countDown();
}
};
FutureCallback<Object> successCallBack2 = new FutureCallback<Object>() {
@Override
public void onSuccess(Object result) {
successSpy2.getAndSet(result);
latch.countDown();
}
@Override
public void onFailure(Throwable t) {
latch.countDown();
}
};
final RegularStatement selectStar = select().from("CompleteBean").where(eq("id", bindMarker("id")));
final List<CompleteBean> list1 = manager.typedQuery(CompleteBean.class, selectStar, paul.getId()).asyncGet(successCallBack1).get();
final CompleteBean foundJohn = manager.typedQuery(CompleteBean.class, selectStar, john.getId()).asyncGetFirst(successCallBack2).get();
assertThat(list1).hasSize(1);
CompleteBean foundPaul = list1.get(0);
Factory factory1 = (Factory) foundPaul;
@SuppressWarnings("unchecked")
ProxyInterceptor<CompleteBean> interceptor1 = (ProxyInterceptor<CompleteBean>) factory1.getCallback(0);
CompleteBean realPaul = (CompleteBean) interceptor1.getTarget();
assertThat(realPaul.getLabel()).isNull();
assertThat(realPaul.getWelcomeTweet()).isNull();
assertThat(realPaul.getName()).isEqualTo(paul.getName());
assertThat(realPaul.getAge()).isEqualTo(paul.getAge());
assertThat(realPaul.getFriends()).containsAll(paul.getFriends());
assertThat(realPaul.getFollowers()).containsAll(paul.getFollowers());
assertThat(realPaul.getPreferences().get(1)).isEqualTo("FR");
assertThat(realPaul.getPreferences().get(2)).isEqualTo("Paris");
assertThat(realPaul.getPreferences().get(3)).isEqualTo("75014");
Factory factory2 = (Factory) foundJohn;
@SuppressWarnings("unchecked")
ProxyInterceptor<CompleteBean> interceptor2 = (ProxyInterceptor<CompleteBean>) factory2.getCallback(0);
CompleteBean realJohn = (CompleteBean) interceptor2.getTarget();
assertThat(realJohn.getLabel()).isNull();
assertThat(realJohn.getWelcomeTweet()).isNull();
assertThat(realJohn.getName()).isEqualTo(john.getName());
assertThat(realJohn.getAge()).isEqualTo(john.getAge());
assertThat(realJohn.getFriends()).containsAll(john.getFriends());
assertThat(realJohn.getFollowers()).containsAll(john.getFollowers());
assertThat(realJohn.getPreferences().get(1)).isEqualTo("US");
assertThat(realJohn.getPreferences().get(2)).isEqualTo("NewYork");
latch.await();
Thread.sleep(100);
assertThat(successSpy1.get()).isNotNull().isInstanceOf(List.class);
assertThat(successSpy2.get()).isNotNull().isInstanceOf(CompleteBean.class).isNotInstanceOf(Factory.class);