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;
}
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();
}
}
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();
}
}
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);
}
}
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));
}
Aggregations