InboxJobHandler { private boolean updateMessages() { Logger.info("Refreshing inbox messages."); URL url = getUserApiUrl(runtimeConfig.getUrlConfig(), user.getId(), MESSAGES_PATH); if (url == null) { Logger.debug("User URL null, unable to update message."); return false; } Logger.verbose("InboxJobHandler - Fetching inbox messages."); Response response = requestFactory.createRequest("GET", url) .setCredentials(user.getId(), user.getPassword()) .setHeader("Accept", "application/vnd.urbanairship+json; version=3;") .setHeader(CHANNEL_ID_HEADER, channel.getId()) .setIfModifiedSince(dataStore.getLong(LAST_MESSAGE_REFRESH_TIME, 0)) .safeExecute(); Logger.verbose("InboxJobHandler - Fetch inbox messages response: %s", response); int status = response == null ? -1 : response.getStatus(); if (status == HttpURLConnection.HTTP_NOT_MODIFIED) { Logger.debug("Inbox messages already up-to-date. "); return true; } if (status == HttpURLConnection.HTTP_OK) { JsonList serverMessages = null; try { JsonMap responseJson = JsonValue.parseString(response.getResponseBody()).getMap(); if (responseJson != null) { serverMessages = responseJson.opt("messages").getList(); } } catch (JsonException e) { Logger.error("Failed to update inbox. Unable to parse response body: %s", response.getResponseBody()); return false; } if (serverMessages == null) { Logger.debug("Inbox message list is empty."); } else { Logger.info("Received %s inbox messages.", serverMessages.size()); updateInbox(serverMessages); dataStore.put(LAST_MESSAGE_REFRESH_TIME, response.getLastModifiedTime()); } return true; } Logger.debug("Unable to update inbox messages."); return false; } 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 testUpdateMessages() { 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\": [ {\"message_id\": \"some_mesg_id\"," + "\"message_url\": \"https: "\"message_body_url\": \"https: "\"message_read_url\": \"https: "\"unread\": true, \"message_sent\": \"2010-09-05 12:13 -0000\"," + "\"title\": \"Message title\", \"extra\": { \"some_key\": \"some_value\"}," + "\"content_type\": \"text/html\", \"content_size\": \"128\"}]}") .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); }