Search in sources :

Example 1 with MockCommonSchemaLog

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

the class DatabasePersistenceAndroidTest method putLargeLogNotSupportedOnCommonSchema.

@Test
public void putLargeLogNotSupportedOnCommonSchema() throws JSONException {
    /* Initialize database persistence. */
    DatabasePersistence persistence = new DatabasePersistence(sContext);
    /* Set a mock log serializer. */
    LogSerializer logSerializer = new DefaultLogSerializer();
    logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
    persistence.setLogSerializer(logSerializer);
    try {
        /* Initial count is 0. */
        assertEquals(0, persistence.countLogs("test-p1"));
        /* Generate a large log. */
        CommonSchemaLog log = new MockCommonSchemaLog();
        int size = 2 * 1024 * 1024;
        StringBuilder largeValue = new StringBuilder(size);
        for (int i = 0; i < size; i++) {
            largeValue.append("x");
        }
        log.setVer("3.0");
        log.setName("test");
        log.setTimestamp(new Date());
        log.addTransmissionTarget("token");
        Data data = new Data();
        log.setData(data);
        data.getProperties().put("key", largeValue.toString());
        /* Persisting that log should fail. */
        try {
            persistence.putLog(log, "test-p1", NORMAL);
            fail("Inserting large common schema log is not supposed to work");
        } catch (PersistenceException e) {
            /* Count logs is still 0 */
            e.printStackTrace();
            assertEquals(0, persistence.countLogs("test-p1"));
        }
    } finally {
        persistence.close();
    }
}
Also used : MockLogFactory(com.microsoft.appcenter.ingestion.models.json.MockLogFactory) MockCommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) MockCommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog) PersistenceException(com.microsoft.appcenter.persistence.Persistence.PersistenceException) Data(com.microsoft.appcenter.ingestion.models.one.Data) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) SuppressLint(android.annotation.SuppressLint) Date(java.util.Date) MediumTest(androidx.test.filters.MediumTest) Test(org.junit.Test)

Example 2 with MockCommonSchemaLog

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

the class DatabasePersistenceAndroidTest method upgradeFromVersion5to6.

@Test
public void upgradeFromVersion5to6() throws PersistenceException, JSONException {
    /* Initialize database persistence with old version. */
    ContentValues schema = new ContentValues(SCHEMA);
    DatabaseManager databaseManager = new DatabaseManager(sContext, DatabasePersistence.DATABASE, DatabasePersistence.TABLE, DatabasePersistence.VERSION_TIMESTAMP_COLUMN, schema, CREATE_LOGS_SQL, mock(DatabaseManager.Listener.class));
    /* Init log serializer. */
    LogSerializer logSerializer = new DefaultLogSerializer();
    logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
    logSerializer.addLogFactory(MockCommonSchemaLog.TYPE, new MockCommonSchemaLogFactory());
    /* Insert old data before upgrade. */
    Log oldLog = AndroidTestUtils.generateMockLog();
    try {
        ContentValues contentValues = new ContentValues();
        contentValues.put(DatabasePersistence.COLUMN_GROUP, "test");
        contentValues.put(DatabasePersistence.COLUMN_LOG, logSerializer.serializeLog(oldLog));
        databaseManager.put(contentValues, DatabasePersistence.COLUMN_PRIORITY);
    } finally {
        databaseManager.close();
    }
    /* Upgrade. */
    DatabasePersistence persistence = new DatabasePersistence(sContext);
    persistence.setLogSerializer(logSerializer);
    /* Prepare a common schema log. */
    MockCommonSchemaLog commonSchemaLog = new MockCommonSchemaLog();
    commonSchemaLog.setName("test");
    commonSchemaLog.setIKey("o:test");
    commonSchemaLog.setTimestamp(new Date());
    commonSchemaLog.setVer("3.0");
    commonSchemaLog.addTransmissionTarget("test-guid");
    /* Check upgrade. */
    try {
        /* Verify old data cleared. */
        assertEquals(0, persistence.countLogs("test"));
        /* Put new data. */
        persistence.putLog(commonSchemaLog, "test/one", NORMAL);
    } finally {
        persistence.close();
    }
    /* Get new data after restart. */
    persistence = new DatabasePersistence(sContext);
    persistence.setLogSerializer(logSerializer);
    try {
        /* Get new data. */
        assertEquals(1, persistence.countLogs("test/one"));
        List<Log> outputLogs = new ArrayList<>();
        persistence.getLogs("test/one", Collections.<String>emptyList(), 1, outputLogs);
        assertEquals(1, outputLogs.size());
        assertEquals(commonSchemaLog, outputLogs.get(0));
        /* Verify target token is encrypted. */
        ContentValues values = getContentValues(persistence, "test/one");
        String token = values.getAsString(DatabasePersistence.COLUMN_TARGET_TOKEN);
        assertNotNull(token);
        assertNotEquals("test-guid", token);
        assertEquals("test-guid", CryptoUtils.getInstance(sContext).decrypt(token).getDecryptedData());
        /* Verify target key stored as well. */
        String targetKey = values.getAsString(DatabasePersistence.COLUMN_TARGET_KEY);
        assertEquals(commonSchemaLog.getIKey(), "o:" + targetKey);
        /* Verify priority stored too. */
        assertEquals((Integer) NORMAL, values.getAsInteger(DatabasePersistence.COLUMN_PRIORITY));
    } finally {
        persistence.close();
    }
}
Also used : ContentValues(android.content.ContentValues) DatabaseManager(com.microsoft.appcenter.utils.storage.DatabaseManager) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) MockCommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog) Log(com.microsoft.appcenter.ingestion.models.Log) MockLog(com.microsoft.appcenter.ingestion.models.json.MockLog) MockCommonSchemaLogFactory(com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLogFactory) ArrayList(java.util.ArrayList) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Mockito.anyString(org.mockito.Mockito.anyString) TestUtils.generateString(com.microsoft.appcenter.test.TestUtils.generateString) Date(java.util.Date) MockLogFactory(com.microsoft.appcenter.ingestion.models.json.MockLogFactory) MockCommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) MediumTest(androidx.test.filters.MediumTest) Test(org.junit.Test)

