@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; }
@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 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()); }
@NonNull public static LogContainer generateMockLogContainer() { LogContainer container = new LogContainer(); List<Log> logs = new ArrayList<>(); logs.add(generateMockLog()); container.setLogs(logs); return container; }
LogContainer logContainer = new LogContainer(); logContainer.setLogs(batch); groupState.mIngestion.sendAsync(mAppSecret, mInstallId, logContainer, new ServiceCallback() {
@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 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 compareLogContainer() { LogContainer container1 = new LogContainer(); LogContainer container2 = new LogContainer(); TestUtils.compareSelfNullClass(container1); TestUtils.checkEquals(container1, container2); log1.setSid(UUID.randomUUID()); container1.setLogs(Collections.singletonList(log1)); TestUtils.compareSelfNullClass(container1); TestUtils.checkNotEquals(container1, container2); container2.setLogs(Collections.singletonList(log1)); TestUtils.compareSelfNullClass(container1); TestUtils.checkEquals(container1, container2); log2.setSid(UUID.randomUUID()); container2.setLogs(Collections.singletonList(log2)); TestUtils.compareSelfNullClass(container1); TestUtils.checkNotEquals(container1, container2);
LogContainer container = new LogContainer(); Log log = mock(Log.class); List<Log> logs = new ArrayList<>(); logs.add(log); container.setLogs(logs); LogSerializer serializer = mock(LogSerializer.class); when(serializer.serializeContainer(any(LogContainer.class))).thenReturn("mockPayload");
LogContainer container = new LogContainer(); Log log = mock(Log.class); List<Log> logs = new ArrayList<>(); logs.add(log); container.setLogs(logs); LogSerializer serializer = mock(LogSerializer.class); JSONException exception = new JSONException("mock");
@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); } }
@Test public void someBatch() throws JSONException { LogContainer expectedContainer = new LogContainer(); Device device = new Device(); device.setSdkName("appcenter.android"); logs.add(startSessionLog); expectedContainer.setLogs(logs);