Repository navigation
Expand file tree
/
Copy pathReplaceTest.java
More file actions
128 lines (92 loc) · 3.84 KB
/
Copy pathReplaceTest.java
File metadata and controls
128 lines (92 loc) · 3.84 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
package com.pubnub.api.datasync;
import com.pubnub.api.Pubnub;
import com.pubnub.api.PubnubException;
import com.pubnub.api.SyncedObject;
import com.pubnub.api.TestHelper;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import java.util.Hashtable;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.*;
public class ReplaceTest {
Pubnub pubnub;
String playerString;
@Before
public void setUp() throws InterruptedException {
pubnub = new Pubnub("demo-36", "demo-36");
pubnub.setCacheBusting(false);
playerString = "player-" + TestHelper.random();
}
@Test
public void testReplaceResponse() throws InterruptedException, JSONException {
final CountDownLatch latch = new CountDownLatch(1);
TestHelper.SimpleCallback cb = new TestHelper.SimpleCallback(latch);
Hashtable args = new Hashtable();
args.put("location", "player.settings.volume");
args.put("data", 70);
pubnub.replace(args, cb);
latch.await(5, TimeUnit.SECONDS);
JSONObject response = (JSONObject) cb.getResponse();
assertEquals("settings.volume", response.getString("location"));
assertEquals("replace", response.getString("op"));
assertEquals(0, latch.getCount());
}
@Test
public void testReplaceObjectResponse() throws InterruptedException, JSONException {
final CountDownLatch latch = new CountDownLatch(1);
TestHelper.SimpleCallback cb = new TestHelper.SimpleCallback(latch);
JSONObject settings = new JSONObject();
settings.put("volume", 70);
settings.put("mute", false);
settings.put("locale", "en");
Hashtable args = new Hashtable();
args.put("location", "player.settings");
args.put("data", settings);
pubnub.replace(args, cb);
latch.await(5, TimeUnit.SECONDS);
JSONObject response = (JSONObject) cb.getResponse();
assertEquals("settings", response.getString("location"));
assertEquals("replace", response.getString("op"));
assertEquals(0, latch.getCount());
}
@Test
public void testReplaceObjectCallback() throws InterruptedException, JSONException, PubnubException {
DataSyncTestHelper.setupSettingsOn(playerString, pubnub);
final CountDownLatch latch1 = new CountDownLatch(2);
TestHelper.SimpleDataSyncCallback cb1 = new TestHelper.SimpleDataSyncCallback() {
@Override
public void readyCallback(SyncedObject syncedObject) {
JSONObject settings = new JSONObject();
try {
settings.put("volume", 70);
settings.put("mute", false);
} catch (JSONException e) {
fail(e.getMessage());
}
syncedObject.replace("settings", settings);
latch1.countDown();
}
@Override
public void replaceCallback(List updates, String path) {
result = updates;
latch1.countDown();
}
};
SyncedObject player = pubnub.sync(playerString, cb1);
latch1.await(30, TimeUnit.SECONDS);
List updates = (List) cb1.getResult();
assertEquals(3, updates.size());
assertEquals(70, player.getInteger("settings.volume"));
assertEquals("", player.optString("settings.locale"));
assertFalse(player.getBoolean("settings.mute"));
SyncedObject settings = player.child("settings");
assertEquals(70, settings.getInteger("volume"));
assertEquals("", settings.optString("locale"));
assertFalse(settings.getBoolean("mute"));
assertEquals(0, latch1.getCount());
}
}