public void testFromJSON_nullJSONObj() { try { Event.fromJSON(null); fail("Expected NPE when calling Event.fromJSON with null"); } catch (NullPointerException ignored) { // success } }
public void testFromJSON_keyOnly() throws JSONException { final Event expected = new Event(); expected.key = "eventKey"; final JSONObject jsonObj = new JSONObject(); jsonObj.put("key", expected.key); final Event actual = Event.fromJSON(jsonObj); assertEquals(expected, actual); assertEquals(expected.count, actual.count); assertEquals(expected.sum, actual.sum); }
public void testFromJSON_keyOnly_nullOtherValues() throws JSONException { final Event expected = new Event(); expected.key = "eventKey"; final JSONObject jsonObj = new JSONObject(); jsonObj.put("key", expected.key); jsonObj.put("timestamp", JSONObject.NULL); jsonObj.put("count", JSONObject.NULL); jsonObj.put("sum", JSONObject.NULL); final Event actual = Event.fromJSON(jsonObj); assertEquals(expected, actual); assertEquals(expected.count, actual.count); assertEquals(expected.sum, actual.sum); }
public void testFromJSON_segmentationNotADictionary() throws JSONException { final Event expected = new Event(); expected.key = "eventKey"; expected.timestamp = 1234; expected.count = 42; expected.sum = 3.2; final JSONObject jsonObj = new JSONObject(); jsonObj.put("key", expected.key); jsonObj.put("timestamp", expected.timestamp); jsonObj.put("count", expected.count); jsonObj.put("sum", expected.sum); jsonObj.put("segmentation", 1234); assertNull(Event.fromJSON(jsonObj)); }
public void testFromJSON_noSegmentation() throws JSONException { final Event expected = new Event(); expected.key = "eventKey"; expected.timestamp = 1234; expected.count = 42; expected.sum = 3.2; final JSONObject jsonObj = new JSONObject(); jsonObj.put("key", expected.key); jsonObj.put("timestamp", expected.timestamp); jsonObj.put("count", expected.count); jsonObj.put("sum", expected.sum); final Event actual = Event.fromJSON(jsonObj); assertEquals(expected, actual); assertEquals(expected.count, actual.count); assertEquals(expected.sum, actual.sum); }
public void testFromJSON_withSegmentation() throws JSONException { final Event expected = new Event(); expected.key = "eventKey"; expected.timestamp = 1234; expected.count = 42; expected.sum = 3.2; expected.segmentation = new HashMap<String, String>(); expected.segmentation.put("segkey", "segvalue"); final JSONObject jsonObj = new JSONObject(); jsonObj.put("key", expected.key); jsonObj.put("timestamp", expected.timestamp); jsonObj.put("count", expected.count); jsonObj.put("sum", expected.sum); jsonObj.put("segmentation", new JSONObject(expected.segmentation)); final Event actual = Event.fromJSON(jsonObj); assertEquals(expected, actual); assertEquals(expected.count, actual.count); assertEquals(expected.sum, actual.sum); }
public void testFromJSON_withSegmentation_nonStringValue() throws JSONException { final Event expected = new Event(); expected.key = "eventKey"; expected.timestamp = 1234; expected.count = 42; expected.sum = 3.2; expected.segmentation = new HashMap<String, String>(); expected.segmentation.put("segkey", "1234"); final Map<Object, Object> badMap = new HashMap<Object, Object>(); badMap.put( "segkey", 1234); // JSONObject.getString will end up converting this to the string "1234" final JSONObject jsonObj = new JSONObject(); jsonObj.put("key", expected.key); jsonObj.put("timestamp", expected.timestamp); jsonObj.put("count", expected.count); jsonObj.put("sum", expected.sum); jsonObj.put("segmentation", new JSONObject(badMap)); final Event actual = Event.fromJSON(jsonObj); assertEquals(expected, actual); assertEquals(expected.count, actual.count); assertEquals(expected.sum, actual.sum); }
public void testFromJSON_emptyKey() throws JSONException { final JSONObject jsonObj = new JSONObject(); jsonObj.put("key", ""); assertNull(Event.fromJSON(jsonObj)); }
public void testFromJSON_noKeyCausesJSONException() { final JSONObject jsonObj = new JSONObject(); assertNull(Event.fromJSON(jsonObj)); }