Search in sources :

Example 81 with Log

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

the class DatabasePersistenceTest method clearPendingLogState.

@Test
public void clearPendingLogState() throws Exception {
    /* 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. */
    DatabaseManager mockDatabaseManager = mock(DatabaseManager.class);
    whenNew(DatabaseManager.class).withAnyArguments().thenReturn(mockDatabaseManager);
    when(mockDatabaseManager.nextValues(any(Cursor.class))).thenCallRealMethod();
    for (int i = 0; i < groupCount; i++) {
        MockCursor mockCursor = new MockCursor(list.get(i));
        mockCursor.mockBuildValues(mockDatabaseManager);
        when(mockDatabaseManager.getCursor(any(SQLiteQueryBuilder.class), any(String[].class), eq(new String[] { String.valueOf(i) }), anyString())).thenReturn(mockCursor);
    }
    LogSerializer mockLogSerializer = mock(LogSerializer.class);
    when(mockLogSerializer.deserializeLog(anyString(), anyString())).thenReturn(mock(Log.class));
    /* Instantiate Database Persistence. */
    DatabasePersistence persistence = new DatabasePersistence(mock(Context.class));
    persistence.setLogSerializer(mockLogSerializer);
    /* Get logs. */
    for (int i = 0; i < groupCount; i++) {
        persistence.getLogs(String.valueOf(i), Collections.<String>emptyList(), 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) Context(android.content.Context) DatabaseManager(com.microsoft.appcenter.utils.storage.DatabaseManager) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) Log(com.microsoft.appcenter.ingestion.models.Log) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Cursor(android.database.Cursor) ArrayList(java.util.ArrayList) List(java.util.List) SQLiteQueryBuilder(android.database.sqlite.SQLiteQueryBuilder) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 82 with Log

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

the class DatabasePersistenceTest method getLogsWithGetCorruptedIdsException.

@Test
public void getLogsWithGetCorruptedIdsException() throws Exception {
    /* Mock instances. */
    mockStatic(AppCenterLog.class);
    DatabaseManager databaseManager = mock(DatabaseManager.class);
    whenNew(DatabaseManager.class).withAnyArguments().thenReturn(databaseManager);
    when(databaseManager.nextValues(any(Cursor.class))).thenCallRealMethod();
    /* Make corrupted log. */
    List<ContentValues> fieldValues = new ArrayList<>();
    {
        /* Empty record, "corrupted", cause identifier is null (and no other field either). */
        ContentValues contentValues = mock(ContentValues.class);
        when(contentValues.getAsLong(DatabaseManager.PRIMARY_KEY)).thenReturn(null);
        fieldValues.add(contentValues);
    }
    /* Mock log sequence retrieved from cursor. */
    MockCursor mockCursor = new MockCursor(fieldValues);
    mockCursor.mockBuildValues(databaseManager);
    when(databaseManager.getCursor(any(SQLiteQueryBuilder.class), isNull(String[].class), any(String[].class), anyString())).thenReturn(mockCursor);
    /* Mock second cursor with identifiers only. */
    Cursor failingCursor = mock(Cursor.class);
    when(failingCursor.moveToNext()).thenThrow(new SQLiteDiskIOException());
    when(databaseManager.getCursor(any(SQLiteQueryBuilder.class), isNotNull(String[].class), any(String[].class), anyString())).thenReturn(failingCursor);
    /* Get logs and verify we get only non corrupted logs. */
    DatabasePersistence persistence = new DatabasePersistence(mock(Context.class));
    ArrayList<Log> outLogs = new ArrayList<>();
    persistence.getLogs("mock", Collections.<String>emptyList(), 50, outLogs);
    assertEquals(0, outLogs.size());
    /* There is an error log. */
    verifyStatic();
    AppCenterLog.error(eq(AppCenter.LOG_TAG), anyString(), any(RuntimeException.class));
}
Also used : ContentValues(android.content.ContentValues) Context(android.content.Context) DatabaseManager(com.microsoft.appcenter.utils.storage.DatabaseManager) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) Log(com.microsoft.appcenter.ingestion.models.Log) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) SQLiteDiskIOException(android.database.sqlite.SQLiteDiskIOException) SQLiteQueryBuilder(android.database.sqlite.SQLiteQueryBuilder) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 83 with Log

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

the class DatabasePersistenceTest method getLogsWithGetCursorException.

