Search in sources :

Example 66 with HttpClientNetworkStateHandler

use of com.microsoft.appcenter.http.HttpClientNetworkStateHandler in project AppCenter-SDK-Android by Microsoft.

the class IngestionHttpTest method sendAsync.

@Test
public void sendAsync() 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);
    when(serializer.serializeContainer(any(LogContainer.class))).thenReturn("mockPayload");
    /* 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" + IngestionHttp.API_PATH), eq(METHOD_POST), eq(expectedHeaders), notNull(HttpClient.CallTemplate.class), eq(serviceCallback));
    assertNotNull(callTemplate.get());
    assertEquals("mockPayload", callTemplate.get().buildRequestBody());
    /* Verify close. */
    ingestionHttp.close();
    verify(httpClient).close();
    /* Verify reopen. */
    ingestionHttp.reopen();
    verify(httpClient).reopen();
}
Also used : Context(android.content.Context) ServiceCall(com.microsoft.appcenter.http.ServiceCall) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) Log(com.microsoft.appcenter.ingestion.models.Log) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Matchers.anyString(org.mockito.Matchers.anyString) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) HttpClientNetworkStateHandler(com.microsoft.appcenter.http.HttpClientNetworkStateHandler) UUID(java.util.UUID) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

HttpClientNetworkStateHandler (com.microsoft.appcenter.http.HttpClientNetworkStateHandler)66 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)64 Matchers.anyString (org.mockito.Matchers.anyString)62 Test (org.junit.Test)49 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)49 HashMap (java.util.HashMap)42 ServiceCall (com.microsoft.appcenter.http.ServiceCall)39 InvocationOnMock (org.mockito.invocation.InvocationOnMock)38 Activity (android.app.Activity)34 UUID (java.util.UUID)15 ArgumentMatcher (org.mockito.ArgumentMatcher)13 Context (android.content.Context)12 DialogInterface (android.content.DialogInterface)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 Semaphore (java.util.concurrent.Semaphore)8 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)6 PackageInfo (android.content.pm.PackageInfo)5 NetworkStateHelper (com.microsoft.appcenter.utils.NetworkStateHelper)5 JSONException (org.json.JSONException)5 PackageManager (android.content.pm.PackageManager)4