use of com.microsoft.azure.mobile.ingestion.models.Log 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();
}
use of com.microsoft.azure.mobile.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);
@SuppressWarnings("ConstantConditions") DefaultChannel channel = new DefaultChannel(mock(Context.class), null, persistence, mock(IngestionHttp.class));
channel.addGroup(TEST_GROUP, 50, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, 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);
verify(listener).onEnqueuingLog(log, TEST_GROUP);
verify(persistence, never()).putLog(TEST_GROUP, log);
}
use of com.microsoft.azure.mobile.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.
the class DefaultChannelTest method shutdown.
@Test
public void shutdown() throws Exception {
Persistence mockPersistence = mock(Persistence.class);
IngestionHttp mockIngestion = mock(IngestionHttp.class);
Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
DatabasePersistenceAsync mockPersistenceAsync = spy(new DatabasePersistenceAsync(mockPersistence));
whenNew(DatabasePersistenceAsync.class).withArguments(mockPersistence).thenReturn(mockPersistenceAsync);
when(mockPersistence.getLogs(any(String.class), anyInt(), Matchers.<List<Log>>any())).then(getGetLogsAnswer(1));
DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion);
channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, mockListener);
/* Enqueuing 1 event. */
channel.enqueue(mock(Log.class), TEST_GROUP);
verify(mockListener).onBeforeSending(notNull(Log.class));
channel.shutdown();
verify(mockListener, never()).onFailure(any(Log.class), any(Exception.class));
verify(mockPersistence).clearPendingLogState();
verify(mockPersistenceAsync).waitForCurrentTasksToComplete(DefaultChannel.SHUTDOWN_TIMEOUT);
}
use of com.microsoft.azure.mobile.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.
the class LogContainerTest method compareLogContainer.
@Test
public void compareLogContainer() {
LogContainer container1 = new LogContainer();
LogContainer container2 = new LogContainer();
TestUtils.compareSelfNullClass(container1);
TestUtils.checkEquals(container1, container2);
Log log1 = new AbstractLog() {
@Override
public String getType() {
return "null";
}
};
log1.setSid(UUID.randomUUID());
container1.setLogs(Collections.singletonList(log1));
TestUtils.compareSelfNullClass(container1);
TestUtils.checkNotEquals(container1, container2);
container2.setLogs(Collections.singletonList(log1));
TestUtils.compareSelfNullClass(container1);
TestUtils.checkEquals(container1, container2);
Log log2 = new AbstractLog() {
@Override
public String getType() {
return null;
}
};
log2.setSid(UUID.randomUUID());
container2.setLogs(Collections.singletonList(log2));
TestUtils.compareSelfNullClass(container1);
TestUtils.checkNotEquals(container1, container2);
}
use of com.microsoft.azure.mobile.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.
the class DatabasePersistenceAsyncTest method getLogs.
@Test
public void getLogs() {
int limit = 35;
List<Log> outLogs = new ArrayList<>();
String batchId = UUID.randomUUID().toString();
when(mPersistence.getLogs(GROUP, limit, outLogs)).thenReturn(batchId);
mDatabase.getLogs(GROUP, limit, outLogs, mCallback);
verify(mPersistence).getLogs(GROUP, limit, outLogs);
verify(mCallback).onSuccess(batchId);
}
Aggregations