Example 3 with MockCommonSchemaLog

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

the class DatabasePersistenceAndroidTest method generateCsLogsWithIKey.

/**
 * Utility for getLogsFilteringOutPausedTargetKeys test.
 */
private void generateCsLogsWithIKey(DatabasePersistence persistence, String iKey, int numberOfLogsPerKey) throws PersistenceException {
    for (int i = 0; i < numberOfLogsPerKey; i++) {
        CommonSchemaLog log = new MockCommonSchemaLog();
        log.setVer("3.0");
        log.setName("test");
        log.setTimestamp(new Date());
        log.setIKey(iKey);
        log.addTransmissionTarget(iKey + "-token");
        persistence.putLog(log, "test", NORMAL);
    }
}
Also used : MockCommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) MockCommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog) SuppressLint(android.annotation.SuppressLint) Date(java.util.Date)

Example 4 with MockCommonSchemaLog

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

the class OneCollectorChannelListenerTest method enqueueConvertedLogs.

@Test
public void enqueueConvertedLogs() {
    /* Mock original log. */
    Log originalLog = mock(Log.class);
    when(originalLog.getTransmissionTargetTokens()).thenReturn(new HashSet<>(Collections.singletonList("t1")));
    /* Mock a log. */
    CommonSchemaLog log1 = new MockCommonSchemaLog();
    log1.setIKey("t1");
    Extensions ext1 = new Extensions();
    ext1.setSdk(new SdkExtension());
    log1.setExt(ext1);
    /* Mock another log. */
    CommonSchemaLog log2 = new MockCommonSchemaLog();
    log2.setIKey("t1");
    Extensions ext2 = new Extensions();
    ext2.setSdk(new SdkExtension());
    log2.setExt(ext2);
    /* Mock conversion of logs. */
    Channel channel = mock(Channel.class);
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.toCommonSchemaLog(any(Log.class))).thenReturn(Arrays.asList(log1, log2));
    /* Init listener. */
    UUID installId = UUID.randomUUID();
    OneCollectorChannelListener listener = new OneCollectorChannelListener(channel, logSerializer, createHttpClient(mock(Context.class)), installId);
    listener.onPreparedLog(originalLog, TEST_GROUP, DEFAULTS);
    listener.onPreparedLog(mock(CommonSchemaLog.class), TEST_GROUP + ONE_COLLECTOR_GROUP_NAME_SUFFIX, DEFAULTS);
    /* Verify conversion. */
    verify(logSerializer).toCommonSchemaLog(originalLog);
    verifyNoMoreInteractions(logSerializer);
    /* Verify flags. */
    assertEquals(Long.valueOf(DEFAULTS), log1.getFlags());
    assertEquals(Long.valueOf(DEFAULTS), log2.getFlags());
    /* Verify same epoch. */
    assertNotNull(log1.getExt().getSdk().getEpoch());
    assertEquals(log1.getExt().getSdk().getEpoch(), log2.getExt().getSdk().getEpoch());
    /* Verify incremented sequence numbers. */
    assertEquals(Long.valueOf(1), log1.getExt().getSdk().getSeq());
    assertEquals(Long.valueOf(2), log2.getExt().getSdk().getSeq());
    /* Verify install ID set. */
    assertEquals(installId, log1.getExt().getSdk().getInstallId());
    assertEquals(installId, log2.getExt().getSdk().getInstallId());
    /* Verify enqueue. */
    verify(channel).enqueue(log1, TEST_GROUP + ONE_COLLECTOR_GROUP_NAME_SUFFIX, DEFAULTS);
    verify(channel).enqueue(log2, TEST_GROUP + ONE_COLLECTOR_GROUP_NAME_SUFFIX, DEFAULTS);
    /* We simulated that we see on prepared log on the enqueued log, verify no more enqueuing. */
    verify(channel, times(2)).enqueue(any(Log.class), anyString(), eq(DEFAULTS));
    /* Mock log with another key to see new seq/epoch. */
    when(originalLog.getTransmissionTargetTokens()).thenReturn(new HashSet<>(Collections.singletonList("t2")));
    CommonSchemaLog log3 = new MockCommonSchemaLog();
    log3.setIKey("t2");
    Extensions ext3 = new Extensions();
    ext3.setSdk(new SdkExtension());
    log3.setExt(ext3);
    when(logSerializer.toCommonSchemaLog(any(Log.class))).thenReturn(Collections.singletonList(log3));
    listener.onPreparedLog(originalLog, TEST_GROUP, CRITICAL);
    assertEquals(Long.valueOf(CRITICAL), log3.getFlags());
    assertEquals(Long.valueOf(1), log3.getExt().getSdk().getSeq());
    assertNotNull(log3.getExt().getSdk().getEpoch());
    assertNotEquals(log1.getExt().getSdk().getEpoch(), log3.getExt().getSdk().getEpoch());
    /* Simulate disable/enable to reset epoch/seq. */
    listener.onGloballyEnabled(false);
    listener.onGloballyEnabled(true);
    /* Mock a 4rd log in first group to check reset. */
    CommonSchemaLog log4 = new MockCommonSchemaLog();
    log4.setIKey("t2");
    Extensions ext4 = new Extensions();
    ext4.setSdk(new SdkExtension());
    log4.setExt(ext4);
    when(logSerializer.toCommonSchemaLog(any(Log.class))).thenReturn(Collections.singletonList(log4));
    listener.onPreparedLog(originalLog, TEST_GROUP, NORMAL);
    /* Verify flags and reset of epoch/seq. */
    assertEquals(Long.valueOf(NORMAL), log4.getFlags());
    assertEquals(Long.valueOf(1), log4.getExt().getSdk().getSeq());
    assertNotNull(log4.getExt().getSdk().getEpoch());
    assertNotEquals(log3.getExt().getSdk().getEpoch(), log4.getExt().getSdk().getEpoch());
}
Also used : CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) MockCommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog) Log(com.microsoft.appcenter.ingestion.models.Log) MockCommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) MockCommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog) SdkExtension(com.microsoft.appcenter.ingestion.models.one.SdkExtension) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Extensions(com.microsoft.appcenter.ingestion.models.one.Extensions) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

CommonSchemaLog (com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog)4 MockCommonSchemaLog (com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog)4 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)3 Date (java.util.Date)3 Test (org.junit.Test)3 SuppressLint (android.annotation.SuppressLint)2 MediumTest (androidx.test.filters.MediumTest)2 Log (com.microsoft.appcenter.ingestion.models.Log)2 DefaultLogSerializer (com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer)2 MockLogFactory (com.microsoft.appcenter.ingestion.models.json.MockLogFactory)2 ContentValues (android.content.ContentValues)1 MockLog (com.microsoft.appcenter.ingestion.models.json.MockLog)1 Data (com.microsoft.appcenter.ingestion.models.one.Data)1 Extensions (com.microsoft.appcenter.ingestion.models.one.Extensions)1 MockCommonSchemaLogFactory (com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLogFactory)1 SdkExtension (com.microsoft.appcenter.ingestion.models.one.SdkExtension)1 PersistenceException (com.microsoft.appcenter.persistence.Persistence.PersistenceException)1 TestUtils.generateString (com.microsoft.appcenter.test.TestUtils.generateString)1 DatabaseManager (com.microsoft.appcenter.utils.storage.DatabaseManager)1 ArrayList (java.util.ArrayList)1