Search in sources :

Example 76 with ServiceCall

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

the class DistributeTest method tryResetWorkflowWhenApplicationEnterForegroundWhenChannelNotNull.

@Test
public void tryResetWorkflowWhenApplicationEnterForegroundWhenChannelNotNull() {
    /* Prepare data. */
    mockStatic(DistributeUtils.class);
    when(DistributeUtils.getStoredDownloadState()).thenReturn(DOWNLOAD_STATE_COMPLETED);
    final ServiceCall call = mock(ServiceCall.class);
    doAnswer(new Answer<ServiceCall>() {

        @Override
        public ServiceCall answer(InvocationOnMock invocationOnMock) {
            ((ServiceCallback) invocationOnMock.getArguments()[4]).onCallSucceeded(new HttpResponse(200, ""));
            return call;
        }
    }).when(mHttpClient).callAsync(anyString(), anyString(), eq(Collections.<String, String>emptyMap()), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    /* Start distribute. */
    start();
    /* Start activity. */
    Distribute.getInstance().onApplicationEnterForeground();
    Distribute.getInstance().onActivityResumed(mActivity);
    /* Verify download is checked after we reset workflow. */
    verify(mHttpClient).callAsync(anyString(), anyString(), eq(Collections.<String, String>emptyMap()), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    /* Stop activity. */
    Distribute.getInstance().onActivityPaused(mActivity);
    Distribute.getInstance().onApplicationEnterBackground();
    /* Verify that all calls were completed. */
    verify(mHttpClient).callAsync(anyString(), anyString(), eq(Collections.<String, String>emptyMap()), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    /* Enter foreground again. */
    Distribute.getInstance().onApplicationEnterForeground();
    Distribute.getInstance().onActivityResumed(mActivity);
    /* Verify download is checked after we reset workflow again. */
    verify(mHttpClient, times(2)).callAsync(anyString(), anyString(), eq(Collections.<String, String>emptyMap()), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
}
Also used : ServiceCall(com.microsoft.appcenter.http.ServiceCall) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpResponse(com.microsoft.appcenter.http.HttpResponse) Matchers.anyString(org.mockito.Matchers.anyString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 77 with ServiceCall

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

the class DistributeTest method tryResetWorkflowWhenApplicationEnterForegroundWhenChannelNull.

@Test
public void tryResetWorkflowWhenApplicationEnterForegroundWhenChannelNull() {
    /* Mock download state. */
    mockStatic(DistributeUtils.class);
    when(DistributeUtils.getStoredDownloadState()).thenReturn(DOWNLOAD_STATE_COMPLETED);
    final ServiceCall call = mock(ServiceCall.class);
    doAnswer(new Answer<ServiceCall>() {

        @Override
        public ServiceCall answer(InvocationOnMock invocationOnMock) {
            ((ServiceCallback) invocationOnMock.getArguments()[4]).onCallSucceeded(new HttpResponse(200, ""));
            return call;
        }
    }).when(mHttpClient).callAsync(anyString(), anyString(), eq(Collections.<String, String>emptyMap()), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    /* Starting distribute. */
    Distribute.getInstance().onStarting(mAppCenterHandler);
    /* Start activity. */
    Distribute.getInstance().onApplicationEnterForeground();
    Distribute.getInstance().onActivityResumed(mActivity);
    /* Verify download is not checked after we reset workflow. */
    verify(mHttpClient, never()).callAsync(anyString(), anyString(), eq(Collections.<String, String>emptyMap()), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
}
Also used : ServiceCall(com.microsoft.appcenter.http.ServiceCall) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpResponse(com.microsoft.appcenter.http.HttpResponse) Matchers.anyString(org.mockito.Matchers.anyString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 78 with ServiceCall

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

the class AppCenterIngestionTest 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. */
    final ServiceCall call = mock(ServiceCall.class);
    final AtomicReference<HttpClient.CallTemplate> callTemplate = new AtomicReference<>();
    when(mHttpClient.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) {
            callTemplate.set((HttpClient.CallTemplate) invocation.getArguments()[3]);
            return call;
        }
    });
    /* Test calling code. */
    AppCenterIngestion ingestion = new AppCenterIngestion(mHttpClient, serializer);
    ingestion.setLogUrl("http://mock");
    String appSecret = UUID.randomUUID().toString();
    UUID installId = UUID.randomUUID();
    ServiceCallback serviceCallback = mock(ServiceCallback.class);
    assertEquals(call, ingestion.sendAsync(appSecret, installId, container, serviceCallback));
    /* Verify call to http client. */
    HashMap<String, String> expectedHeaders = new HashMap<>();
    expectedHeaders.put(Constants.APP_SECRET, appSecret);
    expectedHeaders.put(AppCenterIngestion.INSTALL_ID, installId.toString());
    verify(mHttpClient).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. */
    ingestion.close();
    verify(mHttpClient).close();
}
Also used : 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) JSONException(org.json.JSONException) 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) UUID(java.util.UUID) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 79 with ServiceCall

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

the class AppCenterIngestionTest method sendLogsWhenIngestionDisable.

@Test
public void sendLogsWhenIngestionDisable() throws JSONException, IOException {
    mockStatic(SharedPreferencesManager.class);
    when(SharedPreferencesManager.getBoolean(ALLOWED_NETWORK_REQUEST, true)).thenReturn(false);
    /* 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. */
    final ServiceCall call = mock(ServiceCall.class);
    /* Test calling code. */
    AppCenterIngestion ingestion = new AppCenterIngestion(mHttpClient, serializer);
    ingestion.setLogUrl("http://mock");
    String appSecret = UUID.randomUUID().toString();
    UUID installId = UUID.randomUUID();
    ServiceCallback serviceCallback = mock(ServiceCallback.class);
    assertNull(ingestion.sendAsync(appSecret, installId, container, serviceCallback));
    /* Verify call to http client. */
    HashMap<String, String> expectedHeaders = new HashMap<>();
    expectedHeaders.put(Constants.APP_SECRET, appSecret);
    expectedHeaders.put(AppCenterIngestion.INSTALL_ID, installId.toString());
    verify(mHttpClient, never()).callAsync(eq("http://mock" + AppCenterIngestion.API_PATH), eq(METHOD_POST), eq(expectedHeaders), notNull(HttpClient.CallTemplate.class), eq(serviceCallback));
}
Also used : 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) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Matchers.anyString(org.mockito.Matchers.anyString) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 80 with ServiceCall

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

the class OneCollectorIngestionTest method getCallTemplate.

private HttpClient.CallTemplate getCallTemplate() {
    /* Configure mock HTTP to get an instance of IngestionCallTemplate. */
    ServiceCall call = mock(ServiceCall.class);
    ArgumentCaptor<HttpClient.CallTemplate> callTemplate = ArgumentCaptor.forClass(HttpClient.CallTemplate.class);
    when(mHttpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), callTemplate.capture(), any(ServiceCallback.class))).thenReturn(call);
    OneCollectorIngestion ingestion = new OneCollectorIngestion(mHttpClient, mock(LogSerializer.class));
    ingestion.setLogUrl("http://mock");
    assertEquals(call, ingestion.sendAsync(null, null, mock(LogContainer.class), mock(ServiceCallback.class)));
    return callTemplate.getValue();
}
Also used : ServiceCall(com.microsoft.appcenter.http.ServiceCall) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) HttpClient(com.microsoft.appcenter.http.HttpClient) DefaultHttpClient(com.microsoft.appcenter.http.DefaultHttpClient) Matchers.anyString(org.mockito.Matchers.anyString) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer)

Aggregations

ServiceCall (com.microsoft.appcenter.http.ServiceCall)116 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)116 Matchers.anyString (org.mockito.Matchers.anyString)113 InvocationOnMock (org.mockito.invocation.InvocationOnMock)95 Test (org.junit.Test)91 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)91 Activity (android.app.Activity)59 HttpResponse (com.microsoft.appcenter.http.HttpResponse)57 HashMap (java.util.HashMap)45 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)33 HttpClientNetworkStateHandler (com.microsoft.appcenter.http.HttpClientNetworkStateHandler)26 Log (com.microsoft.appcenter.ingestion.models.Log)25 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)25 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)25 AtomicReference (java.util.concurrent.atomic.AtomicReference)24 DialogInterface (android.content.DialogInterface)21 DistributionStartSessionLog (com.microsoft.appcenter.distribute.ingestion.models.DistributionStartSessionLog)21 HttpClient (com.microsoft.appcenter.http.HttpClient)21 Answer (org.mockito.stubbing.Answer)20 PowerMockito.doAnswer (org.powermock.api.mockito.PowerMockito.doAnswer)20