Search in sources :

Example 6 with HttpException

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

the class DefaultChannelTest method invokeCallbacksAfterSuspendFatal.

@Test
@SuppressWarnings("unchecked")
public void invokeCallbacksAfterSuspendFatal() throws Exception {
    Persistence mockPersistence = mock(Persistence.class);
    IngestionHttp mockIngestion = mock(IngestionHttp.class);
    Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
    when(mockPersistence.getLogs(eq(TEST_GROUP), anyInt(), any(ArrayList.class))).then(getGetLogsAnswer(1)).then(getGetLogsAnswer(DefaultChannel.CLEAR_BATCH_SIZE)).then(getGetLogsAnswer(DefaultChannel.CLEAR_BATCH_SIZE - 1)).then(getGetLogsAnswer(DefaultChannel.CLEAR_BATCH_SIZE));
    when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(getSendAsyncAnswer(new HttpException(403)));
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion, mCoreHandler);
    channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, mockListener);
    channel.addGroup(TEST_GROUP + "2", 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
    /* Enqueuing 1 event. */
    channel.enqueue(mock(Log.class), TEST_GROUP);
    /* Verify callbacks invoked (1 + DefaultChannel.CLEAR_BATCH_SIZE + DefaultChannel.CLEAR_BATCH_SIZE - 1) times. */
    verify(mockListener, times(DefaultChannel.CLEAR_BATCH_SIZE * 2)).onBeforeSending(any(Log.class));
    verify(mockListener, times(DefaultChannel.CLEAR_BATCH_SIZE * 2)).onFailure(any(Log.class), any(Exception.class));
    /* Verify logs were deleted. */
    verify(mockPersistence).deleteLogs(TEST_GROUP);
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) IngestionHttp(com.microsoft.appcenter.ingestion.IngestionHttp) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) Log(com.microsoft.appcenter.ingestion.models.Log) HttpException(com.microsoft.appcenter.http.HttpException) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) HttpException(com.microsoft.appcenter.http.HttpException) SocketException(java.net.SocketException) IOException(java.io.IOException) CancellationException(com.microsoft.appcenter.CancellationException) Test(org.junit.Test)

Example 7 with HttpException

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

the class Distribute method handleApiCallFailure.

/**
 * Handle API call failure.
 */
private synchronized void handleApiCallFailure(Object releaseCallId, Exception e) {
    /* Check if state did not change. */
    if (mCheckReleaseCallId == releaseCallId) {
        /* Complete workflow in error. */
        completeWorkflow();
        /* Delete token on unrecoverable error. */
        if (!HttpUtils.isRecoverableError(e)) {
            /*
                 * Unless its a special case: 404 with json code that no release is found.
                 * Could happen by cleaning releases with remove button.
                 */
            String code = null;
            if (e instanceof HttpException) {
                HttpException httpException = (HttpException) e;
                try {
                    /* We actually don't care of the http code if JSON code is specified. */
                    ErrorDetails errorDetails = ErrorDetails.parse(httpException.getPayload());
                    code = errorDetails.getCode();
                } catch (JSONException je) {
                    AppCenterLog.verbose(LOG_TAG, "Cannot read the error as JSON", je);
                }
            }
            if (ErrorDetails.NO_RELEASES_FOR_USER_CODE.equals(code)) {
                AppCenterLog.info(LOG_TAG, "No release available to the current user.");
            } else {
                AppCenterLog.error(LOG_TAG, "Failed to check latest release:", e);
                PreferencesStorage.remove(PREFERENCE_KEY_DISTRIBUTION_GROUP_ID);
                PreferencesStorage.remove(PREFERENCE_KEY_UPDATE_TOKEN);
                mDistributeInfoTracker.removeDistributionGroupId();
            }
        }
    }
}
Also used : JSONException(org.json.JSONException) HttpException(com.microsoft.appcenter.http.HttpException)

Example 8 with HttpException

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

the class DistributeBeforeApiSuccessTest method disableBeforeCheckReleaseFails.

