Search in sources :

Example 31 with CommonSchemaLog

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();
}
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) JSONException(org.json.JSONException) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Matchers.anyString(org.mockito.Matchers.anyString) Extensions(com.microsoft.appcenter.ingestion.models.one.Extensions) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) HttpClient(com.microsoft.appcenter.http.HttpClient) DefaultHttpClient(com.microsoft.appcenter.http.DefaultHttpClient) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 32 with CommonSchemaLog

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());
}
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) MockCommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog) CommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog) MockCommonSchemaLog(com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog) SdkExtension(com.microsoft.appcenter.ingestion.models.one.SdkExtension) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Extensions(com.microsoft.appcenter.ingestion.models.one.Extensions) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

CommonSchemaLog (com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog)32 Test (org.junit.Test)24 Extensions (com.microsoft.appcenter.ingestion.models.one.Extensions)20 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)17 CommonSchemaEventLog (com.microsoft.appcenter.analytics.ingestion.models.one.CommonSchemaEventLog)16 Log (com.microsoft.appcenter.ingestion.models.Log)10 UserExtension (com.microsoft.appcenter.ingestion.models.one.UserExtension)10 AppExtension (com.microsoft.appcenter.ingestion.models.one.AppExtension)9 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)8 ProtocolExtension (com.microsoft.appcenter.ingestion.models.one.ProtocolExtension)7 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)7 EventLog (com.microsoft.appcenter.analytics.ingestion.models.EventLog)6 ArrayList (java.util.ArrayList)6 Context (android.content.Context)5 MockCommonSchemaLog (com.microsoft.appcenter.ingestion.models.one.MockCommonSchemaLog)5 Matchers.anyString (org.mockito.Matchers.anyString)5 AppCenterHandler (com.microsoft.appcenter.AppCenterHandler)4 ServiceCall (com.microsoft.appcenter.http.ServiceCall)4 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)4 LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)4