Search in sources :

Example 1 with LogContainer

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

the class AnalyticsSerializerTest method someBatch.

@Test
public void someBatch() throws JSONException {
    LogContainer expectedContainer = new LogContainer();
    Device device = new Device();
    device.setSdkName("appcenter.android");
    device.setSdkVersion("1.2.3");
    device.setModel("S5");
    device.setOemName("HTC");
    device.setOsName("Android");
    device.setOsVersion("4.0.3");
    device.setOsBuild("LMY47X");
    device.setOsApiLevel(15);
    device.setLocale("en_US");
    device.setTimeZoneOffset(120);
    device.setScreenSize("800x600");
    device.setAppVersion("3.2.1");
    device.setAppBuild("42");
    List<Log> logs = new ArrayList<>();
    {
        StartSessionLog startSessionLog = new StartSessionLog();
        startSessionLog.setTimestamp(new Date());
        logs.add(startSessionLog);
    }
    expectedContainer.setLogs(logs);
    {
        PageLog pageLog = new PageLog();
        pageLog.setTimestamp(new Date());
        pageLog.setName("home");
        logs.add(pageLog);
    }
    {
        PageLog pageLog = new PageLog();
        pageLog.setTimestamp(new Date());
        pageLog.setName("settings");
        pageLog.setProperties(new HashMap<String, String>() {

            {
                put("from", "home_menu");
                put("orientation", "portrait");
            }
        });
        logs.add(pageLog);
    }
    {
        EventLog eventLog = new EventLog();
        eventLog.setTimestamp(new Date());
        eventLog.setId(UUIDUtils.randomUUID());
        eventLog.setName("subscribe");
        logs.add(eventLog);
    }
    {
        EventLog eventLog = new EventLog();
        eventLog.setTimestamp(new Date());
        eventLog.setId(UUIDUtils.randomUUID());
        eventLog.setName("click");
        eventLog.setProperties(new HashMap<String, String>() {

            {
                put("x", "1");
                put("y", "2");
            }
        });
        logs.add(eventLog);
    }
    UUID sid = UUIDUtils.randomUUID();
    for (Log log : logs) {
        log.setSid(sid);
        log.setDevice(device);
    }
    LogSerializer serializer = new DefaultLogSerializer();
    serializer.addLogFactory(StartSessionLog.TYPE, new StartSessionLogFactory());
    serializer.addLogFactory(PageLog.TYPE, new PageLogFactory());
    serializer.addLogFactory(EventLog.TYPE, new EventLogFactory());
    String payload = serializer.serializeContainer(expectedContainer);
    android.util.Log.v(TAG, payload);
    LogContainer actualContainer = serializer.deserializeContainer(payload);
    Assert.assertEquals(expectedContainer, actualContainer);
}
Also used : PageLog(com.microsoft.appcenter.analytics.ingestion.models.PageLog) StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) Log(com.microsoft.appcenter.ingestion.models.Log) HashMap(java.util.HashMap) Device(com.microsoft.appcenter.ingestion.models.Device) PageLog(com.microsoft.appcenter.analytics.ingestion.models.PageLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) ArrayList(java.util.ArrayList) PageLogFactory(com.microsoft.appcenter.analytics.ingestion.models.json.PageLogFactory) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Date(java.util.Date) StartSessionLogFactory(com.microsoft.appcenter.analytics.ingestion.models.json.StartSessionLogFactory) StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) UUID(java.util.UUID) EventLogFactory(com.microsoft.appcenter.analytics.ingestion.models.json.EventLogFactory) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) Test(org.junit.Test)

Example 2 with LogContainer

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

the class PushSerializerTest method serialize.

@Test
public void serialize() throws JSONException {
    LogContainer expectedContainer = new LogContainer();
    List<Log> logs = new ArrayList<>();
    {
        PushInstallationLog log = new PushInstallationLog();
        log.setTimestamp(new Date());
        log.setPushToken("TEST");
        logs.add(log);
    }
    expectedContainer.setLogs(logs);
    UUID sid = UUIDUtils.randomUUID();
    for (Log log : logs) {
        log.setSid(sid);
    }
    LogSerializer serializer = new DefaultLogSerializer();
    serializer.addLogFactory(PushInstallationLog.TYPE, new PushInstallationLogFactory());
    String payload = serializer.serializeContainer(expectedContainer);
    LogContainer actualContainer = serializer.deserializeContainer(payload);
    Assert.assertEquals(expectedContainer, actualContainer);
}
Also used : PushInstallationLog(com.microsoft.appcenter.push.ingestion.models.PushInstallationLog) Log(com.microsoft.appcenter.ingestion.models.Log) PushInstallationLog(com.microsoft.appcenter.push.ingestion.models.PushInstallationLog) ArrayList(java.util.ArrayList) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) UUID(java.util.UUID) PushInstallationLogFactory(com.microsoft.appcenter.push.ingestion.models.json.PushInstallationLogFactory) Date(java.util.Date) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) Test(org.junit.Test)

Example 3 with LogContainer

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

the class AndroidTestUtils method generateMockLogContainer.

