Search in sources :

Example 31 with Persistence

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

the class DefaultChannelTest method invokeCallbacksAfterSuspendRecoverable.

@Test
public void invokeCallbacksAfterSuspendRecoverable() {
    Persistence mockPersistence = mock(Persistence.class);
    AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
    when(mockIngestion.isEnabled()).thenReturn(true);
    Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
    when(mockPersistence.getLogs(eq(TEST_GROUP), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(1));
    when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(getSendAsyncAnswer(new HttpException(new HttpResponse(503))));
    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);
    channel.addGroup(TEST_GROUP + "2", 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null);
    /* Enqueuing 1 event. */
    channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    /* 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) Log(com.microsoft.appcenter.ingestion.models.Log) HttpResponse(com.microsoft.appcenter.http.HttpResponse) Matchers.anyString(org.mockito.Matchers.anyString) HttpException(com.microsoft.appcenter.http.HttpException) SocketException(java.net.SocketException) IOException(java.io.IOException) CancellationException(com.microsoft.appcenter.CancellationException) Persistence(com.microsoft.appcenter.persistence.Persistence) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) HttpException(com.microsoft.appcenter.http.HttpException) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Example 32 with Persistence

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

the class DefaultChannelAlternateIngestionTest method startWithoutAppSecret.

@Test
public void startWithoutAppSecret() throws Persistence.PersistenceException {
    /* 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);
    /* Simulate we have 1 pending log in storage. */
    when(mockPersistence.countLogs(anyString())).thenReturn(1);
    when(mockPersistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(1));
    /* Create channel and groups. */
    DefaultChannel channel = new DefaultChannel(mock(Context.class), null, mockPersistence, defaultIngestion, mAppCenterHandler);
    channel.addGroup(appCenterGroup, 2, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null);
    channel.addGroup(oneCollectorGroup, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, alternateIngestion, null);
    /* App center previous log not sent yet. */
    verify(defaultIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* One collector previous log sent. */
    verify(alternateIngestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Enqueuing 1 new event for app center. */
    channel.enqueue(mock(Log.class), appCenterGroup, Flags.DEFAULTS);
    /* Not sent. */
    verify(defaultIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Verify we didn't persist the log since AppCenter not started with app secret. */
    verify(mockPersistence, never()).putLog(any(Log.class), eq(appCenterGroup), eq(Flags.NORMAL));
    /* Enqueuing 1 event from one collector group. */
    channel.enqueue(mock(Log.class), oneCollectorGroup, Flags.DEFAULTS);
    /* Verify that we have called sendAsync on the alternate ingestion a second time. */
    verify(alternateIngestion, times(2)).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));
    /* Verify that we can now send logs to app center after we have set app secret. */
    channel.setAppSecret("testAppSecret");
    channel.enqueue(mock(Log.class), appCenterGroup, Flags.DEFAULTS);
    verify(defaultIngestion).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 33 with Persistence

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

the class DefaultChannelAlternateIngestionTest method pendingLogsDisableSetAppSecretThenEnable.

@Test
public void pendingLogsDisableSetAppSecretThenEnable() {
    /* 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 for App Center. */
    when(mockPersistence.countLogs(appCenterGroup)).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);
    channel.addGroup(oneCollectorGroup, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, alternateIngestion, null);
    /* Disable channel. */
    channel.setEnabled(false);
    /* Verify that no log is sent even if we set app secret while channel is disabled. */
    channel.setAppSecret("testAppSecret");
    verify(defaultIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(alternateIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Enable channel. */
    channel.setEnabled(true);
    /* Verify that now logs are sent when channel is enabled. */
    verify(defaultIngestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(alternateIngestion, never()).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 34 with Persistence

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

the class DefaultChannelPauseResumeTest method pauseResumeTargetToken.

@Test
public void pauseResumeTargetToken() throws Persistence.PersistenceException {
    /* Mock database and ingestion. */
    Persistence persistence = mock(Persistence.class);
    OneCollectorIngestion ingestion = mock(OneCollectorIngestion.class);
    when(ingestion.isEnabled()).thenReturn(true);
    /* Create a channel with a log group that send logs 1 by 1. */
    AppCenterIngestion appCenterIngestion = mock(AppCenterIngestion.class);
    when(appCenterIngestion.isEnabled()).thenReturn(true);
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), persistence, appCenterIngestion, mAppCenterHandler);
    channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, ingestion, null);
    /* Reset to verify further interactions besides initial check after adding group. */
    reset(persistence);
    /* Pause token. */
    String targetToken = "iKey-apiKey";
    channel.pauseGroup(TEST_GROUP, targetToken);
    /* Mock the database to return logs now. */
    when(persistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(1));
    when(persistence.countLogs(TEST_GROUP)).thenReturn(1);
    /* Enqueue a log. */
    Log log = mock(Log.class);
    when(log.getTransmissionTargetTokens()).thenReturn(Collections.singleton(targetToken));
    channel.enqueue(log, TEST_GROUP, Flags.DEFAULTS);
    /* Verify persisted but not incrementing and checking logs. */
    verify(persistence).putLog(log, TEST_GROUP, Flags.NORMAL);
    assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
    verify(persistence, never()).countLogs(TEST_GROUP);
    verify(ingestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Pausing a second time has no effect. */
    channel.pauseGroup(TEST_GROUP, targetToken);
    verify(persistence, never()).countLogs(TEST_GROUP);
    /* Enqueueing a log from another transmission target works. */
    Log otherLog = mock(Log.class);
    when(otherLog.getTransmissionTargetTokens()).thenReturn(Collections.singleton("iKey2-apiKey2"));
    channel.enqueue(otherLog, TEST_GROUP, Flags.DEFAULTS);
    verify(ingestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    reset(ingestion);
    /* Resume token. */
    channel.resumeGroup(TEST_GROUP, targetToken);
    verify(ingestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Sending more logs works now. */
    reset(ingestion);
    channel.enqueue(log, TEST_GROUP, Flags.DEFAULTS);
    verify(ingestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Calling resume a second time has 0 effect. */
    reset(persistence);
    reset(ingestion);
    channel.resumeGroup(TEST_GROUP, targetToken);
    verifyZeroInteractions(persistence);
    verifyZeroInteractions(ingestion);
    /* AppCenter ingestion never used. */
    verify(appCenterIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) Log(com.microsoft.appcenter.ingestion.models.Log) OneCollectorIngestion(com.microsoft.appcenter.ingestion.OneCollectorIngestion) Matchers.anyString(org.mockito.Matchers.anyString) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Example 35 with Persistence

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

the class DefaultChannelOtherOperationsTest method clear.

@Test
public void clear() {
    Persistence mockPersistence = mock(Persistence.class);
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, mock(AppCenterIngestion.class), mAppCenterHandler);
    channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null);
    /* Clear an existing channel. */
    channel.clear(TEST_GROUP);
    verify(mockPersistence).deleteLogs(TEST_GROUP);
    reset(mockPersistence);
    /* Clear a non-existing channel. */
    channel.clear(TEST_GROUP + "2");
    verify(mockPersistence, never()).deleteLogs(anyString());
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) 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