Search in sources :

Example 71 with Persistence

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

the class DefaultChannelTest method suspendWithoutFailureCallback.

@Test
public void suspendWithoutFailureCallback() {
    Ingestion mockIngestion = mock(Ingestion.class);
    Persistence mockPersistence = mock(Persistence.class);
    when(mockPersistence.countLogs(anyString())).thenReturn(3);
    when(mockPersistence.getLogs(anyString(), anyListOf(String.class), anyInt(), anyListOf(Log.class))).thenAnswer(getGetLogsAnswer(1));
    when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) {
            return null;
        }
    }).then(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) {
            return null;
        }
    }).then(getSendAsyncAnswer(new SocketException()));
    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, null);
    assertFalse(channel.isEnabled());
}
Also used : Context(android.content.Context) SocketException(java.net.SocketException) Log(com.microsoft.appcenter.ingestion.models.Log) Matchers.anyString(org.mockito.Matchers.anyString) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) Persistence(com.microsoft.appcenter.persistence.Persistence) Answer(org.mockito.stubbing.Answer) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Example 72 with Persistence

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

the class DefaultChannelTest method checkPendingLogsStoresStartTime.

@Test
public void checkPendingLogsStoresStartTime() {
    /* Mock current time. */
    long now = 1;
    when(System.currentTimeMillis()).thenReturn(now);
    /* Create channel and group. */
    Persistence mockPersistence = mock(Persistence.class);
    when(mockPersistence.countLogs(TEST_GROUP)).thenReturn(5);
    AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
    when(mockIngestion.isEnabled()).thenReturn(true);
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, mockIngestion, mAppCenterHandler);
    channel.addGroup(TEST_GROUP, 10, CUSTOM_INTERVAL, MAX_PARALLEL_BATCHES, mockIngestion, mock(Channel.GroupListener.class));
    /* Verify that timer starts and current time is saved into preferences. */
    verifyStatic();
    SharedPreferencesManager.putLong(eq(START_TIMER_PREFIX + TEST_GROUP), eq(now));
    verify(mAppCenterHandler).postDelayed(any(Runnable.class), eq(CUSTOM_INTERVAL));
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) Test(org.junit.Test)

Example 73 with Persistence

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

the class DefaultChannelTest method checkPendingLogsDoesNotStartTimerWithoutLogs.

@Test
public void checkPendingLogsDoesNotStartTimerWithoutLogs() {
    /* Mock current time. */
    long now = 1;
    when(System.currentTimeMillis()).thenReturn(now);
    /* Create channel and group. */
    Persistence mockPersistence = mock(Persistence.class);
    when(mockPersistence.countLogs(TEST_GROUP)).thenReturn(0);
    AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
    when(mockIngestion.isEnabled()).thenReturn(true);
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, mockIngestion, mAppCenterHandler);
    channel.addGroup(TEST_GROUP, 10, CUSTOM_INTERVAL, MAX_PARALLEL_BATCHES, mockIngestion, mock(Channel.GroupListener.class));
    /* Verify that timer isn't started. */
    verifyStatic(never());
    SharedPreferencesManager.putLong(eq(START_TIMER_PREFIX + TEST_GROUP), eq(now));
    verify(mAppCenterHandler, never()).postDelayed(any(Runnable.class), eq(CUSTOM_INTERVAL));
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) Test(org.junit.Test)

Example 74 with Persistence

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

the class DefaultChannelTest method setNetworkRequest.

@Test
public void setNetworkRequest() {
    /* Mock current time - before end of interval. */
    long now = 1;
    when(System.currentTimeMillis()).thenReturn(now);
    /* Create channel and group. */
    Persistence mockPersistence = mock(Persistence.class);
    when(mockPersistence.countLogs(TEST_GROUP)).thenReturn(5);
    when(mockPersistence.getLogs(anyString(), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(1));
    AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, mockIngestion, mAppCenterHandler);
    /* Disallow network requests. */
    when(mockIngestion.isEnabled()).thenReturn(false);
    channel.setNetworkRequests(false);
    channel.addGroup(TEST_GROUP, 3, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, mockIngestion, mock(Channel.GroupListener.class));
    /* Verify that sending logs wasn't called. */
    verify(mockIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Enable network requests and verify that sending logs was called. */
    when(mockIngestion.isEnabled()).thenReturn(true);
    channel.setNetworkRequests(true);
    verify(mockIngestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Disallow network requests and verify that sending logs wasn't called. */
    when(mockIngestion.isEnabled()).thenReturn(false);
    channel.setNetworkRequests(false);
    channel.addGroup(TEST_GROUP, 3, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, mockIngestion, mock(Channel.GroupListener.class));
    verify(mockIngestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Allow network requests and verify that sending logs was called again. */
    when(mockIngestion.isEnabled()).thenReturn(true);
    channel.setNetworkRequests(true);
    verify(mockIngestion, times(2)).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) Matchers.anyString(org.mockito.Matchers.anyString) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Example 75 with Persistence

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

the class DefaultChannelTest method enqueueWithFlags.

@Test
public void enqueueWithFlags() throws Persistence.PersistenceException {
    /* Other tests use default flags, test explicit flags here. */
    /* Setup persistence, channel and a listener. */
    Persistence persistence = mock(Persistence.class);
    Channel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), persistence, mock(AppCenterIngestion.class), mAppCenterHandler);
    channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, mock(Channel.GroupListener.class));
    Channel.Listener listener = mock(Channel.Listener.class);
    channel.addListener(listener);
    /* Enqueuing 1 event with normal persistence. */
    Log normalLog = mock(Log.class);
    channel.enqueue(normalLog, TEST_GROUP, Flags.NORMAL);
    /* Verify listener and database get the same flags. */
    verify(listener).onPreparedLog(normalLog, TEST_GROUP, Flags.NORMAL);
    verify(persistence).putLog(normalLog, TEST_GROUP, Flags.NORMAL);
    /* Enqueuing 1 event with critical persistence. */
    Log criticalLog = mock(Log.class);
    channel.enqueue(criticalLog, TEST_GROUP, Flags.CRITICAL);
    /* Verify listener and database get the same flags. */
    verify(listener).onPreparedLog(criticalLog, TEST_GROUP, Flags.CRITICAL);
    verify(persistence).putLog(criticalLog, TEST_GROUP, Flags.CRITICAL);
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) Log(com.microsoft.appcenter.ingestion.models.Log) 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