@NonNull
public static LogContainer generateMockLogContainer() {
    LogContainer container = new LogContainer();
    List<Log> logs = new ArrayList<>();
    logs.add(generateMockLog());
    container.setLogs(logs);
    return container;
}
Also used : MockLog(com.microsoft.appcenter.ingestion.models.json.MockLog) Log(com.microsoft.appcenter.ingestion.models.Log) ArrayList(java.util.ArrayList) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) NonNull(android.support.annotation.NonNull)

Example 4 with LogContainer

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

the class DefaultChannel method sendLogs.

/**
 * Send logs.
 *
 * @param groupState   The group state.
 * @param currentState The current state.
 * @param batch        The log batch.
 * @param batchId      The batch ID.
 */
@MainThread
private synchronized void sendLogs(final GroupState groupState, final int currentState, List<Log> batch, final String batchId) {
    if (checkStateDidNotChange(groupState, currentState)) {
        /* Send logs. */
        LogContainer logContainer = new LogContainer();
        logContainer.setLogs(batch);
        mIngestion.sendAsync(mAppSecret, mInstallId, logContainer, new ServiceCallback() {

            @Override
            public void onCallSucceeded(String payload) {
                mAppCenterHandler.post(new Runnable() {

                    @Override
                    public void run() {
                        handleSendingSuccess(groupState, currentState, batchId);
                    }
                });
            }

            @Override
            public void onCallFailed(final Exception e) {
                mAppCenterHandler.post(new Runnable() {

                    @Override
                    public void run() {
                        handleSendingFailure(groupState, currentState, batchId, e);
                    }
                });
            }
        });
        /* Check for more pending logs. */
        mAppCenterHandler.post(new Runnable() {

            @Override
            public void run() {
                checkPendingLogsAfterPost(groupState, currentState);
            }
        });
    }
}
Also used : ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) IOException(java.io.IOException) CancellationException(com.microsoft.appcenter.CancellationException) MainThread(android.support.annotation.MainThread)

Example 5 with LogContainer

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

the class IngestionHttpTest method sendAsync.

@Test
public void sendAsync() throws Exception {
    /* 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. */
    HttpClientNetworkStateHandler httpClient = mock(HttpClientNetworkStateHandler.class);
    whenNew(HttpClientNetworkStateHandler.class).withAnyArguments().thenReturn(httpClient);
    final ServiceCall call = mock(ServiceCall.class);
    final AtomicReference<HttpClient.CallTemplate> callTemplate = new AtomicReference<>();
    when(httpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), any(HttpClient.CallTemplate.class), any(ServiceCallback.class))).then(new Answer<ServiceCall>() {

        @Override
        public ServiceCall answer(InvocationOnMock invocation) throws Throwable {
            callTemplate.set((HttpClient.CallTemplate) invocation.getArguments()[3]);
            return call;
        }
    });
    /* Test calling code. */
    IngestionHttp ingestionHttp = new IngestionHttp(mock(Context.class), serializer);
    ingestionHttp.setLogUrl("http://mock");
    String appSecret = UUIDUtils.randomUUID().toString();
    UUID installId = UUIDUtils.randomUUID();
    ServiceCallback serviceCallback = mock(ServiceCallback.class);
    assertEquals(call, ingestionHttp.sendAsync(appSecret, installId, container, serviceCallback));
    /* Verify call to http client. */
    HashMap<String, String> expectedHeaders = new HashMap<>();
    expectedHeaders.put(IngestionHttp.APP_SECRET, appSecret);
    expectedHeaders.put(IngestionHttp.INSTALL_ID, installId.toString());
    verify(httpClient).callAsync(eq("http://mock" + IngestionHttp.API_PATH), eq(METHOD_POST), eq(expectedHeaders), notNull(HttpClient.CallTemplate.class), eq(serviceCallback));
    assertNotNull(callTemplate.get());
    assertEquals("mockPayload", callTemplate.get().buildRequestBody());
    /* Verify close. */
    ingestionHttp.close();
    verify(httpClient).close();
    /* Verify reopen. */
    ingestionHttp.reopen();
    verify(httpClient).reopen();
}
Also used : Context(android.content.Context) 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) AtomicReference(java.util.concurrent.atomic.AtomicReference) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) Matchers.anyString(org.mockito.Matchers.anyString) ServiceCallback(com.microsoft.appcenter.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LogContainer(com.microsoft.appcenter.ingestion.models.LogContainer) HttpClientNetworkStateHandler(com.microsoft.appcenter.http.HttpClientNetworkStateHandler) UUID(java.util.UUID) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

LogContainer (com.microsoft.appcenter.ingestion.models.LogContainer)12 Test (org.junit.Test)9 Log (com.microsoft.appcenter.ingestion.models.Log)8 ArrayList (java.util.ArrayList)7 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)5 UUID (java.util.UUID)5 ServiceCallback (com.microsoft.appcenter.http.ServiceCallback)3 DefaultLogSerializer (com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer)3 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 Context (android.content.Context)2 NonNull (android.support.annotation.NonNull)2 HttpClientNetworkStateHandler (com.microsoft.appcenter.http.HttpClientNetworkStateHandler)2 ServiceCall (com.microsoft.appcenter.http.ServiceCall)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Matchers.anyString (org.mockito.Matchers.anyString)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 MainThread (android.support.annotation.MainThread)1