Java Code Examples for com.google.firebase.FirebaseApp#delete()

The following examples show how to use com.google.firebase.FirebaseApp#delete() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ValidationTest.java    From firebase-android-sdk with Apache License 2.0 6 votes vote down vote up
private static void withApp(String name, Consumer<FirebaseApp> toRun) {
  FirebaseApp app =
      FirebaseApp.initializeApp(
          ApplicationProvider.getApplicationContext(),
          new FirebaseOptions.Builder()
              .setApiKey("key")
              .setApplicationId("appId")
              .setProjectId("projectId")
              .build(),
          name);
  try {
    toRun.accept(app);
  } finally {
    app.delete();
  }
}
 
Example 2
Source File: TransportRegistrationTest.java    From firebase-android-sdk with Apache License 2.0 6 votes vote down vote up
private static void withApp(Consumer<FirebaseApp> consumer) {
  FirebaseApp app =
      FirebaseApp.initializeApp(
          RuntimeEnvironment.application,
          new FirebaseOptions.Builder()
              .setApplicationId("appId")
              .setProjectId("123")
              .setApiKey("apiKey")
              .build(),
          "datatransport-test-app");
  try {
    consumer.accept(app);
  } finally {
    app.delete();
  }
}
 
Example 3
Source File: FirebaseDatabaseTest.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@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();
  }
}
 
Example 4
Source File: FirebaseAuthIT.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testCustomTokenWithIAM() throws Exception {
  FirebaseApp masterApp = IntegrationTestUtils.ensureDefaultApp();
  GoogleCredentials credentials = ImplFirebaseTrampolines.getCredentials(masterApp);
  AccessToken token = credentials.getAccessToken();
  if (token == null) {
    token = credentials.refreshAccessToken();
  }
  FirebaseOptions options = new FirebaseOptions.Builder()
      .setCredentials(GoogleCredentials.create(token))
      .setServiceAccountId(((ServiceAccountSigner) credentials).getAccount())
      .setProjectId(IntegrationTestUtils.getProjectId())
      .build();
  FirebaseApp customApp = FirebaseApp.initializeApp(options, "tempApp");
  try {
    FirebaseAuth auth = FirebaseAuth.getInstance(customApp);
    String customToken = auth.createCustomTokenAsync("user1").get();
    String idToken = signInWithCustomToken(customToken);
    FirebaseToken decoded = auth.verifyIdTokenAsync(idToken).get();
    assertEquals("user1", decoded.getUid());
  } finally {
    customApp.delete();
  }
}
 
Example 5
Source File: FirebaseAuthTest.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testInitAfterAppDelete() throws ExecutionException, InterruptedException,
    TimeoutException {
  FirebaseApp app = FirebaseApp.initializeApp(firebaseOptions, "testInitAfterAppDelete");
  FirebaseAuth auth1 = FirebaseAuth.getInstance(app);
  assertNotNull(auth1);
  app.delete();

  app = FirebaseApp.initializeApp(firebaseOptions, "testInitAfterAppDelete");
  FirebaseAuth auth2 = FirebaseAuth.getInstance(app);
  assertNotNull(auth2);
  assertNotSame(auth1, auth2);

  ApiFuture<String> future = auth2.createCustomTokenAsync("foo");
  assertNotNull(future);
  assertNotNull(future.get(TestUtils.TEST_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS));
}
 
Example 6
Source File: StorageClientTest.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testAppDelete() throws IOException {
  FirebaseApp app = FirebaseApp.initializeApp(new FirebaseOptions.Builder()
      .setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()))
      .setStorageBucket("mock-bucket-name")
      .build());

  assertNotNull(StorageClient.getInstance());
  assertNotNull(StorageClient.getInstance(app));
  app.delete();
  try {
    StorageClient.getInstance(app);
    fail("No error thrown for deleted app");
  } catch (IllegalStateException expected) {
    // ignore
  }
}
 
