Search in sources :

Example 71 with Log

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

the class DefaultChannelTest method packageManagerIsBroken.

@Test
public void packageManagerIsBroken() throws Persistence.PersistenceException, DeviceInfoHelper.DeviceInfoException {
    /* Setup mocking to make device properties generation fail. */
    when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenThrow(new DeviceInfoHelper.DeviceInfoException("mock", new PackageManager.NameNotFoundException()));
    Persistence persistence = mock(Persistence.class);
    DefaultChannel channel = new DefaultChannel(mock(Context.class), null, persistence, mock(AppCenterIngestion.class), mAppCenterHandler);
    channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null);
    Channel.Listener listener = mock(Channel.Listener.class);
    channel.addListener(listener);
    /* Enqueue a log: listener is called before but then attaching device properties fails before saving the log. */
    Log log = mock(Log.class);
    channel.enqueue(log, TEST_GROUP, Flags.DEFAULTS);
    verify(listener).onPreparingLog(log, TEST_GROUP);
    verify(listener, never()).shouldFilter(log);
    verify(persistence, never()).putLog(eq(log), eq(TEST_GROUP), anyInt());
}
Also used : Context(android.content.Context) Persistence(com.microsoft.appcenter.persistence.Persistence) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) Log(com.microsoft.appcenter.ingestion.models.Log) DeviceInfoHelper(com.microsoft.appcenter.utils.DeviceInfoHelper) Test(org.junit.Test)

Example 72 with Log

use of com.microsoft.appcenter.ingestion.models.Log 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 73 with Log

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

the class OneCollectorIngestionTest method ticketsFailToSerialize.

@Test
public void ticketsFailToSerialize() throws Exception {
    /* Build some payload. */
    final CommonSchemaLog log1 = mock(CommonSchemaLog.class);
    final List<String> ticketKeys = new ArrayList<String>() {

        {
            add("key1");
        }
    };
    TicketCache.putTicket("key1", "value1");
    Extensions ext1 = new Extensions() {

        {
            setProtocol(new ProtocolExtension() {

                {
                    setTicketKeys(ticketKeys);
                }
            });
        }
    };
    when(log1.getExt()).thenReturn(ext1);
    LogContainer container = new LogContainer() {

        {
            setLogs(new ArrayList<Log>() {

                {
                    add(log1);
                }
            });
        }
    };
    JSONObject ticketJson = mock(JSONObject.class);
    whenNew(JSONObject.class).withNoArguments().thenReturn(ticketJson);
    when(ticketJson.put(anyString(), anyString())).thenThrow(new JSONException("mock"));
    /* Configure mock HTTP. */
    ServiceCall call = mock(ServiceCall.class);
    when(mHttpClient.callAsync(anyString(), anyString(), mHeadersCaptor.capture(), any(HttpClient.CallTemplate.class), any(ServiceCallback.class))).thenReturn(call);
    /* Verify call to http client. */
    LogSerializer serializer = mock(LogSerializer.class);
    OneCollectorIngestion ingestion = new OneCollectorIngestion(mHttpClient, serializer);
    ingestion.setLogUrl("http://mock");
    ServiceCallback serviceCallback = mock(ServiceCallback.class);
    assertEquals(call, ingestion.sendAsync(null, null, container, serviceCallback));
    /* Verify call to http client was made without headers as JSON failed. */
    Map<String, String> headers = mHeadersCaptor.getValue();
    assertFalse(headers.containsKey(TICKETS));
}
Also used : ServiceCall(com.microsoft.appcenter.http.ServiceCall) ProtocolExtension(com.microsoft.appcenter.ingestion.models.one.ProtocolExtension) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) Matchers.anyString(org.mockito.Matchers.anyString) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Extensions(com.microsoft.appcenter.ingestion.models.one.Extensions) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) JSONObject(org.json.JSONObject) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 74 with Log

