public void testEqualsAndHashCode() { final Event event1 = new Event(); final Event event2 = new Event(); assertFalse(event1.equals(null)); assertFalse(event1.equals(new Object())); assertTrue(event1.equals(event2)); assertEquals(event1.hashCode(), event2.hashCode()); event1.key = "eventKey"; assertFalse(event1.equals(event2)); assertFalse(event2.equals(event1)); assertTrue(event1.hashCode() != event2.hashCode()); event2.key = "eventKey"; assertTrue(event1.equals(event2)); assertTrue(event2.equals(event1)); assertEquals(event1.hashCode(), event2.hashCode()); event1.timestamp = 1234; assertFalse(event1.equals(event2)); assertFalse(event2.equals(event1)); assertTrue(event1.hashCode() != event2.hashCode()); event2.timestamp = 1234; assertTrue(event1.equals(event2)); assertTrue(event2.equals(event1)); assertEquals(event1.hashCode(), event2.hashCode()); event1.segmentation = new HashMap<String, String>(); assertFalse(event1.equals(event2)); assertFalse(event2.equals(event1)); assertTrue(event1.hashCode() != event2.hashCode()); event2.segmentation = new HashMap<String, String>(); assertTrue(event1.equals(event2)); assertTrue(event2.equals(event1)); assertEquals(event1.hashCode(), event2.hashCode()); event1.segmentation.put("segkey", "segvalue"); assertFalse(event1.equals(event2)); assertFalse(event2.equals(event1)); assertTrue(event1.hashCode() != event2.hashCode()); event2.segmentation.put("segkey", "segvalue"); assertTrue(event1.equals(event2)); assertTrue(event2.equals(event1)); assertEquals(event1.hashCode(), event2.hashCode()); event1.sum = 3.2; event2.count = 42; assertTrue(event1.equals(event2)); assertTrue(event2.equals(event1)); assertEquals(event1.hashCode(), event2.hashCode()); }
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 testToJSON_nullSegmentation() throws JSONException { final Event event = new Event(); event.key = "eventKey"; event.timestamp = 1234; event.count = 42; event.sum = 3.2; final JSONObject jsonObj = event.toJSON(); assertEquals(6, jsonObj.length()); assertEquals(event.key, jsonObj.getString("key")); assertEquals(event.timestamp, jsonObj.getInt("timestamp")); assertEquals(event.count, jsonObj.getInt("count")); assertEquals(event.sum, jsonObj.getDouble("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 testToJSON_sumNaNCausesJSONException() throws JSONException { final Event event = new Event(); event.key = "eventKey"; event.timestamp = 1234; event.count = 42; event.sum = Double.NaN; event.segmentation = new HashMap<String, String>(); event.segmentation.put("segkey", "segvalue"); final JSONObject jsonObj = event.toJSON(); assertEquals(6, jsonObj.length()); assertEquals(event.key, jsonObj.getString("key")); assertEquals(event.timestamp, jsonObj.getInt("timestamp")); assertEquals(event.count, jsonObj.getInt("count")); assertEquals(1, jsonObj.getJSONObject("segmentation").length()); assertEquals( event.segmentation.get("segkey"), jsonObj.getJSONObject("segmentation").getString("segkey")); }
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); }