Search in sources :

Example 76 with Persistence

use of com.microsoft.appcenter.persistence.Persistence in project mobile-center-sdk-android by Microsoft.

the class DefaultChannelTest method invalidGroup.

@Test
public void invalidGroup() throws Persistence.PersistenceException {
    Persistence persistence = mock(Persistence.class);
    Channel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), persistence, mock(Ingestion.class), mAppCenterHandler);
    /* Enqueue a log before group is registered = failure. */
    Log log = mock(Log.class);
    channel.enqueue(log, TEST_GROUP, Flags.DEFAULTS);
    verify(log, never()).setDevice(any(Device.class));
    verify(log, never()).setTimestamp(any(Date.class));
    verify(persistence, never()).putLog(eq(log), eq(TEST_GROUP), anyInt());
    /* Trying remove group that not registered. */
    channel.removeGroup(TEST_GROUP);
    verify(mAppCenterHandler, never()).removeCallbacks(any(Runnable.class));
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) Log(com.microsoft.appcenter.ingestion.models.Log) Device(com.microsoft.appcenter.ingestion.models.Device) Date(java.util.Date) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) Test(org.junit.Test)

Example 77 with Persistence

use of com.microsoft.appcenter.persistence.Persistence in project mobile-center-sdk-android by Microsoft.

the class DefaultChannelTest method errorLogSuccess.

@Test
public void errorLogSuccess() throws Persistence.PersistenceException {
    Persistence mockPersistence = mock(Persistence.class);
    Ingestion mockIngestion = mock(Ingestion.class);
    Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
    when(mockPersistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer());
    when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(getSendAsyncAnswer());
    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);
    /* Enqueuing 2 error logs. */
    channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    /* Verify that 2 items have been persisted. */
    verify(mockPersistence, times(2)).putLog(any(Log.class), eq(TEST_GROUP), eq(NORMAL));
    /* Verify that we have called sendAsync on the ingestion twice as batch size is 1. */
    verify(mockIngestion, times(2)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Verify that we have called deleteLogs on the Persistence. */
    verify(mockPersistence, times(2)).deleteLogs(any(String.class), any(String.class));
    /* Verify that we have called onBeforeSending in the listener. */
    verify(mockListener, times(2)).onBeforeSending(any(Log.class));
    /* Verify that we have called onSuccess in the listener. */
    verify(mockListener, times(2)).onSuccess(any(Log.class));
    /* The counter should be 0 now as we sent data. */
    assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
    /* Verify timer. */
    verify(mAppCenterHandler, never()).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    verify(mAppCenterHandler, never()).removeCallbacks(any(Runnable.class));
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) Log(com.microsoft.appcenter.ingestion.models.Log) Matchers.anyString(org.mockito.Matchers.anyString) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) Test(org.junit.Test)

Example 78 with Persistence

use of com.microsoft.appcenter.persistence.Persistence in project mobile-center-sdk-android by Microsoft.

the class DefaultChannelAlternateIngestionTest method useAlternateIngestion.

@Test
public void useAlternateIngestion() throws IOException {
    /* Set up channel with an alternate ingestion. */
    Persistence mockPersistence = mock(Persistence.class);
    Ingestion defaultIngestion = mock(Ingestion.class);
    when(defaultIngestion.isEnabled()).thenReturn(true);
    Ingestion alternateIngestion = mock(Ingestion.class);
    when(alternateIngestion.isEnabled()).thenReturn(true);
    when(mockPersistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(1));
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, defaultIngestion, mAppCenterHandler);
    channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, alternateIngestion, null);
    /* Enqueuing 1 event. */
    channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    /* Verify that we have called sendAsync on the ingestion. */
    verify(alternateIngestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(defaultIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* The counter should be 0 now as we sent data. */
    assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
    /* Disabling the channel should close all channels */
    channel.setEnabled(false);
    verify(alternateIngestion).close();
    verify(defaultIngestion).close();
    /* Enabling the channel should reopen all channels */
    channel.setEnabled(true);
    verify(alternateIngestion).reopen();
    verify(defaultIngestion).reopen();
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) Log(com.microsoft.appcenter.ingestion.models.Log) Matchers.anyString(org.mockito.Matchers.anyString) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) Test(org.junit.Test)

Example 79 with Persistence

use of com.microsoft.appcenter.persistence.Persistence in project mobile-center-sdk-android by Microsoft.