Example 7
Source File: FirebaseMessagingClientImplTest.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testFromApp() throws IOException {
  FirebaseOptions options = new FirebaseOptions.Builder()
      .setCredentials(new MockGoogleCredentials("test-token"))
      .setProjectId("test-project")
      .build();
  FirebaseApp app = FirebaseApp.initializeApp(options);

  try {
    FirebaseMessagingClientImpl client = FirebaseMessagingClientImpl.fromApp(app);

    assertEquals(TEST_FCM_URL, client.getFcmSendUrl());
    assertEquals("fire-admin-java/" + SdkUtils.getVersion(), client.getClientVersion());
    assertSame(options.getJsonFactory(), client.getJsonFactory());

    HttpRequest request = client.getRequestFactory().buildGetRequest(
        new GenericUrl("https://example.com"));
    assertEquals("Bearer test-token", request.getHeaders().getAuthorization());

    request = client.getChildRequestFactory().buildGetRequest(
        new GenericUrl("https://example.com"));
    assertNull(request.getHeaders().getAuthorization());
  } finally {
    app.delete();
  }
}
 
Example 8
Source File: InstanceIdClientImplTest.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testFromApp() throws IOException {
  FirebaseOptions options = new FirebaseOptions.Builder()
      .setCredentials(new MockGoogleCredentials("test-token"))
      .setProjectId("test-project")
      .build();
  FirebaseApp app = FirebaseApp.initializeApp(options);

  try {
    InstanceIdClientImpl client = InstanceIdClientImpl.fromApp(app);

    assertSame(options.getJsonFactory(), client.getJsonFactory());
    HttpRequest request = client.getRequestFactory().buildGetRequest(
        new GenericUrl("https://example.com"));
    assertEquals("Bearer test-token", request.getHeaders().getAuthorization());
  } finally {
    app.delete();
  }
}
 
Example 9
Source File: FirestoreTest.java    From firebase-android-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void testAppDeleteLeadsToFirestoreTerminate() {
  FirebaseApp app = testFirebaseApp();
  FirebaseFirestore instance = FirebaseFirestore.getInstance(app);
  instance.setFirestoreSettings(newTestSettings());
  waitFor(instance.document("abc/123").set(Collections.singletonMap("Field", 100)));

  app.delete();

  assertTrue(instance.getClient().isTerminated());
}
 
Example 10
Source File: FirebaseDatabaseTest.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDbUrlIsEmulatorUrlWhenSettingOptionsManually() {

  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);
      TestUtils.setEnvironmentVariables(
          ImmutableMap.of(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 {
      TestUtils.unsetEnvironmentVariables(
          ImmutableSet.of(EmulatorHelper.FIREBASE_RTDB_EMULATOR_HOST_ENV_VAR));
    }
  }
}
 
Example 11
Source File: FirebaseDatabaseTest.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDbUrlIsEmulatorUrlForDbRefWithPath() {

  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);
      TestUtils.setEnvironmentVariables(
          ImmutableMap.of(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 {
      TestUtils.unsetEnvironmentVariables(
          ImmutableSet.of(EmulatorHelper.FIREBASE_RTDB_EMULATOR_HOST_ENV_VAR));
    }
  }
}
 
Example 12
Source File: FirebaseAuthTest.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testAppDelete() {
  FirebaseApp app = FirebaseApp.initializeApp(firebaseOptions, "testAppDelete");
  FirebaseAuth auth = FirebaseAuth.getInstance(app);
  assertNotNull(auth);
  app.delete();
  try {
    FirebaseAuth.getInstance(app);
    fail("No error thrown when getting auth instance after deleting app");
  } catch (IllegalStateException expected) {
    // ignore
  }
}
 
Example 13
Source File: FirebaseAuthTest.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvokeAfterAppDelete() throws Exception {
  FirebaseApp app = FirebaseApp.initializeApp(firebaseOptions, "testInvokeAfterAppDelete");
  FirebaseAuth auth = FirebaseAuth.getInstance(app);
  assertNotNull(auth);
  app.delete();

  for (Method method : auth.getClass().getDeclaredMethods()) {
    int modifiers = method.getModifiers();
    if (!Modifier.isPublic(modifiers) || Modifier.isStatic(modifiers)) {
      continue;
    }

    List<Object> parameters = new ArrayList<>(method.getParameterTypes().length);
    for (Class<?> parameterType : method.getParameterTypes()) {
      parameters.add(Defaults.defaultValue(parameterType));
    }
    try {
      method.invoke(auth, parameters.toArray());
      fail("No error thrown when invoking auth after deleting app; method: " + method.getName());
    } catch (InvocationTargetException expected) {
      String message = "FirebaseAuth instance is no longer alive. This happens when "
          + "the parent FirebaseApp instance has been deleted.";
      Throwable cause = expected.getCause();
      assertTrue(cause instanceof IllegalStateException);
      assertEquals(message, cause.getMessage());
    }
  }
}
 
