Search in sources :

Example 11 with CommonSchemaLog

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

the class EventLogFactory method toCommonSchemaLogs.

@Override
public Collection<CommonSchemaLog> toCommonSchemaLogs(Log log) {
    Collection<CommonSchemaLog> commonSchemaLogs = new LinkedList<>();
    for (String transmissionTarget : log.getTransmissionTargetTokens()) {
        /* Part A common fields. */
        CommonSchemaEventLog commonSchemaEventLog = new CommonSchemaEventLog();
        /* Event name goes to Part A. */
        EventLog eventLog = (EventLog) log;
        PartAUtils.setName(commonSchemaEventLog, eventLog.getName());
        /* Add common Part A fields. */
        PartAUtils.addPartAFromLog(log, commonSchemaEventLog, transmissionTarget);
        /* Part B, C and Part A metadata. */
        CommonSchemaDataUtils.addCommonSchemaData(eventLog.getTypedProperties(), commonSchemaEventLog);
        commonSchemaLogs.add(commonSchemaEventLog);
        /* Copy tag. */
        commonSchemaEventLog.setTag(log.getTag());
    }
    return commonSchemaLogs;
}
Also used : CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) CommonSchemaEventLog(com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog) CommonSchemaEventLog(com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog) LinkedList(java.util.LinkedList)

Example 12 with CommonSchemaLog

use of com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog 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 13 with CommonSchemaLog

use of com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog 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 14 with CommonSchemaLog

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

the class OneCollectorChannelListener method onPreparedLog.

@Override
public void onPreparedLog(@NonNull Log log, @NonNull String groupName, int flags) {
    /* Nothing to do on common schema log prepared. */
    if (!isOneCollectorCompatible(log)) {
        return;
    }
    /* Convert logs to Common Schema. */
    Collection<CommonSchemaLog> commonSchemaLogs;
    try {
        commonSchemaLogs = mLogSerializer.toCommonSchemaLog(log);
    } catch (IllegalArgumentException e) {
        AppCenterLog.error(LOG_TAG, "Cannot send a log to one collector: " + e.getMessage());
        return;
    }
    /* Add additional part A fields that are not known by the modules during conversion. */
    for (CommonSchemaLog commonSchemaLog : commonSchemaLogs) {
        /* Add flags. */
        commonSchemaLog.setFlags((long) flags);
        /* Add SDK extension missing fields: installId, epoch and seq. libVer is already set. */
        EpochAndSeq epochAndSeq = mEpochsAndSeqsByIKey.get(commonSchemaLog.getIKey());
        if (epochAndSeq == null) {
            epochAndSeq = new EpochAndSeq(UUID.randomUUID().toString());
            mEpochsAndSeqsByIKey.put(commonSchemaLog.getIKey(), epochAndSeq);
        }
        SdkExtension sdk = commonSchemaLog.getExt().getSdk();
        sdk.setEpoch(epochAndSeq.epoch);
        sdk.setSeq(++epochAndSeq.seq);
        sdk.setInstallId(mInstallId);
    }
    /* Enqueue logs to one collector group. */
    String oneCollectorGroupName = getOneCollectorGroupName(groupName);
    for (CommonSchemaLog commonSchemaLog : commonSchemaLogs) {
        mChannel.enqueue(commonSchemaLog, oneCollectorGroupName, flags);
    }
}
Also used : CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) SdkExtension(com.microsoft.appcenter.ingestion.models.one.SdkExtension)

Example 15 with CommonSchemaLog

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

the class OneCollectorIngestionTest method ticketsFailToSerialize.

@Test
public void ticketsFailToSerialize() throws Exception {
    /* Build some payload. */
    final CommonSchemaLog log1 = mock(CommonSchemaLog.class);
    final List<String> ticketKeys = new ArrayList<String>() {

        {
            add("key1");
        }
    };
    TicketCache.putTicket("key1", "value1");
    Extensions ext1 = new Extensions() {

        {
            setProtocol(new ProtocolExtension() {

                {
                    setTicketKeys(ticketKeys);
                }
            });
        }
    };
    when(log1.getExt()).thenReturn(ext1);
    LogContainer container = new LogContainer() {

        {
            setLogs(new ArrayList<Log>() {

                {
                    add(log1);
                }
            });
        }
    };
    JSONObject ticketJson = mock(JSONObject.class);
    whenNew(JSONObject.class).withNoArguments().thenReturn(ticketJson);
    when(ticketJson.put(anyString(), anyString())).thenThrow(new JSONException("mock"));
    /* Configure mock HTTP. */
    ServiceCall call = mock(ServiceCall.class);
    when(mHttpClient.callAsync(anyString(), anyString(), mHeadersCaptor.capture(), any(HttpClient.CallTemplate.class), any(ServiceCallback.class))).thenReturn(call);
    /* Verify call to http client. */
    LogSerializer serializer = mock(LogSerializer.class);
    OneCollectorIngestion ingestion = new OneCollectorIngestion(mHttpClient, serializer);
    ingestion.setLogUrl("http://mock");
    ServiceCallback serviceCallback = mock(ServiceCallback.class);
    assertEquals(call, ingestion.sendAsync(null, null, container, serviceCallback));
    /* Verify call to http client was made without headers as JSON failed. */
    Map<String, String> headers = mHeadersCaptor.getValue();
    assertFalse(headers.containsKey(TICKETS));
}
Also used : ServiceCall(com.microsoft.appcenter.http.ServiceCall) ProtocolExtension(com.microsoft.appcenter.ingestion.models.one.ProtocolExtension) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) Matchers.anyString(org.mockito.Matchers.anyString) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Extensions(com.microsoft.appcenter.ingestion.models.one.Extensions) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) JSONObject(org.json.JSONObject) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

CommonSchemaLog (com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog)32 Test (org.junit.Test)24 Extensions (com.microsoft.appcenter.ingestion.models.one.Extensions)20 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)17 CommonSchemaEventLog (com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog)16 Log (com.microsoft.appcenter.ingestion.models.Log)10 UserExtension (com.microsoft.appcenter.ingestion.models.one.UserExtension)10 AppExtension (com.microsoft.appcenter.ingestion.models.one.AppExtension)9 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)8 ProtocolExtension (com.microsoft.appcenter.ingestion.models.one.ProtocolExtension)7 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)7 EventLog (com.microsoft.appcenter.analytics.ingestion.models.EventLog)6 ArrayList (java.util.ArrayList)6 Context (android.content.Context)5 MockCommonSchemaLog (com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog)5 Matchers.anyString (org.mockito.Matchers.anyString)5 AppCenterHandler (com.microsoft.appcenter.AppCenterHandler)4 ServiceCall (com.microsoft.appcenter.http.ServiceCall)4 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)4 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)4