Search in sources :

Example 16 with LogSerializer

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

the class DatabasePersistenceTest method clearPendingLogState.

@Test
public void clearPendingLogState() throws JSONException {
    /* groupCount should be <= 9. */
    final int groupCount = 4;
    final int logCount = 10;
    /* Mock logs. */
    List<List<ContentValues>> list = new ArrayList<>();
    for (int i = 0; i < groupCount; i++) {
        List<ContentValues> iterator = new ArrayList<>();
        for (long l = 1; l <= logCount; l++) {
            ContentValues values = mock(ContentValues.class);
            when(values.getAsLong(DatabaseManager.PRIMARY_KEY)).thenReturn(l + i * logCount);
            when(values.getAsString(DatabasePersistence.COLUMN_LOG)).thenReturn("{}");
            iterator.add(values);
        }
        list.add(iterator);
    }
    /* Mock instances. */
    mockStatic(StorageHelper.DatabaseStorage.class);
    StorageHelper.DatabaseStorage mockDatabaseStorage = mock(StorageHelper.DatabaseStorage.class);
    when(StorageHelper.DatabaseStorage.getDatabaseStorage(anyString(), anyString(), anyInt(), any(ContentValues.class), anyInt(), any(StorageHelper.DatabaseStorage.DatabaseErrorListener.class))).thenReturn(mockDatabaseStorage);
    for (int i = 0; i < groupCount; i++) {
        StorageHelper.DatabaseStorage.DatabaseScanner mockDatabaseScanner = mock(StorageHelper.DatabaseStorage.DatabaseScanner.class);
        when(mockDatabaseScanner.iterator()).thenReturn(list.get(i).iterator());
        when(mockDatabaseStorage.getScanner(COLUMN_GROUP, String.valueOf(i))).thenReturn(mockDatabaseScanner);
    }
    LogSerializer mockLogSerializer = mock(LogSerializer.class);
    when(mockLogSerializer.deserializeLog(anyString())).thenReturn(mock(Log.class));
    /* Instantiate Database Persistence. */
    DatabasePersistence persistence = new DatabasePersistence();
    persistence.setLogSerializer(mockLogSerializer);
    /* Get logs. */
    for (int i = 0; i < groupCount; i++) {
        persistence.getLogs(String.valueOf(i), logCount, new ArrayList<Log>());
    }
    /* Verify there are 4 pending groups. */
    assertEquals(groupCount, persistence.mPendingDbIdentifiersGroups.size());
    assertEquals(groupCount * logCount, persistence.mPendingDbIdentifiers.size());
    /* Clear all pending groups and verify. */
    persistence.clearPendingLogState();
    assertEquals(0, persistence.mPendingDbIdentifiersGroups.size());
    assertEquals(0, persistence.mPendingDbIdentifiers.size());
}
Also used : ContentValues(android.content.ContentValues) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) ArrayList(java.util.ArrayList) DefaultLogSerializer(com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) ArrayList(java.util.ArrayList) List(java.util.List) StorageHelper(com.microsoft.azure.mobile.utils.storage.StorageHelper) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 17 with LogSerializer

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

the class DatabasePersistenceTest method databaseOperationException.

@Test
public void databaseOperationException() throws Persistence.PersistenceException, IOException {
    /* Mock instances. */
    mockStatic(MobileCenterLog.class);
    LogSerializer mockSerializer = mock(DefaultLogSerializer.class);
    DatabasePersistence mockPersistence = spy(new DatabasePersistence("test-persistence", "operation.exception", 1));
    doReturn(mockSerializer).when(mockPersistence).getLogSerializer();
    DatabasePersistenceAsync mockPersistenceAsync = spy(new DatabasePersistenceAsync(mockPersistence));
    try {
        /* Generate a log and persist. */
        Log log = mock(Log.class);
        mockPersistenceAsync.putLog("test-p1", log, null);
    } finally {
        /* Close. */
        //noinspection ThrowFromFinallyBlock
        mockPersistenceAsync.close();
    }
    verifyStatic();
    MobileCenterLog.error(eq(MobileCenter.LOG_TAG), anyString(), any(RuntimeException.class));
}
Also used : MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) DefaultLogSerializer(com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 18 with LogSerializer

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

the class IngestionHttpTest method failedSerialization.

@Test
public void failedSerialization() 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);
    JSONException exception = new JSONException("mock");
    when(serializer.serializeContainer(any(LogContainer.class))).thenThrow(exception);
    /* 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/logs?api_version=1.0.0-preview20160914"), eq(METHOD_POST), eq(expectedHeaders), notNull(HttpClient.CallTemplate.class), eq(serviceCallback));
    assertNotNull(callTemplate.get());
    try {
        callTemplate.get().buildRequestBody();
        Assert.fail("Expected json exception");
    } catch (JSONException ignored) {
    }
    /* 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) JSONException(org.json.JSONException) 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)

Example 19 with LogSerializer

use of com.microsoft.azure.mobile.ingestion.models.json.LogSerializer 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 20 with LogSerializer

use of com.microsoft.azure.mobile.ingestion.models.json.LogSerializer 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

LogSerializer (com.microsoft.azure.mobile.ingestion.models.json.LogSerializer)30 Test (org.junit.Test)30 Log (com.microsoft.azure.mobile.ingestion.models.Log)20 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)19 ManagedErrorLog (com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog)15 Context (android.content.Context)14 DefaultLogSerializer (com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer)13 File (java.io.File)13 Channel (com.microsoft.azure.mobile.channel.Channel)12 UUID (java.util.UUID)12 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)11 ArrayList (java.util.ArrayList)11 ErrorReport (com.microsoft.azure.mobile.crashes.model.ErrorReport)9 ErrorAttachmentLog (com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog)8 JSONException (org.json.JSONException)8 MediumTest (android.support.test.filters.MediumTest)7 MockLogFactory (com.microsoft.azure.mobile.ingestion.models.json.MockLogFactory)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5 LogContainer (com.microsoft.azure.mobile.ingestion.models.LogContainer)4 Matchers.anyString (org.mockito.Matchers.anyString)4