Example 14
Source File: FirebaseThreadManagersTest.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefaultThreadManager() throws Exception {
  FirebaseOptions options = new FirebaseOptions.Builder()
      .setCredentials(new MockGoogleCredentials())
      .build();
  FirebaseApp defaultApp = FirebaseApp.initializeApp(options);
  final Map<String, Object> threadInfo = new HashMap<>();
  Callable<Void> command = new Callable<Void>() {
    @Override
    public Void call() throws Exception {
      Thread thread = Thread.currentThread();
      threadInfo.put("name", thread.getName());
      threadInfo.put("daemon", thread.isDaemon());
      return null;
    }
  };
  ImplFirebaseTrampolines.submitCallable(defaultApp, command).get();

  // Check for default JVM thread properties.
  assertTrue(threadInfo.get("name").toString().startsWith("firebase-default-"));
  assertTrue((Boolean) threadInfo.get("daemon"));

  defaultApp.delete();
  try {
    ImplFirebaseTrampolines.submitCallable(defaultApp, command);
    fail("No error thrown when submitting to deleted app");
  } catch (RejectedExecutionException expected) {
    // expected
  }
}
 
Example 15
Source File: FirebaseMessagingTest.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testPostDeleteApp() {
  FirebaseApp app = FirebaseApp.initializeApp(TEST_OPTIONS, "custom-app");

  app.delete();

  try {
    FirebaseMessaging.getInstance(app);
    fail("No error thrown for deleted app");
  } catch (IllegalStateException expected) {
    // expected
  }
}
 
Example 16
Source File: FirebaseInstanceIdTest.java    From firebase-admin-java with Apache License 2.0 4 votes vote down vote up
@Test
public void testDeleteInstanceIdError() throws Exception {
  Map<Integer, String> errors = ImmutableMap.of(
      404, "Instance ID \"test-iid\": Failed to find the instance ID.",
      429, "Instance ID \"test-iid\": Request throttled out by the backend server.",
      500, "Instance ID \"test-iid\": Internal server error.",
      501, "Error while invoking instance ID service."
  );

  String url = "https://console.firebase.google.com/v1/project/test-project/instanceId/test-iid";
  for (Map.Entry<Integer, String> entry : errors.entrySet()) {
    MockLowLevelHttpResponse response = new MockLowLevelHttpResponse()
        .setStatusCode(entry.getKey())
        .setContent("test error");
    MockHttpTransport transport = new MockHttpTransport.Builder()
        .setLowLevelHttpResponse(response)
        .build();
    FirebaseOptions options = new FirebaseOptions.Builder()
        .setCredentials(new MockGoogleCredentials("test-token"))
        .setProjectId("test-project")
        .setHttpTransport(transport)
        .build();
    final FirebaseApp app = FirebaseApp.initializeApp(options);

    FirebaseInstanceId instanceId = FirebaseInstanceId.getInstance();
    TestResponseInterceptor interceptor = new TestResponseInterceptor();
    instanceId.setInterceptor(interceptor);
    try {
      instanceId.deleteInstanceIdAsync("test-iid").get();
      fail("No error thrown for HTTP error");
    } catch (ExecutionException e) {
      assertTrue(e.getCause() instanceof FirebaseInstanceIdException);
      assertEquals(entry.getValue(), e.getCause().getMessage());
      assertTrue(e.getCause().getCause() instanceof HttpResponseException);
    }

    assertNotNull(interceptor.getResponse());
    HttpRequest request = interceptor.getResponse().getRequest();
    assertEquals("DELETE", request.getRequestMethod());
    assertEquals(url, request.getUrl().toString());
    assertEquals("Bearer test-token", request.getHeaders().getAuthorization());
    app.delete();
  }
}