2525import java .util .*;
2626import java .util .stream .Collectors ;
2727
28+ import static junit .framework .TestCase .assertTrue ;
2829import static org .junit .Assert .assertEquals ;
30+ import static org .junit .Assert .fail ;
2931
32+ /**
33+ * EventHandlerRule is a JUnit rule that implements an Optimizely {@link EventHandler}.
34+ *
35+ * This implementation captures events being dispatched in a List.
36+ *
37+ * The List of "actual" events are compared, in order, against a list of "expected" events.
38+ *
39+ * Expected events are validated immediately against the head of actual events. If the queue is empty,
40+ * then a failure is raised. This is to make it easy to map back to the failing test line number.
41+ *
42+ * A failure is raised if at the end of the test there remain non-validated actual events. This is by design
43+ * to ensure that all outbound traffic is known and validated.
44+ *
45+ * TODO this rule does not yet support validation of event tags found in the {@link Event} payload.
46+ */
3047public class EventHandlerRule implements EventHandler , TestRule {
3148
32- private List <CanonicalEvent > expectedEvents = new LinkedList <>();
33- private List <CanonicalEvent > actualEvents = new LinkedList <>();
34-
35- public List <CanonicalEvent > getExpectedEvents () {
36- return expectedEvents ;
37- }
38-
39- public List <CanonicalEvent > getActualEvents () {
40- return actualEvents ;
41- }
42-
43- public boolean isEmpty () {
44- return actualEvents .isEmpty ();
45- }
49+ private LinkedList <CanonicalEvent > actualEvents ;
4650
4751 @ Override
4852 public Statement apply (final Statement base , Description description ) {
@@ -61,26 +65,14 @@ public void evaluate() throws Throwable {
6165 }
6266
6367 private void before () {
64- expectedEvents = new LinkedList <>();
68+ actualEvents = new LinkedList <>();
6569 }
6670
6771 private void after () {
6872 }
6973
7074 private void verify () {
71- assertEquals (expectedEvents .size (), actualEvents .size ());
72-
73- ListIterator <CanonicalEvent > expectedIterator = expectedEvents .listIterator ();
74- ListIterator <CanonicalEvent > actualIterator = actualEvents .listIterator ();
75-
76- while (expectedIterator .hasNext ()) {
77- CanonicalEvent expected = expectedIterator .next ();
78- CanonicalEvent actual = actualIterator .next ();
79-
80- assertEquals (expected , actual );
81- }
82-
83- // TODO make assertions when adding and verify that all actuals were exhausted.
75+ assertTrue (actualEvents .isEmpty ());
8476 }
8577
8678 public void expectImpression (String experientId , String variationId , String userId ) throws Exception {
@@ -89,7 +81,7 @@ public void expectImpression(String experientId, String variationId, String user
8981
9082 public void expectImpression (String experientId , String variationId , String userId , Map <String , ?> attributes ) throws Exception {
9183 CanonicalEvent expectedEvent = new CanonicalEvent (experientId , variationId , "campaign_activated" , userId , attributes );
92- expectedEvents . add (expectedEvent );
84+ verify (expectedEvent );
9385 }
9486
9587 public void expectConversion (String eventName , String userId ) throws Exception {
@@ -102,7 +94,16 @@ public void expectConversion(String eventName, String userId, Map<String, ?> att
10294
10395 public void expectConversion (String experientId , String variationId , String eventName , String userId , Map <String , ?> attributes ) throws Exception {
10496 CanonicalEvent expectedEvent = new CanonicalEvent (experientId , variationId , eventName , userId , attributes );
105- expectedEvents .add (expectedEvent );
97+ verify (expectedEvent );
98+ }
99+
100+ public void verify (CanonicalEvent expected ) {
101+ if (actualEvents .isEmpty ()) {
102+ fail (String .format ("Expected: %s, but not events are queued" , expected ));
103+ }
104+
105+ CanonicalEvent actual = actualEvents .removeFirst ();
106+ assertEquals (expected , actual );
106107 }
107108
108109 @ Override
0 commit comments