/** * Send custom properties. * Unit test requires top level methods when PowerMock.whenNew. * * @param properties properties to send. */ @WorkerThread private void queueCustomProperties(@NonNull Map<String, Object> properties) { CustomPropertiesLog customPropertiesLog = new CustomPropertiesLog(); customPropertiesLog.setProperties(properties); mChannel.enqueue(customPropertiesLog, CORE_GROUP, Flags.DEFAULTS); }
@Override public void write(JSONStringer writer) throws JSONException { super.write(writer); writeProperties(writer, getProperties()); }
@Override public void read(JSONObject object) throws JSONException { super.read(object); setProperties(readProperties(object)); }
@Test public void compare() { /* Empty objects. */ CustomPropertiesLog a = new CustomPropertiesLog(); CustomPropertiesLog b = new CustomPropertiesLog(); checkEquals(a, b); checkEquals(a.getType(), CustomPropertiesLog.TYPE); UUID sid = UUIDUtils.randomUUID(); a.setSid(sid); checkNotEquals(a, b); b.setSid(sid); checkEquals(a, b); /* Properties. */ Map<String, Object> properties = new HashMap<>(); properties.put("test", "test"); a.setProperties(properties); checkEquals(a.getProperties(), properties); checkNotEquals(a, b); b.setProperties(new HashMap<String, Object>()); checkNotEquals(a, b); b.setProperties(properties); checkEquals(a, b); } }
@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(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(expected = JSONException.class) public void serializeWithoutProperties() throws JSONException { LogSerializer serializer = new DefaultLogSerializer(); CustomPropertiesLog invalidTypeLog = new CustomPropertiesLog(); invalidTypeLog.setTimestamp(new Date()); serializer.serializeLog(invalidTypeLog); }
@Override public Log create() { return new CustomPropertiesLog(); } }
properties.set("test", "test"); AppCenter.setCustomProperties(properties); verify(log).setProperties(eq(properties.getProperties())); verify(mChannel).enqueue(eq(log), eq(CORE_GROUP), eq(DEFAULTS));
private static Map<String, Object> readProperties(JSONObject object) throws JSONException { JSONArray jArray = object.getJSONArray(PROPERTIES); Map<String, Object> properties = new HashMap<>(); for (int i = 0; i < jArray.length(); i++) { JSONObject jProperty = jArray.getJSONObject(i); String key = jProperty.getString(PROPERTY_NAME); Object value = readPropertyValue(jProperty); properties.put(key, value); } return properties; }
private static void writeProperties(JSONStringer writer, Map<String, Object> properties) throws JSONException { if (properties != null) { writer.key(PROPERTIES).array(); for (Map.Entry<String, Object> property : properties.entrySet()) { writer.object(); JSONUtils.write(writer, PROPERTY_NAME, property.getKey()); writePropertyValue(writer, property.getValue()); writer.endObject(); } writer.endArray(); } else { throw new JSONException("Properties cannot be null"); } }
@Test public void compareDifferentType() { TestUtils.compareSelfNullClass(new CustomPropertiesLog()); }