use of com.microsoft.appcenter.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method packageManagerIsBroken.
@Test
public void packageManagerIsBroken() throws Persistence.PersistenceException, DeviceInfoHelper.DeviceInfoException {
/* Setup mocking to make device properties generation fail. */
when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenThrow(new DeviceInfoHelper.DeviceInfoException("mock", new PackageManager.NameNotFoundException()));
Persistence persistence = mock(Persistence.class);
DefaultChannel channel = new DefaultChannel(mock(Context.class), null, persistence, mock(AppCenterIngestion.class), mAppCenterHandler);
channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null);
Channel.Listener listener = mock(Channel.Listener.class);
channel.addListener(listener);
/* Enqueue a log: listener is called before but then attaching device properties fails before saving the log. */
Log log = mock(Log.class);
channel.enqueue(log, TEST_GROUP, Flags.DEFAULTS);
verify(listener).onPreparingLog(log, TEST_GROUP);
verify(listener, never()).shouldFilter(log);
verify(persistence, never()).putLog(eq(log), eq(TEST_GROUP), anyInt());
}
use of com.microsoft.appcenter.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method invokeCallbacksAfterSuspendRecoverable.
@Test
public void invokeCallbacksAfterSuspendRecoverable() {
Persistence mockPersistence = mock(Persistence.class);
AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
when(mockIngestion.isEnabled()).thenReturn(true);
Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
when(mockPersistence.getLogs(eq(TEST_GROUP), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(1));
when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(getSendAsyncAnswer(new HttpException(new HttpResponse(503))));
DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, mockIngestion, mAppCenterHandler);
channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, mockListener);
channel.addGroup(TEST_GROUP + "2", 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null);
/* Enqueuing 1 event. */
channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
/* Verify callbacks invoked only for the first log. */
verify(mockListener).onBeforeSending(any(Log.class));
/* Verify no failure forwarded. */
verify(mockListener, never()).onFailure(any(Log.class), any(Exception.class));
/* Verify no log was deleted. */
verify(mockPersistence, never()).deleteLogs(TEST_GROUP);
/* But that we cleared batch state. */
verify(mockPersistence).clearPendingLogState();
}
use of com.microsoft.appcenter.ingestion.models.Log 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));
}
use of com.microsoft.appcenter.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelPauseResumeTest method pauseResumeTargetToken.
@Test
public void pauseResumeTargetToken() throws Persistence.PersistenceException {
/* Mock database and ingestion. */
Persistence persistence = mock(Persistence.class);
OneCollectorIngestion ingestion = mock(OneCollectorIngestion.class);
when(ingestion.isEnabled()).thenReturn(true);
/* Create a channel with a log group that send logs 1 by 1. */
AppCenterIngestion appCenterIngestion = mock(AppCenterIngestion.class);
when(appCenterIngestion.isEnabled()).thenReturn(true);
DefaultChannel channel = new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), persistence, appCenterIngestion, mAppCenterHandler);
channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, ingestion, null);
/* Reset to verify further interactions besides initial check after adding group. */
reset(persistence);
/* Pause token. */
String targetToken = "iKey-apiKey";
channel.pauseGroup(TEST_GROUP, targetToken);
/* Mock the database to return logs now. */
when(persistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(1));
when(persistence.countLogs(TEST_GROUP)).thenReturn(1);
/* Enqueue a log. */
Log log = mock(Log.class);
when(log.getTransmissionTargetTokens()).thenReturn(Collections.singleton(targetToken));
channel.enqueue(log, TEST_GROUP, Flags.DEFAULTS);
/* Verify persisted but not incrementing and checking logs. */
verify(persistence).putLog(log, TEST_GROUP, Flags.NORMAL);
assertEquals(0, channel.getGroupState(TEST_GROUP).mPendingLogCount);
verify(persistence, never()).countLogs(TEST_GROUP);
verify(ingestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
/* Pausing a second time has no effect. */
channel.pauseGroup(TEST_GROUP, targetToken);
verify(persistence, never()).countLogs(TEST_GROUP);
/* Enqueueing a log from another transmission target works. */
Log otherLog = mock(Log.class);
when(otherLog.getTransmissionTargetTokens()).thenReturn(Collections.singleton("iKey2-apiKey2"));
channel.enqueue(otherLog, TEST_GROUP, Flags.DEFAULTS);
verify(ingestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
reset(ingestion);
/* Resume token. */
channel.resumeGroup(TEST_GROUP, targetToken);
verify(ingestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
/* Sending more logs works now. */
reset(ingestion);
channel.enqueue(log, TEST_GROUP, Flags.DEFAULTS);
verify(ingestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
/* Calling resume a second time has 0 effect. */
reset(persistence);
reset(ingestion);
channel.resumeGroup(TEST_GROUP, targetToken);
verifyZeroInteractions(persistence);
verifyZeroInteractions(ingestion);
/* AppCenter ingestion never used. */
verify(appCenterIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
}
use of com.microsoft.appcenter.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method checkPendingLogsSendsAllBatchesIfTimerIsOver.
@Test
public void checkPendingLogsSendsAllBatchesIfTimerIsOver() {
/* Mock current time. */
long now = 5000;
when(System.currentTimeMillis()).thenReturn(now);
/* Mock stored start time. */
long startTimer = 1000;
when(SharedPreferencesManager.getLong(eq(START_TIMER_PREFIX + TEST_GROUP))).thenReturn(startTimer);
/* Mock persistence. */
Persistence mockPersistence = mock(Persistence.class);
when(mockPersistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer()).then(getGetLogsAnswer(50)).then(getGetLogsAnswer(50)).then(getGetLogsAnswer(50)).then(getGetLogsAnswer(50)).then(getGetLogsAnswer(0));
/* Mock sending logs. */
final List<ServiceCallback> callbacks = new ArrayList<>();
AppCenterIngestion mockIngestion = mock(AppCenterIngestion.class);
when(mockIngestion.isEnabled()).thenReturn(true);
when(mockIngestion.sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class))).then(new Answer<Object>() {
public Object answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
if (args[3] instanceof ServiceCallback) {
callbacks.add((ServiceCallback) invocation.getArguments()[3]);
}
return null;
}
});
/* Create channel and group. */
DefaultChannel channel = spy(new DefaultChannel(mock(Context.class), UUID.randomUUID().toString(), mockPersistence, mockIngestion, mAppCenterHandler));
channel.addGroup(TEST_GROUP, 50, CUSTOM_INTERVAL, MAX_PARALLEL_BATCHES, null, mock(Channel.GroupListener.class));
/* Prepare to mock timer. */
long timeDelay = CUSTOM_INTERVAL - (now - startTimer);
/* Enqueuing 200 events. */
for (int i = 0; i < 200; i++) {
channel.enqueue(mock(Log.class), TEST_GROUP, Flags.DEFAULTS);
}
/* Check invoke the timer with custom timestamp. */
ArgumentCaptor<Runnable> delayedRunnable = ArgumentCaptor.forClass(Runnable.class);
verify(mAppCenterHandler).postDelayed(delayedRunnable.capture(), eq(timeDelay));
/* Wait for timer. */
now = 12000;
when(System.currentTimeMillis()).thenReturn(now);
delayedRunnable.getValue().run();
/* Check sending the logs batches. */
verify(mockIngestion, times(3)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
/* Successful finish one of sending the log. */
callbacks.get(0).onCallSucceeded(new HttpResponse(200, ""));
verify(mockPersistence).deleteLogs(any(String.class), any(String.class));
/* Check rest logs sending. */
verify(mockIngestion, times(4)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
}
Aggregations