forked from intercom/intercom-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessagesTest.java
More file actions
114 lines (101 loc) · 4.37 KB
/
Copy pathMessagesTest.java
File metadata and controls
114 lines (101 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.intercom.api.integration;
import com.intercom.api.Intercom;
import com.intercom.api.core.pagination.SyncPagingIterable;
import com.intercom.api.resources.contacts.requests.DeleteContactRequest;
import com.intercom.api.resources.contacts.types.Contact;
import com.intercom.api.resources.conversations.types.Conversation;
import com.intercom.api.resources.messages.types.Message;
import com.intercom.api.types.CreateContactRequest;
import com.intercom.api.types.CreateMessageRequest;
import com.intercom.api.types.SearchRequest;
import com.intercom.api.types.SingleFilterSearchRequest;
import com.intercom.api.utils.TestClientFactory;
import com.intercom.api.utils.Utils;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class MessagesTest {
private Intercom client;
private String adminId;
private Contact user;
@BeforeEach
public void before() {
// arrange
client = TestClientFactory.create();
adminId = client.admins().list().getAdmins().get(0).getId();
user = client.contacts()
.create(CreateContactRequest.of(CreateContactRequest.WithExternalId.builder()
.externalId(Utils.randomString())
.name("Message Test User")
.build()));
}
@AfterEach
public void after() {
// cleanup
tryDeleteContact();
}
@Test
@SuppressWarnings("CallToPrintStackTrace")
public void testMessageThatCreatesAConversation() {
// act
Message message = client.messages()
.create(CreateMessageRequest.inapp(CreateMessageRequest.Inapp.builder()
.body("Hey, look at me! I am the conversations creator now!")
.from(CreateMessageRequest.Inapp.From.builder()
.id(Integer.parseInt(adminId))
.build())
.to(CreateMessageRequest.Inapp.To.builder()
.type(CreateMessageRequest.Inapp.To.Type.USER)
.id(user.getId())
.build())
.createConversationWithoutContactReply(true)
.build()));
// Allow for some time to index the conversation
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
System.out.println("Test execution interrupted: ");
e.printStackTrace();
after();
}
SyncPagingIterable<Conversation> searchResults = client.conversations()
.search(SearchRequest.builder()
.query(SearchRequest.Query.of(SingleFilterSearchRequest.builder()
.field("source.id")
.operator(SingleFilterSearchRequest.Operator.EQUALS)
.value(SingleFilterSearchRequest.Value.of(message.getId()))
.build()))
.build());
// assert
Assertions.assertFalse(searchResults.getItems().isEmpty());
}
@Test
public void testCreateMessageNoConversation() {
// act
Message response = client.messages()
.create(CreateMessageRequest.inapp(CreateMessageRequest.Inapp.builder()
.body("Message without creating conversation")
.from(CreateMessageRequest.Inapp.From.builder()
.id(Integer.parseInt(adminId))
.build())
.to(CreateMessageRequest.Inapp.To.builder()
.type(CreateMessageRequest.Inapp.To.Type.USER)
.id(user.getId())
.build())
.build()));
// assert
Assertions.assertNotNull(response);
}
private void tryDeleteContact() {
try {
client.contacts()
.delete(DeleteContactRequest.builder()
.contactId(user.getId())
.build());
} catch (Exception e) {
throw new RuntimeException("Failed to delete contact.", e);
}
}
}