Search in sources :

Example 6 with HttpException

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

the class DefaultChannelTest method suspendWithFailureCallback.

@Test
@SuppressWarnings("unchecked")
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(), anyInt(), anyList())).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) throws Throwable {
            return null;
        }
    }).then(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return null;
        }
    }).then(getSendAsyncAnswer(new HttpException(404)));
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion);
    channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, 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) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) Ingestion(com.microsoft.azure.mobile.ingestion.Ingestion) Persistence(com.microsoft.azure.mobile.persistence.Persistence) Answer(org.mockito.stubbing.Answer) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpException(com.microsoft.azure.mobile.http.HttpException) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Example 7 with HttpException

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

the class DistributeBeforeApiSuccessTest method checkReleaseFailsWithSome404noRelease.

@Test
public void checkReleaseFailsWithSome404noRelease() throws Exception {
    /* Mock error parsing. */
    ErrorDetails errorDetails = mock(ErrorDetails.class);
    when(errorDetails.getCode()).thenReturn(ErrorDetails.NO_RELEASES_FOR_USER_CODE);
    mockStatic(ErrorDetails.class);
    String errorPayload = "{code: 'no_releases_for_user'}";
    when(ErrorDetails.parse(errorPayload)).thenReturn(errorDetails);
    checkReleaseFailure(new HttpException(404, errorPayload), never());
}
Also used : HttpException(com.microsoft.azure.mobile.http.HttpException) Matchers.anyString(org.mockito.Matchers.anyString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with HttpException

use of com.microsoft.azure.mobile.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) {
                    MobileCenterLog.verbose(LOG_TAG, "Cannot read the error as JSON", je);
                }
            }
            if (ErrorDetails.NO_RELEASES_FOR_USER_CODE.equals(code)) {
                MobileCenterLog.info(LOG_TAG, "No release available to the current user.");
            } else {
                MobileCenterLog.error(LOG_TAG, "Failed to check latest release:", e);
                PreferencesStorage.remove(PREFERENCE_KEY_UPDATE_TOKEN);
            }
        }
    }
}
Also used : JSONException(org.json.JSONException) HttpException(com.microsoft.azure.mobile.http.HttpException)

Example 9 with HttpException

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

the class DefaultChannelTest method analyticsFatal.

@Test
@SuppressWarnings("unchecked")
public void analyticsFatal() throws Exception {
    Persistence mockPersistence = mock(Persistence.class);
    IngestionHttp mockIngestion = mock(IngestionHttp.class);
    DatabasePersistenceAsync mockPersistenceAsync = spy(new DatabasePersistenceAsync(mockPersistence));
    whenNew(DatabasePersistenceAsync.class).withArguments(mockPersistence).thenReturn(mockPersistenceAsync);
    when(mockPersistence.getLogs(any(String.class), anyInt(), any(ArrayList.class))).then(getGetLogsAnswer(50)).then(getGetLogsAnswer(50)).then(getGetLogsAnswer(20));
    when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(getSendAsyncAnswer(new HttpException(403))).then(getSendAsyncAnswer());
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion);
    channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
    /* Enqueuing 50 events. */
    for (int i = 0; i < 50; i++) {
        channel.enqueue(mock(Log.class), TEST_GROUP);
    }
    verify(mHandler).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    verify(mHandler).removeCallbacks(any(Runnable.class));
    /* Verify that 50 items have been persisted. */
    verify(mockPersistence, times(50)).putLog(eq(TEST_GROUP), any(Log.class));
    /* Verify that we have called sendAsync on the ingestion. */
    verify(mockIngestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Verify that the Channel is disabled. */
    assertFalse(channel.isEnabled());
    /* Verify that we have cleared the logs. */
    verify(mockPersistence).deleteLogs(TEST_GROUP);
    /* Verify counter. */
    assertEquals(0, channel.getCounter(TEST_GROUP));
    /* Enqueuing 20 more events. */
    for (int i = 0; i < 20; i++) {
        channel.enqueue(mock(Log.class), TEST_GROUP);
    }
    /* The counter should still be 0 as logs are discarded by channel now. */
    assertEquals(0, channel.getCounter(TEST_GROUP));
    /* No more timer yet at this point. */
    verify(mHandler).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    verify(mHandler).removeCallbacks(any(Runnable.class));
    /* Prepare to mock timer. */
    AtomicReference<Runnable> runnable = catchPostRunnable();
    /* Enable channel to see if it can work again after that error state. */
    channel.setEnabled(true);
    /* Enqueuing 20 more events. */
    for (int i = 0; i < 20; i++) {
        channel.enqueue(mock(Log.class), TEST_GROUP);
    }
    assertEquals(20, channel.getCounter(TEST_GROUP));
    /* Wait for timer. */
    assertNotNull(runnable.get());
    runnable.get().run();
    /* The counter should back to 0 now. */
    assertEquals(0, channel.getCounter(TEST_GROUP));
    /* Verify that we have called sendAsync on the ingestion 2 times total: 1 earlier failure then 1 success. */
    verify(mockIngestion, times(2)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Verify that we have called deleteLogs on the Persistence for the successful batch after re-enabling. */
    verify(mockPersistence).deleteLogs(any(String.class), any(String.class));
    /* Verify 1 more timer call. */
    verify(mHandler, times(2)).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    /* Verify no more cancel timer. */
    verify(mHandler).removeCallbacks(any(Runnable.class));
}
Also used : Context(android.content.Context) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) Persistence(com.microsoft.azure.mobile.persistence.Persistence) IngestionHttp(com.microsoft.azure.mobile.ingestion.IngestionHttp) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) HttpException(com.microsoft.azure.mobile.http.HttpException) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) UUID(java.util.UUID) DatabasePersistenceAsync(com.microsoft.azure.mobile.persistence.DatabasePersistenceAsync) Test(org.junit.Test)

Aggregations

HttpException (com.microsoft.azure.mobile.http.HttpException)9 Test (org.junit.Test)8 Context (android.content.Context)5 ServiceCallback (com.microsoft.azure.mobile.http.ServiceCallback)5 Log (com.microsoft.azure.mobile.ingestion.models.Log)5 Persistence (com.microsoft.azure.mobile.persistence.Persistence)5 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)5 IngestionHttp (com.microsoft.azure.mobile.ingestion.IngestionHttp)4 LogContainer (com.microsoft.azure.mobile.ingestion.models.LogContainer)4 DatabasePersistenceAsync (com.microsoft.azure.mobile.persistence.DatabasePersistenceAsync)4 SocketException (java.net.SocketException)4 UUID (java.util.UUID)4 Matchers.anyString (org.mockito.Matchers.anyString)4 CancellationException (com.microsoft.azure.mobile.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