From 339a4bf89568f873e63c8186204356562f3a0e46 Mon Sep 17 00:00:00 2001 From: hiranya911 Date: Thu, 12 Dec 2019 14:17:08 -0800 Subject: [PATCH 1/3] Fixing some failing RTDB tests --- .../firebase/database/FirebaseDatabaseTest.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/google/firebase/database/FirebaseDatabaseTest.java b/src/test/java/com/google/firebase/database/FirebaseDatabaseTest.java index 74e835884..2e3072773 100644 --- a/src/test/java/com/google/firebase/database/FirebaseDatabaseTest.java +++ b/src/test/java/com/google/firebase/database/FirebaseDatabaseTest.java @@ -23,6 +23,7 @@ import static org.junit.Assert.assertSame; import static org.junit.Assert.fail; +import com.google.auth.oauth2.GoogleCredentials; import com.google.common.base.Strings; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; @@ -33,6 +34,7 @@ import com.google.firebase.database.util.EmulatorHelper; import com.google.firebase.testing.ServiceAccount; import com.google.firebase.testing.TestUtils; +import java.io.IOException; import java.util.List; import org.junit.Test; @@ -194,7 +196,7 @@ public void testInitAfterAppDelete() { } @Test - public void testDbUrlIsEmulatorUrlWhenSettingOptionsManually() { + public void testDbUrlIsEmulatorUrlWhenSettingOptionsManually() throws IOException { List testCases = ImmutableList.of( // cases where the env var is ignored because the supplied DB URL is a valid emulator URL @@ -210,6 +212,11 @@ public void testDbUrlIsEmulatorUrlWhenSettingOptionsManually() { new CustomTestCase("https://test.firebaseio.com?ns=valid-namespace", "localhost:90", "http://localhost:90", "valid-namespace") ); + + GoogleCredentials credentials = GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()); + FirebaseOptions options = FirebaseOptions.builder() + .setCredentials(credentials) + .build(); for (CustomTestCase tc : testCases) { try { FirebaseApp app = FirebaseApp.initializeApp(); @@ -230,7 +237,7 @@ public void testDbUrlIsEmulatorUrlWhenSettingOptionsManually() { } @Test - public void testDbUrlIsEmulatorUrlForDbRefWithPath() { + public void testDbUrlIsEmulatorUrlForDbRefWithPath() throws IOException { List testCases = ImmutableList.of( new CustomTestCase("http://my-custom-hosted-emulator.com:80?ns=dummy-ns", @@ -247,9 +254,13 @@ public void testDbUrlIsEmulatorUrlForDbRefWithPath() { "http://localhost:8080", "valid-namespace", "/a/b/c/d") ); + GoogleCredentials credentials = GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()); + FirebaseOptions options = FirebaseOptions.builder() + .setCredentials(credentials) + .build(); for (CustomTestCase tc : testCases) { try { - FirebaseApp app = FirebaseApp.initializeApp(); + FirebaseApp app = FirebaseApp.initializeApp(options); TestUtils.setEnvironmentVariables( ImmutableMap.of(EmulatorHelper.FIREBASE_RTDB_EMULATOR_HOST_ENV_VAR, Strings.nullToEmpty(tc.envVariableUrl))); From 4a78505d7b1265464075e3c194548cd80bbf0044 Mon Sep 17 00:00:00 2001 From: hiranya911 Date: Thu, 12 Dec 2019 14:22:42 -0800 Subject: [PATCH 2/3] Using explicit options --- .../java/com/google/firebase/database/FirebaseDatabaseTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/google/firebase/database/FirebaseDatabaseTest.java b/src/test/java/com/google/firebase/database/FirebaseDatabaseTest.java index 2e3072773..f30183256 100644 --- a/src/test/java/com/google/firebase/database/FirebaseDatabaseTest.java +++ b/src/test/java/com/google/firebase/database/FirebaseDatabaseTest.java @@ -219,7 +219,7 @@ public void testDbUrlIsEmulatorUrlWhenSettingOptionsManually() throws IOExceptio .build(); for (CustomTestCase tc : testCases) { try { - FirebaseApp app = FirebaseApp.initializeApp(); + FirebaseApp app = FirebaseApp.initializeApp(options); TestUtils.setEnvironmentVariables( ImmutableMap.of(EmulatorHelper.FIREBASE_RTDB_EMULATOR_HOST_ENV_VAR, Strings.nullToEmpty(tc.envVariableUrl))); From 4a77a217eb411e84fc3583d76219336e9ea0da1d Mon Sep 17 00:00:00 2001 From: hiranya911 Date: Thu, 12 Dec 2019 15:03:23 -0800 Subject: [PATCH 3/3] Refactored options initialization --- .../database/FirebaseDatabaseTest.java | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/test/java/com/google/firebase/database/FirebaseDatabaseTest.java b/src/test/java/com/google/firebase/database/FirebaseDatabaseTest.java index f30183256..d8395035e 100644 --- a/src/test/java/com/google/firebase/database/FirebaseDatabaseTest.java +++ b/src/test/java/com/google/firebase/database/FirebaseDatabaseTest.java @@ -23,7 +23,6 @@ import static org.junit.Assert.assertSame; import static org.junit.Assert.fail; -import com.google.auth.oauth2.GoogleCredentials; import com.google.common.base.Strings; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; @@ -34,17 +33,20 @@ import com.google.firebase.database.util.EmulatorHelper; 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 FirebaseOptions firebaseOptions = + private static final FirebaseOptions firebaseOptions = new FirebaseOptions.Builder() .setCredentials(TestUtils.getCertCredential(ServiceAccount.EDITOR.asStream())) .setDatabaseUrl("https://firebase-db-test.firebaseio.com") .build(); + private static final FirebaseOptions firebaseOptionsWithoutDatabaseUrl = + new FirebaseOptions.Builder() + .setCredentials(TestUtils.getCertCredential(ServiceAccount.EDITOR.asStream())) + .build(); @Test public void testGetInstance() { @@ -196,7 +198,7 @@ public void testInitAfterAppDelete() { } @Test - public void testDbUrlIsEmulatorUrlWhenSettingOptionsManually() throws IOException { + public void testDbUrlIsEmulatorUrlWhenSettingOptionsManually() { List testCases = ImmutableList.of( // cases where the env var is ignored because the supplied DB URL is a valid emulator URL @@ -213,13 +215,9 @@ public void testDbUrlIsEmulatorUrlWhenSettingOptionsManually() throws IOExceptio "http://localhost:90", "valid-namespace") ); - GoogleCredentials credentials = GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()); - FirebaseOptions options = FirebaseOptions.builder() - .setCredentials(credentials) - .build(); for (CustomTestCase tc : testCases) { try { - FirebaseApp app = FirebaseApp.initializeApp(options); + FirebaseApp app = FirebaseApp.initializeApp(firebaseOptionsWithoutDatabaseUrl); TestUtils.setEnvironmentVariables( ImmutableMap.of(EmulatorHelper.FIREBASE_RTDB_EMULATOR_HOST_ENV_VAR, Strings.nullToEmpty(tc.envVariableUrl))); @@ -237,7 +235,7 @@ public void testDbUrlIsEmulatorUrlWhenSettingOptionsManually() throws IOExceptio } @Test - public void testDbUrlIsEmulatorUrlForDbRefWithPath() throws IOException { + public void testDbUrlIsEmulatorUrlForDbRefWithPath() { List testCases = ImmutableList.of( new CustomTestCase("http://my-custom-hosted-emulator.com:80?ns=dummy-ns", @@ -254,13 +252,9 @@ public void testDbUrlIsEmulatorUrlForDbRefWithPath() throws IOException { "http://localhost:8080", "valid-namespace", "/a/b/c/d") ); - GoogleCredentials credentials = GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()); - FirebaseOptions options = FirebaseOptions.builder() - .setCredentials(credentials) - .build(); for (CustomTestCase tc : testCases) { try { - FirebaseApp app = FirebaseApp.initializeApp(options); + FirebaseApp app = FirebaseApp.initializeApp(firebaseOptionsWithoutDatabaseUrl); TestUtils.setEnvironmentVariables( ImmutableMap.of(EmulatorHelper.FIREBASE_RTDB_EMULATOR_HOST_ENV_VAR, Strings.nullToEmpty(tc.envVariableUrl)));