forked from ringcentral/pubnub-jtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFourElementsResponseTest.java
More file actions
159 lines (117 loc) · 5.72 KB
/
Copy pathFourElementsResponseTest.java
File metadata and controls
159 lines (117 loc) · 5.72 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.pubnub.api;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import java.util.Hashtable;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.times;
@RunWith(PowerMockRunner.class)
@PrepareForTest(Pubnub.class)
public class FourElementsResponseTest {
Subscriptions channelSubscriptions;
Subscriptions channelGroupSubscriptions;
Callback channelCallback;
Callback channelCallback2;
Callback channelGroupCallback;
String third; // cg position
String fourth; // channel position
String channel;
String timetoken;
Object message;
HttpRequest hreq;
Pubnub pubnub;
@Before
public void setUp() throws InterruptedException {
channelSubscriptions = new Subscriptions();
channelGroupSubscriptions = new Subscriptions();
channelCallback = new Callback() {
};
channelCallback2 = new Callback() {
};
channelGroupCallback = new Callback() {
};
timetoken = "123";
hreq = new HttpRequest(new String[] { "a", "b", "c" }, new Hashtable(), new ResponseHandler() {
@Override
public void handleResponse(HttpRequest hreq, String response) {
}
@Override
public void handleError(HttpRequest hreq, PubnubError error) {
}
});
pubnub = PowerMockito.spy(new Pubnub("demo", "demo"));
Whitebox.setInternalState(pubnub, "channelSubscriptions", channelSubscriptions);
Whitebox.setInternalState(pubnub, "channelGroupSubscriptions", channelGroupSubscriptions);
}
@Test
public void testChannelMessage() throws Exception {
third = "foo";
fourth = "foo";
message = "hello";
SubscriptionItem thirdSubscriptionItem = new SubscriptionItem(third, channelCallback);
channelSubscriptions.addItem(thirdSubscriptionItem);
Whitebox.invokeMethod(pubnub, "handleFourElementsSubscribeResponse", third, fourth, message, timetoken, hreq);
PowerMockito.verifyPrivate(pubnub, times(1)).invoke("invokeSubscribeCallback", eq(fourth), eq(channelCallback),
eq(message), Mockito.anyString(), eq(hreq));
}
@Test
public void testChannelGroupMessage() throws Exception {
third = "bar";
fourth = "foo";
message = "hello";
SubscriptionItem fourthSubscriptionItem = new SubscriptionItem(fourth, channelCallback);
SubscriptionItem channelGroupSubscriptionItem = new SubscriptionItem(third, channelGroupCallback);
channelSubscriptions.addItem(fourthSubscriptionItem);
channelGroupSubscriptions.addItem(channelGroupSubscriptionItem);
Whitebox.invokeMethod(pubnub, "handleFourElementsSubscribeResponse", third, fourth, message, timetoken, hreq);
PowerMockito.verifyPrivate(pubnub, times(1)).invoke("invokeSubscribeCallback", eq(fourth),
eq(channelGroupCallback), eq(message), Mockito.anyString(), eq(hreq));
}
@Test
public void testWildcardMessage() throws Exception {
String wildcardChannel = "foo.*";
String channel = "foo.bar";
message = "hello";
SubscriptionItem thirdSubscriptionItem = new SubscriptionItem(wildcardChannel, channelCallback);
channelSubscriptions.addItem(thirdSubscriptionItem);
Whitebox.invokeMethod(pubnub, "handleFourElementsSubscribeResponse", wildcardChannel, channel, message,
timetoken, hreq);
PowerMockito.verifyPrivate(pubnub, times(1)).invoke("invokeSubscribeCallback", eq(channel),
eq(channelCallback), eq(message), Mockito.anyString(), eq(hreq));
}
@Test
public void testWildcardPresenceSameLevelWithSubscribe() throws Exception {
String presenceChannel = "foo.*-pnpres";
String messagesChannel = "foo.*";
String channel = "foo.*-pnpres";
message = "hello";
SubscriptionItem presenceSubscriptionItem = new SubscriptionItem(presenceChannel, channelCallback);
SubscriptionItem messagesSubscriptionItem = new SubscriptionItem(messagesChannel, channelCallback2);
channelSubscriptions.addItem(presenceSubscriptionItem);
channelSubscriptions.addItem(messagesSubscriptionItem);
Whitebox.invokeMethod(pubnub, "handleFourElementsSubscribeResponse", messagesChannel, channel, message,
timetoken, hreq);
PowerMockito.verifyPrivate(pubnub, times(1)).invoke("invokeSubscribeCallback", eq(channel),
eq(channelCallback), eq(message), Mockito.anyString(), eq(hreq));
}
@Test
public void testWildcardPresenceDifferentLevelWithSubscribe() throws Exception {
String presenceChannel = "foo.*-pnpres";
String messagesChannel = "foo.*";
String channel = "foo.bar.baz.*-pnpres";
message = "hello";
SubscriptionItem presenceSubscriptionItem = new SubscriptionItem(presenceChannel, channelCallback);
SubscriptionItem messagesSubscriptionItem = new SubscriptionItem(messagesChannel, channelCallback2);
channelSubscriptions.addItem(presenceSubscriptionItem);
channelSubscriptions.addItem(messagesSubscriptionItem);
Whitebox.invokeMethod(pubnub, "handleFourElementsSubscribeResponse", messagesChannel, channel, message,
timetoken, hreq);
PowerMockito.verifyPrivate(pubnub, times(1)).invoke("invokeSubscribeCallback", eq(channel),
eq(channelCallback2), eq(message), Mockito.anyString(), eq(hreq));
}
}