DeferredScheduleClient { public Response<Result> performRequest(@NonNull URL url, @NonNull String channelId, @Nullable TriggerContext triggerContext, @NonNull List<TagGroupsMutation> tagOverrides, @NonNull List<AttributeMutation> attributeOverrides) throws RequestException, AuthException { String token = authManager.getToken(); JsonMap.Builder requestBodyBuilder = JsonMap.newBuilder() .put(PLATFORM_KEY, runtimeConfig.getPlatform() == UAirship.AMAZON_PLATFORM ? PLATFORM_AMAZON : PLATFORM_ANDROID) .put(CHANNEL_ID_KEY, channelId); if (triggerContext != null) { requestBodyBuilder.put(TRIGGER_KEY, JsonMap.newBuilder() .put(TRIGGER_TYPE_KEY, triggerContext.getTrigger().getTriggerName()) .put(TRIGGER_GOAL_KEY, triggerContext.getTrigger().getGoal()) .put(TRIGGER_EVENT_KEY, triggerContext.getEvent()) .build()); } if (!tagOverrides.isEmpty()) { requestBodyBuilder.put(TAG_OVERRIDES_KEY, JsonValue.wrapOpt(tagOverrides)); } if (!attributeOverrides.isEmpty()) { requestBodyBuilder.put(ATTRIBUTE_OVERRIDES_KEY, JsonValue.wrapOpt(attributeOverrides)); } JsonMap requestBody = requestBodyBuilder.build(); Response<Result> response = performRequest(url, token, requestBody); if (response.getStatus() == HttpsURLConnection.HTTP_UNAUTHORIZED) { authManager.tokenExpired(token); return performRequest(url, authManager.getToken(), requestBody); } else { return response; } } DeferredScheduleClient(@NonNull AirshipRuntimeConfig runtimeConfig, @NonNull AuthManager authManager); @VisibleForTesting  DeferredScheduleClient(@NonNull AirshipRuntimeConfig runtimeConfig,
                           @NonNull AuthManager authManager,
                           @NonNull RequestFactory requestFactory); Response<Result> performRequest(@NonNull URL url,
                                           @NonNull String channelId,
                                           @Nullable TriggerContext triggerContext,
                                           @NonNull List<TagGroupsMutation> tagOverrides,
                                           @NonNull List<AttributeMutation> attributeOverrides);  }
