Search in sources :

Example 1 with LogContainer

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

the class DefaultLogSerializer method deserializeContainer.

@NonNull
@Override
public LogContainer deserializeContainer(@NonNull String json) throws JSONException {
    JSONObject jContainer = new JSONObject(json);
    LogContainer container = new LogContainer();
    JSONArray jLogs = jContainer.getJSONArray(LOGS);
    List<Log> logs = new ArrayList<>();
    for (int i = 0; i < jLogs.length(); i++) {
        JSONObject jLog = jLogs.getJSONObject(i);
        Log log = readLog(jLog);
        logs.add(log);
    }
    container.setLogs(logs);
    return container;
}
Also used : JSONObject(org.json.JSONObject) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) NonNull(android.support.annotation.NonNull)

Example 2 with LogContainer

use of com.microsoft.azure.mobile.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) {
                handleSendingSuccess(groupState, currentState, batchId);
            }

            @Override
            public void onCallFailed(Exception e) {
                handleSendingFailure(groupState, currentState, batchId, e);
            }
        });
        /* Check for more pending logs. */
        checkPendingLogs(groupState.mName);
    }
}
Also used : ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) IOException(java.io.IOException) CancellationException(com.microsoft.azure.mobile.CancellationException) MainThread(android.support.annotation.MainThread)

Example 3 with LogContainer

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

the class LogSerializerAndroidTest method oneLog.

@Test
public void oneLog() throws JSONException {
    LogContainer expectedContainer = AndroidTestUtils.generateMockLogContainer();
    LogSerializer serializer = new DefaultLogSerializer();
    serializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
    String payload = serializer.serializeContainer(expectedContainer);
    android.util.Log.v(TAG, payload);
    LogContainer actualContainer = serializer.deserializeContainer(payload);
    Assert.assertEquals(expectedContainer, actualContainer);
    Assert.assertEquals(expectedContainer.hashCode(), actualContainer.hashCode());
}
Also used : LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) Test(org.junit.Test)

Example 4 with LogContainer

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

the class IngestionHttpTest method failedSerialization.

@Test
public void failedSerialization() throws Exception {
    /* Build some payload. */
    LogContainer container = new LogContainer();
    Log log = mock(Log.class);
    long logAbsoluteTime = 123L;
    when(log.getToffset()).thenReturn(logAbsoluteTime);
    List<Log> logs = new ArrayList<>();
    logs.add(log);
    container.setLogs(logs);
    LogSerializer serializer = mock(LogSerializer.class);
    JSONException exception = new JSONException("mock");
    when(serializer.serializeContainer(any(LogContainer.class))).thenThrow(exception);
    /* Stable time. */
    mockStatic(System.class);
    long now = 456L;
    when(System.currentTimeMillis()).thenReturn(now);
    /* 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(APP_SECRET, appSecret);
    expectedHeaders.put(IngestionHttp.INSTALL_ID, installId.toString());
    verify(httpClient).callAsync(eq("http://mock/logs?api_version=1.0.0-preview20160914"), eq(METHOD_POST), eq(expectedHeaders), notNull(HttpClient.CallTemplate.class), eq(serviceCallback));
    assertNotNull(callTemplate.get());
    try {
        callTemplate.get().buildRequestBody();
        Assert.fail("Expected json exception");
    } catch (JSONException ignored) {
    }
    /* Verify toffset manipulation. */
    verify(log).setToffset(now - logAbsoluteTime);
    verify(log).setToffset(logAbsoluteTime);
    /* Verify close. */
    ingestionHttp.close();
    verify(httpClient).close();
}
Also used : Context(android.content.Context) ServiceCall(com.microsoft.azure.mobile.http.ServiceCall) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) AtomicReference(java.util.concurrent.atomic.AtomicReference) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) Matchers.anyString(org.mockito.Matchers.anyString) ServiceCallback(com.microsoft.azure.mobile.http.ServiceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) HttpClientNetworkStateHandler(com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler) UUID(java.util.UUID) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with LogContainer

use of com.microsoft.azure.mobile.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.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.azure.mobile.push.ingestion.models.PushInstallationLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) PushInstallationLog(com.microsoft.azure.mobile.push.ingestion.models.PushInstallationLog) ArrayList(java.util.ArrayList) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) DefaultLogSerializer(com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) UUID(java.util.UUID) PushInstallationLogFactory(com.microsoft.azure.mobile.push.ingestion.models.json.PushInstallationLogFactory) DefaultLogSerializer(com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer) Test(org.junit.Test)

Aggregations

LogContainer (com.microsoft.azure.mobile.ingestion.models.LogContainer)11 Test (org.junit.Test)8 Log (com.microsoft.azure.mobile.ingestion.models.Log)7 ArrayList (java.util.ArrayList)6 LogSerializer (com.microsoft.azure.mobile.ingestion.models.json.LogSerializer)4 UUID (java.util.UUID)4 ServiceCallback (com.microsoft.azure.mobile.http.ServiceCallback)3 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)3 HashMap (java.util.HashMap)3 Context (android.content.Context)2 NonNull (android.support.annotation.NonNull)2 HttpClientNetworkStateHandler (com.microsoft.azure.mobile.http.HttpClientNetworkStateHandler)2 ServiceCall (com.microsoft.azure.mobile.http.ServiceCall)2 DefaultLogSerializer (com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer)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 CancellationException (com.microsoft.azure.mobile.CancellationException)1