Search in sources :

Example 26 with Log

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

the class DefaultChannelTest method setEnabled.

@Test
@SuppressWarnings("unchecked")
public void setEnabled() throws IOException, InterruptedException {
    /* Send a log. */
    Ingestion ingestion = mock(Ingestion.class);
    doThrow(new IOException()).when(ingestion).close();
    Persistence persistence = mock(Persistence.class);
    when(persistence.getLogs(anyString(), anyInt(), anyList())).thenAnswer(getGetLogsAnswer(1));
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), persistence, ingestion, mCoreHandler);
    channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null);
    channel.enqueue(mock(Log.class), TEST_GROUP);
    verify(mHandler).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    /* Disable before timer is triggered. */
    channel.setEnabled(false);
    verify(mHandler).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));
    /* Enable and send a new log. */
    AtomicReference<Runnable> runnable = catchPostRunnable();
    channel.setEnabled(true);
    channel.enqueue(mock(Log.class), TEST_GROUP);
    assertNotNull(runnable.get());
    runnable.get().run();
    verify(ingestion).reopen();
    verify(ingestion).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) IOException(java.io.IOException) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) Test(org.junit.Test)

Example 27 with Log

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

the class DefaultChannelTest method maxRequestsInitial.

@Test
@SuppressWarnings("unchecked")
public void maxRequestsInitial() throws Persistence.PersistenceException {
    Persistence mockPersistence = mock(Persistence.class);
    IngestionHttp mockIngestion = mock(IngestionHttp.class);
    when(mockPersistence.countLogs(any(String.class))).thenReturn(100);
    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, mCoreHandler);
    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 < 100; i++) {
        channel.enqueue(mock(Log.class), TEST_GROUP);
    }
    /* Verify all logs stored, N requests sent, not log deleted yet. */
    verify(mockPersistence, times(100)).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));
    /* Only 2 batches after channel start (non initial logs), verify timer interactions. */
    verify(mHandler, times(2)).postDelayed(any(Runnable.class), eq(BATCH_TIME_INTERVAL));
    verify(mHandler, times(2)).removeCallbacks(any(Runnable.class));
}
Also used : Context(android.content.Context) Log(com.microsoft.appcenter.ingestion.models.Log) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) Persistence(com.microsoft.appcenter.persistence.Persistence) IngestionHttp(com.microsoft.appcenter.ingestion.IngestionHttp) 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 28 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);
    @SuppressWarnings("ConstantConditions") DefaultChannel channel = new DefaultChannel(mock(Context.class), null, persistence, mock(IngestionHttp.class), mCoreHandler);
    channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, 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);
    verify(listener).onEnqueuingLog(log, TEST_GROUP);
    verify(listener, never()).shouldFilter(log);
    verify(persistence, never()).putLog(TEST_GROUP, log);
}
Also used : Context(android.content.Context) Persistence(com.microsoft.appcenter.persistence.Persistence) IngestionHttp(com.microsoft.appcenter.ingestion.IngestionHttp) Log(com.microsoft.appcenter.ingestion.models.Log) DeviceInfoHelper(com.microsoft.appcenter.utils.DeviceInfoHelper) Test(org.junit.Test)

Example 29 with Log

use of com.microsoft.appcenter.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);
    List<Log> logs = new ArrayList<>();
    logs.add(log);
    container.setLogs(logs);
    LogSerializer serializer = mock(LogSerializer.class);
    when(serializer.serializeContainer(any(LogContainer.class))).thenReturn("mockPayload");
    /* 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(IngestionHttp.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 close. */
    ingestionHttp.close();
    verify(httpClient).close();
    /* Verify reopen. */
    ingestionHttp.reopen();
    verify(httpClient).reopen();
}
Also used : Context(android.content.Context) ServiceCall(com.microsoft.appcenter.http.ServiceCall) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) Log(com.microsoft.appcenter.ingestion.models.Log) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Matchers.anyString(org.mockito.Matchers.anyString) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) HttpClientNetworkStateHandler(com.microsoft.appcenter.http.HttpClientNetworkStateHandler) UUID(java.util.UUID) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 30 with Log

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

the class LogContainerTest method compareLogContainer.

@Test
public void compareLogContainer() {
    LogContainer container1 = new LogContainer();
    LogContainer container2 = new LogContainer();
    TestUtils.compareSelfNullClass(container1);
    TestUtils.checkEquals(container1, container2);
    Log log1 = new AbstractLog() {

        @Override
        public String getType() {
            return "null";
        }
    };
    log1.setSid(UUID.randomUUID());
    container1.setLogs(Collections.singletonList(log1));
    TestUtils.compareSelfNullClass(container1);
    TestUtils.checkNotEquals(container1, container2);
    container2.setLogs(Collections.singletonList(log1));
    TestUtils.compareSelfNullClass(container1);
    TestUtils.checkEquals(container1, container2);
    Log log2 = new AbstractLog() {

        @Override
        public String getType() {
            return null;
        }
    };
    log2.setSid(UUID.randomUUID());
    container2.setLogs(Collections.singletonList(log2));
    TestUtils.compareSelfNullClass(container1);
    TestUtils.checkNotEquals(container1, container2);
}
Also used : AbstractLog(com.microsoft.appcenter.ingestion.models.AbstractLog) Log(com.microsoft.appcenter.ingestion.models.Log) AbstractLog(com.microsoft.appcenter.ingestion.models.AbstractLog) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) Test(org.junit.Test)

Aggregations

Log (com.microsoft.appcenter.ingestion.models.Log)64 Test (org.junit.Test)50 ArrayList (java.util.ArrayList)27 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)25 Context (android.content.Context)24 UUID (java.util.UUID)23 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)22 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)20 EventLog (com.microsoft.appcenter.analytics.ingestion.models.EventLog)15 DefaultLogSerializer (com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer)15 StartSessionLog (com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog)14 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)14 Channel (com.microsoft.appcenter.channel.Channel)12 Persistence (com.microsoft.appcenter.persistence.Persistence)12 HashMap (java.util.HashMap)11 Matchers.anyString (org.mockito.Matchers.anyString)11 InvocationOnMock (org.mockito.invocation.InvocationOnMock)11 Date (java.util.Date)10 MediumTest (android.support.test.filters.MediumTest)9 IngestionHttp (com.microsoft.appcenter.ingestion.IngestionHttp)9