forked from ringcentral/ringcentral-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SubscriptionTest.java
123 lines (113 loc) · 5.2 KB
/
SubscriptionTest.java
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
115
116
117
118
119
120
121
122
123
package com.ringcentral;
import com.pubnub.api.models.consumer.PNStatus;
import com.pubnub.api.models.consumer.pubsub.PNPresenceEventResult;
import com.ringcentral.definitions.CreateSMSMessage;
import com.ringcentral.definitions.MessageStoreCallerInfoRequest;
import com.ringcentral.definitions.SubscriptionInfo;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import java.io.IOException;
import java.util.function.Consumer;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
public class SubscriptionTest extends BaseTest {
@Test
public void testSubscribe() throws IOException, RestException {
Subscription subscription = restClient.subscription(
new String[]{"/restapi/v1.0/account/~/extension/~/message-store"},
(jsonString) -> {
}
);
subscription.subscribe();
SubscriptionInfo sub = subscription.getSubscription();
assertEquals("Active", sub.status);
}
private void sendSms() throws IOException, RestException {
CreateSMSMessage postParameters = new CreateSMSMessage();
postParameters.from = new MessageStoreCallerInfoRequest().phoneNumber(config.get("username"));
postParameters.to = new MessageStoreCallerInfoRequest[]{new MessageStoreCallerInfoRequest().phoneNumber(config.get("receiver"))};
postParameters.text = "hello world";
restClient.post("/restapi/v1.0/account/~/extension/~/sms", postParameters);
}
@Test
public void testNotification() throws IOException, RestException, InterruptedException {
final Consumer<String> consumer = mock(Consumer.class);
Subscription subscription = restClient.subscription(
new String[]{"/restapi/v1.0/account/~/extension/~/message-store"},
consumer);
subscription.subscribe();
Thread.sleep(3000);
sendSms();
Thread.sleep(16000);
ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
verify(consumer, atLeastOnce()).accept(argument.capture());
assertTrue(argument.getValue().contains("uuid"));
}
@Test
public void testRefresh() throws IOException, RestException, InterruptedException {
final Consumer<String> consumer = mock(Consumer.class);
Subscription subscription = restClient.subscription(
new String[]{"/restapi/v1.0/account/~/extension/~/message-store"},
consumer);
subscription.refresh(); // should not cause any issue when _subscription is null
subscription.subscribe();
Thread.sleep(3000);
subscription.refresh();
Thread.sleep(3000);
sendSms();
Thread.sleep(16000);
ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
verify(consumer, atLeastOnce()).accept(argument.capture());
assertTrue(argument.getValue().contains("uuid"));
}
@Test
public void testRevoke() throws IOException, RestException, InterruptedException {
final Consumer<String> consumer = mock(Consumer.class);
Subscription subscription = restClient.subscription(
new String[]{"/restapi/v1.0/account/~/extension/~/message-store"},
consumer);
subscription.revoke(); // should not cause any issue when _subscription is null
subscription.subscribe();
Thread.sleep(1000);
subscription.revoke();
Thread.sleep(1000);
sendSms();
Thread.sleep(16000);
verify(consumer, never()).accept(any());
}
@Test
public void testStatusCallback() throws IOException, RestException, InterruptedException {
final Consumer<String> consumer1 = mock(Consumer.class);
final Consumer<PNStatus> consumer2 = mock(Consumer.class);
final Consumer<PNPresenceEventResult> consumer3 = mock(Consumer.class);
Subscription subscription = new Subscription(restClient,
new String[]{"/restapi/v1.0/account/~/extension/~/message-store"},
consumer1, consumer2, consumer3);
subscription.subscribe();
Thread.sleep(3000);
sendSms();
Thread.sleep(16000);
ArgumentCaptor<PNStatus> argument = ArgumentCaptor.forClass(PNStatus.class);
verify(consumer2, atLeastOnce()).accept(argument.capture());
assertEquals(argument.getValue().getStatusCode(), 200);
}
@Test
public void testAutoRefresh() throws IOException, RestException, InterruptedException {
final Consumer<String> consumer = mock(Consumer.class);
Subscription subscription = restClient.subscription(
new String[]{"/restapi/v1.0/account/~/extension/~/message-store"},
consumer);
subscription.subscribe();
SubscriptionInfo subInfo = subscription.getSubscription();
subInfo.expiresIn = 123L;
subscription.setSubscription(subInfo);
Thread.sleep(6000);
sendSms();
Thread.sleep(16000);
ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
verify(consumer, atLeastOnce()).accept(argument.capture());
assertTrue(argument.getValue().contains("uuid"));
}
}