use of com.microsoft.appcenter.ingestion.models.json.LogSerializer in project mobile-center-sdk-android by Microsoft.
the class DatabasePersistenceAndroidTest method putLargeLogFails.
@Test
public void putLargeLogFails() throws PersistenceException, IOException {
/* Initialize database persistence. */
String path = Constants.FILES_PATH;
Constants.FILES_PATH = null;
DatabasePersistence persistence = new DatabasePersistence("test-persistence", "putLargeLogException", 1);
/* 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 and persist. */
LogWithProperties log = AndroidTestUtils.generateMockLog();
int size = 2 * 1024 * 1024;
StringBuilder largeValue = new StringBuilder(size);
for (int i = 0; i < size; i++) {
largeValue.append("x");
}
Map<String, String> properties = new HashMap<>();
properties.put("key", largeValue.toString());
log.setProperties(properties);
persistence.putLog("test-p1", log);
fail("putLog was expected to fail");
} catch (Persistence.PersistenceException e) {
assertTrue(e.getCause() instanceof IOException);
/* Make sure database entry has been removed. */
assertEquals(0, persistence.countLogs("test-p1"));
} finally {
/* Close. */
// noinspection ThrowFromFinallyBlock
persistence.close();
/* Restore path. */
Constants.FILES_PATH = path;
}
}
use of com.microsoft.appcenter.ingestion.models.json.LogSerializer in project mobile-center-sdk-android by Microsoft.
the class DatabasePersistenceAndroidTest method putTooManyLogs.
@Test
public void putTooManyLogs() throws PersistenceException, IOException {
/* Initialize database persistence. */
DatabasePersistence persistence = new DatabasePersistence("test-persistence", "putTooManyLogs", 1, 2);
/* Set a mock log serializer. */
LogSerializer logSerializer = new DefaultLogSerializer();
logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
persistence.setLogSerializer(logSerializer);
try {
/* Generate too many logs and persist. */
Log log1 = AndroidTestUtils.generateMockLog();
Log log2 = AndroidTestUtils.generateMockLog();
Log log3 = AndroidTestUtils.generateMockLog();
Log log4 = AndroidTestUtils.generateMockLog();
persistence.putLog("test-p1", log1);
persistence.putLog("test-p1", log2);
persistence.putLog("test-p1", log3);
persistence.putLog("test-p1", log4);
/* Get logs from persistence. */
List<Log> outputLogs = new ArrayList<>();
persistence.getLogs("test-p1", 4, outputLogs);
assertEquals(2, outputLogs.size());
assertEquals(log3, outputLogs.get(0));
assertEquals(log4, outputLogs.get(1));
assertEquals(2, persistence.countLogs("test-p1"));
} finally {
/* Close. */
// noinspection ThrowFromFinallyBlock
persistence.close();
}
}
use of com.microsoft.appcenter.ingestion.models.json.LogSerializer in project mobile-center-sdk-android by Microsoft.
the class DatabasePersistenceAndroidTest method deleteLogsForGroup.
@Test
public void deleteLogsForGroup() throws PersistenceException, IOException {
/* Initialize database persistence. */
DatabasePersistence persistence = new DatabasePersistence("test-persistence", "deleteLogsForGroup", 1);
/* Set a mock log serializer. */
LogSerializer logSerializer = new DefaultLogSerializer();
logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
persistence.setLogSerializer(logSerializer);
try {
/* Generate a log and persist. */
Log log1 = AndroidTestUtils.generateMockLog();
Log log2 = AndroidTestUtils.generateMockLog();
Log log3 = AndroidTestUtils.generateMockLog();
Log log4 = AndroidTestUtils.generateMockLog();
persistence.putLog("test-p1", log1);
persistence.putLog("test-p1", log2);
persistence.putLog("test-p2", log3);
persistence.putLog("test-p3", log4);
/* Get a log from persistence. */
List<Log> outputLogs = new ArrayList<>();
String id1 = persistence.getLogs("test-p1", 5, outputLogs);
String id2 = persistence.getLogs("test-p2", 5, outputLogs);
assertNotNull(id1);
assertNotNull(id2);
/* Delete. */
persistence.deleteLogs("test-p1");
persistence.deleteLogs("test-p3");
/* Try another get for verification. */
outputLogs.clear();
persistence.getLogs("test-p3", 5, outputLogs);
/* Verify. */
Map<String, List<Long>> pendingGroups = persistence.mPendingDbIdentifiersGroups;
assertNull(pendingGroups.get("test-p1" + id1));
assertEquals(1, pendingGroups.get("test-p2" + id2).size());
assertEquals(1, pendingGroups.size());
assertEquals(0, outputLogs.size());
assertEquals(1, persistence.mDatabaseStorage.size());
/* Verify one log still persists in the database. */
persistence.clearPendingLogState();
outputLogs.clear();
persistence.getLogs("test-p2", 5, outputLogs);
assertEquals(1, outputLogs.size());
assertEquals(log3, outputLogs.get(0));
/* Count for groups. */
assertEquals(0, persistence.countLogs("test-p1"));
assertEquals(1, persistence.countLogs("test-p2"));
assertEquals(0, persistence.countLogs("test-p3"));
} finally {
/* Close. */
// noinspection ThrowFromFinallyBlock
persistence.close();
}
}
use of com.microsoft.appcenter.ingestion.models.json.LogSerializer in project mobile-center-sdk-android by Microsoft.
the class DatabasePersistenceAndroidTest method getLogs.
@Test
public void getLogs() throws PersistenceException, IOException {
/* Initialize database persistence. */
DatabasePersistence persistence = new DatabasePersistence("test-persistence", "getLogs", 1);
/* Set a mock log serializer. */
LogSerializer logSerializer = new DefaultLogSerializer();
logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
persistence.setLogSerializer(logSerializer);
try {
/* Test constants. */
int numberOfLogs = 10;
int sizeForGetLogs = 4;
/* Generate a log and persist. */
Log[] logs = new Log[numberOfLogs];
for (int i = 0; i < logs.length; i++) logs[i] = AndroidTestUtils.generateMockLog();
/* Put. */
for (Log log : logs) persistence.putLog("test", log);
/* Get. */
getAllLogs(persistence, numberOfLogs, sizeForGetLogs);
/* Clear ids, we should be able to get the logs again in the same sequence. */
persistence.clearPendingLogState();
getAllLogs(persistence, numberOfLogs, sizeForGetLogs);
/* Count. */
assertEquals(10, persistence.countLogs("test"));
/* Clear. Nothing to get after. */
persistence.mDatabaseStorage.clear();
List<Log> outputLogs = new ArrayList<>();
assertNull(persistence.getLogs("test", sizeForGetLogs, outputLogs));
assertTrue(outputLogs.isEmpty());
assertEquals(0, persistence.countLogs("test"));
} finally {
/* Close. */
// noinspection ThrowFromFinallyBlock
persistence.close();
}
}
use of com.microsoft.appcenter.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);
List<Log> logs = new ArrayList<>();
logs.add(log);
container.setLogs(logs);
LogSerializer serializer = mock(LogSerializer.class);
when(serializer.serializeContainer(any(LogContainer.class))).thenReturn("mockPayload");
/* 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(IngestionHttp.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 close. */
ingestionHttp.close();
verify(httpClient).close();
/* Verify reopen. */
ingestionHttp.reopen();
verify(httpClient).reopen();
}
Aggregations