Search in sources :

Example 36 with Log

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

the class DatabasePersistenceAsyncTest method putLogFailure.

@Test
public void putLogFailure() throws Persistence.PersistenceException {
    doThrow(new Persistence.PersistenceException("", new IOException())).when(mPersistence).putLog(anyString(), any(Log.class));
    Log log = mock(Log.class);
    mDatabase.putLog(GROUP, log, mCallback);
    verify(mPersistence).putLog(GROUP, log);
    verify(mCallback).onFailure(notNull(Persistence.PersistenceException.class));
}
Also used : MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) IOException(java.io.IOException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 37 with Log

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

the class PushSerializerTest method serialize.

@Test
public void serialize() throws JSONException {
    LogContainer expectedContainer = new LogContainer();
    List<Log> logs = new ArrayList<>();
    {
        PushInstallationLog log = new PushInstallationLog();
        log.setPushToken("TEST");
        logs.add(log);
    }
    expectedContainer.setLogs(logs);
    UUID sid = UUIDUtils.randomUUID();
    for (Log log : logs) {
        log.setSid(sid);
    }
    LogSerializer serializer = new DefaultLogSerializer();
    serializer.addLogFactory(PushInstallationLog.TYPE, new PushInstallationLogFactory());
    String payload = serializer.serializeContainer(expectedContainer);
    LogContainer actualContainer = serializer.deserializeContainer(payload);
    Assert.assertEquals(expectedContainer, actualContainer);
}
Also used : PushInstallationLog(com.microsoft.azure.mobile.push.ingestion.models.PushInstallationLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) PushInstallationLog(com.microsoft.azure.mobile.push.ingestion.models.PushInstallationLog) ArrayList(java.util.ArrayList) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) DefaultLogSerializer(com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) UUID(java.util.UUID) PushInstallationLogFactory(com.microsoft.azure.mobile.push.ingestion.models.json.PushInstallationLogFactory) DefaultLogSerializer(com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer) Test(org.junit.Test)

Example 38 with Log

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

the class DefaultChannelTest method analyticsSuccess.

