/** * 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(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); }
@NonNull @Override public Log deserializeLog(@NonNull String json, String type) throws JSONException { return readLog(new JSONObject(json), type); }
@NonNull @Override public String serializeLog(@NonNull Log log) throws JSONException { return writeLog(new JSONStringer(), log).toString(); }
@NonNull @Override public String serializeContainer(@NonNull LogContainer logContainer) throws JSONException { /* Init JSON serializer. */ JSONStringer writer = new JSONStringer(); /* Start writing JSON. */ writer.object(); writer.key(LOGS).array(); for (Log log : logContainer.getLogs()) { writeLog(writer, log); } writer.endArray(); writer.endObject(); return writer.toString(); }
@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); }
@NonNull @Override public LogContainer deserializeContainer(@NonNull String json, String type) throws JSONException { JSONObject jContainer = new JSONObject(json); LogContainer container = new LogContainer(); JSONArray jLogs = jContainer.getJSONArray(LOGS); List<Log> logs = new ArrayList<>(); for (int i = 0; i < jLogs.length(); i++) { JSONObject jLog = jLogs.getJSONObject(i); Log log = readLog(jLog, type); logs.add(log); } container.setLogs(logs); return container; }
@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 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 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); }
@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(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); }
@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(expected = JSONException.class) public void deserializeWithoutProperties() throws JSONException { LogSerializer serializer = new DefaultLogSerializer(); serializer.addLogFactory(CustomPropertiesLog.TYPE, new CustomPropertiesLogFactory()); serializer.deserializeLog("{" + "\"type\": \"customProperties\"," + "\"timestamp\": \"2017-07-08T00:32:58.123Z\"" + "}", null); }
@Test(expected = JSONException.class) public void deserializeWithInvalidDate() 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\":\"dateTime\",\"value\":\"today\"}]" + "}", null); }
@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 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); }
LogSerializer logSerializer = new DefaultLogSerializer(); logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory()); persistence.setLogSerializer(logSerializer);
LogSerializer logSerializer = new DefaultLogSerializer(); logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory()); persistence.setLogSerializer(logSerializer);
LogSerializer logSerializer = new DefaultLogSerializer(); logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory()); persistence.setLogSerializer(logSerializer);