Search in sources :

Example 21 with LogSerializer

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

the class CrashesTest method queuePendingCrashesAlwaysSend.

@Test
public void queuePendingCrashesAlwaysSend() throws IOException, ClassNotFoundException, JSONException {
    Context mockContext = mock(Context.class);
    Channel mockChannel = mock(Channel.class);
    ErrorAttachmentLog mockAttachment = mock(ErrorAttachmentLog.class);
    when(mockAttachment.getId()).thenReturn(UUID.randomUUID());
    when(mockAttachment.getErrorId()).thenReturn(UUID.randomUUID());
    when(mockAttachment.getContentType()).thenReturn("");
    when(mockAttachment.getFileName()).thenReturn("");
    when(mockAttachment.getData()).thenReturn(new byte[0]);
    when(mockAttachment.isValid()).thenReturn(true);
    List<ErrorAttachmentLog> errorAttachmentLogList = Arrays.asList(mockAttachment, mockAttachment);
    ErrorReport report = new ErrorReport();
    mockStatic(ErrorLogHelper.class);
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
    when(ErrorLogHelper.getStoredThrowableFile(any(UUID.class))).thenReturn(mock(File.class));
    when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), any(Throwable.class))).thenReturn(report);
    when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
    when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(new RuntimeException());
    when(StorageHelper.PreferencesStorage.getBoolean(eq(Crashes.PREF_KEY_ALWAYS_SEND), anyBoolean())).thenReturn(true);
    CrashesListener mockListener = mock(CrashesListener.class);
    when(mockListener.shouldProcess(report)).thenReturn(true);
    when(mockListener.shouldProcess(report)).thenReturn(true);
    when(mockListener.shouldAwaitUserConfirmation()).thenReturn(false);
    when(mockListener.getErrorAttachments(report)).thenReturn(errorAttachmentLogList);
    Crashes crashes = Crashes.getInstance();
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString())).thenReturn(mErrorLog);
    crashes.setLogSerializer(logSerializer);
    crashes.setInstanceListener(mockListener);
    crashes.onStarted(mockContext, "", mockChannel);
    verify(mockListener).shouldProcess(report);
    verify(mockListener, never()).shouldAwaitUserConfirmation();
    verify(mockListener).getErrorAttachments(report);
    verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object log) {
            return log.equals(mErrorLog);
        }
    }), eq(crashes.getGroupName()));
    verify(mockChannel, times(errorAttachmentLogList.size())).enqueue(mockAttachment, crashes.getGroupName());
}
Also used : Context(android.content.Context) Channel(com.microsoft.azure.mobile.channel.Channel) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) ArgumentMatcher(org.mockito.ArgumentMatcher) ErrorAttachmentLog(com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog) UUID(java.util.UUID) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 22 with LogSerializer

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

the class CrashesTest method saveWrapperSdkErrorLogIOException.

@Test
public void saveWrapperSdkErrorLogIOException() throws IOException, JSONException {
    mockStatic(MobileCenterLog.class);
    ManagedErrorLog errorLog = new ManagedErrorLog();
    errorLog.setId(UUIDUtils.randomUUID());
    mockStatic(StorageHelper.InternalStorage.class);
    doThrow(new IOException()).when(StorageHelper.InternalStorage.class);
    StorageHelper.InternalStorage.write(any(File.class), anyString());
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.serializeLog(errorLog)).thenReturn("mock");
    Crashes.getInstance().setLogSerializer(logSerializer);
    WrapperSdkExceptionManager.saveWrapperSdkErrorLog(errorLog);
    verifyStatic();
    MobileCenterLog.error(anyString(), anyString(), any(IOException.class));
}
Also used : ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) StorageHelper(com.microsoft.azure.mobile.utils.storage.StorageHelper) IOException(java.io.IOException) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 23 with LogSerializer

use of com.microsoft.azure.mobile.ingestion.models.json.LogSerializer 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 24 with LogSerializer

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

the class CrashesTest method handleUserConfirmationDoNotSend.