@Test
@SuppressWarnings("unchecked")
public void analyticsSuccess() throws Persistence.PersistenceException, InterruptedException {
    Persistence mockPersistence = mock(Persistence.class);
    IngestionHttp mockIngestion = mock(IngestionHttp.class);
    Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
    when(mockPersistence.getLogs(any(String.class), anyInt(), any(ArrayList.class))).then(getGetLogsAnswer(50)).then(getGetLogsAnswer(1)).then(getGetLogsAnswer(2));
    when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(getSendAsyncAnswer());
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion);
    channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, mockListener);
    /* Enqueuing 49 events. */
    for (int i = 1; i <= 49; i++) {
        channel.enqueue(mock(Log.class), TEST_GROUP);
        assertEquals(i, channel.getCounter(TEST_GROUP));
    }
    verify(mHandler).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    /* Enqueue another event. */
    channel.enqueue(mock(Log.class), TEST_GROUP);
    verify(mHandler).removeCallbacks(any(Runnable.class));
    /* The counter should be 0 as we reset the counter after reaching the limit of 50. */
    assertEquals(0, channel.getCounter(TEST_GROUP));
    /* Verify that 5 items have been persisted. */
    verify(mockPersistence, times(50)).putLog(eq(TEST_GROUP), any(Log.class));
    /* Verify that we have called sendAsync on the ingestion. */
    verify(mockIngestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Verify that we have called deleteLogs on the Persistence. */
    verify(mockPersistence).deleteLogs(any(String.class), any(String.class));
    /* Verify that we have called onBeforeSending in the listener. */
    verify(mockListener, times(50)).onBeforeSending(any(Log.class));
    /* Verify that we have called onSuccess in the listener. */
    verify(mockListener, times(50)).onSuccess(any(Log.class));
    /* The counter should be 0 now as we sent data. */
    assertEquals(0, channel.getCounter(TEST_GROUP));
    /* Prepare to mock timer. */
    AtomicReference<Runnable> runnable = catchPostRunnable();
    /* Schedule only 1 log after that. */
    channel.enqueue(mock(Log.class), TEST_GROUP);
    assertEquals(1, channel.getCounter(TEST_GROUP));
    verify(mockPersistence, times(51)).putLog(eq(TEST_GROUP), any(Log.class));
    verify(mockIngestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(mockPersistence).deleteLogs(any(String.class), any(String.class));
    verify(mockListener, times(50)).onSuccess(any(Log.class));
    /* Simulate the timer. */
    assertNotNull(runnable.get());
    runnable.get().run();
    runnable.set(null);
    assertEquals(0, channel.getCounter(TEST_GROUP));
    verify(mockIngestion, times(2)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(mockPersistence, times(2)).deleteLogs(any(String.class), any(String.class));
    verify(mockListener, times(51)).onSuccess(any(Log.class));
    /* 2 more timed logs. */
    channel.enqueue(mock(Log.class), TEST_GROUP);
    channel.enqueue(mock(Log.class), TEST_GROUP);
    assertEquals(2, channel.getCounter(TEST_GROUP));
    verify(mockPersistence, times(53)).putLog(eq(TEST_GROUP), any(Log.class));
    verify(mockIngestion, times(2)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(mockPersistence, times(2)).deleteLogs(any(String.class), any(String.class));
    verify(mockListener, times(51)).onSuccess(any(Log.class));
    /* Simulate the timer. */
    assertNotNull(runnable.get());
    runnable.get().run();
    assertEquals(0, channel.getCounter(TEST_GROUP));
    verify(mockIngestion, times(3)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(mockPersistence, times(3)).deleteLogs(any(String.class), any(String.class));
    verify(mockListener, times(53)).onSuccess(any(Log.class));
    /* Check total timers. */
    verify(mHandler, times(3)).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    verify(mHandler).removeCallbacks(any(Runnable.class));
    /* Check channel clear clear */
    channel.clear(TEST_GROUP);
    verify(mockPersistence).deleteLogs(eq(TEST_GROUP));
}
Also used : Context(android.content.Context) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) Persistence(com.microsoft.azure.mobile.persistence.Persistence) IngestionHttp(com.microsoft.azure.mobile.ingestion.IngestionHttp) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Example 39 with Log

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

the class DefaultChannelTest method maxRequests.

@Test
@SuppressWarnings("unchecked")
public void maxRequests() throws Persistence.PersistenceException {
    Persistence mockPersistence = mock(Persistence.class);
    IngestionHttp mockIngestion = mock(IngestionHttp.class);
    when(mockPersistence.getLogs(any(String.class), anyInt(), any(ArrayList.class))).then(getGetLogsAnswer());
    final List<ServiceCallback> callbacks = new ArrayList<>();
    when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(new Answer<Object>() {

        public Object answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            if (args[3] instanceof ServiceCallback) {
                callbacks.add((ServiceCallback) invocation.getArguments()[3]);
            }
            return null;
        }
    });
    /* Init channel with mocks. */
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion);
    channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
    /* Enqueue enough logs to be split in N + 1 maximum requests. */
    for (int i = 0; i < 200; i++) {
        channel.enqueue(mock(Log.class), TEST_GROUP);
    }
    verify(mHandler, times(4)).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    verify(mHandler, times(4)).removeCallbacks(any(Runnable.class));
    /* Verify all logs stored, N requests sent, not log deleted yet. */
    verify(mockPersistence, times(200)).putLog(eq(TEST_GROUP), any(Log.class));
    verify(mockIngestion, times(3)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(mockPersistence, never()).deleteLogs(any(String.class), any(String.class));
    /* Make 1 of the call succeed. Verify log deleted. */
    callbacks.get(0).onCallSucceeded("");
    verify(mockPersistence).deleteLogs(any(String.class), any(String.class));
    /* The request N+1 is now unlocked. */
    verify(mockIngestion, times(4)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Unlock all requests and check logs deleted. */
    for (int i = 1; i < 4; i++) callbacks.get(i).onCallSucceeded("");
    verify(mockPersistence, times(4)).deleteLogs(any(String.class), any(String.class));
    /* The counter should be 0 now as we sent data. */
    assertEquals(0, channel.getCounter(TEST_GROUP));
}
Also used : Context(android.content.Context) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) Persistence(com.microsoft.azure.mobile.persistence.Persistence) IngestionHttp(com.microsoft.azure.mobile.ingestion.IngestionHttp) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) UUID(java.util.UUID) Test(org.junit.Test)

Example 40 with Log

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

the class IngestionHttpTest method sendAsync.

@Test
public void sendAsync() throws Exception {
    /* Build some payload. */
    LogContainer container = new LogContainer();
    Log log = mock(Log.class);
    long logAbsoluteTime = 123L;
    when(log.getToffset()).thenReturn(logAbsoluteTime);
    List<Log> logs = new ArrayList<>();
    logs.add(log);
    container.setLogs(logs);
    LogSerializer serializer = mock(LogSerializer.class);
    when(serializer.serializeContainer(any(LogContainer.class))).thenReturn("mockPayload");
    /* Stable time. */
    mockStatic(System.class);
    long now = 456L;
    when(System.currentTimeMillis()).thenReturn(now);
    /* Configure mock HTTP. */
    HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
    whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
    final ServiceCall call = mock(ServiceCall.class);
    final AtomicReference<HttpClient.CallTemplate> callTemplate = new AtomicReference<>();
    when(httpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class))).then(new Answer<ServiceCall>() {

        @Override
        public ServiceCall answer(InvocationOnMock invocation) throws Throwable {
            callTemplate.set((HttpClient.CallTemplate) invocation.getArguments()[3]);
            return call;
        }
    });
    /* Test calling code. */
    IngestionHttp ingestionHttp = new IngestionHttp(mock(Context.class), serializer);
    ingestionHttp.setLogUrl("http://mock");
    String appSecret = UUIDUtils.randomUUID().toString();
    UUID installId = UUIDUtils.randomUUID();
    ServiceCallback serviceCallback = mock(ServiceCallback.class);
    assertEquals(call, ingestionHttp.sendAsync(appSecret, installId, container, serviceCallback));
    /* Verify call to http client. */
    HashMap<String, String> expectedHeaders = new HashMap<>();
    expectedHeaders.put(APP_SECRET, appSecret);
    expectedHeaders.put(IngestionHttp.INSTALL_ID, installId.toString());
    verify(httpClient).callAsync(eq("http://mock" + IngestionHttp.API_PATH), eq(METHOD_POST), eq(expectedHeaders), notNull(HttpClient.CallTemplate.class), eq(serviceCallback));
    assertNotNull(callTemplate.get());
    assertEquals("mockPayload", callTemplate.get().buildRequestBody());
    /* Verify toffset manipulation. */
    verify(log).setToffset(now - logAbsoluteTime);
    verify(log).setToffset(logAbsoluteTime);
    /* Verify close. */
    ingestionHttp.close();
    verify(httpClient).close();
}
Also used : Context(android.content.Context) ServiceCall(com.microsoft.azure.mobile.http.ServiceCall) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) Matchers.anyString(org.mockito.Matchers.anyString) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) HttpClientNetworkStateHandler(com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler) UUID(java.util.UUID) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Log (com.microsoft.azure.mobile.ingestion.models.Log)59 Test (org.junit.Test)44 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)37 ArrayList (java.util.ArrayList)24 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)20 Context (android.content.Context)18 UUID (java.util.UUID)18 EventLog (com.microsoft.azure.mobile.analytics.ingestion.models.EventLog)13 LogSerializer (com.microsoft.azure.mobile.ingestion.models.json.LogSerializer)13 StartSessionLog (com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog)12 LogContainer (com.microsoft.azure.mobile.ingestion.models.LogContainer)12 DefaultLogSerializer (com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer)11 Persistence (com.microsoft.azure.mobile.persistence.Persistence)11 Matchers.anyString (org.mockito.Matchers.anyString)10 IOException (java.io.IOException)9 Channel (com.microsoft.azure.mobile.channel.Channel)8 IngestionHttp (com.microsoft.azure.mobile.ingestion.IngestionHttp)8 StartServiceLog (com.microsoft.azure.mobile.ingestion.models.StartServiceLog)8 JSONException (org.json.JSONException)8 MediumTest (android.support.test.filters.MediumTest)7