use of com.microsoft.appcenter.http.ServiceCall in project mobile-center-sdk-android by Microsoft.
the class IngestionHttpTest method failedSerialization.
@Test
public void failedSerialization() throws Exception {
/* Build some payload. */
LogContainer container = new LogContainer();
Log log = mock(Log.class);
List<Log> logs = new ArrayList<>();
logs.add(log);
container.setLogs(logs);
LogSerializer serializer = mock(LogSerializer.class);
JSONException exception = new JSONException("mock");
when(serializer.serializeContainer(any(LogContainer.class))).thenThrow(exception);
/* Configure mock HTTP. */
HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
final ServiceCall call = mock(ServiceCall.class);
final AtomicReference<HttpClient.CallTemplate> callTemplate = new AtomicReference<>();
when(httpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class))).then(new Answer<ServiceCall>() {
@Override
public ServiceCall answer(InvocationOnMock invocation) throws Throwable {
callTemplate.set((HttpClient.CallTemplate) invocation.getArguments()[3]);
return call;
}
});
/* Test calling code. */
IngestionHttp ingestionHttp = new IngestionHttp(mock(Context.class), serializer);
ingestionHttp.setLogUrl("http://mock");
String appSecret = UUIDUtils.randomUUID().toString();
UUID installId = UUIDUtils.randomUUID();
ServiceCallback serviceCallback = mock(ServiceCallback.class);
assertEquals(call, ingestionHttp.sendAsync(appSecret, installId, container, serviceCallback));
/* Verify call to http client. */
HashMap<String, String> expectedHeaders = new HashMap<>();
expectedHeaders.put(IngestionHttp.APP_SECRET, appSecret);
expectedHeaders.put(IngestionHttp.INSTALL_ID, installId.toString());
verify(httpClient).callAsync(eq("http://mock/logs?api-version=1.0.0"), eq(METHOD_POST), eq(expectedHeaders), notNull(HttpClient.CallTemplate.class), eq(serviceCallback));
assertNotNull(callTemplate.get());
try {
callTemplate.get().buildRequestBody();
Assert.fail("Expected json exception");
} catch (JSONException ignored) {
}
/* Verify close. */
ingestionHttp.close();
verify(httpClient).close();
}
Aggregations