Skip to content

Commit fd1abcd

Browse files
author
Mike Davis
committed
Update doc strings.
1 parent 2913d26 commit fd1abcd

File tree

3 files changed

+35
-32
lines changed

3 files changed

+35
-32
lines changed

core-api/src/test/java/com/optimizely/ab/EventHandlerRule.java

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,28 @@
2525
import java.util.*;
2626
import java.util.stream.Collectors;
2727

28+
import static junit.framework.TestCase.assertTrue;
2829
import 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+
*/
3047
public 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

core-api/src/test/java/com/optimizely/ab/OptimizelyTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,6 @@ public void activateForNullVariation() throws Exception {
419419
Optimizely optimizely = optimizelyBuilder.withBucketing(mockBucketer).build();
420420
Variation actualVariation = optimizely.activate(activatedExperiment.getKey(), testUserId, testUserAttributes);
421421
assertNull(actualVariation);
422-
423-
assertTrue(eventHandler.isEmpty());
424422
}
425423

426424
/**

core-api/src/test/java/com/optimizely/ab/internal/LogbackVerifier.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
import static org.junit.Assert.fail;
3333

3434
/**
35+
* TODO As a usability improvement we should require expected messages be added after the message are expected to be
36+
* logged. This will allow us to map the failure immediately back to the test line number as opposed to the async
37+
* validation now that happens at the end of each individual test.
38+
*
3539
* From http://techblog.kenshoo.com/2013/08/junit-rule-for-verifying-logback-logging.html
3640
*/
3741
public class LogbackVerifier implements TestRule {

0 commit comments

Comments
 (0)