@Test
public void handleUserConfirmationDoNotSend() throws IOException, ClassNotFoundException, JSONException {
    mockStatic(ErrorLogHelper.class);
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
    when(ErrorLogHelper.getStoredThrowableFile(any(UUID.class))).thenReturn(mock(File.class));
    when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), any(Throwable.class))).thenReturn(new ErrorReport());
    when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
    when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(null);
    CrashesListener mockListener = mock(CrashesListener.class);
    when(mockListener.shouldProcess(any(ErrorReport.class))).thenReturn(true);
    when(mockListener.shouldAwaitUserConfirmation()).thenReturn(true);
    Crashes crashes = Crashes.getInstance();
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString())).thenReturn(mErrorLog);
    crashes.setLogSerializer(logSerializer);
    crashes.setInstanceListener(mockListener);
    crashes.onStarted(mock(Context.class), "", mock(Channel.class));
    Crashes.notifyUserConfirmation(Crashes.DONT_SEND);
    verify(mockListener, never()).getErrorAttachments(any(ErrorReport.class));
    verify(mMockLooper).quit();
    verifyStatic();
    ErrorLogHelper.removeStoredErrorLogFile(mErrorLog.getId());
    verifyStatic();
    ErrorLogHelper.removeStoredThrowableFile(mErrorLog.getId());
}
Also used : ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) Context(android.content.Context) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) Channel(com.microsoft.azure.mobile.channel.Channel) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) UUID(java.util.UUID) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 25 with LogSerializer

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

the class CrashesTest method crashInLastSessionError.

@Test
public void crashInLastSessionError() throws JSONException, IOException, ClassNotFoundException {
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString())).thenReturn(mock(ManagedErrorLog.class));
    mockStatic(ErrorLogHelper.class);
    File lastErrorLogFile = errorStorageDirectory.newFile("last-error-log.json");
    when(ErrorLogHelper.getLastErrorLogFile()).thenReturn(lastErrorLogFile);
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { lastErrorLogFile });
    when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
    Crashes.getInstance().setLogSerializer(logSerializer);
    assertFalse(Crashes.hasCrashedInLastSession());
    JSONException jsonException = new JSONException("Fake JSON exception");
    when(logSerializer.deserializeLog(anyString())).thenThrow(jsonException);
    ResultCallback<ErrorReport> callback = new ResultCallback<ErrorReport>() {

        @Override
        public void onResult(ErrorReport data) {
            assertNull(data);
        }
    };
    /*
         * Last session error is only fetched upon initialization: enabled and channel ready.
         * Here the service is enabled by default but we are waiting channel to be ready, simulate that.
         */
    assertTrue(Crashes.isEnabled());
    Crashes.getLastSessionCrashReport(callback);
    Crashes.getInstance().onStarted(mock(Context.class), "", mock(Channel.class));
    assertFalse(Crashes.hasCrashedInLastSession());
    Crashes.getLastSessionCrashReport(callback);
    /*
         * De-serializing fails twice: processing the log from last time as part of the bulk processing.
         * And loading that same file for exposing it in getLastErrorReport.
         */
    verifyStatic(times(2));
    MobileCenterLog.error(eq(Crashes.LOG_TAG), anyString(), eq(jsonException));
}
Also used : ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) Context(android.content.Context) ResultCallback(com.microsoft.azure.mobile.ResultCallback) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) Channel(com.microsoft.azure.mobile.channel.Channel) JSONException(org.json.JSONException) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

LogSerializer (com.microsoft.azure.mobile.ingestion.models.json.LogSerializer)30 Test (org.junit.Test)30 Log (com.microsoft.azure.mobile.ingestion.models.Log)20 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)19 ManagedErrorLog (com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog)15 Context (android.content.Context)14 DefaultLogSerializer (com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer)13 File (java.io.File)13 Channel (com.microsoft.azure.mobile.channel.Channel)12 UUID (java.util.UUID)12 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)11 ArrayList (java.util.ArrayList)11 ErrorReport (com.microsoft.azure.mobile.crashes.model.ErrorReport)9 ErrorAttachmentLog (com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog)8 JSONException (org.json.JSONException)8 MediumTest (android.support.test.filters.MediumTest)7 MockLogFactory (com.microsoft.azure.mobile.ingestion.models.json.MockLogFactory)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5 LogContainer (com.microsoft.azure.mobile.ingestion.models.LogContainer)4 Matchers.anyString (org.mockito.Matchers.anyString)4