@Test
public void disableBeforeCheckReleaseFails() throws Exception {
    /* Mock we already have token. */
    when(PreferencesStorage.getString(PREFERENCE_KEY_UPDATE_TOKEN)).thenReturn("some token");
    HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
    whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
    final Semaphore beforeSemaphore = new Semaphore(0);
    final Semaphore afterSemaphore = new Semaphore(0);
    when(httpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class))).thenAnswer(new Answer<ServiceCall>() {

        @Override
        public ServiceCall answer(final InvocationOnMock invocation) throws Throwable {
            new Thread() {

                @Override
                public void run() {
                    beforeSemaphore.acquireUninterruptibly();
                    ((ServiceCallback) invocation.getArguments()[4]).onCallFailed(new HttpException(403));
                    afterSemaphore.release();
                }
            }.start();
            return mock(ServiceCall.class);
        }
    });
    HashMap<String, String> headers = new HashMap<>();
    headers.put(DistributeConstants.HEADER_API_TOKEN, "some token");
    /* Trigger call. */
    start();
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
    /* Disable before it fails. */
    Distribute.setEnabled(false);
    verifyStatic();
    PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
    verifyStatic(never());
    PreferencesStorage.remove(PREFERENCE_KEY_UPDATE_TOKEN);
    beforeSemaphore.release();
    afterSemaphore.acquireUninterruptibly();
    /* Verify complete workflow call ignored. i.e. no more call to delete the state. */
    verifyStatic();
    PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
    /* After that if we resume app nothing happens. */
    Distribute.getInstance().onActivityPaused(mock(Activity.class));
    Distribute.getInstance().onActivityResumed(mock(Activity.class));
    verify(httpClient).callAsync(anyString(), anyString(), eq(headers), any(HttpClient.CallTemplate.class), any(ServiceCallback.class));
}
Also used : ServiceCall(com.microsoft.appcenter.http.ServiceCall) HashMap(java.util.HashMap) Activity(android.app.Activity) Semaphore(java.util.concurrent.Semaphore) Matchers.anyString(org.mockito.Matchers.anyString) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpException(com.microsoft.appcenter.http.HttpException) HttpClientNetworkStateHandler(com.microsoft.appcenter.http.HttpClientNetworkStateHandler) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 9 with HttpException

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

the class DefaultChannelTest method invokeCallbacksAfterSuspendFatalNoListener.

@Test
@SuppressWarnings("unchecked")
public void invokeCallbacksAfterSuspendFatalNoListener() throws Exception {
    Persistence mockPersistence = mock(Persistence.class);
    IngestionHttp mockIngestion = mock(IngestionHttp.class);
    Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
    /* Simulate a lot of logs already in database. */
    when(mockPersistence.getLogs(eq(TEST_GROUP), anyInt(), any(ArrayList.class))).then(getGetLogsAnswer(1)).then(getGetLogsAnswer(1)).then(getGetLogsAnswer(DefaultChannel.CLEAR_BATCH_SIZE));
    /* Make first call hang, and the second call return a fatal error. */
    when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).thenReturn(null).then(getSendAsyncAnswer(new HttpException(403)));
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion, mCoreHandler);
    channel.addGroup(TEST_GROUP, 1, 1, MAX_PARALLEL_BATCHES, null);
    channel.addGroup(TEST_GROUP + "2", 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, mockListener);
    /* Enqueuing 2 events. */
    channel.enqueue(mock(Log.class), TEST_GROUP);
    channel.enqueue(mock(Log.class), TEST_GROUP);
    /* Verify callbacks not invoked. */
    verify(mockListener, never()).onBeforeSending(any(Log.class));
    verify(mockListener, never()).onFailure(any(Log.class), any(Exception.class));
    /* Verify logs were deleted. */
    verify(mockPersistence).deleteLogs(TEST_GROUP);
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) IngestionHttp(com.microsoft.appcenter.ingestion.IngestionHttp) Log(com.microsoft.appcenter.ingestion.models.Log) ArrayList(java.util.ArrayList) HttpException(com.microsoft.appcenter.http.HttpException) HttpException(com.microsoft.appcenter.http.HttpException) SocketException(java.net.SocketException) IOException(java.io.IOException) CancellationException(com.microsoft.appcenter.CancellationException) Test(org.junit.Test)

Aggregations

HttpException (com.microsoft.appcenter.http.HttpException)9 Test (org.junit.Test)8 Context (android.content.Context)5 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)5 Log (com.microsoft.appcenter.ingestion.models.Log)5 Persistence (com.microsoft.appcenter.persistence.Persistence)5 IngestionHttp (com.microsoft.appcenter.ingestion.IngestionHttp)4 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)4 SocketException (java.net.SocketException)4 UUID (java.util.UUID)4 Matchers.anyString (org.mockito.Matchers.anyString)4 CancellationException (com.microsoft.appcenter.CancellationException)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 JSONException (org.json.JSONException)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Activity (android.app.Activity)1 HttpClientNetworkStateHandler (com.microsoft.appcenter.http.HttpClientNetworkStateHandler)1 ServiceCall (com.microsoft.appcenter.http.ServiceCall)1