Search in sources :

Example 1 with ProtocolExtension

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();
}
Also used : Context(android.content.Context) AppCenterHandler(com.microsoft.appcenter.AppCenterHandler) ProtocolExtension(com.microsoft.appcenter.ingestion.models.one.ProtocolExtension) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) CommonSchemaEventLog(com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) CommonSchemaEventLog(com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog) Extensions(com.microsoft.appcenter.ingestion.models.one.Extensions) Test(org.junit.Test)

Example 2 with ProtocolExtension

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();
}
Also used : Context(android.content.Context) ProtocolExtension(com.microsoft.appcenter.ingestion.models.one.ProtocolExtension) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) CommonSchemaEventLog(com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog) ArrayList(java.util.ArrayList) Extensions(com.microsoft.appcenter.ingestion.models.one.Extensions) AppCenterHandler(com.microsoft.appcenter.AppCenterHandler) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) CommonSchemaEventLog(com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog) Test(org.junit.Test)

Example 3 with ProtocolExtension

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));
}
Also used : ServiceCall(com.microsoft.appcenter.http.ServiceCall) ProtocolExtension(com.microsoft.appcenter.ingestion.models.one.ProtocolExtension) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) Matchers.anyString(org.mockito.Matchers.anyString) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Extensions(com.microsoft.appcenter.ingestion.models.one.Extensions) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) JSONObject(org.json.JSONObject) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with ProtocolExtension

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();
}
Also used : Context(android.content.Context) AppCenterHandler(com.microsoft.appcenter.AppCenterHandler) ProtocolExtension(com.microsoft.appcenter.ingestion.models.one.ProtocolExtension) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) CommonSchemaEventLog(com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog) Extensions(com.microsoft.appcenter.ingestion.models.one.Extensions) Test(org.junit.Test)

Example 5 with ProtocolExtension

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;
}
Also used : ServiceCall(com.microsoft.appcenter.http.ServiceCall) ProtocolExtension(com.microsoft.appcenter.ingestion.models.one.ProtocolExtension) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Extensions(com.microsoft.appcenter.ingestion.models.one.Extensions) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer)

Aggregations

CommonSchemaLog (com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog)7 Extensions (com.microsoft.appcenter.ingestion.models.one.Extensions)7 ProtocolExtension (com.microsoft.appcenter.ingestion.models.one.ProtocolExtension)7 Log (com.microsoft.appcenter.ingestion.models.Log)6 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)6 Test (org.junit.Test)6 ServiceCall (com.microsoft.appcenter.http.ServiceCall)4 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)4 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)4 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)4 Matchers.anyString (org.mockito.Matchers.anyString)4 Context (android.content.Context)3 AppCenterHandler (com.microsoft.appcenter.AppCenterHandler)3 CommonSchemaEventLog (com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog)3 ArrayList (java.util.ArrayList)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 EventLog (com.microsoft.appcenter.analytics.ingestion.models.EventLog)2 DefaultHttpClient (com.microsoft.appcenter.http.DefaultHttpClient)2 HttpClient (com.microsoft.appcenter.http.HttpClient)2 JSONException (org.json.JSONException)2