Search in sources :

Example 11 with Ingestion

use of com.microsoft.appcenter.ingestion.Ingestion in project mobile-center-sdk-android by Microsoft.

the class DefaultChannelTest method initialLogs.

@Test
public void initialLogs() throws IOException {
    ArgumentCaptor<Runnable> delayedRunnable = ArgumentCaptor.forClass(Runnable.class);
    when(mAppCenterHandler.postDelayed(delayedRunnable.capture(), anyLong())).thenReturn(true);
    Ingestion ingestion = mock(Ingestion.class);
    when(ingestion.isEnabled()).thenReturn(true);
    doThrow(new IOException()).when(ingestion).close();
    Persistence persistence = mock(Persistence.class);
    when(persistence.countLogs(anyString())).thenReturn(3);
    when(persistence.getLogs(anyString(), anyListOf(String.class), anyInt(), anyListOf(Log.class))).thenAnswer(getGetLogsAnswer(3));
    /* Create channel. */
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), persistence, ingestion, mAppCenterHandler);
    channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null);
    verify(ingestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    assertEquals(3, channel.getGroupState(TEST_GROUP).mPendingLogCount);
    delayedRunnable.getValue().run();
    verify(ingestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(mAppCenterHandler).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) IOException(java.io.IOException) 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 12 with Ingestion

use of com.microsoft.appcenter.ingestion.Ingestion in project mobile-center-sdk-android by Microsoft.

the class DefaultChannelTest method setEnabled.

@Test
public void setEnabled() throws IOException {
    /* Send a log. */
    Ingestion ingestion = mock(Ingestion.class);
    when(ingestion.isEnabled()).thenReturn(true);
    doThrow(new IOException()).when(ingestion).close();
    Persistence persistence = mock(Persistence.class);
    when(persistence.getLogs(anyString(), anyListOf(String.class), anyInt(), anyListOf(Log.class))).thenAnswer(getGetLogsAnswer(1));
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), persistence, ingestion, mAppCenterHandler);
    Channel.Listener listener = spy(new AbstractChannelListener());
    channel.addListener(listener);
    channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null);
    channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    verify(mAppCenterHandler).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    /* Disable before timer is triggered. */
    channel.setEnabled(false);
    verify(mAppCenterHandler).removeCallbacks(any(Runnable.class));
    verify(ingestion).close();
    verify(persistence).deleteLogs(TEST_GROUP);
    verify(ingestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(listener).onGloballyEnabled(false);
    /* Enable and send a new log. */
    ArgumentCaptor<Runnable> delayedRunnable = ArgumentCaptor.forClass(Runnable.class);
    when(mAppCenterHandler.postDelayed(delayedRunnable.capture(), anyLong())).thenReturn(true);
    channel.setEnabled(true);
    channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    delayedRunnable.getValue().run();
    verify(ingestion).reopen();
    verify(ingestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(listener).onGloballyEnabled(true);
}
Also used : Context(android.content.Context) Log(com.microsoft.appcenter.ingestion.models.Log) IOException(java.io.IOException) Matchers.anyString(org.mockito.Matchers.anyString) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) Persistence(com.microsoft.appcenter.persistence.Persistence) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Example 13 with Ingestion

use of com.microsoft.appcenter.ingestion.Ingestion 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 14 with Ingestion

use of com.microsoft.appcenter.ingestion.Ingestion 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 15 with Ingestion

use of com.microsoft.appcenter.ingestion.Ingestion in project mobile-center-sdk-android by Microsoft.

the class DefaultChannelOtherOperationsTest method setLogUrl.

@Test
public void setLogUrl() {
    Ingestion ingestion = mock(Ingestion.class);
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mock(Persistence.class), ingestion, mAppCenterHandler);
    String logUrl = "http://mockUrl";
    channel.setLogUrl(logUrl);
    verify(ingestion).setLogUrl(logUrl);
}
Also used : Context(android.content.Context) Persistence(com.microsoft.appcenter.persistence.Persistence) Matchers.anyString(org.mockito.Matchers.anyString) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) Test(org.junit.Test)

Aggregations

Ingestion (com.microsoft.appcenter.ingestion.Ingestion)31 Context (android.content.Context)29 Persistence (com.microsoft.appcenter.persistence.Persistence)29 Test (org.junit.Test)29 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)24 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)24 UUID (java.util.UUID)24 Log (com.microsoft.appcenter.ingestion.models.Log)20 Matchers.anyString (org.mockito.Matchers.anyString)19 AppCenterIngestion (com.microsoft.appcenter.ingestion.AppCenterIngestion)14 IOException (java.io.IOException)13 SocketException (java.net.SocketException)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 HttpException (com.microsoft.appcenter.http.HttpException)4 Answer (org.mockito.stubbing.Answer)4 CancellationException (com.microsoft.appcenter.CancellationException)3 ArrayList (java.util.ArrayList)3 HttpResponse (com.microsoft.appcenter.http.HttpResponse)2 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)1 HashMap (java.util.HashMap)1