@Test
public void getLogsWithGetCursorException() throws Exception {
    /* Mock instances. */
    mockStatic(AppCenterLog.class);
    DatabaseManager databaseManager = mock(DatabaseManager.class);
    whenNew(DatabaseManager.class).withAnyArguments().thenReturn(databaseManager);
    when(databaseManager.nextValues(any(Cursor.class))).thenCallRealMethod();
    when(databaseManager.getCursor(any(SQLiteQueryBuilder.class), any(String[].class), any(String[].class), anyString())).thenThrow(new RuntimeException());
    DatabasePersistence persistence = new DatabasePersistence(mock(Context.class), 1, DatabasePersistence.SCHEMA);
    /* Try to get logs. */
    ArrayList<Log> outLogs = new ArrayList<>();
    persistence.getLogs("mock", Collections.<String>emptyList(), 50, outLogs);
    assertEquals(0, outLogs.size());
    /* There is an error log. */
    verifyStatic();
    AppCenterLog.error(eq(AppCenter.LOG_TAG), anyString(), any(RuntimeException.class));
}
Also used : Context(android.content.Context) DatabaseManager(com.microsoft.appcenter.utils.storage.DatabaseManager) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) Log(com.microsoft.appcenter.ingestion.models.Log) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) SQLiteQueryBuilder(android.database.sqlite.SQLiteQueryBuilder) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 84 with Log

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

the class DatabasePersistenceTest method getLogsWithCorruption.

@Test
public void getLogsWithCorruption() throws Exception {
    /* Mock instances. */
    int logCount = 3;
    DatabaseManager databaseManager = mock(DatabaseManager.class);
    whenNew(DatabaseManager.class).withAnyArguments().thenReturn(databaseManager);
    when(databaseManager.nextValues(any(Cursor.class))).thenCallRealMethod();
    /* Make 3 logs, the second one will be corrupted. */
    List<ContentValues> fieldValues = new ArrayList<>(logCount);
    {
        /* Valid record. */
        ContentValues contentValues = mock(ContentValues.class);
        when(contentValues.getAsLong(DatabaseManager.PRIMARY_KEY)).thenReturn(0L);
        when(contentValues.getAsString(DatabasePersistence.COLUMN_LOG)).thenReturn("first");
        fieldValues.add(contentValues);
    }
    {
        /* Empty record, "corrupted", cause identifier is null (and no other field either). */
        ContentValues contentValues = mock(ContentValues.class);
        when(contentValues.getAsLong(DatabaseManager.PRIMARY_KEY)).thenReturn(null);
        fieldValues.add(contentValues);
    }
    {
        /* Valid record. */
        ContentValues contentValues = mock(ContentValues.class);
        when(contentValues.getAsLong(DatabaseManager.PRIMARY_KEY)).thenReturn(2L);
        when(contentValues.getAsString(DatabasePersistence.COLUMN_LOG)).thenReturn("last");
        fieldValues.add(contentValues);
    }
    /* Mock log sequence retrieved from cursor. */
    MockCursor mockCursor = new MockCursor(fieldValues);
    mockCursor.mockBuildValues(databaseManager);
    when(databaseManager.getCursor(any(SQLiteQueryBuilder.class), isNull(String[].class), any(String[].class), anyString())).thenReturn(mockCursor);
    /* Mock second cursor with identifiers only. */
    List<ContentValues> idValues = new ArrayList<>(logCount);
    for (long i = 0; i < logCount; i++) {
        ContentValues contentValues = mock(ContentValues.class);
        when(contentValues.getAsLong(DatabaseManager.PRIMARY_KEY)).thenReturn(i);
        idValues.add(contentValues);
    }
    MockCursor mockIdCursor = new MockCursor(idValues);
    mockIdCursor.mockBuildValues(databaseManager);
    when(databaseManager.getCursor(any(SQLiteQueryBuilder.class), isNotNull(String[].class), any(String[].class), anyString())).thenReturn(mockIdCursor);
    /* Mock serializer and eventually the database. */
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString(), anyString())).thenAnswer(new Answer<Log>() {

        @Override
        public Log answer(InvocationOnMock invocation) {
            /* Hack serializer to return type = payload to simplify checking. */
            Log log = mock(Log.class);
            when(log.getType()).thenReturn((String) invocation.getArguments()[0]);
            return log;
        }
    });
    DatabasePersistence persistence = new DatabasePersistence(mock(Context.class));
    persistence.setLogSerializer(logSerializer);
    /* Get logs and verify we get only non corrupted logs. */
    ArrayList<Log> outLogs = new ArrayList<>();
    persistence.getLogs("mock", Collections.<String>emptyList(), 50, outLogs);
    assertEquals(logCount - 1, outLogs.size());
    assertEquals("first", outLogs.get(0).getType());
    assertEquals("last", outLogs.get(1).getType());
    /* Verify we detected and deleted the corrupted log, the second one. */
    verify(databaseManager).delete(1);
    /* Verify next call is empty logs as they are pending. */
    outLogs = new ArrayList<>();
    persistence.getLogs("mock", Collections.<String>emptyList(), 50, outLogs);
    assertEquals(0, outLogs.size());
    /*
         * Add new logs with corruption again. First 2 logs are still there.
         * Also this time the corrupted log will not even return its identifier when scanning
         * with only id fields, to test that the delete fails gracefully and that we can still
         * work with other logs.
         */
    logCount = 4;
    fieldValues = new ArrayList<>(logCount);
    {
        /* Valid record. */
        ContentValues contentValues = mock(ContentValues.class);
        when(contentValues.getAsLong(DatabaseManager.PRIMARY_KEY)).thenReturn(0L);
        when(contentValues.getAsString(DatabasePersistence.COLUMN_LOG)).thenReturn("first");
        fieldValues.add(contentValues);
    }
    {
        /* Valid record. */
        ContentValues contentValues = mock(ContentValues.class);
        when(contentValues.getAsLong(DatabaseManager.PRIMARY_KEY)).thenReturn(2L);
        when(contentValues.getAsString(DatabasePersistence.COLUMN_LOG)).thenReturn("last");
        fieldValues.add(contentValues);
    }
    {
        /* New corrupted record. */
        ContentValues contentValues = mock(ContentValues.class);
        when(contentValues.getAsLong(DatabaseManager.PRIMARY_KEY)).thenReturn(null);
        fieldValues.add(contentValues);
    }
    {
        /* Valid new record. */
        ContentValues contentValues = mock(ContentValues.class);
        when(contentValues.getAsLong(DatabaseManager.PRIMARY_KEY)).thenReturn(4L);
        when(contentValues.getAsString(DatabasePersistence.COLUMN_LOG)).thenReturn("true last");
        fieldValues.add(contentValues);
    }
    mockCursor = new MockCursor(fieldValues) {

        @Override
        public void close() {
            /* It should be ignored. */
            throw new RuntimeException();
        }
    };
    mockCursor.mockBuildValues(databaseManager);
    when(databaseManager.getCursor(any(SQLiteQueryBuilder.class), isNull(String[].class), any(String[].class), anyString())).thenReturn(mockCursor);
    idValues = new ArrayList<>(4);
    /* Here the id cursor will also skip the new corrupted log which id would be 3. */
    for (long i = 0; i < logCount; i += 2) {
        ContentValues contentValues = mock(ContentValues.class);
        when(contentValues.getAsLong(DatabaseManager.PRIMARY_KEY)).thenReturn(i);
        idValues.add(contentValues);
    }
    mockIdCursor = new MockCursor(idValues) {

        @Override
        public void close() {
            /* It should be ignored. */
            throw new RuntimeException();
        }
    };
    mockIdCursor.mockBuildValues(databaseManager);
    when(databaseManager.getCursor(any(SQLiteQueryBuilder.class), isNotNull(String[].class), any(String[].class), anyString())).thenReturn(mockIdCursor);
    /* Verify next call is only the new valid log as others are marked pending. */
    outLogs = new ArrayList<>();
    persistence.getLogs("mock", Collections.<String>emptyList(), 50, outLogs);
    assertEquals(1, outLogs.size());
    assertEquals("true last", outLogs.get(0).getType());
    /* Verify that the only log we deleted in the entire test was the one from previous test (id=1). */
    verify(databaseManager).delete(anyLong());
}
Also used : ContentValues(android.content.ContentValues) Context(android.content.Context) DatabaseManager(com.microsoft.appcenter.utils.storage.DatabaseManager) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) Log(com.microsoft.appcenter.ingestion.models.Log) ArrayList(java.util.ArrayList) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Matchers.anyString(org.mockito.Matchers.anyString) Cursor(android.database.Cursor) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SQLiteQueryBuilder(android.database.sqlite.SQLiteQueryBuilder) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 85 with Log

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

