use of com.android.example.devsummit.archdemo.api.FeedResponse in project dev-summit-architecture-demo by yigit.
the class FetchFeedJobTest method testFailure500.
@Test
public void testFailure500() throws Throwable {
FetchFeedJob job = new FetchFeedJob(BaseJob.BACKGROUND, null);
when(mApiService.feed(0L)).thenReturn(createCall(500, new FeedResponse()));
job.inject(getTestComponent());
Throwable exception = safeRun(job);
assertThat(exception, notNullValue());
assertThat(exception, instanceOf(NetworkException.class));
RetryConstraint retryConstraint = job.shouldReRunOnThrowable(exception, 1, 10);
assertThat(retryConstraint.shouldRetry(), is(true));
}
use of com.android.example.devsummit.archdemo.api.FeedResponse in project dev-summit-architecture-demo by yigit.
the class FetchFeedJob method onRun.
@Override
public void onRun() throws Throwable {
long timestamp = mFeedModel.getLatestTimestamp(mUserId);
final Call<FeedResponse> feed;
if (mUserId == null) {
feed = mApiService.feed(timestamp);
} else {
feed = mApiService.userFeed(mUserId, timestamp);
}
Response<FeedResponse> response = feed.execute();
if (response.isSuccess()) {
Post oldest = handleResponse(response.body());
mEventBus.post(new FetchedFeedEvent(true, mUserId, oldest));
} else {
throw new NetworkException(response.code());
}
}
use of com.android.example.devsummit.archdemo.api.FeedResponse in project dev-summit-architecture-demo by yigit.
the class FetchFeedJobTest method testFailure404.
@Test
public void testFailure404() throws Throwable {
FetchFeedJob job = new FetchFeedJob(BaseJob.BACKGROUND, null);
when(mApiService.feed(0L)).thenReturn(createCall(404, new FeedResponse()));
job.inject(getTestComponent());
Throwable exception = safeRun(job);
assertThat(exception, notNullValue());
assertThat(exception, instanceOf(NetworkException.class));
RetryConstraint retryConstraint = job.shouldReRunOnThrowable(exception, 1, 10);
assertThat(retryConstraint.shouldRetry(), is(false));
}
use of com.android.example.devsummit.archdemo.api.FeedResponse in project dev-summit-architecture-demo by yigit.
the class FetchFeedJobTest method testRun.
@Test
public void testRun() throws Throwable {
FeedResponse response = new FeedResponse();
List<User> users = Arrays.asList(createDummyUser(), createDummyUser());
List<Post> posts = Arrays.asList(createDummyPost(users.get(0).getId()), createDummyPost(users.get(0).getId()), createDummyPost(users.get(1).getId()));
posts.get(0).setCreated(10);
posts.get(1).setCreated(11);
posts.get(2).setCreated(12);
response.setPosts(posts);
response.setUsers(users);
FetchFeedJob job = new FetchFeedJob(BaseJob.BACKGROUND, 101L);
when(mApiService.userFeed(101L, 0L)).thenReturn(createCall(response));
job.inject(getTestComponent());
job.onRun();
FetchedFeedEvent event = mLoggingBus.findEvent(FetchedFeedEvent.class);
assertThat(event, notNullValue());
assertThat(event.isSuccess(), is(true));
assertThat(event.getUserId(), is(101L));
// TODO Better to mock the models and verify the save calls so that this test wont
// be effected by model failures.
assertThat(mUserModel.load(users.get(0).getId()), notNullValue());
assertThat(mUserModel.load(users.get(1).getId()), notNullValue());
assertThat(mPostModel.load(posts.get(0).getId()), notNullValue());
assertThat(mPostModel.load(posts.get(1).getId()), notNullValue());
assertThat(mPostModel.load(posts.get(2).getId()), notNullValue());
assertThat(mFeedModel.getLatestTimestamp(101L), is(12L));
}
Aggregations