Skip to content

Commit ab178b0

Browse files
author
Devendra
committed
adding presence tests
1 parent c91e3d2 commit ab178b0

3 files changed

Lines changed: 324 additions & 2 deletions

File tree

‎java/build.xml‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<pathelement location="bin"/>
1818
<pathelement location="libs/bcprov-jdk15on-1.47.jar"/>
1919
<pathelement location="libs/json-20090211.jar"/>
20+
<pathelement location="libs/json-simple-1.1.1.jar"/>
2021
<pathelement location="libs/slf4j-api-1.7.5.jar"/>
2122
<pathelement location="libs/junit-4.11.jar"/>
2223
<pathelement location="jars/Pubnub-UnitTests-${VERSION}.jar"/>
@@ -81,7 +82,7 @@
8182
<antcall target="unsetDebugTrue" />
8283
</target>
8384

84-
<target depends="create-jar, create-test-jar, create-examples-jar, create-demo-console-jar, clean_bin, doc" name="build-jar"/>
85+
<target depends="create-jar, create-test-jar, create-examples-jar, create-demo-console-jar, create-presence-test-jar, clean_bin, doc" name="build-jar"/>
8586
<target depends="build-project" name="create-jar">
8687
<jar destfile="${jar_name}"
8788
basedir="bin"
@@ -147,7 +148,7 @@
147148
<attribute name="Company" value="Pubnub" />
148149
<attribute name="Project" value="pubnub-api" />
149150
<attribute name="Java-Version" value="${java.version}" />
150-
<attribute name="Rsrc-Class-Path" value="./ bcprov-jdk15on-1.47.jar json-20090211.jar slf4j-api-1.7.5.jar slf4j-simple-1.7.5.jar" />
151+
<attribute name="Rsrc-Class-Path" value="./ bcprov-jdk15on-1.47.jar json-simple-1.1.1.jar json-20090211.jar slf4j-api-1.7.5.jar slf4j-simple-1.7.5.jar" />
151152
<attribute name="Class-Path" value="." />
152153
<attribute name="Rsrc-Main-Class" value="@{jar.classname}" />
153154
<attribute name="Main-Class" value="org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader" />
@@ -196,6 +197,9 @@
196197
<target name="create-demo-console-jar">
197198
<create-jar jar.type="examples" jar.classname="com.pubnub.examples.PubnubDemoConsole" jar.filename="PubnubDemoConsole.jar" />
198199
</target>
200+
<target name="create-presence-test-jar">
201+
<create-jar jar.type="examples" jar.classname="com.pubnub.examples.PubnubPresenceTest" jar.filename="PubnubPresenceTest.jar" />
202+
</target>
199203
<target name="doc" depends="init" description="generate documentation">
200204
<javadoc access="public" sourcepath="srcPubnubApi:src:srcLogging:src1" destdir="doc" classpathref="PubnubApi.classpath"/>
201205
</target>
Lines changed: 318 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,318 @@
1+
package com.pubnub.examples;
2+
3+
import java.io.File;
4+
import java.io.FileNotFoundException;
5+
import java.io.FileReader;
6+
import java.io.IOException;
7+
import java.util.ArrayList;
8+
import java.util.Calendar;
9+
import java.util.Date;
10+
import java.util.HashMap;
11+
import java.util.Hashtable;
12+
import java.util.LinkedHashMap;
13+
import java.util.LinkedHashSet;
14+
import java.util.List;
15+
import java.util.Map;
16+
import java.util.Set;
17+
18+
import org.json.simple.JSONArray;
19+
import org.json.simple.JSONObject;
20+
import org.json.simple.parser.JSONParser;
21+
import org.json.simple.parser.ParseException;
22+
23+
24+
import com.pubnub.api.Callback;
25+
import com.pubnub.api.Pubnub;
26+
import com.pubnub.api.PubnubException;
27+
28+
enum Status {
29+
PASS,
30+
FAIL
31+
};
32+
33+
class Test {
34+
private String subkey;
35+
private String origin;
36+
private Map<String, String> channels;
37+
private String channelA;
38+
private String channelB;
39+
private JSONObject steps;
40+
private JSONArray events;
41+
private Status result;
42+
private int id;
43+
private String description;
44+
private boolean ssl;
45+
private int currentStep = -1;
46+
47+
Test(int id, String description, String subkey, String origin, String channelA, String channelB, boolean ssl, JSONObject steps) {
48+
this.id = id;
49+
this.description = description;
50+
this.subkey = subkey;
51+
this.origin = origin;
52+
this.channels = new LinkedHashMap<String,String>();
53+
channels.put("channelA", channelA);
54+
channels.put("channelB", channelB);
55+
this.ssl = ssl;
56+
this.result = Status.FAIL;
57+
this.steps = steps;
58+
}
59+
60+
void updateStatus() {
61+
for (int i = 0; i < steps.size(); i++) {
62+
63+
JSONObject step = (JSONObject) steps.get(String.valueOf(i));
64+
JSONArray expectedEvents = (JSONArray) step.get("listener");
65+
JSONArray observedEvents = (JSONArray) step.get("events");
66+
67+
if (expectedEvents.size() != observedEvents.size()) {
68+
result = Status.FAIL;
69+
return;
70+
}
71+
72+
if (expectedEvents.size() <= 2) {
73+
for (int j = 0; j < expectedEvents.size(); j++) {
74+
String expectedEventChannel = channels.get((String)((JSONArray)expectedEvents.get(j)).get(1));
75+
String expectedEventAction = (String)((JSONArray)expectedEvents.get(j)).get(0);
76+
77+
String observedEventChannel = (String)((JSONObject)observedEvents.get(j)).get("channel");
78+
String observedEventAction = (String)((JSONObject)observedEvents.get(j)).get("action");
79+
80+
if ( !expectedEventChannel.equals(observedEventChannel) ||
81+
!expectedEventAction.equals(observedEventAction)) {
82+
result = Status.FAIL;
83+
return;
84+
}
85+
}
86+
} else {
87+
Set expected = new LinkedHashSet();
88+
Set observed = new LinkedHashSet();
89+
for (int j = 0; j < expectedEvents.size(); j++) {
90+
String expectedEventChannel = channels.get((String)((JSONArray)expectedEvents.get(j)).get(1));
91+
String expectedEventAction = (String)((JSONArray)expectedEvents.get(j)).get(0);
92+
93+
String observedEventChannel = (String)((JSONObject)observedEvents.get(j)).get("channel");
94+
String observedEventAction = (String)((JSONObject)observedEvents.get(j)).get("action");
95+
96+
expected.add(expectedEventChannel + "," + expectedEventAction);
97+
observed.add(observedEventChannel + "," + observedEventAction);
98+
}
99+
if (!expected.equals(observed)) {
100+
result = Status.FAIL;
101+
return;
102+
}
103+
104+
}
105+
result = Status.PASS;
106+
}
107+
}
108+
void printResult () {
109+
updateStatus();
110+
System.out.println();
111+
System.out.println("-----------------");
112+
System.out.println("Test Id : " + id + ", " + description);
113+
System.out.println("Status : " + result);
114+
System.out.println("Origin : " + origin);
115+
System.out.println("SSL : " + ssl);
116+
System.out.println("Sub Key : " + subkey);
117+
System.out.println();
118+
119+
for (int i = 0 ; i < steps.size() ; i++) {
120+
JSONObject jso = (JSONObject) steps.get(String.valueOf(i));
121+
jso.toJSONString();
122+
System.out.println(jso.toJSONString());
123+
}
124+
System.out.println("-----------------");
125+
System.out.println();
126+
}
127+
128+
void run() {
129+
130+
Pubnub listener = new Pubnub("", subkey);
131+
Pubnub actor = new Pubnub("",subkey, ssl);
132+
listener.setCacheBusting(false);
133+
actor.setCacheBusting(false);
134+
listener.setOrigin(origin);
135+
actor.setOrigin(origin);
136+
137+
138+
try {
139+
listener.presence(channels.get("channelA"), new Callback(){
140+
141+
@Override
142+
public void successCallback(String channel, Object message) {
143+
System.out.println(channel + " : " + message);
144+
JSONObject jso = null;
145+
try {
146+
jso = (JSONObject)new JSONParser().parse(message.toString());
147+
} catch (ParseException e) {
148+
// TODO Auto-generated catch block
149+
e.printStackTrace();
150+
}
151+
jso.put("channel", channel.split("-pnpres")[0]);
152+
JSONObject step = (JSONObject)steps.get(String.valueOf(currentStep));
153+
JSONArray events = (JSONArray) step.get("events");
154+
if (events == null) {
155+
step.put("events", new JSONArray());
156+
events = (JSONArray) step.get("events");
157+
}
158+
events.add(jso);
159+
}
160+
161+
});
162+
} catch (PubnubException e1) {
163+
// TODO Auto-generated catch block
164+
e1.printStackTrace();
165+
}
166+
try {
167+
listener.presence(channels.get("channelB"), new Callback(){
168+
169+
@Override
170+
public void successCallback(String channel, Object message) {
171+
System.out.println(channel + " : " + message);
172+
JSONObject jso = null;
173+
try {
174+
jso = (JSONObject)new JSONParser().parse(message.toString());
175+
} catch (ParseException e) {
176+
// TODO Auto-generated catch block
177+
e.printStackTrace();
178+
}
179+
jso.put("channel", channel.split("-pnpres")[0]);
180+
181+
JSONObject step = (JSONObject)steps.get(String.valueOf(currentStep));
182+
JSONArray events = (JSONArray) step.get("events");
183+
if (events == null) {
184+
step.put("events", new JSONArray());
185+
events = (JSONArray) step.get("events");
186+
}
187+
events.add(jso);
188+
189+
}
190+
191+
});
192+
} catch (PubnubException e1) {
193+
// TODO Auto-generated catch block
194+
e1.printStackTrace();
195+
}
196+
197+
try {
198+
Thread.sleep(3000);
199+
} catch (InterruptedException e1) {
200+
// TODO Auto-generated catch block
201+
e1.printStackTrace();
202+
}
203+
204+
205+
for (int i = 0 ; i < steps.size(); i++) {
206+
currentStep++;
207+
JSONObject step = (JSONObject) steps.get(String.valueOf(i));
208+
JSONArray actorStep = (JSONArray) step.get("actor");
209+
210+
if (actorStep.get(0).equals("subscribe")) {
211+
System.out.println("SUBSCRIBE : " + channels.get(actorStep.get(1)));
212+
try {
213+
actor.subscribe(channels.get(actorStep.get(1)), new Callback(){
214+
215+
@Override
216+
public void successCallback(String channel,
217+
Object message) {
218+
219+
}
220+
221+
});
222+
} catch (PubnubException e) {
223+
// TODO Auto-generated catch block
224+
e.printStackTrace();
225+
}
226+
} else {
227+
System.out.println("UNSUBSCRIBE : " + channels.get(actorStep.get(1)));
228+
actor.unsubscribe(channels.get(actorStep.get(1)));
229+
230+
}
231+
JSONArray listenerStep = (JSONArray)step.get("listener");
232+
233+
Long stepDelay = 0L;
234+
235+
for (int k = 0; k < listenerStep.size(); k++) {
236+
Long delay = (Long) ((JSONArray)listenerStep.get(k)).get(2);
237+
if (delay > stepDelay) stepDelay = delay;
238+
}
239+
240+
System.out.println("[" + Calendar.getInstance().getTime() + "] : wait for " + (stepDelay/1000.00) + " seconds");
241+
try {
242+
Thread.sleep(stepDelay);
243+
} catch (InterruptedException e) {
244+
// TODO Auto-generated catch block
245+
e.printStackTrace();
246+
}
247+
248+
249+
}
250+
251+
}
252+
253+
254+
}
255+
256+
257+
public class PubnubPresenceTest {
258+
static List<Test> testsList = new ArrayList<Test>();
259+
static String client = "3.6";
260+
261+
public static void main(String[] args) {
262+
263+
JSONParser parser = new JSONParser();
264+
265+
266+
267+
try {
268+
269+
Object obj = parser.parse(new FileReader("./tests.json"));
270+
271+
JSONArray tests = (JSONArray) obj;
272+
obj = parser.parse(new FileReader("./keysets.json"));
273+
JSONObject keysets = (JSONObject) obj;
274+
for (int i = 0 ; i < tests.size();i++) {
275+
JSONObject jso = (JSONObject) tests.get(i);
276+
JSONObject common = (JSONObject) jso.get("common");
277+
String description = (String) common.get("description");
278+
JSONObject steps = (JSONObject) jso.get("steps");
279+
boolean ssl = (Boolean) common.get("ssl");
280+
String server = (String) common.get("server");
281+
String clientStr = (String) common.get("client");
282+
if (!client.equals(clientStr)) {
283+
System.out.println("Skipping Test Due to Client Mismatch : " + clientStr);
284+
continue;
285+
}
286+
String origin = (server.equals("3.6"))?"presence-beta":"pubsub";
287+
String subkey = (String) ((JSONObject)(keysets.get(common.get("keyset")))).get("subKey") ;
288+
long time = new Date().getTime();
289+
String channelA = "A-java-" + i + 1 + "-" + time;
290+
String channelB = "B-java-" + i + 1 + "-"+ time;
291+
292+
293+
Test t = new Test(i + 1, description, subkey, origin, channelA, channelB, ssl, steps);
294+
testsList.add(t);
295+
}
296+
297+
298+
} catch (FileNotFoundException e) {
299+
e.printStackTrace();
300+
} catch (IOException e) {
301+
e.printStackTrace();
302+
} catch (ParseException e) {
303+
e.printStackTrace();
304+
}
305+
306+
System.out.println("Total Number of Tests : " + testsList.size());
307+
for (int i = 0; i < testsList.size(); i++) {
308+
System.out.println("Running Test : " + (i + 1));
309+
testsList.get(i).run();
310+
testsList.get(i).printResult();
311+
}
312+
System.out.println("++++++++ RESULTS ++++++++");
313+
for (int i = 0; i < testsList.size(); i++) {
314+
testsList.get(i).printResult();
315+
}
316+
}
317+
318+
}

‎java/libs/json-simple-1.1.1.jar‎

23.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)