Search in sources :

Example 6 with LogContainer

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

the class JSONUtilsAndroidTest method serializeContainerWithDefaultWriter.

@Test
public void serializeContainerWithDefaultWriter() throws JSONException {
    /* Create a mock log container. */
    LogContainer mockContainer = mock(LogContainer.class);
    /* Set log level to VERBOSE to instantiate JSONStringer for pretty JSON string. */
    MobileCenterLog.setLogLevel(Log.VERBOSE);
    LogSerializer serializer = new DefaultLogSerializer();
    String json = serializer.serializeContainer(mockContainer);
    /* Remove new lines and spaces. */
    json = json.replace("\n", "").replace(" ", "");
    /* Set log level to ERROR to instantiate JSONStringer without indentations. */
    MobileCenterLog.setLogLevel(Log.ERROR);
    /* Verify. */
    assertEquals(json, serializer.serializeContainer(mockContainer));
}
Also used : LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) Test(org.junit.Test)

Example 7 with LogContainer

use of com.microsoft.azure.mobile.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);
    long logAbsoluteTime = 123L;
    when(log.getToffset()).thenReturn(logAbsoluteTime);
    List<Log> logs = new ArrayList<>();
    logs.add(log);
    container.setLogs(logs);
    LogSerializer serializer = mock(LogSerializer.class);
    when(serializer.serializeContainer(any(LogContainer.class))).thenReturn("mockPayload");
    /* 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" + IngestionHttp.API_PATH), eq(METHOD_POST), eq(expectedHeaders), notNull(HttpClient.CallTemplate.class), eq(serviceCallback));
    assertNotNull(callTemplate.get());
    assertEquals("mockPayload", callTemplate.get().buildRequestBody());
    /* 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) 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 8 with LogContainer

use of com.microsoft.azure.mobile.ingestion.models.LogContainer 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 : Log(com.microsoft.azure.mobile.ingestion.models.Log) AbstractLog(com.microsoft.azure.mobile.ingestion.models.AbstractLog) AbstractLog(com.microsoft.azure.mobile.ingestion.models.AbstractLog) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) Test(org.junit.Test)

Example 9 with LogContainer

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

the class LogSerializerAndroidTest method emptyLogs.

@Test
public void emptyLogs() throws JSONException {
    LogContainer expectedContainer = new LogContainer();
    expectedContainer.setLogs(Collections.<Log>emptyList());
    LogSerializer serializer = new DefaultLogSerializer();
    String payload = serializer.serializeContainer(expectedContainer);
    android.util.Log.v(TAG, payload);
    LogContainer actualContainer = serializer.deserializeContainer(payload);
    Assert.assertEquals(expectedContainer, actualContainer);
}
Also used : LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) Test(org.junit.Test)

Example 10 with LogContainer

use of com.microsoft.azure.mobile.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 : Log(com.microsoft.azure.mobile.ingestion.models.Log) MockLog(com.microsoft.azure.mobile.ingestion.models.json.MockLog) ArrayList(java.util.ArrayList) LogContainer(com.microsoft.azure.mobile.ingestion.models.LogContainer) NonNull(android.support.annotation.NonNull)

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