Search in sources :

Example 76 with Log

use of com.microsoft.appcenter.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.

the class OneCollectorChannelListenerTest method shouldFilterAppCenterLog.

@Test
public void shouldFilterAppCenterLog() {
    Channel channel = mock(Channel.class);
    OneCollectorChannelListener listener = new OneCollectorChannelListener(channel, mock(LogSerializer.class), createHttpClient(mock(Context.class)), UUID.randomUUID());
    /* App center log with no transmission target must not be filtered. */
    Log log = mock(Log.class);
    assertFalse(listener.shouldFilter(log));
    /* App center log with transmission target must be filtered. */
    when(log.getTransmissionTargetTokens()).thenReturn(new HashSet<>(Collections.singletonList("token")));
    assertTrue(listener.shouldFilter(log));
    /* Common schema logs never filtered out by this listener if no validation. */
    assertFalse(listener.shouldFilter(mock(CommonSchemaLog.class)));
}
Also used : CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) MockCommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog) Log(com.microsoft.appcenter.ingestion.models.Log) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Test(org.junit.Test)

Example 77 with Log

use of com.microsoft.appcenter.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.

the class DefaultChannelAlternateIngestionTest method startWithoutAppSecret.

@Test
public void startWithoutAppSecret() throws Persistence.PersistenceException {
    /* Set up channel without app secret. */
    String appCenterGroup = "test_group1";
    String oneCollectorGroup = "test_group2";
    Persistence mockPersistence = mock(Persistence.class);
    Ingestion defaultIngestion = mock(Ingestion.class);
    when(defaultIngestion.isEnabled()).thenReturn(true);
    Ingestion alternateIngestion = mock(Ingestion.class);
    when(alternateIngestion.isEnabled()).thenReturn(true);
    /* Simulate we have 1 pending log in storage. */
    when(mockPersistence.countLogs(anyString())).thenReturn(1);
    when(mockPersistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(1));
    /* Create channel and groups. */
    DefaultChannel channel = new DefaultChannel(mock(Context.class), null, mockPersistence, defaultIngestion, mAppCenterHandler);
    channel.addGroup(appCenterGroup, 2, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null);
    channel.addGroup(oneCollectorGroup, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, alternateIngestion, null);
    /* App center previous log not sent yet. */
    verify(defaultIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* One collector previous log sent. */
    verify(alternateIngestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Enqueuing 1 new event for app center. */
    channel.enqueue(mock(Log.class), appCenterGroup, Flags.DEFAULTS);
    /* Not sent. */
    verify(defaultIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Verify we didn't persist the log since AppCenter not started with app secret. */
    verify(mockPersistence, never()).putLog(any(Log.class), eq(appCenterGroup), eq(Flags.NORMAL));
    /* Enqueuing 1 event from one collector group. */
    channel.enqueue(mock(Log.class), oneCollectorGroup, Flags.DEFAULTS);
    /* Verify that we have called sendAsync on the alternate ingestion a second time. */
    verify(alternateIngestion, times(2)).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(defaultIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Verify that we can now send logs to app center after we have set app secret. */
    channel.setAppSecret("testAppSecret");
    channel.enqueue(mock(Log.class), appCenterGroup, Flags.DEFAULTS);
    verify(defaultIngestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) Log(com.microsoft.appcenter.ingestion.models.Log) Matchers.anyString(org.mockito.Matchers.anyString) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) Test(org.junit.Test)

Example 78 with Log

use of com.microsoft.appcenter.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.

the class DefaultChannelAlternateIngestionTest method pendingLogsDisableSetAppSecretThenEnable.

@Test
public void pendingLogsDisableSetAppSecretThenEnable() {
    /* Set up channel without app secret. */
    String appCenterGroup = "test_group1";
    String oneCollectorGroup = "test_group2";
    Persistence mockPersistence = mock(Persistence.class);
    Ingestion defaultIngestion = mock(Ingestion.class);
    when(defaultIngestion.isEnabled()).thenReturn(true);
    Ingestion alternateIngestion = mock(Ingestion.class);
    when(alternateIngestion.isEnabled()).thenReturn(true);
    when(mockPersistence.getLogs(any(String.class), anyListOf(String.class), anyInt(), anyListOf(Log.class))).then(getGetLogsAnswer(1));
    /* Simulate we have 1 pending log in storage for App Center. */
    when(mockPersistence.countLogs(appCenterGroup)).thenReturn(1);
    /* Create channel with the two groups. */
    DefaultChannel channel = new DefaultChannel(mock(Context.class), null, mockPersistence, defaultIngestion, mAppCenterHandler);
    channel.addGroup(appCenterGroup, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, null, null);
    channel.addGroup(oneCollectorGroup, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, alternateIngestion, null);
    /* Disable channel. */
    channel.setEnabled(false);
    /* Verify that no log is sent even if we set app secret while channel is disabled. */
    channel.setAppSecret("testAppSecret");
    verify(defaultIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(alternateIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    /* Enable channel. */
    channel.setEnabled(true);
    /* Verify that now logs are sent when channel is enabled. */
    verify(defaultIngestion).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
    verify(alternateIngestion, never()).sendAsync(anyString(), any(UUID.class), any(LogContainer.class), any(ServiceCallback.class));
}
Also used : Persistence(com.microsoft.appcenter.persistence.Persistence) Context(android.content.Context) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) Log(com.microsoft.appcenter.ingestion.models.Log) Matchers.anyString(org.mockito.Matchers.anyString) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) Ingestion(com.microsoft.appcenter.ingestion.Ingestion) Test(org.junit.Test)

Example 79 with Log

use of com.microsoft.appcenter.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.

the class AppCenterIngestionTest method sendLogsWhenIngestionDisable.

@Test
public void sendLogsWhenIngestionDisable() throws JSONException, IOException {
    mockStatic(SharedPreferencesManager.class);
    when(SharedPreferencesManager.getBoolean(ALLOWED_NETWORK_REQUEST, true)).thenReturn(false);
    /* Build some payload. */
    LogContainer container = new LogContainer();
    Log log = mock(Log.class);
    List<Log> logs = new ArrayList<>();
    logs.add(log);
    container.setLogs(logs);
    LogSerializer serializer = mock(LogSerializer.class);
    when(serializer.serializeContainer(any(LogContainer.class))).thenReturn("mockPayload");
    /* Configure mock HTTP. */
    final ServiceCall call = mock(ServiceCall.class);
    /* Test calling code. */
    AppCenterIngestion ingestion = new AppCenterIngestion(mHttpClient, serializer);
    ingestion.setLogUrl("http://mock");
    String appSecret = UUID.randomUUID().toString();
    UUID installId = UUID.randomUUID();
    ServiceCallback serviceCallback = mock(ServiceCallback.class);
    assertNull(ingestion.sendAsync(appSecret, installId, container, serviceCallback));
    /* Verify call to http client. */
    HashMap<String, String> expectedHeaders = new HashMap<>();
    expectedHeaders.put(Constants.APP_SECRET, appSecret);
    expectedHeaders.put(AppCenterIngestion.INSTALL_ID, installId.toString());
    verify(mHttpClient, never()).callAsync(eq("http://mock" + AppCenterIngestion.API_PATH), eq(METHOD_POST), eq(expectedHeaders), notNull(HttpClient.CallTemplate.class), eq(serviceCallback));
}
Also used : ServiceCall(com.microsoft.appcenter.http.ServiceCall) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) Log(com.microsoft.appcenter.ingestion.models.Log) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Matchers.anyString(org.mockito.Matchers.anyString) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 80 with Log

use of com.microsoft.appcenter.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.

the class LogContainerTest method compareLogContainer.

@Test
public void compareLogContainer() {
    LogContainer container1 = new LogContainer();
    LogContainer container2 = new LogContainer();
    TestUtils.compareSelfNullClass(container1);
    TestUtils.checkEquals(container1, container2);
    Log log1 = new AbstractLog() {

        @Override
        public String getType() {
            return "null";
        }
    };
    log1.setSid(UUID.randomUUID());
    container1.setLogs(Collections.singletonList(log1));
    TestUtils.compareSelfNullClass(container1);
    TestUtils.checkNotEquals(container1, container2);
    container2.setLogs(Collections.singletonList(log1));
    TestUtils.compareSelfNullClass(container1);
    TestUtils.checkEquals(container1, container2);
    Log log2 = new AbstractLog() {

        @Override
        public String getType() {
            return null;
        }
    };
    log2.setSid(UUID.randomUUID());
    container2.setLogs(Collections.singletonList(log2));
    TestUtils.compareSelfNullClass(container1);
    TestUtils.checkNotEquals(container1, container2);
}
Also used : AbstractLog(com.microsoft.appcenter.ingestion.models.AbstractLog) Log(com.microsoft.appcenter.ingestion.models.Log) AbstractLog(com.microsoft.appcenter.ingestion.models.AbstractLog) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) Test(org.junit.Test)

Aggregations

Log (com.microsoft.appcenter.ingestion.models.Log)189 Test (org.junit.Test)150 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)83 ArrayList (java.util.ArrayList)75 Context (android.content.Context)74 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)65 UUID (java.util.UUID)57 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)56 Matchers.anyString (org.mockito.Matchers.anyString)51 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)45 DefaultLogSerializer (com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer)44 Persistence (com.microsoft.appcenter.persistence.Persistence)38 EventLog (com.microsoft.appcenter.analytics.ingestion.models.EventLog)34 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)32 CommonSchemaLog (com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog)32 HashMap (java.util.HashMap)32 StartSessionLog (com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog)29 Channel (com.microsoft.appcenter.channel.Channel)27 Date (java.util.Date)27 InvocationOnMock (org.mockito.invocation.InvocationOnMock)26