use of com.microsoft.appcenter.ingestion.models.one.ProtocolExtension in project mobile-center-sdk-android by Microsoft.
the class AnalyticsTransmissionTargetTest method addTicketToLogBeforeStart.
@Test
public void addTicketToLogBeforeStart() {
/* Simulate not started. */
Analytics.unsetInstance();
when(AppCenter.isConfigured()).thenReturn(false);
/* No actions are prepared without authentication provider. */
CommonSchemaLog log = new CommonSchemaEventLog();
AnalyticsTransmissionTarget.getChannelListener().onPreparingLog(log, "test");
/* Add authentication provider before start. */
AuthenticationProvider.TokenProvider tokenProvider = mock(AuthenticationProvider.TokenProvider.class);
AuthenticationProvider authenticationProvider = spy(new AuthenticationProvider(AuthenticationProvider.Type.MSA_COMPACT, "key1", tokenProvider));
AnalyticsTransmissionTarget.addAuthenticationProvider(authenticationProvider);
assertEquals(authenticationProvider, AnalyticsTransmissionTarget.sAuthenticationProvider);
verify(authenticationProvider).acquireTokenAsync();
/* Start analytics. */
when(AppCenter.isConfigured()).thenReturn(true);
Analytics analytics = Analytics.getInstance();
AppCenterHandler handler = mock(AppCenterHandler.class);
ArgumentCaptor<Runnable> normalRunnable = ArgumentCaptor.forClass(Runnable.class);
ArgumentCaptor<Runnable> disabledRunnable = ArgumentCaptor.forClass(Runnable.class);
doNothing().when(handler).post(normalRunnable.capture(), disabledRunnable.capture());
analytics.onStarting(handler);
analytics.onStarted(mock(Context.class), mChannel, null, null, false);
/* No actions are prepared with no CommonSchemaLog. */
AnalyticsTransmissionTarget.getChannelListener().onPreparingLog(mock(Log.class), "test");
verify(authenticationProvider, never()).checkTokenExpiry();
/* Call prepare log. */
final ProtocolExtension protocol = new ProtocolExtension();
log.setExt(new Extensions() {
{
setProtocol(protocol);
}
});
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.ProtocolExtension in project mobile-center-sdk-android by Microsoft.
the class AnalyticsTransmissionTargetTest method updateAuthProviderAndLog.
@Test
public void updateAuthProviderAndLog() {
/* When we enqueue app center log from track event. */
final List<CommonSchemaLog> sentLogs = new ArrayList<>();
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) {
ProtocolExtension protocol = new ProtocolExtension();
Extensions ext = new Extensions();
ext.setProtocol(protocol);
CommonSchemaLog log = new CommonSchemaEventLog();
log.setExt(ext);
sentLogs.add(log);
/* Call the listener after conversion of common schema for authentication decoration. */
AnalyticsTransmissionTarget.getChannelListener().onPreparingLog(log, "test");
return null;
}
}).when(mChannel).enqueue(any(Log.class), anyString(), anyInt());
/* Start analytics and simulate background thread handler (we hold the thread command and run it in the test). */
Analytics analytics = Analytics.getInstance();
AppCenterHandler handler = mock(AppCenterHandler.class);
ArgumentCaptor<Runnable> backgroundRunnable = ArgumentCaptor.forClass(Runnable.class);
doNothing().when(handler).post(backgroundRunnable.capture(), any(Runnable.class));
analytics.onStarting(handler);
analytics.onStarted(mock(Context.class), mChannel, null, "test", true);
/* Add first authentication provider. */
AuthenticationProvider.TokenProvider tokenProvider1 = mock(AuthenticationProvider.TokenProvider.class);
AuthenticationProvider authenticationProvider1 = spy(new AuthenticationProvider(AuthenticationProvider.Type.MSA_COMPACT, "key1", tokenProvider1));
AnalyticsTransmissionTarget.addAuthenticationProvider(authenticationProvider1);
/* Check provider updated in background thread only when AppCenter is configured/started. */
assertNull(AnalyticsTransmissionTarget.sAuthenticationProvider);
verify(authenticationProvider1, never()).acquireTokenAsync();
assertNotNull(backgroundRunnable.getValue());
/* Run background thread. */
backgroundRunnable.getValue().run();
/* Check update. */
assertEquals(authenticationProvider1, AnalyticsTransmissionTarget.sAuthenticationProvider);
verify(authenticationProvider1).acquireTokenAsync();
/* Track an event. */
Analytics.trackEvent("test1");
Runnable trackEvent1Command = backgroundRunnable.getValue();
/* Update authentication provider before the commands run and track a second event. */
AuthenticationProvider.TokenProvider tokenProvider2 = mock(AuthenticationProvider.TokenProvider.class);
AuthenticationProvider authenticationProvider2 = spy(new AuthenticationProvider(AuthenticationProvider.Type.MSA_COMPACT, "key2", tokenProvider2));
AnalyticsTransmissionTarget.addAuthenticationProvider(authenticationProvider2);
Runnable addAuthProvider2Command = backgroundRunnable.getValue();
Analytics.trackEvent("test2");
Runnable trackEvent2Command = backgroundRunnable.getValue();
/* Simulate background thread doing everything in a sequence. */
trackEvent1Command.run();
addAuthProvider2Command.run();
trackEvent2Command.run();
/* Verify first log has first ticket. */
assertEquals(Collections.singletonList(authenticationProvider1.getTicketKeyHash()), sentLogs.get(0).getExt().getProtocol().getTicketKeys());
/* And that we checked expiry. */
verify(authenticationProvider1).checkTokenExpiry();
/* Verify second log has the second ticket. */
assertEquals(Collections.singletonList(authenticationProvider2.getTicketKeyHash()), sentLogs.get(1).getExt().getProtocol().getTicketKeys());
/* And that we checked expiry. */
verify(authenticationProvider2).checkTokenExpiry();
}
use of com.microsoft.appcenter.ingestion.models.one.ProtocolExtension 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.one.ProtocolExtension 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.ProtocolExtension 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;
}
Aggregations