Repository navigation
Expand file tree
/
Copy pathDataSnapshotTest.java
More file actions
200 lines (165 loc) · 6.38 KB
/
Copy pathDataSnapshotTest.java
File metadata and controls
200 lines (165 loc) · 6.38 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
/*
* 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 org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import com.google.auth.oauth2.GoogleCredentials;
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.core.DatabaseConfig;
import com.google.firebase.database.snapshot.IndexedNode;
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.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class DataSnapshotTest {
private static FirebaseApp testApp;
private static DatabaseConfig config;
@BeforeClass
public static void setUpClass() throws IOException {
testApp = FirebaseApp.initializeApp(
FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()))
.setDatabaseUrl("https://admin-java-sdk.firebaseio.com")
.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();
}
private DataSnapshot snapFor(Object data) {
Node node = NodeUtilities.NodeFromJSON(data);
DatabaseReference ref = new DatabaseReference("https://test.firebaseio.com", config);
return new DataSnapshot(ref, IndexedNode.from(node));
}
@Test
public void testBasicIteration() {
DataSnapshot snap1 = snapFor(null);
assertFalse(snap1.hasChildren());
assertFalse(snap1.getChildren().iterator().hasNext());
DataSnapshot snap2 = snapFor(1L);
assertFalse(snap2.hasChildren());
assertFalse(snap2.getChildren().iterator().hasNext());
DataSnapshot snap3 = snapFor(MapBuilder.of("a", 1L, "b", 2L));
assertTrue(snap3.hasChildren());
Iterator<DataSnapshot> iter = snap3.getChildren().iterator();
assertTrue(iter.hasNext());
String[] children = new String[] {null, null};
int i = 0;
for (DataSnapshot child : snap3.getChildren()) {
children[i] = child.getKey();
i++;
}
assertArrayEquals(children, new String[] {"a", "b"});
}
@Test
public void testExists() {
DataSnapshot snap;
snap = snapFor(new HashMap<>());
assertFalse(snap.exists());
snap = snapFor(MapBuilder.of(".priority", 1));
assertFalse(snap.exists());
snap = snapFor(null);
assertFalse(snap.exists());
snap = snapFor(true);
assertTrue(snap.exists());
snap = snapFor(5);
assertTrue(snap.exists());
snap = snapFor(MapBuilder.of("x", 5));
assertTrue(snap.exists());
}
@Test
public void testChild() {
DataSnapshot snapshot = snapFor(ImmutableMap.of("foo", "bar"));
assertTrue(snapshot.hasChild("foo"));
assertFalse(snapshot.hasChild("bar"));
assertEquals(1, snapshot.getChildrenCount());
DataSnapshot child = snapshot.child("foo");
assertNotNull(child);
assertEquals("foo", child.getKey());
assertTrue(child.exists());
child = snapshot.child("bar");
assertNotNull(child);
assertEquals("bar", child.getKey());
assertFalse(child.exists());
Iterator<DataSnapshot> iter = snapshot.getChildren().iterator();
int count = 0;
while (iter.hasNext()) {
DataSnapshot next = iter.next();
count++;
assertNotNull(next);
try {
iter.remove();
fail("No error thrown for remove() operation");
} catch (UnsupportedOperationException expected) {
// expected
}
}
assertEquals(1, count);
}
@Test
public void testGetValue() {
DataSnapshot snapshot = snapFor("foo");
assertEquals("foo", snapshot.getValue(String.class));
snapshot = snapFor(9);
assertEquals(9, (int) snapshot.getValue(Integer.class));
snapshot = snapFor(9.9);
assertEquals(9.9, snapshot.getValue(Double.class), 0.0001);
snapshot = snapFor(true);
assertTrue(snapshot.getValue(Boolean.class));
ImmutableMap<String, String> map = ImmutableMap.of("foo", "bar");
snapshot = snapFor(map);
GenericTypeIndicator<Map<String, String>> generic =
new GenericTypeIndicator<Map<String, String>>() {};
assertEquals(map, snapshot.getValue(generic));
}
@Test
public void testGetPriority() {
DataSnapshot snapshot = snapFor(ImmutableMap.of(".value", "foo", ".priority", 10L));
assertEquals(10.0, snapshot.getPriority());
snapshot = snapFor(ImmutableMap.of(".value", "foo", ".priority", "p"));
assertEquals("p", snapshot.getPriority());
snapshot = snapFor("foo");
assertNull(snapshot.getPriority());
}
@Test
public void testToString() {
DataSnapshot snapshot = snapFor(ImmutableMap.of("foo", "bar"));
assertEquals("DataSnapshot { key = null, value = {foo=bar} }", snapshot.toString());
DatabaseReference reference = new DatabaseReference("https://test.firebaseio.com/test", config);
Node node = NodeUtilities.NodeFromJSON("foo");
snapshot = new DataSnapshot(reference, IndexedNode.from(node));
assertEquals("DataSnapshot { key = test, value = foo }", snapshot.toString());
}
}