InboxJobHandler { @JobInfo.JobResult int performJob(@NonNull JobInfo jobInfo) { switch (jobInfo.getAction()) { case ACTION_RICH_PUSH_USER_UPDATE: onUpdateUser(jobInfo.getExtras().opt(EXTRA_FORCEFULLY).getBoolean(false)); break; case ACTION_RICH_PUSH_MESSAGES_UPDATE: onUpdateMessages(); break; case ACTION_SYNC_MESSAGE_STATE: onSyncMessages(); break; } return JobInfo.JOB_FINISHED; } InboxJobHandler(@NonNull Context context,
                    @NonNull Inbox inbox,
                    @NonNull User user,
                    @NonNull AirshipChannel channel,
                    @NonNull AirshipRuntimeConfig runtimeConfig,
                    @NonNull PreferenceDataStore dataStore); @VisibleForTesting  InboxJobHandler(@NonNull Inbox inbox,
                    @NonNull User user,
                    @NonNull AirshipChannel channel,
                    @NonNull AirshipRuntimeConfig runtimeConfig,
                    @NonNull PreferenceDataStore dataStore,
                    @NonNull RequestFactory requestFactory,
                    @NonNull MessageCenterResolver resolver);   }
@Test public void testUserNotCreated() { user.setUser(null, null); JobInfo jobInfo = JobInfo.newBuilder() .setAction(InboxJobHandler.ACTION_RICH_PUSH_MESSAGES_UPDATE) .build(); assertEquals(JobInfo.JOB_FINISHED, jobHandler.performJob(jobInfo)); verify(inbox).onUpdateMessagesFinished(false); assertEquals(0, requests.size()); }
@Test public void testUpdateMessagesNull() { user.setUser("fakeUserId", "password"); dataStore.put(InboxJobHandler.LAST_MESSAGE_REFRESH_TIME, 300L); responses.put("https: JobInfo jobInfo = JobInfo.newBuilder() .setAction(InboxJobHandler.ACTION_RICH_PUSH_MESSAGES_UPDATE) .build(); assertEquals(JobInfo.JOB_FINISHED, jobHandler.performJob(jobInfo)); verify(inbox).onUpdateMessagesFinished(false); TestRequest testRequest = requests.get(0); assertEquals("GET", testRequest.getRequestMethod()); assertEquals("https: assertEquals(300L, testRequest.getIfModifiedSince()); assertEquals(300L, dataStore.getLong(InboxJobHandler.LAST_MESSAGE_REFRESH_TIME, 0)); }
@Test public void testUpdateMessagesNotModified() { user.setUser("fakeUserId", "password"); dataStore.put(InboxJobHandler.LAST_MESSAGE_REFRESH_TIME, 300L); responses.put("https: new Response.Builder<Void>(HttpURLConnection.HTTP_NOT_MODIFIED).build()); JobInfo jobInfo = JobInfo.newBuilder() .setAction(InboxJobHandler.ACTION_RICH_PUSH_MESSAGES_UPDATE) .build(); assertEquals(JobInfo.JOB_FINISHED, jobHandler.performJob(jobInfo)); verify(inbox).onUpdateMessagesFinished(true); TestRequest testRequest = requests.get(0); assertEquals("GET", testRequest.getRequestMethod()); assertEquals("https: assertEquals(300L, testRequest.getIfModifiedSince()); assertEquals(300L, dataStore.getLong(InboxJobHandler.LAST_MESSAGE_REFRESH_TIME, 0)); }
@Test public void testUpdateMessagesEmpty() { user.setUser("fakeUserId", "password"); when(mockChannel.getId()).thenReturn("channelID"); dataStore.put(InboxJobHandler.LAST_MESSAGE_REFRESH_TIME, 300L); responses.put("https: new Response.Builder<Void>(HttpURLConnection.HTTP_OK) .setLastModified(600L) .setResponseBody("{ \"messages\": []}") .build()); JobInfo jobInfo = JobInfo.newBuilder() .setAction(InboxJobHandler.ACTION_RICH_PUSH_MESSAGES_UPDATE) .build(); assertEquals(JobInfo.JOB_FINISHED, jobHandler.performJob(jobInfo)); verify(inbox).onUpdateMessagesFinished(true); TestRequest testRequest = requests.get(0); assertEquals("GET", testRequest.getRequestMethod()); assertEquals("https: assertEquals(300L, testRequest.getIfModifiedSince()); assertEquals("channelID", testRequest.getRequestHeaders().get("X-UA-Channel-ID")); assertEquals(600L, dataStore.getLong(InboxJobHandler.LAST_MESSAGE_REFRESH_TIME, 0)); verify(inbox).refresh(true); }
@Test public void testUpdateMessagesServerError() { user.setUser("fakeUserId", "password"); dataStore.put(InboxJobHandler.LAST_MESSAGE_REFRESH_TIME, 300L); responses.put("https: new Response.Builder<Void>(HttpURLConnection.HTTP_INTERNAL_ERROR) .setResponseBody("{ failed }") .build()); JobInfo jobInfo = JobInfo.newBuilder() .setAction(InboxJobHandler.ACTION_RICH_PUSH_MESSAGES_UPDATE) .build(); assertEquals(JobInfo.JOB_FINISHED, jobHandler.performJob(jobInfo)); verify(inbox).onUpdateMessagesFinished(false); TestRequest testRequest = requests.get(0); assertEquals("GET", testRequest.getRequestMethod()); assertEquals("https: assertEquals(300L, testRequest.getIfModifiedSince()); assertEquals(300L, dataStore.getLong(InboxJobHandler.LAST_MESSAGE_REFRESH_TIME, 0)); verify(inbox).refresh(true); }
@Test public void testCreateUserWithAmazonChannel() { runtimeConfig.setPlatform(UAirship.AMAZON_PLATFORM); when(mockChannel.getId()).thenReturn("ba7beaaf-b6e9-416c-a1f9-a6ff5a81f588"); responses.put("https: new Response.Builder<Void>(HttpURLConnection.HTTP_CREATED) .setResponseBody("{ \"user_id\": \"someUserId\", \"password\": \"someUserToken\" }") .build()); JobInfo jobInfo = JobInfo.newBuilder() .setAction(InboxJobHandler.ACTION_RICH_PUSH_USER_UPDATE) .build(); assertEquals(JobInfo.JOB_FINISHED, jobHandler.performJob(jobInfo)); assertTrue(userListener.lastUpdateUserResult); assertEquals("someUserId", user.getId()); assertEquals("someUserToken", user.getPassword()); assertEquals(requests.size(), 1); assertEquals("POST", requests.get(0).getRequestMethod()); assertEquals("https: assertEquals("{\"amazon_channels\":[\"ba7beaaf-b6e9-416c-a1f9-a6ff5a81f588\"]}", requests.get(0).getRequestBody()); assertEquals("application/vnd.urbanairship+json; version=3;", requests.get(0).getRequestHeaders().get("Accept")); }
@Test public void testCreateUserWithAndroidChannel() { runtimeConfig.setPlatform(UAirship.ANDROID_PLATFORM); when(mockChannel.getId()).thenReturn("ba7beaaf-b6e9-416c-a1f9-a6ff5a81f588"); responses.put("https: new Response.Builder<Void>(HttpURLConnection.HTTP_CREATED) .setResponseBody("{ \"user_id\": \"someUserId\", \"password\": \"someUserToken\" }") .build()); JobInfo jobInfo = JobInfo.newBuilder() .setAction(InboxJobHandler.ACTION_RICH_PUSH_USER_UPDATE) .build(); assertEquals(JobInfo.JOB_FINISHED, jobHandler.performJob(jobInfo)); assertTrue(userListener.lastUpdateUserResult); assertEquals("someUserId", user.getId()); assertEquals("someUserToken", user.getPassword()); assertEquals(requests.size(), 1); assertEquals("POST", requests.get(0).getRequestMethod()); assertEquals("https: assertEquals("{\"android_channels\":[\"ba7beaaf-b6e9-416c-a1f9-a6ff5a81f588\"]}", requests.get(0).getRequestBody()); assertEquals("application/vnd.urbanairship+json; version=3;", requests.get(0).getRequestHeaders().get("Accept")); }
@Test public void testCreateUserNoChannel() { when(mockChannel.getId()).thenReturn(null); responses.put("https: new Response.Builder<Void>(HttpURLConnection.HTTP_CREATED) .setResponseBody("{ \"user_id\": \"someUserId\", \"password\": \"someUserToken\" }") .build()); JobInfo jobInfo = JobInfo.newBuilder() .setAction(InboxJobHandler.ACTION_RICH_PUSH_USER_UPDATE) .build(); assertEquals(JobInfo.JOB_FINISHED, jobHandler.performJob(jobInfo)); assertFalse(userListener.lastUpdateUserResult); assertNull(user.getId()); assertNull(user.getPassword()); }
@Test public void testCreateUserFailed() { when(mockChannel.getId()).thenReturn("ba7beaaf-b6e9-416c-a1f9-a6ff5a81f588"); responses.put("https: new Response.Builder<Void>(HttpURLConnection.HTTP_INTERNAL_ERROR).build()); JobInfo jobInfo = JobInfo.newBuilder() .setAction(InboxJobHandler.ACTION_RICH_PUSH_USER_UPDATE) .build(); assertEquals(JobInfo.JOB_FINISHED, jobHandler.performJob(jobInfo)); assertFalse(userListener.lastUpdateUserResult); assertNull(user.getId()); assertNull(user.getPassword()); }
@Test public void testUpdateUserAmazon() { runtimeConfig.setPlatform(UAirship.AMAZON_PLATFORM); when(mockChannel.getId()).thenReturn("ba7beaaf-b6e9-416c-a1f9-a6ff5a81f588"); user.setUser("someUserId", "someUserToken"); responses.put("https: new Response.Builder<Void>(HttpURLConnection.HTTP_OK) .setResponseBody("{ \"ok\" }") .build()); JobInfo jobInfo = JobInfo.newBuilder() .setAction(InboxJobHandler.ACTION_RICH_PUSH_USER_UPDATE) .build(); assertEquals(JobInfo.JOB_FINISHED, jobHandler.performJob(jobInfo)); assertTrue(userListener.lastUpdateUserResult); assertEquals(requests.size(), 1); assertEquals("POST", requests.get(0).getRequestMethod()); assertEquals("https: assertEquals("{\"amazon_channels\":{\"add\":[\"ba7beaaf-b6e9-416c-a1f9-a6ff5a81f588\"]}}", requests.get(0).getRequestBody()); assertEquals("application/vnd.urbanairship+json; version=3;", requests.get(0).getRequestHeaders().get("Accept")); }
@Test public void testUpdateUserAndroid() { runtimeConfig.setPlatform(UAirship.ANDROID_PLATFORM); when(mockChannel.getId()).thenReturn("ba7beaaf-b6e9-416c-a1f9-a6ff5a81f588"); user.setUser("someUserId", "someUserToken"); responses.put("https: new Response.Builder<Void>(HttpURLConnection.HTTP_OK) .setResponseBody("{ \"ok\" }") .build()); JobInfo jobInfo = JobInfo.newBuilder() .setAction(InboxJobHandler.ACTION_RICH_PUSH_USER_UPDATE) .build(); assertEquals(JobInfo.JOB_FINISHED, jobHandler.performJob(jobInfo)); assertTrue(userListener.lastUpdateUserResult); assertEquals(requests.size(), 1); assertEquals("POST", requests.get(0).getRequestMethod()); assertEquals("https: assertEquals("{\"android_channels\":{\"add\":[\"ba7beaaf-b6e9-416c-a1f9-a6ff5a81f588\"]}}", requests.get(0).getRequestBody()); assertEquals("application/vnd.urbanairship+json; version=3;", requests.get(0).getRequestHeaders().get("Accept")); }
@Test public void testUpdateUserNoChannel() { user.setUser("someUserId", "someUserToken"); when(mockChannel.getId()).thenReturn(null); JobInfo jobInfo = JobInfo.newBuilder() .setAction(InboxJobHandler.ACTION_RICH_PUSH_USER_UPDATE) .build(); assertEquals(JobInfo.JOB_FINISHED, jobHandler.performJob(jobInfo)); assertFalse(userListener.lastUpdateUserResult); }
@Test public void testUpdateUserRequestFail() { user.setUser("someUserId", "someUserToken"); responses.put("https: new Response.Builder<Void>(HttpURLConnection.HTTP_INTERNAL_ERROR).build()); JobInfo jobInfo = JobInfo.newBuilder() .setAction(InboxJobHandler.ACTION_RICH_PUSH_USER_UPDATE) .build(); assertEquals(JobInfo.JOB_FINISHED, jobHandler.performJob(jobInfo)); assertFalse(userListener.lastUpdateUserResult); }