Repository navigation
Expand file tree
/
Copy pathDatabaseReferenceTest.java
More file actions
288 lines (258 loc) · 9.85 KB
/
Copy pathDatabaseReferenceTest.java
File metadata and controls
288 lines (258 loc) · 9.85 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.firebase.database;
import static com.google.firebase.database.TestHelpers.mockRepo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.times;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.TestOnlyImplFirebaseTrampolines;
import com.google.firebase.database.DatabaseReference.CompletionListener;
import com.google.firebase.database.Transaction.Handler;
import com.google.firebase.database.Transaction.Result;
import com.google.firebase.database.core.CompoundWrite;
import com.google.firebase.database.core.DatabaseConfig;
import com.google.firebase.database.core.Path;
import com.google.firebase.database.core.Repo;
import com.google.firebase.database.core.ServerValues;
import com.google.firebase.database.snapshot.ChildKey;
import com.google.firebase.database.snapshot.Node;
import com.google.firebase.database.snapshot.NodeUtilities;
import com.google.firebase.testing.ServiceAccount;
import java.io.IOException;
import java.util.Map;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;
public class DatabaseReferenceTest {
private static final String DB_URL = "https://admin-java-sdk.firebaseio.com";
private static final Path path = new Path("foo");
private static FirebaseApp testApp;
private static DatabaseConfig config;
@BeforeClass
public static void setUpClass() throws IOException {
testApp = FirebaseApp.initializeApp(
new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()))
.setDatabaseUrl(DB_URL)
.build());
// Obtain a new DatabaseConfig instance for testing. Since we are not connecting to an
// actual Firebase database, it is necessary to use a stand-in DatabaseConfig here.
config = TestHelpers.newTestConfig(testApp);
}
@AfterClass
public static void tearDownClass() throws InterruptedException {
// Tear down and clean up the test DatabaseConfig.
TestHelpers.interruptConfig(config);
TestOnlyImplFirebaseTrampolines.clearInstancesForTest();
}
@Test
public void testRootReference() {
DatabaseReference reference = new DatabaseReference(DB_URL, config);
assertNull(reference.getKey());
assertNull(reference.getParent());
assertEquals(ImmutableList.of(), reference.getPath().asList());
assertEquals(DB_URL, reference.toString());
assertNotNull(reference.getDatabase());
}
@Test
public void testChild() {
DatabaseReference reference = new DatabaseReference(DB_URL, config);
DatabaseReference child = reference.child("bar");
assertNotNull(child);
assertEquals("bar", child.getKey());
assertEquals(reference, child.getParent());
assertEquals(ImmutableList.of("bar"), child.getPath().asList());
assertEquals(DB_URL + "/bar", child.toString());
assertNotEquals(reference.hashCode(), child.hashCode());
assertSame(reference.getDatabase(), child.getDatabase());
try {
reference.child(null);
fail("No error thrown for null child key");
} catch (NullPointerException expected) {
// expected
}
}
@Test
public void testChildWithSpace() {
DatabaseReference reference = new DatabaseReference(DB_URL, config);
DatabaseReference child = reference.child("bar 1");
assertNotNull(child);
assertEquals("bar 1", child.getKey());
assertEquals(reference, child.getParent());
assertEquals(ImmutableList.of("bar 1"), child.getPath().asList());
assertEquals(DB_URL + "/bar%201", child.toString());
}
@Test
public void testPush() {
DatabaseReference reference = new DatabaseReference(DB_URL, config);
DatabaseReference child = reference.push();
assertNotNull(child);
assertTrue(!Strings.isNullOrEmpty(child.getKey()));
assertEquals(reference, child.getParent());
assertEquals(ImmutableList.of(child.getKey()), child.getPath().asList());
}
@Test
public void testSetValue() throws Exception {
Repo repo = mockRepo();
DatabaseReference reference = new DatabaseReference(repo, path);
reference.setValueAsync("value");
reference.setValue("value", null);
Mockito.verify(repo, times(2))
.scheduleNow(Mockito.any(Runnable.class));
Mockito.verify(repo, times(2))
.setValue(
Mockito.same(path),
Mockito.eq(NodeUtilities.NodeFromJSON("value")),
Mockito.any(CompletionListener.class));
}
@Test
public void testSetValueWithPriority() throws Exception {
Repo repo = mockRepo();
DatabaseReference reference = new DatabaseReference(repo, path);
reference.setValueAsync("value", 10);
reference.setValue("value", 10,null);
Mockito.verify(repo, times(2))
.scheduleNow(Mockito.any(Runnable.class));
Mockito.verify(repo, times(2))
.setValue(
Mockito.same(path),
Mockito.eq(NodeUtilities.NodeFromJSON(
ImmutableMap.of(".value", "value", ".priority", 10))),
Mockito.any(CompletionListener.class));
}
@Test
public void testSetPriority() throws Exception {
Repo repo = mockRepo();
DatabaseReference reference = new DatabaseReference(repo, path);
reference.setPriorityAsync(10);
reference.setPriority(10,null);
Mockito.verify(repo, times(2))
.scheduleNow(Mockito.any(Runnable.class));
Mockito.verify(repo, times(2))
.setValue(
Mockito.eq(path.child(ChildKey.fromString(".priority"))),
Mockito.eq(NodeUtilities.NodeFromJSON(10.0)),
Mockito.any(CompletionListener.class));
}
@Test
public void testUpdateChildren() throws Exception {
Map<String, Object> update = ImmutableMap.<String, Object>of("foo", "bar");
Repo repo = mockRepo();
DatabaseReference reference = new DatabaseReference(repo, path);
reference.updateChildrenAsync(update);
reference.updateChildren(update, null);
Mockito.verify(repo, times(2))
.scheduleNow(Mockito.any(Runnable.class));
Mockito.verify(repo, times(2))
.updateChildren(
Mockito.same(path),
Mockito.any(CompoundWrite.class),
Mockito.any(CompletionListener.class),
Mockito.eq(update));
}
@Test
public void testRemoveValue() throws Exception {
Repo repo = mockRepo();
DatabaseReference reference = new DatabaseReference(repo, path);
reference.removeValueAsync();
reference.removeValue(null);
Mockito.verify(repo, times(2))
.scheduleNow(Mockito.any(Runnable.class));
Mockito.verify(repo, times(2))
.setValue(
Mockito.same(path),
Mockito.eq(NodeUtilities.NodeFromJSON(null)),
Mockito.any(CompletionListener.class));
}
@Test
public void testRunTransaction() throws Exception {
Repo repo = mockRepo();
DatabaseReference reference = new DatabaseReference(repo, path);
try {
reference.runTransaction(null);
fail("No error thrown for null handler");
} catch (NullPointerException expected) {
// expected
}
try {
reference.runTransaction(null, true);
fail("No error thrown for null handler");
} catch (NullPointerException expected) {
// expected
}
Handler handler = new Handler() {
@Override
public Result doTransaction(MutableData currentData) {
return null;
}
@Override
public void onComplete(DatabaseError error, boolean committed, DataSnapshot currentData) {
}
};
reference.runTransaction(handler);
reference.runTransaction(handler, true);
Mockito.verify(repo, times(2))
.scheduleNow(Mockito.any(Runnable.class));
Mockito.verify(repo, times(2))
.startTransaction(
Mockito.same(path),
Mockito.same(handler),
Mockito.anyBoolean());
}
@Test
public void testOnDisconnect() {
DatabaseReference reference = new DatabaseReference(DB_URL, config);
assertNotNull(reference.onDisconnect());
}
@Test
public void testSetHijackHash() throws Exception {
Repo repo = mockRepo();
DatabaseReference reference = new DatabaseReference(repo, path);
reference.setHijackHash(true);
Mockito.verify(repo, times(1))
.scheduleNow(Mockito.any(Runnable.class));
Mockito.verify(repo, times(1))
.setHijackHash(true);
}
@Test
public void testServerValue() {
assertNotNull(ServerValue.TIMESTAMP);
assertTrue(ServerValue.TIMESTAMP.containsKey(ServerValues.NAME_SUBKEY_SERVERVALUE));
}
@Test
public void testTransactionResult() {
Result result = Transaction.abort();
assertFalse(result.isSuccess());
assertNull(result.getNode());
Node node = NodeUtilities.NodeFromJSON("node");
result = Transaction.success(new MutableData(node));
assertTrue(result.isSuccess());
assertSame(node, result.getNode());
}
}