Skip to content

Commit 20373a9

Browse files
author
Mike Davis
committed
Add support for event tag validation.
1 parent 3472bfd commit 20373a9

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

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

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
*/
4848
public class EventHandlerRule implements EventHandler, TestRule {
4949

50+
private static final String IMPRESSION_EVENT_NAME = "campaign_activated";
51+
5052
private LinkedList<CanonicalEvent> actualEvents;
5153

5254
@Override
@@ -76,25 +78,29 @@ private void verify() {
7678
assertTrue(actualEvents.isEmpty());
7779
}
7880

79-
public void expectImpression(String experientId, String variationId, String userId) throws Exception {
81+
public void expectImpression(String experientId, String variationId, String userId) {
8082
expectImpression(experientId, variationId, userId, Collections.emptyMap());
8183
}
8284

83-
public void expectImpression(String experientId, String variationId, String userId, Map<String, ?> attributes) throws Exception {
84-
CanonicalEvent expectedEvent = new CanonicalEvent(experientId, variationId, "campaign_activated", userId, attributes);
85-
verify(expectedEvent);
85+
public void expectImpression(String experientId, String variationId, String userId, Map<String, ?> attributes) {
86+
verify(experientId, variationId, IMPRESSION_EVENT_NAME, userId, attributes, null);
87+
}
88+
89+
public void expectConversion(String eventName, String userId) {
90+
expectConversion(eventName, userId, Collections.emptyMap());
8691
}
8792

88-
public void expectConversion(String eventName, String userId) throws Exception {
89-
expectConversion(null, null, eventName, userId, Collections.emptyMap());
93+
public void expectConversion(String eventName, String userId, Map<String, ?> attributes) {
94+
expectConversion(eventName, userId, attributes, Collections.emptyMap());
9095
}
9196

92-
public void expectConversion(String eventName, String userId, Map<String, ?> attributes) throws Exception {
93-
expectConversion(null, null, eventName, userId, attributes);
97+
public void expectConversion(String eventName, String userId, Map<String, ?> attributes, Map<String, ?> tags) {
98+
verify(null, null, eventName, userId, attributes, tags);
9499
}
95100

96-
public void expectConversion(String experientId, String variationId, String eventName, String userId, Map<String, ?> attributes) throws Exception {
97-
CanonicalEvent expectedEvent = new CanonicalEvent(experientId, variationId, eventName, userId, attributes);
101+
public void verify(String experientId, String variationId, String eventName, String userId,
102+
Map<String, ?> attributes, Map<String, ?> tags) {
103+
CanonicalEvent expectedEvent = new CanonicalEvent(experientId, variationId, eventName, userId, attributes, tags);
98104
verify(expectedEvent);
99105
}
100106

@@ -108,7 +114,7 @@ public void verify(CanonicalEvent expected) {
108114
}
109115

110116
@Override
111-
public void dispatchEvent(LogEvent logEvent) throws Exception {
117+
public void dispatchEvent(LogEvent logEvent) {
112118
List<Visitor> visitors = logEvent.getEventBatch().getVisitors();
113119

114120
if (visitors == null) {
@@ -135,7 +141,8 @@ public void dispatchEvent(LogEvent logEvent) throws Exception {
135141
visitor.getVisitorId(),
136142
visitor.getAttributes().stream()
137143
.filter(attribute -> !attribute.getKey().startsWith(RESERVED_ATTRIBUTE_PREFIX))
138-
.collect(Collectors.toMap(Attribute::getKey, Attribute::getValue))
144+
.collect(Collectors.toMap(Attribute::getKey, Attribute::getValue)),
145+
event.getTags()
139146
);
140147

141148
actualEvents.add(actual);
@@ -151,13 +158,16 @@ private static class CanonicalEvent {
151158
private String eventName;
152159
private String visitorId;
153160
private Map<String, ?> attributes;
161+
private Map<String, ?> tags;
154162

155-
public CanonicalEvent(String experimentId, String variationId, String eventName, String visitorId, Map<String, ?> attributes) {
163+
public CanonicalEvent(String experimentId, String variationId, String eventName,
164+
String visitorId, Map<String, ?> attributes, Map<String, ?> tags) {
156165
this.experimentId = experimentId;
157166
this.variationId = variationId;
158167
this.eventName = eventName;
159168
this.visitorId = visitorId;
160169
this.attributes = attributes;
170+
this.tags = tags;
161171
}
162172

163173
@Override
@@ -169,12 +179,13 @@ public boolean equals(Object o) {
169179
Objects.equals(variationId, that.variationId) &&
170180
Objects.equals(eventName, that.eventName) &&
171181
Objects.equals(visitorId, that.visitorId) &&
172-
Objects.equals(attributes, that.attributes);
182+
Objects.equals(attributes, that.attributes) &&
183+
Objects.equals(tags, that.tags);
173184
}
174185

175186
@Override
176187
public int hashCode() {
177-
return Objects.hash(experimentId, variationId, eventName, visitorId, attributes);
188+
return Objects.hash(experimentId, variationId, eventName, visitorId, attributes, tags);
178189
}
179190

180191
@Override
@@ -185,6 +196,7 @@ public String toString() {
185196
.add("eventName='" + eventName + "'")
186197
.add("visitorId='" + visitorId + "'")
187198
.add("attributes=" + attributes)
199+
.add("tags=" + tags)
188200
.toString();
189201
}
190202
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,7 +1131,7 @@ public void trackEventWithEventTags() throws Exception {
11311131
validProjectConfig.getEventNameMapping().get(EVENT_BASIC_EVENT_KEY) :
11321132
validProjectConfig.getEventTypes().get(0);
11331133

1134-
Map<String, Object> eventTags = new HashMap<String, Object>();
1134+
Map<String, Object> eventTags = new HashMap<>();
11351135
eventTags.put("int_param", 123);
11361136
eventTags.put("string_param", "123");
11371137
eventTags.put("boolean_param", false);
@@ -1143,7 +1143,7 @@ public void trackEventWithEventTags() throws Exception {
11431143

11441144
// call track
11451145
optimizely.track(eventType.getKey(), genericUserId, Collections.emptyMap(), eventTags);
1146-
eventHandler.expectConversion(eventType.getKey(), genericUserId);
1146+
eventHandler.expectConversion(eventType.getKey(), genericUserId, Collections.emptyMap(), eventTags);
11471147

11481148
// TODO capture event tags.
11491149
}
@@ -1168,7 +1168,7 @@ public void trackEventWithNullEventTags() throws Exception {
11681168

11691169
// call track
11701170
optimizely.track(eventType.getKey(), genericUserId, Collections.emptyMap(), null);
1171-
eventHandler.expectConversion(eventType.getKey(), genericUserId);
1171+
eventHandler.expectConversion(eventType.getKey(), genericUserId, Collections.emptyMap(), null);
11721172

11731173
// TODO capture event tags.
11741174
}

0 commit comments

Comments
 (0)