the class DefaultChannelAlternateIngestionTest method sendPendingLogsAfterSettingAppSecret.

@Test
public void sendPendingLogsAfterSettingAppSecret() {
    /* Set up channel without app secret. */
    String appCenterGroup = "test_group1";
    String oneCollectorGroup = "test_group2";
    Persistence mockPersistence = mock(Persistence.class);
    Ingestion defaultIngestion = mock(Ingestion.class);
    when(defaultIngestion.isEnabled()).thenReturn(true);
    Ingestion alternateIngestion = mock(Ingestion.class);
    when(alternateIngestion.isEnabled()).thenReturn(true);
    when(mockPersistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(1));
    /* Simulate we have 1 pending log in storage. */
    when(mockPersistence.countLogs(anyString())).thenReturn(1);
    /* Create channel with the two groups. */
    DefaultChannel channel = new DefaultChannel(mock(Context.class), null, mockPersistence, defaultIngestion, mAppCenterHandler);
    channel.addGroup(appCenterGroup, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null);
    /* Verify that we can now send logs to app center after we have set app secret. */
    channel.setAppSecret("testAppSecret");
    verify(defaultIngestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* If we add a one collector group it also resumes. */
    channel.addGroup(oneCollectorGroup, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, alternateIngestion, null);
    verify(alternateIngestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) Log(com.microsoft.appcenter.ingestion.models.Log) Matchers.anyString(org.mockito.Matchers.anyString) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) Test(org.junit.Test)

Example 80 with Persistence

use of com.microsoft.appcenter.persistence.Persistence in project mobile-center-sdk-android by Microsoft.

the class DefaultChannelAlternateIngestionTest method nullAppSecretProvided.

@Test
public void nullAppSecretProvided() throws Persistence.PersistenceException {
    /*
         * We don't test empty app secret in channel (and thus in tests) as it's already checked by AppCenter.
         * If app secret is set in channel, the assumption is that it's a well formed string.
         * Channel is not meant to be used publicly directly.
         */
    /* Given a mock channel. */
    Persistence persistence = mock(Persistence.class);
    Ingestion ingestion = mock(Ingestion.class);
    when(ingestion.isEnabled()).thenReturn(true);
    DefaultChannel channel = new DefaultChannel(mock(Context.class), null, persistence, ingestion, mAppCenterHandler);
    channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null);
    /* Check log url. */
    String logUrl = "http://mockUrl";
    channel.setLogUrl(logUrl);
    verify(ingestion).setLogUrl(logUrl);
    /* Check enqueue. */
    Log log = mock(Log.class);
    channel.enqueue(log, TEST_GROUP, Flags.DEFAULTS);
    verify(persistence, never()).putLog(eq(log), eq(TEST_GROUP), anyInt());
    channel.enqueue(mock(Log.class), "other", Flags.DEFAULTS);
    verify(persistence, never()).putLog(any(Log.class), anyString(), anyInt());
    /* Check clear. Even without app secret it works as it could be logs from previous process. */
    channel.clear(TEST_GROUP);
    verify(persistence).deleteLogs(eq(TEST_GROUP));
    /* Check shutdown. */
    channel.shutdown();
    verify(persistence).clearPendingLogState();
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) Log(com.microsoft.appcenter.ingestion.models.Log) Matchers.anyString(org.mockito.Matchers.anyString) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) Test(org.junit.Test)

Aggregations

Persistence (com.microsoft.appcenter.persistence.Persistence)80 Test (org.junit.Test)77 Context (android.content.Context)76 Log (com.microsoft.appcenter.ingestion.models.Log)64 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)56 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)56 UUID (java.util.UUID)56 AppCenterIngestion (com.microsoft.appcenter.ingestion.AppCenterIngestion)42 Matchers.anyString (org.mockito.Matchers.anyString)42 Ingestion (com.microsoft.appcenter.ingestion.Ingestion)29 IOException (java.io.IOException)26 IngestionHttp (com.microsoft.appcenter.ingestion.IngestionHttp)19 InvocationOnMock (org.mockito.invocation.InvocationOnMock)17 CancellationException (com.microsoft.appcenter.CancellationException)16 HttpException (com.microsoft.appcenter.http.HttpException)16 SocketException (java.net.SocketException)16 ArrayList (java.util.ArrayList)13 HttpResponse (com.microsoft.appcenter.http.HttpResponse)10 Semaphore (java.util.concurrent.Semaphore)6 Answer (org.mockito.stubbing.Answer)6