the class DatabasePersistenceTest method getLogsWithMoveNextException.

@Test
public void getLogsWithMoveNextException() throws Exception {
    /* Mock instances. */
    mockStatic(AppCenterLog.class);
    DatabaseManager databaseManager = mock(DatabaseManager.class);
    whenNew(DatabaseManager.class).withAnyArguments().thenReturn(databaseManager);
    when(databaseManager.nextValues(any(Cursor.class))).thenCallRealMethod();
    Cursor mockCursor = mock(Cursor.class);
    when(mockCursor.moveToNext()).thenThrow(new RuntimeException());
    when(databaseManager.getCursor(any(SQLiteQueryBuilder.class), any(String[].class), any(String[].class), anyString())).thenReturn(mockCursor);
    DatabasePersistence persistence = new DatabasePersistence(mock(Context.class), 1, DatabasePersistence.SCHEMA);
    /* Try to get logs. */
    ArrayList<Log> outLogs = new ArrayList<>();
    persistence.getLogs("mock", Collections.<String>emptyList(), 50, outLogs);
    assertEquals(0, outLogs.size());
    /* There is an error log. */
    verifyStatic();
    AppCenterLog.error(eq(AppCenter.LOG_TAG), anyString(), any(RuntimeException.class));
}
Also used : Context(android.content.Context) DatabaseManager(com.microsoft.appcenter.utils.storage.DatabaseManager) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) Log(com.microsoft.appcenter.ingestion.models.Log) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) SQLiteQueryBuilder(android.database.sqlite.SQLiteQueryBuilder) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) 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