Search in sources :

Example 1 with HttpException

use of com.microsoft.azure.mobile.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. */
    DatabasePersistenceAsync mockPersistenceAsync = spy(new DatabasePersistenceAsync(mockPersistence));
    whenNew(DatabasePersistenceAsync.class).withArguments(mockPersistence).thenReturn(mockPersistenceAsync);
    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);
    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.azure.mobile.persistence.Persistence) Context(android.content.Context) IngestionHttp(com.microsoft.azure.mobile.ingestion.IngestionHttp) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) ArrayList(java.util.ArrayList) HttpException(com.microsoft.azure.mobile.http.HttpException) DatabasePersistenceAsync(com.microsoft.azure.mobile.persistence.DatabasePersistenceAsync) SocketException(java.net.SocketException) IOException(java.io.IOException) HttpException(com.microsoft.azure.mobile.http.HttpException) CancellationException(com.microsoft.azure.mobile.CancellationException) Test(org.junit.Test)

Example 2 with HttpException

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

the class DefaultChannelTest method invokeCallbacksAfterSuspendRecoverable.

@Test
@SuppressWarnings("unchecked")
public void invokeCallbacksAfterSuspendRecoverable() throws Exception {
    Persistence mockPersistence = mock(Persistence.class);
    IngestionHttp mockIngestion = mock(IngestionHttp.class);
    Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
    DatabasePersistenceAsync mockPersistenceAsync = spy(new DatabasePersistenceAsync(mockPersistence));
    whenNew(DatabasePersistenceAsync.class).withArguments(mockPersistence).thenReturn(mockPersistenceAsync);
    when(mockPersistence.getLogs(eq(TEST_GROUP), anyInt(), any(ArrayList.class))).then(getGetLogsAnswer(1));
    when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(getSendAsyncAnswer(new HttpException(503)));
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion);
    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 only for the first log. */
    verify(mockListener).onBeforeSending(any(Log.class));
    /* Verify no failure forwarded. */
    verify(mockListener, never()).onFailure(any(Log.class), any(Exception.class));
    /* Verify no log was deleted. */
    verify(mockPersistence, never()).deleteLogs(TEST_GROUP);
    /* But that we cleared batch state. */
    verify(mockPersistence).clearPendingLogState();
}
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) SocketException(java.net.SocketException) IOException(java.io.IOException) HttpException(com.microsoft.azure.mobile.http.HttpException) CancellationException(com.microsoft.azure.mobile.CancellationException) 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)

Example 3 with HttpException

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

the class DistributeBeforeApiSuccessTest method checkReleaseFailsWithSome404urlNotFound.

@Test
public void checkReleaseFailsWithSome404urlNotFound() throws Exception {
    /* Mock error parsing. */
    mockStatic(ErrorDetails.class);
    final String errorPayload = "<html>Not Found</html>";
    when(ErrorDetails.parse(errorPayload)).thenThrow(new JSONException("Expected {"));
    final Exception exception = new HttpException(404, errorPayload);
    checkReleaseFailure(exception, times(1));
}
Also used : JSONException(org.json.JSONException) HttpException(com.microsoft.azure.mobile.http.HttpException) Matchers.anyString(org.mockito.Matchers.anyString) JSONException(org.json.JSONException) HttpException(com.microsoft.azure.mobile.http.HttpException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with HttpException

use of com.microsoft.azure.mobile.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. */
    Distribute.getInstance().onStarted(mContext, "a", mock(Channel.class));
    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.azure.mobile.http.ServiceCall) HashMap(java.util.HashMap) Channel(com.microsoft.azure.mobile.channel.Channel) Activity(android.app.Activity) Semaphore(java.util.concurrent.Semaphore) Matchers.anyString(org.mockito.Matchers.anyString) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpException(com.microsoft.azure.mobile.http.HttpException) HttpClientNetworkStateHandler(com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with HttpException

use of com.microsoft.azure.mobile.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);
    DatabasePersistenceAsync mockPersistenceAsync = spy(new DatabasePersistenceAsync(mockPersistence));
    whenNew(DatabasePersistenceAsync.class).withArguments(mockPersistence).thenReturn(mockPersistenceAsync);
    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);
    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 : Context(android.content.Context) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) SocketException(java.net.SocketException) IOException(java.io.IOException) HttpException(com.microsoft.azure.mobile.http.HttpException) CancellationException(com.microsoft.azure.mobile.CancellationException) 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