Skip to content

Commit 51ef1e4

Browse files
committed
validate event details before posting
1 parent 154f79a commit 51ef1e4

File tree

3 files changed

+127
-1
lines changed

3 files changed

+127
-1
lines changed

intercom-java/src/main/java/io/intercom/api/Error.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ public class Error extends TypedData {
2020
public Error() {
2121
}
2222

23+
public Error(String code, String message) {
24+
this.code = code;
25+
this.message = message;
26+
}
27+
2328
public String getType() {
2429
return type;
2530
}

intercom-java/src/main/java/io/intercom/api/Event.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
44
import com.fasterxml.jackson.annotation.JsonInclude;
55
import com.fasterxml.jackson.annotation.JsonProperty;
6+
import com.google.common.annotations.VisibleForTesting;
7+
import com.google.common.base.Strings;
8+
import com.google.common.collect.Lists;
69
import com.google.common.collect.Maps;
710

8-
import java.util.Date;
911
import java.util.Map;
1012

1113
@SuppressWarnings("UnusedDeclaration")
@@ -14,12 +16,35 @@
1416
public class Event extends TypedData {
1517

1618
public static void create(Event event) throws InvalidException, AuthorizationException {
19+
20+
validateCreateEvent(event);
21+
1722
if (event.getCreatedAt() == 0L) {
1823
event.setCreatedAt(System.currentTimeMillis() / 1000);
1924
}
2025
DataResource.create(event, "events", Void.class);
2126
}
2227

28+
private static final ErrorCollection INVALID_NAME = new ErrorCollection(
29+
Lists.newArrayList(
30+
new Error("invalid", "an event must supply an event name")));
31+
32+
private static final ErrorCollection INVALID_USER = new ErrorCollection(
33+
Lists.newArrayList(
34+
new Error("invalid", "an event must supply either an email or a user id")));
35+
36+
@VisibleForTesting
37+
static void validateCreateEvent(Event event) {
38+
if(Strings.isNullOrEmpty(event.getEventName())) {
39+
throw new InvalidException(INVALID_NAME);
40+
}
41+
42+
if(Strings.isNullOrEmpty(event.getUserID())
43+
&& Strings.isNullOrEmpty(event.getEmail())) {
44+
throw new InvalidException(INVALID_USER);
45+
}
46+
}
47+
2348
@SuppressWarnings("FieldCanBeLocal")
2449
@JsonProperty("type")
2550
private final String type = "event";
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package io.intercom.api;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertTrue;
6+
import static org.junit.Assert.fail;
7+
8+
public class EventTest {
9+
10+
@Test
11+
public void testMissingUser() {
12+
final Event event1 = new Event().setEventName("bought-hat");
13+
try {
14+
Event.create(event1);
15+
fail("an event with no user id or email should be invalid");
16+
} catch (InvalidException e) {
17+
assertTrue(e.getFirstError() != null);
18+
}
19+
20+
final Event event2 = new Event()
21+
.setEventName("bought-hat")
22+
.setUserID("")
23+
.putMetadata("invitee_email", "jayne@serenity.io");
24+
try {
25+
Event.create(event2);
26+
fail("an event with a blank user id should be invalid");
27+
} catch (InvalidException e) {
28+
assertTrue(e.getFirstError() != null);
29+
}
30+
31+
final Event event3 = new Event().setEmail("");
32+
try {
33+
Event.create(event3);
34+
fail("an event with a blank email should be invalid");
35+
} catch (InvalidException e) {
36+
assertTrue(e.getFirstError() != null);
37+
}
38+
39+
}
40+
41+
@Test
42+
public void testMissingEventName() {
43+
Event event1 = new Event()
44+
.setEmail("jayne@serenity.io");
45+
try {
46+
Event.create(event1);
47+
fail("an event with a blank name should be invalid");
48+
} catch (InvalidException e) {
49+
assertTrue(e.getFirstError() != null);
50+
51+
}
52+
53+
Event event2 = new Event()
54+
.setEmail("jayne@serenity.io")
55+
.setEventName("");
56+
try {
57+
Event.create(event2);
58+
fail("an event with a blank name should be invalid");
59+
} catch (InvalidException e) {
60+
assertTrue(e.getFirstError() != null);
61+
}
62+
}
63+
64+
@Test
65+
public void testValid() {
66+
Event event1 = new Event()
67+
.setEmail("jayne@serenity.io")
68+
.setEventName("bought-hat");
69+
try {
70+
Event.validateCreateEvent(event1);
71+
} catch (InvalidException e) {
72+
fail("an event with an email and a name should be valid");
73+
}
74+
75+
Event event2 = new Event()
76+
.setUserID("1")
77+
.setEventName("bought-hat");
78+
try {
79+
Event.validateCreateEvent(event2);
80+
} catch (InvalidException e) {
81+
fail("an event with a user id and a name should be valid");
82+
}
83+
84+
Event event3 = new Event()
85+
.setUserID("1")
86+
.setEmail("jayne@serenity.io")
87+
.setEventName("bought-hat");
88+
try {
89+
Event.validateCreateEvent(event3);
90+
} catch (InvalidException e) {
91+
fail("an event with a user id, email and a name should be valid");
92+
}
93+
}
94+
95+
96+
}

0 commit comments

Comments
 (0)