use of com.microsoft.azure.mobile.ingestion.models.json.LogSerializer in project mobile-center-sdk-android by Microsoft.
the class DatabasePersistenceTest method clearPendingLogState.
@Test
public void clearPendingLogState() throws JSONException {
/* 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. */
mockStatic(StorageHelper.DatabaseStorage.class);
StorageHelper.DatabaseStorage mockDatabaseStorage = mock(StorageHelper.DatabaseStorage.class);
when(StorageHelper.DatabaseStorage.getDatabaseStorage(anyString(), anyString(), anyInt(), any(ContentValues.class), anyInt(), any(StorageHelper.DatabaseStorage.DatabaseErrorListener.class))).thenReturn(mockDatabaseStorage);
for (int i = 0; i < groupCount; i++) {
StorageHelper.DatabaseStorage.DatabaseScanner mockDatabaseScanner = mock(StorageHelper.DatabaseStorage.DatabaseScanner.class);
when(mockDatabaseScanner.iterator()).thenReturn(list.get(i).iterator());
when(mockDatabaseStorage.getScanner(COLUMN_GROUP, String.valueOf(i))).thenReturn(mockDatabaseScanner);
}
LogSerializer mockLogSerializer = mock(LogSerializer.class);
when(mockLogSerializer.deserializeLog(anyString())).thenReturn(mock(Log.class));
/* Instantiate Database Persistence. */
DatabasePersistence persistence = new DatabasePersistence();
persistence.setLogSerializer(mockLogSerializer);
/* Get logs. */
for (int i = 0; i < groupCount; i++) {
persistence.getLogs(String.valueOf(i), 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());
}
use of com.microsoft.azure.mobile.ingestion.models.json.LogSerializer in project mobile-center-sdk-android by Microsoft.
the class DatabasePersistenceTest method databaseOperationException.
@Test
public void databaseOperationException() throws Persistence.PersistenceException, IOException {
/* Mock instances. */
mockStatic(MobileCenterLog.class);
LogSerializer mockSerializer = mock(DefaultLogSerializer.class);
DatabasePersistence mockPersistence = spy(new DatabasePersistence("test-persistence", "operation.exception", 1));
doReturn(mockSerializer).when(mockPersistence).getLogSerializer();
DatabasePersistenceAsync mockPersistenceAsync = spy(new DatabasePersistenceAsync(mockPersistence));
try {
/* Generate a log and persist. */
Log log = mock(Log.class);
mockPersistenceAsync.putLog("test-p1", log, null);
} finally {
/* Close. */
//noinspection ThrowFromFinallyBlock
mockPersistenceAsync.close();
}
verifyStatic();
MobileCenterLog.error(eq(MobileCenter.LOG_TAG), anyString(), any(RuntimeException.class));
}
use of com.microsoft.azure.mobile.ingestion.models.json.LogSerializer in project mobile-center-sdk-android by Microsoft.
the class IngestionHttpTest method failedSerialization.
@Test
public void failedSerialization() throws Exception {
/* Build some payload. */
LogContainer container = new LogContainer();
Log log = mock(Log.class);
long logAbsoluteTime = 123L;
when(log.getToffset()).thenReturn(logAbsoluteTime);
List<Log> logs = new ArrayList<>();
logs.add(log);
container.setLogs(logs);
LogSerializer serializer = mock(LogSerializer.class);
JSONException exception = new JSONException("mock");
when(serializer.serializeContainer(any(LogContainer.class))).thenThrow(exception);
/* Stable time. */
mockStatic(System.class);
long now = 456L;
when(System.currentTimeMillis()).thenReturn(now);
/* 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(APP_SECRET, appSecret);
expectedHeaders.put(IngestionHttp.INSTALL_ID, installId.toString());
verify(httpClient).callAsync(eq("http://mock/logs?api_version=1.0.0-preview20160914"), eq(METHOD_POST), eq(expectedHeaders), notNull(HttpClient.CallTemplate.class), eq(serviceCallback));
assertNotNull(callTemplate.get());
try {
callTemplate.get().buildRequestBody();
Assert.fail("Expected json exception");
} catch (JSONException ignored) {
}
/* Verify toffset manipulation. */
verify(log).setToffset(now - logAbsoluteTime);
verify(log).setToffset(logAbsoluteTime);
/* Verify close. */
ingestionHttp.close();
verify(httpClient).close();
}
use of com.microsoft.azure.mobile.ingestion.models.json.LogSerializer in project mobile-center-sdk-android by Microsoft.
the class PushSerializerTest method serialize.
@Test
public void serialize() throws JSONException {
LogContainer expectedContainer = new LogContainer();
List<Log> logs = new ArrayList<>();
{
PushInstallationLog log = new PushInstallationLog();
log.setPushToken("TEST");
logs.add(log);
}
expectedContainer.setLogs(logs);
UUID sid = UUIDUtils.randomUUID();
for (Log log : logs) {
log.setSid(sid);
}
LogSerializer serializer = new DefaultLogSerializer();
serializer.addLogFactory(PushInstallationLog.TYPE, new PushInstallationLogFactory());
String payload = serializer.serializeContainer(expectedContainer);
LogContainer actualContainer = serializer.deserializeContainer(payload);
Assert.assertEquals(expectedContainer, actualContainer);
}
use of com.microsoft.azure.mobile.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);
long logAbsoluteTime = 123L;
when(log.getToffset()).thenReturn(logAbsoluteTime);
List<Log> logs = new ArrayList<>();
logs.add(log);
container.setLogs(logs);
LogSerializer serializer = mock(LogSerializer.class);
when(serializer.serializeContainer(any(LogContainer.class))).thenReturn("mockPayload");
/* Stable time. */
mockStatic(System.class);
long now = 456L;
when(System.currentTimeMillis()).thenReturn(now);
/* 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(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 toffset manipulation. */
verify(log).setToffset(now - logAbsoluteTime);
verify(log).setToffset(logAbsoluteTime);
/* Verify close. */
ingestionHttp.close();
verify(httpClient).close();
}
Aggregations