@Test public void testAmazonRequest() throws AuthException, MalformedURLException, RequestException { runtimeConfig.setPlatform(UAirship.AMAZON_PLATFORM); when(mockAuthManager.getToken()).thenReturn("some_token"); URL url = new URL("https: client.performRequest(url, "channel", null, EMPTY_TAGS, EMPTY_ATTRIBUTES); assertEquals(url, testRequest.getUrl()); assertEquals("POST", testRequest.getRequestMethod()); assertEquals("Bearer some_token", testRequest.getRequestHeaders().get("Authorization")); JsonMap expected = JsonMap.newBuilder() .put("platform", "amazon") .put("channel_id", "channel") .build(); assertEquals(expected.toString(), testRequest.getRequestBody()); }
@Test public void testTriggeringContext() throws AuthException, MalformedURLException, RequestException { when(mockAuthManager.getToken()).thenReturn("some token"); CustomEvent event = CustomEvent.newBuilder("some event").build(); TriggerContext triggerContext = new TriggerContext(Triggers.newCustomEventTriggerBuilder().build(), event.toJsonValue()); client.performRequest(new URL("https: JsonMap expected = JsonMap.newBuilder() .put("platform", "android") .put("channel_id", "channel") .put("trigger", JsonMap.newBuilder() .put("goal", 1) .put("type", "custom_event_count") .put("event", event.toJsonValue()) .build()) .build(); assertEquals(expected.toString(), testRequest.getRequestBody()); }
@Test public void testTagOverrides() throws AuthException, MalformedURLException, RequestException { when(mockAuthManager.getToken()).thenReturn("some token"); List<TagGroupsMutation> tagOverrides = new ArrayList<>(); tagOverrides.add(TagGroupsMutation.newRemoveTagsMutation("foo", tagSet("one", "two"))); tagOverrides.add(TagGroupsMutation.newSetTagsMutation("bar", tagSet("a"))); client.performRequest(new URL("https: JsonMap expected = JsonMap.newBuilder() .put("platform", "android") .put("channel_id", "channel") .put("tag_overrides", JsonValue.wrapOpt(tagOverrides)) .build(); assertEquals(expected.toString(), testRequest.getRequestBody()); }
@Test public void testAttributeOverrides() throws AuthException, MalformedURLException, RequestException { when(mockAuthManager.getToken()).thenReturn("some token"); List<AttributeMutation> attributeOverrides = new ArrayList<>(); attributeOverrides.add(AttributeMutation.newRemoveAttributeMutation("foo", 100)); attributeOverrides.add(AttributeMutation.newRemoveAttributeMutation("bar", 100)); client.performRequest(new URL("https: JsonMap expected = JsonMap.newBuilder() .put("platform", "android") .put("channel_id", "channel") .put("attribute_overrides", JsonValue.wrapOpt(attributeOverrides)) .build(); assertEquals(expected.toString(), testRequest.getRequestBody()); }
@Test public void testMissedResponse() throws AuthException, MalformedURLException, RequestException { when(mockAuthManager.getToken()).thenReturn("some_token"); testRequest.responseStatus = 200; testRequest.responseBody = JsonMap.newBuilder() .put("audience_match", false) .build() .toString(); Response<DeferredScheduleClient.Result> response = client.performRequest(new URL("https: "channel", null, EMPTY_TAGS, EMPTY_ATTRIBUTES); assertFalse(response.getResult().isAudienceMatch()); assertNull(response.getResult().getMessage()); }
@Test public void testNoMessage() throws AuthException, MalformedURLException, RequestException { when(mockAuthManager.getToken()).thenReturn("some_token"); testRequest.responseStatus = 200; testRequest.responseBody = JsonMap.newBuilder() .put("audience_match", true) .build() .toString(); Response<DeferredScheduleClient.Result> response = client.performRequest(new URL("https: "channel", null, EMPTY_TAGS, EMPTY_ATTRIBUTES); assertTrue(response.getResult().isAudienceMatch()); assertNull(response.getResult().getMessage()); }
@Test public void testMessage() throws AuthException, MalformedURLException, RequestException { InAppMessage message = InAppMessage.newBuilder() .setDisplayContent(new CustomDisplayContent(JsonValue.NULL)) .build(); when(mockAuthManager.getToken()).thenReturn("some_token"); testRequest.responseStatus = 200; testRequest.responseBody = JsonMap.newBuilder() .put("audience_match", true) .put("type", "in_app_message") .put("message", message) .build() .toString(); Response<DeferredScheduleClient.Result> response = client.performRequest(new URL("https: "channel", null, EMPTY_TAGS, EMPTY_ATTRIBUTES); assertTrue(response.getResult().isAudienceMatch()); assertEquals(message, response.getResult().getMessage()); }
@Test public void testExpiredToken() throws AuthException, MalformedURLException, RequestException { when(mockAuthManager.getToken()) .thenReturn("expired") .thenReturn("some_other_token"); testRequest.responseStatus = 401; client.performRequest(new URL("https: assertEquals("Bearer some_other_token", testRequest.getRequestHeaders().get("Authorization")); verify(mockAuthManager).tokenExpired("expired"); }
@Test(expected = AuthException.class) public void testAuthManagerExceptions() throws AuthException, MalformedURLException, RequestException { when(mockAuthManager.getToken()).thenThrow(new AuthException("neat")); client.performRequest(new URL("https: }
@Test public void testAndroidRequest() throws AuthException, MalformedURLException, RequestException { runtimeConfig.setPlatform(UAirship.ANDROID_PLATFORM); when(mockAuthManager.getToken()).thenReturn("some_token"); URL url = new URL("https: client.performRequest(url, "channel", null, EMPTY_TAGS, EMPTY_ATTRIBUTES); assertEquals(url, testRequest.getUrl()); assertEquals("POST", testRequest.getRequestMethod()); assertEquals("Bearer some_token", testRequest.getRequestHeaders().get("Authorization")); JsonMap expected = JsonMap.newBuilder() .put("platform", "android") .put("channel_id", "channel") .build(); assertEquals(expected.toString(), testRequest.getRequestBody()); }