Repository navigation
Expand file tree
/
Copy pathFirebaseDatabaseTest.java
More file actions
298 lines (267 loc) · 10.8 KB
/
Copy pathFirebaseDatabaseTest.java
File metadata and controls
298 lines (267 loc) · 10.8 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
289
290
291
292
293
294
295
296
297
298
/*
* 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.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.TestOnlyImplFirebaseTrampolines;
import com.google.firebase.database.util.EmulatorHelper;
import com.google.firebase.internal.FirebaseProcessEnvironment;
import com.google.firebase.testing.ServiceAccount;
import com.google.firebase.testing.TestUtils;
import java.io.IOException;
import java.util.List;
import org.junit.Test;
public class FirebaseDatabaseTest {
private static final FirebaseOptions firebaseOptions =
FirebaseOptions.builder()
.setCredentials(TestUtils.getCertCredential(ServiceAccount.EDITOR.asStream()))
.setDatabaseUrl("https://firebase-db-test.firebaseio.com")
.build();
private static final FirebaseOptions firebaseOptionsWithoutDatabaseUrl =
FirebaseOptions.builder()
.setCredentials(TestUtils.getCertCredential(ServiceAccount.EDITOR.asStream()))
.build();
@Test
public void testGetInstance() {
FirebaseApp.initializeApp(firebaseOptions);
try {
FirebaseDatabase defaultDatabase = FirebaseDatabase.getInstance();
assertNotNull(defaultDatabase);
assertSame(defaultDatabase, FirebaseDatabase.getInstance());
assertSame(FirebaseApp.getInstance(), defaultDatabase.getApp());
} finally {
TestOnlyImplFirebaseTrampolines.clearInstancesForTest();
}
}
@Test
public void testGetInstanceForUrl() {
FirebaseApp.initializeApp(firebaseOptions);
try {
String url = "https://firebase-db-test2.firebaseio.com";
FirebaseDatabase otherDatabase = FirebaseDatabase.getInstance(url);
assertNotNull(otherDatabase);
assertNotSame(otherDatabase, FirebaseDatabase.getInstance());
} finally {
TestOnlyImplFirebaseTrampolines.clearInstancesForTest();
}
}
@Test
public void testInvalidUrl() {
FirebaseApp.initializeApp(firebaseOptions);
try {
String[] urls = new String[]{
null, "", "https://firebase-db-test.firebaseio.com/foo"
};
for (String url : urls) {
try {
FirebaseDatabase.getInstance(url);
fail("No error thrown for URL: " + url);
} catch (DatabaseException expected) {
// expected
}
}
} finally {
TestOnlyImplFirebaseTrampolines.clearInstancesForTest();
}
}
@Test
public void testGetInstanceForApp() {
FirebaseApp app = FirebaseApp.initializeApp(firebaseOptions, "testGetInstanceForApp");
try {
FirebaseDatabase db = FirebaseDatabase.getInstance(app);
assertNotNull(db);
assertSame(db, FirebaseDatabase.getInstance(app));
} finally {
TestOnlyImplFirebaseTrampolines.clearInstancesForTest();
}
}
@Test
public void testReference() {
FirebaseApp.initializeApp(firebaseOptions);
try {
FirebaseDatabase defaultDatabase = FirebaseDatabase.getInstance();
DatabaseReference reference = defaultDatabase.getReference();
assertNotNull(reference);
assertNull(reference.getKey());
assertNull(reference.getParent());
reference = defaultDatabase.getReference("foo");
assertNotNull(reference);
assertEquals("foo", reference.getKey());
assertNull(reference.getParent().getKey());
reference = defaultDatabase.getReference("foo/bar");
assertNotNull(reference);
assertEquals("bar", reference.getKey());
assertEquals("foo", reference.getParent().getKey());
} finally {
TestOnlyImplFirebaseTrampolines.clearInstancesForTest();
}
}
@Test
public void testReferenceFromUrl() {
FirebaseApp.initializeApp(firebaseOptions);
try {
FirebaseDatabase defaultDatabase = FirebaseDatabase.getInstance();
DatabaseReference reference = defaultDatabase.getReferenceFromUrl(
"https://firebase-db-test.firebaseio.com/foo/bar");
assertNotNull(reference);
assertEquals("bar", reference.getKey());
assertEquals("foo", reference.getParent().getKey());
try {
defaultDatabase.getReferenceFromUrl(null);
fail("No error thrown for null URL");
} catch (NullPointerException expected) {
// expected
}
try {
defaultDatabase.getReferenceFromUrl("https://other-db-test.firebaseio.com/foo/bar");
fail("No error thrown for invalid URL");
} catch (DatabaseException expected) {
// expected
}
} finally {
TestOnlyImplFirebaseTrampolines.clearInstancesForTest();
}
}
@Test
public void testAppDelete() {
FirebaseApp app = FirebaseApp.initializeApp(firebaseOptions, "testAppDelete");
FirebaseDatabase db = FirebaseDatabase.getInstance(app);
assertNotNull(db);
app.delete();
try {
db.getReference();
fail("No error thrown when calling method on database after delete");
} catch (IllegalStateException expected) {
// ignore
}
try {
FirebaseDatabase.getInstance(app);
fail("No error thrown when getting db instance after deleting app");
} catch (IllegalStateException expected) {
// ignore
}
}
@Test
public void testInitAfterAppDelete() {
try {
FirebaseApp app = FirebaseApp.initializeApp(firebaseOptions, "testInitAfterAppDelete");
FirebaseDatabase db1 = FirebaseDatabase.getInstance(app);
assertNotNull(db1);
app.delete();
app = FirebaseApp.initializeApp(firebaseOptions, "testInitAfterAppDelete");
FirebaseDatabase db2 = FirebaseDatabase.getInstance(app);
assertNotNull(db2);
assertNotSame(db1, db2);
} finally {
TestOnlyImplFirebaseTrampolines.clearInstancesForTest();
}
}
@Test
public void testDbUrlIsEmulatorUrlWhenSettingOptionsManually() throws IOException {
List<CustomTestCase> testCases = ImmutableList.of(
// cases where the env var is ignored because the supplied DB URL is a valid emulator URL
new CustomTestCase("http://my-custom-hosted-emulator.com:80?ns=dummy-ns", "",
"http://my-custom-hosted-emulator.com:80", "dummy-ns"),
new CustomTestCase("http://localhost:9000?ns=test-ns", null,
"http://localhost:9000", "test-ns"),
// cases where the supplied DB URL is not an emulator URL, so we extract ns from it
// and append it to the emulator URL from env var(if it is valid)
new CustomTestCase("https://valid-namespace.firebaseio.com", "localhost:8080",
"http://localhost:8080", "valid-namespace"),
new CustomTestCase("https://test.firebaseio.com?ns=valid-namespace", "localhost:90",
"http://localhost:90", "valid-namespace")
);
for (CustomTestCase tc : testCases) {
try {
FirebaseApp app = FirebaseApp.initializeApp(firebaseOptionsWithoutDatabaseUrl);
FirebaseProcessEnvironment.setenv(
EmulatorHelper.FIREBASE_RTDB_EMULATOR_HOST_ENV_VAR,
Strings.nullToEmpty(tc.envVariableUrl));
FirebaseDatabase instance = FirebaseDatabase.getInstance(app, tc.rootDbUrl);
assertEquals(tc.expectedEmulatorRootUrl,
instance.getReference().repo.getRepoInfo().toString());
assertEquals(tc.namespace, instance.getReference().repo.getRepoInfo().namespace);
// clean up after
app.delete();
} finally {
FirebaseProcessEnvironment.clearCache();
}
}
}
@Test
public void testDbUrlIsEmulatorUrlForDbRefWithPath() throws IOException {
List<CustomTestCase> testCases = ImmutableList.of(
new CustomTestCase("http://my-custom-hosted-emulator.com:80?ns=dummy-ns",
"http://my-custom-hosted-emulator.com:80?ns=dummy-ns", "",
"http://my-custom-hosted-emulator.com:80", "dummy-ns", "/"),
new CustomTestCase("http://localhost:9000?ns=test-ns",
"http://localhost:9000/a/b/c/d?ns=test-ns", null,
"http://localhost:9000", "test-ns", "/a/b/c/d"),
new CustomTestCase("http://localhost:9000?ns=test-ns",
"https://valid-namespace.firebaseio.com/a/b/c/d?ns=test-ns", "localhost:9000",
"http://localhost:9000", "test-ns", "/a/b/c/d"),
new CustomTestCase("https://valid-namespace.firebaseio.com",
"http://valid-namespace.firebaseio.com/a/b/c/d", "localhost:8080",
"http://localhost:8080", "valid-namespace", "/a/b/c/d")
);
for (CustomTestCase tc : testCases) {
try {
FirebaseApp app = FirebaseApp.initializeApp(firebaseOptionsWithoutDatabaseUrl);
FirebaseProcessEnvironment.setenv(
EmulatorHelper.FIREBASE_RTDB_EMULATOR_HOST_ENV_VAR,
Strings.nullToEmpty(tc.envVariableUrl));
FirebaseDatabase instance = FirebaseDatabase.getInstance(app, tc.rootDbUrl);
DatabaseReference dbRef = instance.getReferenceFromUrl(tc.pathUrl);
assertEquals(tc.expectedEmulatorRootUrl, dbRef.repo.getRepoInfo().toString());
assertEquals(tc.namespace, dbRef.repo.getRepoInfo().namespace);
assertEquals(tc.path, dbRef.path.toString());
// clean up after
app.delete();
} finally {
FirebaseProcessEnvironment.clearCache();
}
}
}
private static class CustomTestCase {
private String rootDbUrl;
private String pathUrl;
private String envVariableUrl;
private String expectedEmulatorRootUrl;
private String namespace;
private String path;
private CustomTestCase(String rootDbUrl, String envVariableUrl,
String expectedEmulatorRootUrl, String namespace) {
this(rootDbUrl, null, envVariableUrl, expectedEmulatorRootUrl, namespace, null);
}
private CustomTestCase(String rootDbUrl, String pathUrl, String envVariableUrl,
String expectedEmulatorRootUrl, String namespace, String path) {
this.rootDbUrl = rootDbUrl;
this.pathUrl = pathUrl;
this.envVariableUrl = envVariableUrl;
this.expectedEmulatorRootUrl = expectedEmulatorRootUrl;
this.namespace = namespace;
this.path = path;
}
}
}