Search in sources :

Example 1 with FeedResponse

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));
}
Also used : RetryConstraint(com.path.android.jobqueue.RetryConstraint) FeedResponse(com.android.example.devsummit.archdemo.api.FeedResponse) NetworkException(com.android.example.devsummit.archdemo.job.NetworkException) BaseTest(com.android.example.devsummit.archdemo.BaseTest) Test(org.junit.Test)

Example 2 with FeedResponse

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());
    }
}
Also used : Post(com.android.example.devsummit.archdemo.vo.Post) FeedResponse(com.android.example.devsummit.archdemo.api.FeedResponse) FetchedFeedEvent(com.android.example.devsummit.archdemo.event.feed.FetchedFeedEvent) NetworkException(com.android.example.devsummit.archdemo.job.NetworkException)

Example 3 with FeedResponse

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));
}
Also used : RetryConstraint(com.path.android.jobqueue.RetryConstraint) FeedResponse(com.android.example.devsummit.archdemo.api.FeedResponse) NetworkException(com.android.example.devsummit.archdemo.job.NetworkException) BaseTest(com.android.example.devsummit.archdemo.BaseTest) Test(org.junit.Test)

Example 4 with FeedResponse

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));
}
Also used : TestUtil.createDummyUser(com.android.example.devsummit.archdemo.util.TestUtil.createDummyUser) User(com.android.example.devsummit.archdemo.vo.User) Post(com.android.example.devsummit.archdemo.vo.Post) TestUtil.createDummyPost(com.android.example.devsummit.archdemo.util.TestUtil.createDummyPost) FeedResponse(com.android.example.devsummit.archdemo.api.FeedResponse) FetchedFeedEvent(com.android.example.devsummit.archdemo.event.feed.FetchedFeedEvent) BaseTest(com.android.example.devsummit.archdemo.BaseTest) Test(org.junit.Test)

Aggregations

FeedResponse (com.android.example.devsummit.archdemo.api.FeedResponse)4 BaseTest (com.android.example.devsummit.archdemo.BaseTest)3 NetworkException (com.android.example.devsummit.archdemo.job.NetworkException)3 Test (org.junit.Test)3 FetchedFeedEvent (com.android.example.devsummit.archdemo.event.feed.FetchedFeedEvent)2 Post (com.android.example.devsummit.archdemo.vo.Post)2 RetryConstraint (com.path.android.jobqueue.RetryConstraint)2 TestUtil.createDummyPost (com.android.example.devsummit.archdemo.util.TestUtil.createDummyPost)1 TestUtil.createDummyUser (com.android.example.devsummit.archdemo.util.TestUtil.createDummyUser)1 User (com.android.example.devsummit.archdemo.vo.User)1