use of com.microsoft.appcenter.ingestion.models.Log 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 75 with Log

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

the class DefaultChannelTest method checkPendingLogsSendsAllBatchesIfTimerIsOver.

@Test
public void checkPendingLogsSendsAllBatchesIfTimerIsOver() {
    /* Mock current time. */
    long now = 5000;
    when(System.currentTimeMillis()).thenReturn(now);
    /* Mock stored start time. */
    long startTimer = 1000;
    when(SharedPreferencesManager.getLong(eq(START_TIMER_PREFIX + TEST_GROUP))).thenReturn(startTimer);
    /* Mock persistence. */
    Persistence mockPersistence = mock(Persistence.class);
    when(mockPersistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer()).then(getGetLogsAnswer(50)).then(getGetLogsAnswer(50)).then(getGetLogsAnswer(50)).then(getGetLogsAnswer(50)).then(getGetLogsAnswer(0));
    /* Mock sending logs. */
    final List<ServiceCallback> callbacks = new ArrayList<>();
    AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
    when(mockIngestion.isEnabled()).thenReturn(true);
    when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(new Answer<Object>() {

        public Object answer(InvocationOnMock invocation) {
            Object[] args = invocation.getArguments();
            if (args[3] instanceof ServiceCallback) {
                callbacks.add((ServiceCallback) invocation.getArguments()[3]);
            }
            return null;
        }
    });
    /* Create channel and group. */
    DefaultChannel channel = spy(new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, mockIngestion, mAppCenterHandler));
    channel.addGroup(TEST_GROUP, 50, CUSTOM_INTERVAL, MAX_PARALLEL_BATCHES, null, mock(Channel.GroupListener.class));
    /* Prepare to mock timer. */
    long timeDelay = CUSTOM_INTERVAL - (now - startTimer);
    /* Enqueuing 200 events. */
    for (int i = 0; i < 200; i++) {
        channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
    }
    /* Check invoke the timer with custom timestamp. */
    ArgumentCaptor<Runnable> delayedRunnable = ArgumentCaptor.forClass(Runnable.class);
    verify(mAppCenterHandler).postDelayed(delayedRunnable.capture(), eq(timeDelay));
    /* Wait for timer. */
    now = 12000;
    when(System.currentTimeMillis()).thenReturn(now);
    delayedRunnable.getValue().run();
    /* Check sending the logs batches. */
    verify(mockIngestion, times(3)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Successful finish one of sending the log. */
    callbacks.get(0).onCallSucceeded(new HttpResponse(200, ""));
    verify(mockPersistence).deleteLogs(any(String.class), any(String.class));
    /* Check rest logs sending. */
    verify(mockIngestion, times(4)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
}
Also used : Log(com.microsoft.appcenter.ingestion.models.Log) ArrayList(java.util.ArrayList) HttpResponse(com.microsoft.appcenter.http.HttpResponse) Matchers.anyString(org.mockito.Matchers.anyString) Persistence(com.microsoft.appcenter.persistence.Persistence) AppCenterIngestion(com.microsoft.appcenter.ingestion.AppCenterIngestion) 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)

Aggregations

Log (com.microsoft.appcenter.ingestion.models.Log)189 Test (org.junit.Test)150 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)83 ArrayList (java.util.ArrayList)75 Context (android.content.Context)74 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)65 UUID (java.util.UUID)57 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)56 Matchers.anyString (org.mockito.Matchers.anyString)51 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)45 DefaultLogSerializer (com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer)44 Persistence (com.microsoft.appcenter.persistence.Persistence)38 EventLog (com.microsoft.appcenter.analytics.ingestion.models.EventLog)34 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)32 CommonSchemaLog (com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog)32 HashMap (java.util.HashMap)32 StartSessionLog (com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog)29 Channel (com.microsoft.appcenter.channel.Channel)27 Date (java.util.Date)27 InvocationOnMock (org.mockito.invocation.InvocationOnMock)26