RemoteDataApiClient { @Nullable Response fetchRemoteData(@Nullable String lastModified, @NonNull Locale locale) { URL url = getRemoteDataURL(locale); if (url == null) { Logger.debug("Remote Data URL null. Unable to update tagGroups."); return null; } Request request = requestFactory.createRequest("GET", url) .setCredentials(runtimeConfig.getConfigOptions().appKey, runtimeConfig.getConfigOptions().appSecret); if (lastModified != null) { request.setHeader("If-Modified-Since", lastModified); } return request.safeExecute(); } RemoteDataApiClient(@NonNull AirshipRuntimeConfig runtimeConfig,
                        @NonNull PushProviders pushProviders); @VisibleForTesting  RemoteDataApiClient(@NonNull AirshipRuntimeConfig runtimeConfig,
                        @NonNull PushProviders pushProviders,
                        @NonNull RequestFactory requestFactory);   }
@Test public void testFetchRemoteDataRequestSuccess() { String responseTimestamp = DateUtils.createIso8601TimeStamp(System.currentTimeMillis()); Map<String, List<String>> headers = new HashMap<>(); headers.put("Last-Modified", Collections.singletonList(responseTimestamp)); JsonMap map = JsonMap.newBuilder().put("foo", "bar").build(); JsonMap payload = JsonMap.newBuilder().put("type", "test").put("timestamp", responseTimestamp).put("data", map).build(); JsonList list = new JsonList(Collections.singletonList(payload.toJsonValue())); testRequest.response = new Response.Builder<Void>(HttpURLConnection.HTTP_OK) .setResponseHeaders(headers) .setResponseBody(list.toString()) .build(); String requestTimestamp = DateUtils.createIso8601TimeStamp(0); Response response = client.fetchRemoteData(requestTimestamp, new Locale("en")); assertEquals("Headers should contain timestamp", testRequest.getRequestHeaders().get("If-Modified-Since"), requestTimestamp); assertNotNull("Response should not be null", response); assertEquals("Response status should be 200", HttpURLConnection.HTTP_OK, response.getStatus()); assertEquals("Response should be the JSON list", response.getResponseBody(), list.toString()); assertEquals("Last-Modified should match with timestamp", responseTimestamp, response.getResponseHeader("Last-Modified")); }
@Test public void testSdkVersion() { client.fetchRemoteData(DateUtils.createIso8601TimeStamp(System.currentTimeMillis()), new Locale("en")); Uri uri = Uri.parse(testRequest.getURL().toString()); assertEquals(uri.getQueryParameter("sdk_version"), UAirship.getVersion()); }
@Test public void testPushProviders() { availableProviders.add(new TestPushProvider(PushProvider.FCM_DELIVERY_TYPE)); availableProviders.add(new TestPushProvider(PushProvider.FCM_DELIVERY_TYPE)); availableProviders.add(new TestPushProvider(PushProvider.ADM_DELIVERY_TYPE)); client.fetchRemoteData(DateUtils.createIso8601TimeStamp(System.currentTimeMillis()), new Locale("en")); Uri uri = Uri.parse(testRequest.getURL().toString()); assertEquals(uri.getQueryParameter("push_providers"), "fcm,adm"); }
@Test public void testEmptyPushProviders() { client.fetchRemoteData(DateUtils.createIso8601TimeStamp(System.currentTimeMillis()), new Locale("en")); Uri uri = Uri.parse(testRequest.getURL().toString()); assertNull(uri.getQueryParameter("push_providers")); }
@Test public void testManufacturer() { ShadowBuild.setManufacturer("huawei"); client.fetchRemoteData(DateUtils.createIso8601TimeStamp(System.currentTimeMillis()), new Locale("en")); Uri uri = Uri.parse(testRequest.getURL().toString()); assertEquals(uri.getQueryParameter("manufacturer"), "huawei"); }
@Test public void testManufacturerNotIncluded() { ShadowBuild.setManufacturer("google"); client.fetchRemoteData(DateUtils.createIso8601TimeStamp(System.currentTimeMillis()), new Locale("en")); Uri uri = Uri.parse(testRequest.getURL().toString()); assertNull(uri.getQueryParameter("manufacturer")); }
@Test public void testLocale() { Locale locale = new Locale("en", "US"); client.fetchRemoteData(DateUtils.createIso8601TimeStamp(System.currentTimeMillis()), locale); Uri uri = Uri.parse(testRequest.getURL().toString()); assertEquals(uri.getQueryParameter("language"), "en"); assertEquals(uri.getQueryParameter("country"), "US"); }
@Test public void testLocaleMissingCountry() { client.fetchRemoteData(DateUtils.createIso8601TimeStamp(System.currentTimeMillis()), new Locale("de")); Uri uri = Uri.parse(testRequest.getURL().toString()); assertEquals(uri.getQueryParameter("language"), "de"); assertNull(uri.getQueryParameter("country")); }
@Test public void testLocaleMissingLanguage() { Locale locale = new Locale("", "US"); client.fetchRemoteData(DateUtils.createIso8601TimeStamp(System.currentTimeMillis()), locale); Uri uri = Uri.parse(testRequest.getURL().toString()); assertNull(uri.getQueryParameter("language")); assertEquals(uri.getQueryParameter("country"), "US"); }
@Test public void testFetchRemoteDataRequestNoTimestamp() { String responseTimestamp = DateUtils.createIso8601TimeStamp(System.currentTimeMillis()); Map<String, List<String>> headers = new HashMap<>(); headers.put("Last-Modified", Collections.singletonList(responseTimestamp)); JsonMap map = JsonMap.newBuilder().put("foo", "bar").build(); JsonMap payload = JsonMap.newBuilder().put("type", "test").put("timestamp", responseTimestamp).put("data", map).build(); JsonList list = new JsonList(Collections.singletonList(payload.toJsonValue())); testRequest.response = new Response.Builder<Void>(HttpURLConnection.HTTP_OK) .setResponseHeaders(headers) .setResponseBody(list.toString()) .build(); Response response = client.fetchRemoteData(null, new Locale("en")); assertNull("Headers should not contain timestamp", testRequest.getRequestHeaders().get("If-Modified-Since")); assertNotNull("Response should not be null", response); assertEquals("Response status should be 200", HttpURLConnection.HTTP_OK, response.getStatus()); assertEquals("Response should be the JSON list", response.getResponseBody(), list.toString()); assertEquals("Last-Modified should match with timestamp", responseTimestamp, response.getResponseHeader("Last-Modified")); }
@Test public void testFetchRemoteDataRequestFailure() { Map<String, List<String>> headers = new HashMap<>(); testRequest.response = new Response.Builder<Void>(HttpURLConnection.HTTP_NOT_IMPLEMENTED) .setResponseHeaders(headers) .build(); String requestTimestamp = DateUtils.createIso8601TimeStamp(0); Response response = client.fetchRemoteData(requestTimestamp, new Locale("en")); assertNotNull("Response should not be null", response); assertEquals("Response status should be 501", HttpURLConnection.HTTP_NOT_IMPLEMENTED, response.getStatus()); }