use of com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog in project mobile-center-sdk-android by Microsoft.
the class OneCollectorIngestionTest method failedSerialization.
@Test
public void failedSerialization() throws Exception {
/* Build some payload. */
final CommonSchemaLog log = mock(CommonSchemaLog.class);
when(log.getExt()).thenReturn(new Extensions() {
{
setProtocol(new ProtocolExtension());
}
});
LogContainer container = new LogContainer() {
{
setLogs(new ArrayList<Log>() {
{
add(log);
}
});
}
};
LogSerializer serializer = mock(LogSerializer.class);
JSONException exception = new JSONException("mock");
when(serializer.serializeLog(log)).thenThrow(exception);
/* Configure mock HTTP. */
ServiceCall call = mock(ServiceCall.class);
ArgumentCaptor<HttpClient.CallTemplate> callTemplate = ArgumentCaptor.forClass(HttpClient.CallTemplate.class);
when(mHttpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), callTemplate.capture(), any(ServiceCallback.class))).thenReturn(call);
/* Test calling code. */
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. */
assertNotNull(callTemplate.getValue());
try {
callTemplate.getValue().buildRequestBody();
Assert.fail("Expected json exception");
} catch (JSONException ignored) {
}
/* Verify close. */
ingestion.close();
verify(mHttpClient).close();
}
use of com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog in project mobile-center-sdk-android by Microsoft.
the class OneCollectorChannelListenerTest method enqueueConvertedLogs.
@Test
public void enqueueConvertedLogs() {
/* Mock original log. */
Log originalLog = mock(Log.class);
when(originalLog.getTransmissionTargetTokens()).thenReturn(new HashSet<>(Collections.singletonList("t1")));
/* Mock a log. */
CommonSchemaLog log1 = new MockCommonSchemaLog();
log1.setIKey("t1");
Extensions ext1 = new Extensions();
ext1.setSdk(new SdkExtension());
log1.setExt(ext1);
/* Mock another log. */
CommonSchemaLog log2 = new MockCommonSchemaLog();
log2.setIKey("t1");
Extensions ext2 = new Extensions();
ext2.setSdk(new SdkExtension());
log2.setExt(ext2);
/* Mock conversion of logs. */
Channel channel = mock(Channel.class);
LogSerializer logSerializer = mock(LogSerializer.class);
when(logSerializer.toCommonSchemaLog(any(Log.class))).thenReturn(Arrays.asList(log1, log2));
/* Init listener. */
UUID installId = UUID.randomUUID();
OneCollectorChannelListener listener = new OneCollectorChannelListener(channel, logSerializer, createHttpClient(mock(Context.class)), installId);
listener.onPreparedLog(originalLog, TEST_GROUP, DEFAULTS);
listener.onPreparedLog(mock(CommonSchemaLog.class), TEST_GROUP + ONE_COLLECTOR_GROUP_NAME_SUFFIX, DEFAULTS);
/* Verify conversion. */
verify(logSerializer).toCommonSchemaLog(originalLog);
verifyNoMoreInteractions(logSerializer);
/* Verify flags. */
assertEquals(Long.valueOf(DEFAULTS), log1.getFlags());
assertEquals(Long.valueOf(DEFAULTS), log2.getFlags());
/* Verify same epoch. */
assertNotNull(log1.getExt().getSdk().getEpoch());
assertEquals(log1.getExt().getSdk().getEpoch(), log2.getExt().getSdk().getEpoch());
/* Verify incremented sequence numbers. */
assertEquals(Long.valueOf(1), log1.getExt().getSdk().getSeq());
assertEquals(Long.valueOf(2), log2.getExt().getSdk().getSeq());
/* Verify install ID set. */
assertEquals(installId, log1.getExt().getSdk().getInstallId());
assertEquals(installId, log2.getExt().getSdk().getInstallId());
/* Verify enqueue. */
verify(channel).enqueue(log1, TEST_GROUP + ONE_COLLECTOR_GROUP_NAME_SUFFIX, DEFAULTS);
verify(channel).enqueue(log2, TEST_GROUP + ONE_COLLECTOR_GROUP_NAME_SUFFIX, DEFAULTS);
/* We simulated that we see on prepared log on the enqueued log, verify no more enqueuing. */
verify(channel, times(2)).enqueue(any(Log.class), anyString(), eq(DEFAULTS));
/* Mock log with another key to see new seq/epoch. */
when(originalLog.getTransmissionTargetTokens()).thenReturn(new HashSet<>(Collections.singletonList("t2")));
CommonSchemaLog log3 = new MockCommonSchemaLog();
log3.setIKey("t2");
Extensions ext3 = new Extensions();
ext3.setSdk(new SdkExtension());
log3.setExt(ext3);
when(logSerializer.toCommonSchemaLog(any(Log.class))).thenReturn(Collections.singletonList(log3));
listener.onPreparedLog(originalLog, TEST_GROUP, CRITICAL);
assertEquals(Long.valueOf(CRITICAL), log3.getFlags());
assertEquals(Long.valueOf(1), log3.getExt().getSdk().getSeq());
assertNotNull(log3.getExt().getSdk().getEpoch());
assertNotEquals(log1.getExt().getSdk().getEpoch(), log3.getExt().getSdk().getEpoch());
/* Simulate disable/enable to reset epoch/seq. */
listener.onGloballyEnabled(false);
listener.onGloballyEnabled(true);
/* Mock a 4rd log in first group to check reset. */
CommonSchemaLog log4 = new MockCommonSchemaLog();
log4.setIKey("t2");
Extensions ext4 = new Extensions();
ext4.setSdk(new SdkExtension());
log4.setExt(ext4);
when(logSerializer.toCommonSchemaLog(any(Log.class))).thenReturn(Collections.singletonList(log4));
listener.onPreparedLog(originalLog, TEST_GROUP, NORMAL);
/* Verify flags and reset of epoch/seq. */
assertEquals(Long.valueOf(NORMAL), log4.getFlags());
assertEquals(Long.valueOf(1), log4.getExt().getSdk().getSeq());
assertNotNull(log4.getExt().getSdk().getEpoch());
assertNotEquals(log3.getExt().getSdk().getEpoch(), log4.getExt().getSdk().getEpoch());
}
Aggregations