@Override public String buildRequestBody() throws JSONException { /* Serialize payload. */ return mLogSerializer.serializeContainer(mLogContainer); }
@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 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); }
container.setLogs(logs); LogSerializer serializer = mock(LogSerializer.class); when(serializer.serializeContainer(any(LogContainer.class))).thenReturn("mockPayload");
LogSerializer serializer = mock(LogSerializer.class); JSONException exception = new JSONException("mock"); when(serializer.serializeContainer(any(LogContainer.class))).thenThrow(exception);
@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 serialize() throws JSONException { LogContainer expectedContainer = new LogContainer(); List<Log> logs = new ArrayList<>(); { DistributionStartSessionLog log = new DistributionStartSessionLog(); log.setTimestamp(new Date()); logs.add(log); } expectedContainer.setLogs(logs); UUID sid = UUIDUtils.randomUUID(); for (Log log : logs) { log.setSid(sid); } /* Serialize and deserialize logs container. */ LogSerializer serializer = new DefaultLogSerializer(); serializer.addLogFactory(DistributionStartSessionLog.TYPE, new DistributionStartSessionLogFactory()); String payload = serializer.serializeContainer(expectedContainer); LogContainer actualContainer = serializer.deserializeContainer(payload, null); /* Verify that logs container successfully deserialized. */ Assert.assertEquals(expectedContainer, actualContainer); } }
@Test public void serialize() throws JSONException { LogContainer expectedContainer = new LogContainer(); List<Log> logs = new ArrayList<>(); { PushInstallationLog log = new PushInstallationLog(); log.setTimestamp(new Date()); log.setPushToken("TEST"); logs.add(log); } expectedContainer.setLogs(logs); UUID sid = UUIDUtils.randomUUID(); for (Log log : logs) { log.setSid(sid); } LogSerializer serializer = new DefaultLogSerializer(); serializer.addLogFactory(PushInstallationLog.TYPE, new PushInstallationLogFactory()); String payload = serializer.serializeContainer(expectedContainer); LogContainer actualContainer = serializer.deserializeContainer(payload, null); Assert.assertEquals(expectedContainer, actualContainer); } }
serializer.addLogFactory(PageLog.TYPE, new PageLogFactory()); serializer.addLogFactory(EventLog.TYPE, new EventLogFactory()); String payload = serializer.serializeContainer(expectedContainer); android.util.Log.v(TAG, payload); LogContainer actualContainer = serializer.deserializeContainer(payload, null);