Search in sources :

Example 11 with HttpResponse

use of com.microsoft.appcenter.http.HttpResponse 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 12 with HttpResponse

use of com.microsoft.appcenter.http.HttpResponse 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 13 with HttpResponse

use of com.microsoft.appcenter.http.HttpResponse 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 14 with HttpResponse

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

the class DefaultChannel method sendLogs.

/**
 * Send logs.
 *
 * @param groupState   The group state.
 * @param currentState The current state.
 * @param batch        The log batch.
 * @param batchId      The batch ID.
 */
@MainThread
private void sendLogs(final GroupState groupState, final int currentState, List<Log> batch, final String batchId) {
    /* Send logs. */
    LogContainer logContainer = new LogContainer();
    logContainer.setLogs(batch);
    groupState.mIngestion.sendAsync(mAppSecret, mInstallId, logContainer, new ServiceCallback() {

        @Override
        public void onCallSucceeded(HttpResponse httpResponse) {
            mAppCenterHandler.post(new Runnable() {

                @Override
                public void run() {
                    handleSendingSuccess(groupState, batchId);
                }
            });
        }

        @Override
        public void onCallFailed(final Exception e) {
            mAppCenterHandler.post(new Runnable() {

                @Override
                public void run() {
                    handleSendingFailure(groupState, batchId, e);
                }
            });
        }
    });
    /* Check for more pending logs. */
    mAppCenterHandler.post(new Runnable() {

        @Override
        public void run() {
            checkPendingLogsAfterPost(groupState, currentState);
        }
    });
}
Also used : ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) HttpResponse(com.microsoft.appcenter.http.HttpResponse) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) IOException(java.io.IOException) CancellationException(com.microsoft.appcenter.CancellationException) MainThread(androidx.annotation.MainThread)

Example 15 with HttpResponse

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

the class DefaultChannelTest method suspendWithFailureCallback.

@Test
public void suspendWithFailureCallback() {
    Ingestion mockIngestion = mock(Ingestion.class);
    Persistence mockPersistence = mock(Persistence.class);
    Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
    when(mockPersistence.countLogs(anyString())).thenReturn(30);
    when(mockPersistence.getLogs(anyString(), anyListOf(String.class), anyInt(), anyListOf(Log.class))).thenAnswer(getGetLogsAnswer(10));
    when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) {
            return null;
        }
    }).then(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) {
            return null;
        }
    }).then(getSendAsyncAnswer(new HttpException(new HttpResponse(404))));
    when(mockIngestion.isEnabled()).thenReturn(true);
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, mockIngestion, mAppCenterHandler);
    channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, mockListener);
    /* 30 from countLogs and 10 new logs from getLogs. */
    verify(mockListener, times(40)).onBeforeSending(any(Log.class));
    verify(mockListener, times(40)).onFailure(any(Log.class), any(SocketException.class));
    assertFalse(channel.isEnabled());
}
Also used : Context(android.content.Context) SocketException(java.net.SocketException) Log(com.microsoft.appcenter.ingestion.models.Log) HttpResponse(com.microsoft.appcenter.http.HttpResponse) Matchers.anyString(org.mockito.Matchers.anyString) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) Persistence(com.microsoft.appcenter.persistence.Persistence) Answer(org.mockito.stubbing.Answer) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpException(com.microsoft.appcenter.http.HttpException) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

HttpResponse (com.microsoft.appcenter.http.HttpResponse)36 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)33 Matchers.anyString (org.mockito.Matchers.anyString)31 Test (org.junit.Test)30 InvocationOnMock (org.mockito.invocation.InvocationOnMock)25 ServiceCall (com.microsoft.appcenter.http.ServiceCall)22 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)20 Activity (android.app.Activity)18 Context (android.content.Context)12 HttpException (com.microsoft.appcenter.http.HttpException)12 AppCenterIngestion (com.microsoft.appcenter.ingestion.AppCenterIngestion)10 Log (com.microsoft.appcenter.ingestion.models.Log)10 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)10 Persistence (com.microsoft.appcenter.persistence.Persistence)10 HashMap (java.util.HashMap)9 UUID (java.util.UUID)9 DistributionStartSessionLog (com.microsoft.appcenter.distribute.ingestion.models.DistributionStartSessionLog)7 Answer (org.mockito.stubbing.Answer)7 PowerMockito.doAnswer (org.powermock.api.mockito.PowerMockito.doAnswer)6 DialogInterface (android.content.DialogInterface)5