@Override public String buildRequestBody() throws JSONException { /* Serialize payload. */ StringBuilder jsonStream = new StringBuilder(); for (Log log : mLogContainer.getLogs()) { jsonStream.append(mLogSerializer.serializeLog(log)); /* We have to use a different delimiter specific to OneCollector. */ jsonStream.append('\n'); } return jsonStream.toString(); }
/** * Verify JSON error as long as required fields (required as per SDK) are missing. */ private void assertJsonFailure(LogSerializer serializer, MockCommonSchemaLog log) { try { serializer.serializeLog(log); fail("Was supposed to fail with JSONException"); } catch (JSONException ignore) { /* Expected. */ } }
private void checkSerialization(LogSerializer serializer, MockCommonSchemaLog log) throws JSONException { Log copy = serializer.deserializeLog(serializer.serializeLog(log), MockCommonSchemaLog.TYPE); assertEquals(log, copy); } }
@Test(expected = PersistenceException.class) public void putLogWithJSONException() throws Exception { DatabaseManager databaseManager = mock(DatabaseManager.class); whenNew(DatabaseManager.class).withAnyArguments().thenReturn(databaseManager); DatabasePersistence persistence = new DatabasePersistence(mock(Context.class)); /* Set a mock log serializer. */ LogSerializer logSerializer = mock(LogSerializer.class); when(logSerializer.serializeLog(any(Log.class))).thenThrow(new JSONException("JSON exception")); persistence.setLogSerializer(logSerializer); /* Persist a log. */ persistence.putLog(mock(Log.class), "test-p1", PERSISTENCE_NORMAL); }
private static void checkSerialization(Log log, LogSerializer serializer) throws JSONException { String payload = serializer.serializeLog(log); Log deSerializedLog = serializer.deserializeLog(payload, null); checkEquals(log, deSerializedLog); }
@Test(expected = PersistenceException.class) public void putLogWithOpenDatabaseException() throws Exception { DatabaseManager databaseManager = mock(DatabaseManager.class); whenNew(DatabaseManager.class).withAnyArguments().thenReturn(databaseManager); when(databaseManager.getMaxSize()).thenReturn(-1L); DatabasePersistence persistence = new DatabasePersistence(mock(Context.class)); /* Set a mock log serializer. */ LogSerializer logSerializer = mock(LogSerializer.class); when(logSerializer.serializeLog(any(Log.class))).thenReturn("mock"); persistence.setLogSerializer(logSerializer); /* Persist a log. */ persistence.putLog(mock(Log.class), "test-p1", PERSISTENCE_NORMAL); }
@Test(expected = JSONException.class) public void serializeWithInvalidType() throws JSONException { LogSerializer serializer = new DefaultLogSerializer(); CustomPropertiesLog invalidTypeLog = new CustomPropertiesLog(); invalidTypeLog.setTimestamp(new Date()); Map<String, Object> invalidTypeProperties = new HashMap<>(); invalidTypeProperties.put("nested", new HashMap<String, Object>()); invalidTypeLog.setProperties(invalidTypeProperties); serializer.serializeLog(invalidTypeLog); }
@Test public void saveWrapperExceptionWhenSDKDisabled() throws JSONException { when(SharedPreferencesManager.getBoolean(CRASHES_ENABLED_KEY, true)).thenReturn(false); LogSerializer logSerializer = Mockito.mock(LogSerializer.class); Crashes.getInstance().setLogSerializer(logSerializer); WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), new byte[]{'d'}); verify(logSerializer, never()).serializeLog(any(Log.class)); verifyNoMoreInteractions(ErrorLogHelper.class); } }
@Test public void testInvalidJsonException() throws JSONException { mExceptionHandler.register(); LogSerializer logSerializer = mock(LogSerializer.class); final JSONException jsonException = new JSONException("Fake JSON serializing exception"); when(logSerializer.serializeLog(any(Log.class))).thenThrow(jsonException); Whitebox.setInternalState(Crashes.getInstance(), "mLogSerializer", logSerializer); final Thread thread = Thread.currentThread(); final RuntimeException exception = new RuntimeException(); mExceptionHandler.uncaughtException(thread, exception); verifyStatic(); AppCenterLog.error(eq(Crashes.LOG_TAG), anyString(), eq(jsonException)); verify(mDefaultExceptionHandler).uncaughtException(thread, exception); }
@Test(expected = JSONException.class) public void serializeWithoutProperties() throws JSONException { LogSerializer serializer = new DefaultLogSerializer(); CustomPropertiesLog invalidTypeLog = new CustomPropertiesLog(); invalidTypeLog.setTimestamp(new Date()); serializer.serializeLog(invalidTypeLog); }
@Test public void saveWrapperSdkCrash() throws JSONException, IOException { LogSerializer logSerializer = Mockito.mock(LogSerializer.class); when(logSerializer.serializeLog(any(ManagedErrorLog.class))).thenReturn("mock"); Crashes.getInstance().setLogSerializer(logSerializer); byte[] data = new byte[]{'d'}; WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), data); verifyStatic(); FileManager.writeObject(any(File.class), eq(data)); /* We can't do it twice in the same process. */ data = new byte[]{'e'}; WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), data); verifyStatic(never()); FileManager.writeObject(any(File.class), eq(data)); }
@Test public void saveWrapperSdkCrashWithOnlyJavaThrowable() throws JSONException, IOException { LogSerializer logSerializer = Mockito.mock(LogSerializer.class); when(logSerializer.serializeLog(any(ManagedErrorLog.class))).thenReturn("mock"); Crashes.getInstance().setLogSerializer(logSerializer); Throwable throwable = new Throwable(); WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), throwable, new Exception(), null); verifyStatic(never()); FileManager.writeObject(any(File.class), isNull(byte[].class)); verifyStatic(); FileManager.writeObject(any(File.class), eq(throwable)); /* We can't do it twice in the same process. */ WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), throwable, new Exception(), null); verifyStatic(); FileManager.writeObject(any(File.class), eq(throwable)); }
@Test public void saveWrapperSdkCrashWithJavaThrowable() throws JSONException, IOException { LogSerializer logSerializer = Mockito.mock(LogSerializer.class); when(logSerializer.serializeLog(any(ManagedErrorLog.class))).thenReturn("mock"); Crashes.getInstance().setLogSerializer(logSerializer); byte[] data = new byte[]{'d'}; Throwable throwable = new Throwable(); WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), throwable, new Exception(), data); verifyStatic(); FileManager.writeObject(any(File.class), eq(data)); verifyStatic(); FileManager.writeObject(any(File.class), eq(throwable)); /* We can't do it twice in the same process. */ data = new byte[]{'e'}; WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), throwable, new Exception(), data); verifyStatic(never()); FileManager.writeObject(any(File.class), eq(data)); verifyStatic(); FileManager.writeObject(any(File.class), eq(throwable)); }
@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); //noinspection ConstantConditions 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, null); assertEquals(log, actualContainer); }
@Test public void saveWrapperSdkCrashFailsWithIOException() throws IOException, JSONException { doThrow(new IOException()).when(FileManager.class); FileManager.write(any(File.class), anyString()); LogSerializer logSerializer = Mockito.mock(LogSerializer.class); when(logSerializer.serializeLog(any(ManagedErrorLog.class))).thenReturn("mock"); Crashes.getInstance().setLogSerializer(logSerializer); WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), new byte[]{'d'}); verifyStatic(); AppCenterLog.error(anyString(), anyString(), argThat(new ArgumentMatcher<Throwable>() { @Override public boolean matches(Object argument) { return argument instanceof IOException; } })); /* Second call is ignored. */ WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), new byte[]{'e'}); /* No more error. */ verifyStatic(); AppCenterLog.error(anyString(), anyString(), argThat(new ArgumentMatcher<Throwable>() { @Override public boolean matches(Object argument) { return argument instanceof IOException; } })); }
@Test public void saveWrapperSdkCrashFailsWithIOExceptionAfterLog() throws IOException, JSONException { byte[] data = {'d'}; doThrow(new IOException()).when(FileManager.class); FileManager.writeObject(any(File.class), eq(data)); LogSerializer logSerializer = Mockito.mock(LogSerializer.class); when(logSerializer.serializeLog(any(ManagedErrorLog.class))).thenReturn("mock"); Crashes.getInstance().setLogSerializer(logSerializer); WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), data); verifyStatic(); AppCenterLog.error(anyString(), anyString(), argThat(new ArgumentMatcher<Throwable>() { @Override public boolean matches(Object argument) { return argument instanceof IOException; } })); /* Second call is ignored. */ WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), new byte[]{'e'}); /* No more error. */ verifyStatic(); AppCenterLog.error(anyString(), anyString(), argThat(new ArgumentMatcher<Throwable>() { @Override public boolean matches(Object argument) { return argument instanceof IOException; } })); }
@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, null); assertEquals(log, actualContainer); }
@Test(expected = JSONException.class) public void deserializeUnknownType() throws JSONException { MockLog log = AndroidTestUtils.generateMockLog(); LogSerializer serializer = new DefaultLogSerializer(); serializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory()); String payload = serializer.serializeLog(log); android.util.Log.v(TAG, payload); new DefaultLogSerializer().deserializeLog(payload, null); }
@Test public void saveWrapperSdkCrashFailsWithJSONException() throws JSONException { LogSerializer logSerializer = Mockito.mock(LogSerializer.class); when(logSerializer.serializeLog(any(ManagedErrorLog.class))).thenThrow(new JSONException("mock")); Crashes.getInstance().setLogSerializer(logSerializer); WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), new byte[]{'d'}); verifyStatic(); AppCenterLog.error(anyString(), anyString(), argThat(new ArgumentMatcher<Throwable>() { @Override public boolean matches(Object argument) { return argument instanceof JSONException; } })); /* Second call is ignored. */ WrapperSdkExceptionManager.saveWrapperException(Thread.currentThread(), null, new Exception(), new byte[]{'e'}); /* No more error. */ verifyStatic(); AppCenterLog.error(anyString(), anyString(), argThat(new ArgumentMatcher<Throwable>() { @Override public boolean matches(Object argument) { return argument instanceof JSONException; } })); }
@Test public void logWithUserId() throws JSONException { MockLog expectedLog = AndroidTestUtils.generateMockLog(); expectedLog.setTimestamp(new Date()); expectedLog.setUserId("charlie"); /* Verify serialize and deserialize. */ LogSerializer serializer = new DefaultLogSerializer(); serializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory()); String payload = serializer.serializeLog(expectedLog); Log actualLog = serializer.deserializeLog(payload, null); assertEquals(expectedLog, actualLog); assertEquals("charlie", actualLog.getUserId()); }