use of com.microsoft.appcenter.ingestion.models.one.Extensions in project mobile-center-sdk-android by Microsoft.
the class AnalyticsTransmissionTargetTest method registerCallbackWhenDisabledWorks.
@Test
public void registerCallbackWhenDisabledWorks() {
/* Simulate disabling and background thread. */
Analytics analytics = Analytics.getInstance();
AppCenterHandler handler = mock(AppCenterHandler.class);
ArgumentCaptor<Runnable> backgroundRunnable = ArgumentCaptor.forClass(Runnable.class);
ArgumentCaptor<Runnable> disabledRunnable = ArgumentCaptor.forClass(Runnable.class);
doNothing().when(handler).post(backgroundRunnable.capture(), disabledRunnable.capture());
analytics.onStarting(handler);
analytics.onStarted(mock(Context.class), mChannel, null, "test", true);
/* Disable. */
Analytics.setEnabled(false);
backgroundRunnable.getValue().run();
/* Add authentication provider while disabled. */
AuthenticationProvider.TokenProvider tokenProvider = mock(AuthenticationProvider.TokenProvider.class);
AuthenticationProvider authenticationProvider = spy(new AuthenticationProvider(AuthenticationProvider.Type.MSA_COMPACT, "key1", tokenProvider));
AnalyticsTransmissionTarget.addAuthenticationProvider(authenticationProvider);
/* Unlock command. */
disabledRunnable.getValue().run();
/* Verify update while disabled. */
assertEquals(authenticationProvider, AnalyticsTransmissionTarget.sAuthenticationProvider);
verify(authenticationProvider).acquireTokenAsync();
/* Enable. */
Analytics.setEnabled(true);
disabledRunnable.getValue().run();
/* Call prepare log. */
ProtocolExtension protocol = new ProtocolExtension();
Extensions ext = new Extensions();
ext.setProtocol(protocol);
CommonSchemaLog log = new CommonSchemaEventLog();
log.setExt(ext);
AnalyticsTransmissionTarget.getChannelListener().onPreparingLog(log, "test");
/* Verify log. */
assertEquals(Collections.singletonList(authenticationProvider.getTicketKeyHash()), protocol.getTicketKeys());
/* And that we check expiry. */
verify(authenticationProvider).checkTokenExpiry();
}
use of com.microsoft.appcenter.ingestion.models.one.Extensions in project mobile-center-sdk-android by Microsoft.
the class OneCollectorIngestionTest method passTickets.
private Map<String, String> passTickets() {
/* Build some payload. */
final CommonSchemaLog log1 = mock(CommonSchemaLog.class);
final CommonSchemaLog log2 = mock(CommonSchemaLog.class);
final List<String> ticketKeys = new ArrayList<String>() {
{
add("key1");
add("key2");
add(null);
}
};
TicketCache.putTicket("key2", "value2");
Extensions ext1 = new Extensions() {
{
setProtocol(new ProtocolExtension() {
{
setTicketKeys(ticketKeys);
}
});
}
};
Extensions ext2 = new Extensions() {
{
setProtocol(new ProtocolExtension());
}
};
when(log1.getExt()).thenReturn(ext1);
when(log2.getExt()).thenReturn(ext2);
LogContainer container = new LogContainer() {
{
setLogs(new ArrayList<Log>() {
{
add(log1);
add(log2);
}
});
}
};
/* 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. */
Map<String, String> headers = mHeadersCaptor.getValue();
assertTrue(headers.containsKey(TICKETS));
assertEquals("{\"key2\":\"value2\"}", headers.get(TICKETS));
return headers;
}
use of com.microsoft.appcenter.ingestion.models.one.Extensions in project mobile-center-sdk-android by Microsoft.
the class OneCollectorIngestionTest method sendAsync.
@Test
public void sendAsync() throws Exception {
/* Mock time. */
mockStatic(System.class);
when(System.currentTimeMillis()).thenReturn(1234L);
/* Build some payload. */
Extensions ext = new Extensions() {
{
setProtocol(new ProtocolExtension());
}
};
final CommonSchemaLog log1 = mock(CommonSchemaLog.class);
when(log1.getExt()).thenReturn(ext);
when(log1.getTransmissionTargetTokens()).thenReturn(Collections.singleton("token1"));
final CommonSchemaLog log2 = mock(CommonSchemaLog.class);
when(log2.getExt()).thenReturn(ext);
when(log2.getTransmissionTargetTokens()).thenReturn(new HashSet<>(Arrays.asList("token2", "token3")));
LogContainer container = new LogContainer() {
{
setLogs(new ArrayList<Log>() {
{
add(log1);
add(log2);
}
});
}
};
LogSerializer serializer = mock(LogSerializer.class);
when(serializer.serializeLog(log1)).thenReturn("mockPayload1");
when(serializer.serializeLog(log2)).thenReturn("mockPayload2");
/* 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. */
HashMap<String, String> expectedHeaders = new HashMap<>();
expectedHeaders.put(OneCollectorIngestion.API_KEY, "token1,token2,token3");
expectedHeaders.put(OneCollectorIngestion.CLIENT_VERSION_KEY, String.format("ACS-Android-Java-no-%s-no", VERSION_NAME));
expectedHeaders.put(OneCollectorIngestion.UPLOAD_TIME_KEY, "1234");
expectedHeaders.put(DefaultHttpClient.CONTENT_TYPE_KEY, "application/x-json-stream; charset=utf-8");
verify(mHttpClient).callAsync(eq("http://mock"), eq(METHOD_POST), eq(expectedHeaders), notNull(HttpClient.CallTemplate.class), eq(serviceCallback));
assertNotNull(callTemplate.getValue());
assertEquals("mockPayload1\nmockPayload2\n", callTemplate.getValue().buildRequestBody());
/* Verify close. */
ingestion.close();
verify(mHttpClient).close();
/* Verify reopen. */
ingestion.reopen();
verify(mHttpClient).reopen();
}
use of com.microsoft.appcenter.ingestion.models.one.Extensions 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.Extensions 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