Search in sources :

Example 31 with ServiceCall

use of com.microsoft.appcenter.http.ServiceCall in project mobile-center-sdk-android by Microsoft.

the class DistributeCustomizationTest method mockHtpCallFailed.

private void mockHtpCallFailed(String payload) {
    final HttpException httpException = new HttpException(new HttpResponse(404, payload));
    /* Mock http call. */
    when(mHttpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class))).thenAnswer(new Answer<ServiceCall>() {

        @Override
        public ServiceCall answer(InvocationOnMock invocation) {
            ((ServiceCallback) invocation.getArguments()[4]).onCallFailed(httpException);
            return mock(ServiceCall.class);
        }
    });
}
Also used : ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) ServiceCall(com.microsoft.appcenter.http.ServiceCall) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpResponse(com.microsoft.appcenter.http.HttpResponse) HttpException(com.microsoft.appcenter.http.HttpException) Matchers.anyString(org.mockito.Matchers.anyString)

Example 32 with ServiceCall

use of com.microsoft.appcenter.http.ServiceCall in project mobile-center-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 33 with ServiceCall

use of com.microsoft.appcenter.http.ServiceCall in project mobile-center-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 34 with ServiceCall

use of com.microsoft.appcenter.http.ServiceCall in project mobile-center-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 35 with ServiceCall

use of com.microsoft.appcenter.http.ServiceCall in project mobile-center-sdk-android by Microsoft.

the class AppCenterIngestionTest method getCallTemplate.

private HttpClient.CallTemplate getCallTemplate(String appSecret) {
    /* Configure mock HTTP to get an instance of IngestionCallTemplate. */
    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;
        }
    });
    AppCenterIngestion ingestion = new AppCenterIngestion(mHttpClient, mock(LogSerializer.class));
    ingestion.setLogUrl("http://mock");
    assertEquals(call, ingestion.sendAsync(appSecret, UUID.randomUUID(), mock(LogContainer.class), mock(ServiceCallback.class)));
    return callTemplate.get();
}
Also used : ServiceCall(com.microsoft.appcenter.http.ServiceCall) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) AtomicReference(java.util.concurrent.atomic.AtomicReference) 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