Search in sources :

Example 96 with Log

use of com.microsoft.appcenter.ingestion.models.Log 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);
    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);
    /* 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/logs?api-version=1.0.0"), 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 close. */
    ingestionHttp.close();
    verify(httpClient).close();
}
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) JSONException(org.json.JSONException) 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)

Example 97 with Log

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

the class DatabasePersistenceTest method databaseOperationException.

@Test
public void databaseOperationException() throws Persistence.PersistenceException, IOException, JSONException {
    /* Mock instances. */
    mockStatic(AppCenterLog.class);
    LogSerializer mockSerializer = mock(DefaultLogSerializer.class);
    when(mockSerializer.serializeLog(any(Log.class))).thenReturn("{}");
    DatabasePersistence mockPersistence = spy(new DatabasePersistence("test-persistence", "operation.exception", 1));
    doReturn(mockSerializer).when(mockPersistence).getLogSerializer();
    try {
        /* Generate a log and persist. */
        Log log = mock(Log.class);
        mockPersistence.putLog("test-p1", log);
    } finally {
        /* Close. */
        // noinspection ThrowFromFinallyBlock
        mockPersistence.close();
    }
    verifyStatic();
    AppCenterLog.error(eq(AppCenter.LOG_TAG), anyString(), any(RuntimeException.class));
}
Also used : AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) Log(com.microsoft.appcenter.ingestion.models.Log) DefaultLogSerializer(com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 98 with Log

use of com.microsoft.appcenter.ingestion.models.Log in project AppCenter-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 99 with Log

use of com.microsoft.appcenter.ingestion.models.Log in project AppCenter-SDK-Android by Microsoft.

the class LogSerializerAndroidTest method customPropertiesLog.

@Test
public void customPropertiesLog() throws JSONException {
    CustomPropertiesLog log = new CustomPropertiesLog();
    Map<String, Object> properties = new HashMap<>();
    properties.put("t1", "test");
    properties.put("t2", new Date(0));
    properties.put("t3", 0);
    properties.put("t4", false);
    properties.put("t5", null);
    log.setProperties(properties);
    UUID sid = UUIDUtils.randomUUID();
    log.setSid(sid);
    log.setTimestamp(new Date());
    /* Verify serialize and deserialize. */
    LogSerializer serializer = new DefaultLogSerializer();
    serializer.addLogFactory(CustomPropertiesLog.TYPE, new CustomPropertiesLogFactory());
    String payload = serializer.serializeLog(log);
    Log actualContainer = serializer.deserializeLog(payload);
    Assert.assertEquals(log, actualContainer);
}
Also used : HashMap(java.util.HashMap) StartServiceLog(com.microsoft.appcenter.ingestion.models.StartServiceLog) CustomPropertiesLog(com.microsoft.appcenter.ingestion.models.CustomPropertiesLog) Log(com.microsoft.appcenter.ingestion.models.Log) UUID(java.util.UUID) Date(java.util.Date) CustomPropertiesLog(com.microsoft.appcenter.ingestion.models.CustomPropertiesLog) Test(org.junit.Test)

Example 100 with Log

use of com.microsoft.appcenter.ingestion.models.Log in project AppCenter-SDK-Android by Microsoft.

the class LogSerializerAndroidTest method startServiceLog.

@Test
public void startServiceLog() throws JSONException {
    StartServiceLog log = new StartServiceLog();
    List<String> services = new ArrayList<>();
    services.add("FIRST");
    services.add("SECOND");
    log.setServices(services);
    UUID sid = UUIDUtils.randomUUID();
    log.setSid(sid);
    log.setTimestamp(new Date());
    /* Verify serialize and deserialize. */
    LogSerializer serializer = new DefaultLogSerializer();
    serializer.addLogFactory(StartServiceLog.TYPE, new StartServiceLogFactory());
    String payload = serializer.serializeLog(log);
    Log actualContainer = serializer.deserializeLog(payload);
    Assert.assertEquals(log, actualContainer);
}
Also used : StartServiceLog(com.microsoft.appcenter.ingestion.models.StartServiceLog) CustomPropertiesLog(com.microsoft.appcenter.ingestion.models.CustomPropertiesLog) Log(com.microsoft.appcenter.ingestion.models.Log) ArrayList(java.util.ArrayList) StartServiceLog(com.microsoft.appcenter.ingestion.models.StartServiceLog) UUID(java.util.UUID) Date(java.util.Date) 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