use of com.microsoft.azure.mobile.ingestion.models.LogContainer in project mobile-center-sdk-android by Microsoft.
the class DefaultLogSerializer method deserializeContainer.
@NonNull
@Override
public LogContainer deserializeContainer(@NonNull String json) throws JSONException {
JSONObject jContainer = new JSONObject(json);
LogContainer container = new LogContainer();
JSONArray jLogs = jContainer.getJSONArray(LOGS);
List<Log> logs = new ArrayList<>();
for (int i = 0; i < jLogs.length(); i++) {
JSONObject jLog = jLogs.getJSONObject(i);
Log log = readLog(jLog);
logs.add(log);
}
container.setLogs(logs);
return container;
}
use of com.microsoft.azure.mobile.ingestion.models.LogContainer in project mobile-center-sdk-android by Microsoft.
the class DefaultChannel method sendLogs.
/**
* Send logs.
*
* @param groupState The group state.
* @param currentState The current state.
* @param batch The log batch.
* @param batchId The batch ID.
*/
@MainThread
private synchronized void sendLogs(final GroupState groupState, final int currentState, List<Log> batch, final String batchId) {
if (checkStateDidNotChange(groupState, currentState)) {
/* Send logs. */
LogContainer logContainer = new LogContainer();
logContainer.setLogs(batch);
mIngestion.sendAsync(mAppSecret, mInstallId, logContainer, new ServiceCallback() {
@Override
public void onCallSucceeded(String payload) {
handleSendingSuccess(groupState, currentState, batchId);
}
@Override
public void onCallFailed(Exception e) {
handleSendingFailure(groupState, currentState, batchId, e);
}
});
/* Check for more pending logs. */
checkPendingLogs(groupState.mName);
}
}
use of com.microsoft.azure.mobile.ingestion.models.LogContainer in project mobile-center-sdk-android by Microsoft.
the class LogSerializerAndroidTest method oneLog.
@Test
public void oneLog() throws JSONException {
LogContainer expectedContainer = AndroidTestUtils.generateMockLogContainer();
LogSerializer serializer = new DefaultLogSerializer();
serializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
String payload = serializer.serializeContainer(expectedContainer);
android.util.Log.v(TAG, payload);
LogContainer actualContainer = serializer.deserializeContainer(payload);
Assert.assertEquals(expectedContainer, actualContainer);
Assert.assertEquals(expectedContainer.hashCode(), actualContainer.hashCode());
}
use of com.microsoft.azure.mobile.ingestion.models.LogContainer 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.LogContainer 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);
}
Aggregations