ChannelApiClient { @NonNull Response<String> createChannelWithPayload(@NonNull ChannelRegistrationPayload channelPayload) throws RequestException { Logger.verbose("ChannelApiClient - Creating channel with payload: %s", channelPayload); return requestFactory.createRequest() .setOperation("POST", getDeviceUrl(null)) .setCredentials(runtimeConfig.getConfigOptions().appKey, runtimeConfig.getConfigOptions().appSecret) .setRequestBody(channelPayload) .setAirshipJsonAcceptsHeader() .execute(new ResponseParser<String>() { @Override public String parseResponse(int status, @Nullable Map<String, List<String>> headers, @Nullable String responseBody) throws Exception { if (UAHttpStatusUtil.inSuccessRange(status)) { return JsonValue.parseString(responseBody).optMap().opt(CHANNEL_ID_KEY).getString(); } return null; } }); } ChannelApiClient(@NonNull AirshipRuntimeConfig runtimeConfig); @VisibleForTesting  ChannelApiClient(@NonNull AirshipRuntimeConfig runtimeConfig,
                     @NonNull RequestFactory requestFactory);   }
@Test public void testCreateChannelSucceedsRequest() throws Exception { testRequest.responseBody = "{ \"ok\": true, \"channel_id\": \"someChannelId\"}"; testRequest.responseStatus = 200; Response<String> response = client.createChannelWithPayload(payload); assertEquals("POST", testRequest.getRequestMethod()); assertEquals("https: assertEquals(testRequest.getRequestBody(), payload.toJsonValue().toString()); assertEquals("someChannelId", response.getResult()); assertEquals(200, response.getStatus()); }
@Test public void testCreateChannelFailsRequest() throws Exception { testRequest.responseStatus = 501; Response<String> response = client.createChannelWithPayload(payload); assertEquals("POST", testRequest.getRequestMethod()); assertEquals("https: assertEquals(testRequest.getRequestBody(), payload.toJsonValue().toString()); assertNull(response.getResult()); assertEquals(501, response.getStatus()); }