TagGroupLookupApiClient { @Nullable TagGroupResponse lookupTagGroups(String channelId, Map<String, Set<String>> requestedTags, @Nullable TagGroupResponse cachedResponse) { URL url = runtimeConfig.getUrlConfig() .deviceUrl() .appendEncodedPath(CHANNEL_TAG_LOOKUP_PATH) .build(); if (url == null) { Logger.debug("Tag Group URL is null, unable to fetch tag groups."); return null; } String deviceType = runtimeConfig.getPlatform() == UAirship.AMAZON_PLATFORM ? AMAZON_PLATFORM : ANDROID_PLATFORM; JsonMap payload = JsonMap.newBuilder() .put(CHANNEL_ID_KEY, channelId) .put(DEVICE_TYPE_KEY, deviceType) .putOpt(TAG_GROUPS_KEY, requestedTags) .put(IF_MODIFIED_SINCE_KEY, cachedResponse != null ? cachedResponse.lastModifiedTime : null) .build(); String tagPayload = payload.toString(); Logger.debug("Looking up tags with payload: %s", tagPayload); Response<Void> response; try { response = requestFactory.createRequest() .setOperation("POST", url) .setCredentials(runtimeConfig.getConfigOptions().appKey, runtimeConfig.getConfigOptions().appSecret) .setRequestBody(tagPayload, "application/json") .setHeader("Accept", "application/vnd.urbanairship+json; version=3;") .execute(); } catch (RequestException e) { Logger.error(e, "Failed to refresh the cache."); return null; } TagGroupResponse parsedResponse; try { parsedResponse = TagGroupResponse.fromResponse(response); } catch (JsonException e) { Logger.error(e, "Failed to parse tag group response."); return null; } if (parsedResponse.status == HttpsURLConnection.HTTP_OK) { if (cachedResponse != null && parsedResponse.lastModifiedTime != null && UAStringUtil.equals(parsedResponse.lastModifiedTime, cachedResponse.lastModifiedTime)) { return cachedResponse; } } return parsedResponse; } TagGroupLookupApiClient(@NonNull AirshipRuntimeConfig runtimeConfig); @VisibleForTesting  TagGroupLookupApiClient(@NonNull AirshipRuntimeConfig runtimeConfig,
                            @NonNull RequestFactory requestFactory);   }
@Test public void lookupTagsAndroid() throws JsonException { TagGroupResponse response = client.lookupTagGroups("some-channel", requestTags, null); assertEquals(200, response.status); assertEquals("lastModifiedTime", response.lastModifiedTime); assertEquals(responseTags, response.tags); verifyRequest(UAirship.ANDROID_PLATFORM, "some-channel", requestTags, null); }
@Test public void lookupTagsAmazon() throws JsonException { runtimeConfig.setPlatform(UAirship.AMAZON_PLATFORM); TagGroupResponse response = client.lookupTagGroups("some-channel", requestTags, null); assertEquals(200, response.status); assertEquals("lastModifiedTime", response.lastModifiedTime); assertEquals(responseTags, response.tags); verifyRequest(UAirship.AMAZON_PLATFORM, "some-channel", requestTags, null); }
@Test public void testFailedResponse() { testRequest.responseStatus = 400; TagGroupResponse response = client.lookupTagGroups("some-channel", requestTags, null); assertEquals(400, response.status); assertNull(response.lastModifiedTime); assertNull(response.tags); }
@Test public void testCachedResponse() throws JsonException { TagGroupResponse response = client.lookupTagGroups("some-channel", requestTags, null); verifyRequest(UAirship.ANDROID_PLATFORM, "some-channel", requestTags, null); testRequest.responseBody = JsonMap.newBuilder() .put("last_modified", "lastModifiedTime") .build().toString(); testRequest.responseStatus = HttpURLConnection.HTTP_OK; TagGroupResponse cachedResponse = client.lookupTagGroups("some-channel", requestTags, response); verifyRequest(UAirship.ANDROID_PLATFORM, "some-channel", requestTags, response.lastModifiedTime); assertEquals(cachedResponse, response); }
@Test public void testCachedResponseNewData() { TagGroupResponse response = client.lookupTagGroups("some-channel", requestTags, null); testRequest.responseBody = JsonMap.newBuilder() .putOpt("tag_groups", responseTags) .put("last_modified", "newDate") .build().toString(); testRequest.responseStatus = HttpURLConnection.HTTP_OK; TagGroupResponse newResponse = client.lookupTagGroups("some-channel", requestTags, response); assertNotEquals(newResponse, response); assertEquals("newDate", newResponse.lastModifiedTime); }