/** * Init. */ private Crashes() { mFactories = new HashMap<>(); mFactories.put(ManagedErrorLog.TYPE, ManagedErrorLogFactory.getInstance()); mFactories.put(HandledErrorLog.TYPE, HandledErrorLogFactory.getInstance()); mFactories.put(ErrorAttachmentLog.TYPE, ErrorAttachmentLogFactory.getInstance()); mLogSerializer = new DefaultLogSerializer(); mLogSerializer.addLogFactory(ManagedErrorLog.TYPE, ManagedErrorLogFactory.getInstance()); mLogSerializer.addLogFactory(ErrorAttachmentLog.TYPE, ErrorAttachmentLogFactory.getInstance()); mCrashesListener = DEFAULT_ERROR_REPORTING_LISTENER; mUnprocessedErrorReports = new LinkedHashMap<>(); mErrorReportCache = new LinkedHashMap<>(); }
@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, null); assertEquals(expectedContainer, actualContainer); assertEquals(expectedContainer.hashCode(), actualContainer.hashCode()); }
@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); }
private void checkSerialization(LogSerializer serializer, MockCommonSchemaLog log) throws JSONException { Log copy = serializer.deserializeLog(serializer.serializeLog(log), MockCommonSchemaLog.TYPE); assertEquals(log, copy); } }
@Test(expected = JSONException.class) public void deserializeWithInvalidNumber() throws JSONException { LogSerializer serializer = new DefaultLogSerializer(); serializer.addLogFactory(CustomPropertiesLog.TYPE, new CustomPropertiesLogFactory()); serializer.deserializeLog("{" + "\"type\": \"customProperties\"," + "\"timestamp\": \"2017-07-08T00:32:58.123Z\"," + "\"properties\":[{\"name\":\"test\",\"type\":\"number\",\"value\":false}]" + "}", null); }
@Test public void toCommonSchemaLog() { LogFactory logFactory = mock(LogFactory.class); MockLog log = AndroidTestUtils.generateMockLog(); LogSerializer serializer = new DefaultLogSerializer(); serializer.addLogFactory(MOCK_LOG_TYPE, logFactory); serializer.toCommonSchemaLog(log); verify(logFactory).toCommonSchemaLogs(log); } }
@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); }
@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(); }
@Test public void noQueueNullLog() throws JSONException { Context mockContext = mock(Context.class); Channel mockChannel = mock(Channel.class); mockStatic(ErrorLogHelper.class); when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[]{mock(File.class)}); when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]); Crashes crashes = Crashes.getInstance(); LogSerializer logSerializer = mock(LogSerializer.class); when(logSerializer.deserializeLog(anyString(), anyString())).thenReturn(null); crashes.setLogSerializer(logSerializer); crashes.onStarting(mAppCenterHandler); crashes.onStarted(mockContext, mockChannel, "", null, true); verify(mockChannel, never()).enqueue(any(Log.class), anyString(), anyInt()); }
@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, null); assertEquals(expectedContainer, actualContainer); }
@Override public String buildRequestBody() throws JSONException { /* Serialize payload. */ return mLogSerializer.serializeContainer(mLogContainer); }
@Test public void dontConvertCommonSchemaLogs() { /* Setup mocks. */ Channel channel = mock(Channel.class); LogSerializer logSerializer = mock(LogSerializer.class); /* Init listener. */ OneCollectorChannelListener listener = new OneCollectorChannelListener(mock(Context.class), channel, logSerializer, UUIDUtils.randomUUID()); listener.onPreparedLog(mock(CommonSchemaLog.class), TEST_GROUP, DEFAULTS); /* Verify no conversion. */ verify(logSerializer, never()).toCommonSchemaLog(any(Log.class)); /* Verify no enqueuing. */ verify(channel, never()).enqueue(any(Log.class), anyString(), anyInt()); }
@Test(expected = JSONException.class) public void deserializeWithInvalidType() throws JSONException { LogSerializer serializer = new DefaultLogSerializer(); serializer.addLogFactory(CustomPropertiesLog.TYPE, new CustomPropertiesLogFactory()); serializer.deserializeLog("{" + "\"type\": \"customProperties\"," + "\"timestamp\": \"2017-07-08T00:32:58.123Z\"," + "\"properties\":[{\"name\":\"test\",\"type\":\"unknown\",\"value\":42}]" + "}", null); }
logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory()); logSerializer.addLogFactory(MockCommonSchemaLog.TYPE, new MockCommonSchemaLogFactory()); ContentValues contentValues = new ContentValues(); contentValues.put(DatabasePersistence.COLUMN_GROUP, "test"); contentValues.put(DatabasePersistence.COLUMN_LOG, logSerializer.serializeLog(oldLog)); contentValues.put(DatabasePersistence.COLUMN_DATA_TYPE, MOCK_LOG_TYPE); databaseManager.put(contentValues, DatabasePersistence.COLUMN_PRIORITY);
/** * 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. */ } }
@Test public void processPendingErrorsCorrupted() throws JSONException { mockStatic(ErrorLogHelper.class); when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[]{mock(File.class)}); when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]); when(FileManager.read(any(File.class))).thenReturn(""); Crashes crashes = Crashes.getInstance(); LogSerializer logSerializer = mock(LogSerializer.class); when(logSerializer.deserializeLog(anyString(), anyString())).thenReturn(mock(ManagedErrorLog.class)); crashes.setLogSerializer(logSerializer); CrashesListener listener = mock(CrashesListener.class); crashes.setInstanceListener(listener); Channel channel = mock(Channel.class); crashes.onStarting(mAppCenterHandler); crashes.onStarted(mock(Context.class), channel, "", null, true); verifyZeroInteractions(listener); verify(channel, never()).enqueue(any(Log.class), anyString(), anyInt()); }
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 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. */ AppCenterLog.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. */ AppCenterLog.setLogLevel(Log.ERROR); /* Verify. */ assertEquals(json, serializer.serializeContainer(mockContainer)); } }
@Test public void validateCommonSchemaLogs() { /* Setup mocks. */ Channel channel = mock(Channel.class); LogSerializer logSerializer = mock(LogSerializer.class); when(logSerializer.toCommonSchemaLog(any(Log.class))).thenThrow(new IllegalArgumentException()); Log log = mock(Log.class); when(log.getTransmissionTargetTokens()).thenReturn(Collections.singleton("token")); /* Init listener. */ OneCollectorChannelListener listener = new OneCollectorChannelListener(mock(Context.class), channel, logSerializer, UUIDUtils.randomUUID()); listener.onPreparedLog(log, TEST_GROUP, DEFAULTS); /* Verify conversion attempted. */ verify(logSerializer).toCommonSchemaLog(any(Log.class)); /* Verify no enqueuing as the log was invalid. */ verify(channel, never()).enqueue(any(Log.class), anyString(), anyInt()); }
if (logFactories != null) { for (Map.Entry<String, LogFactory> logFactory : logFactories.entrySet()) { mLogSerializer.addLogFactory(logFactory.getKey(), logFactory.getValue());