Java Code Examples for com.google.firebase.database.DatabaseReference#updateChildrenAsync()

The following examples show how to use com.google.firebase.database.DatabaseReference#updateChildrenAsync() . 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: DataTestIT.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateRaisesCorrectLocalEvents()
    throws InterruptedException, TestFailure, TimeoutException {
  DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);

  final ReadFuture readFuture = ReadFuture.untilCountAfterNull(ref, 2);

  ref.setValueAsync(new MapBuilder().put("a", 1).put("b", 2).put("c", 3).put("d", 4).build());

  EventHelper helper = new EventHelper().addValueExpectation(ref.child("a"))
      .addValueExpectation(ref.child("d"))
      .addChildExpectation(ref, Event.EventType.CHILD_CHANGED, "a")
      .addChildExpectation(ref, Event.EventType.CHILD_CHANGED, "d").addValueExpectation(ref)
      .startListening(true);

  ref.updateChildrenAsync(new MapBuilder().put("a", 4).put("d", 1).build());
  helper.waitForEvents();
  List<EventRecord> events = readFuture.timedGet();
  helper.cleanup();

  Map<String, Object> expected = new MapBuilder().put("a", 4L).put("b", 2L).put("c", 3L)
      .put("d", 1L).build();
  Object result = events.get(events.size() - 1).getSnapshot().getValue();
  TestHelpers.assertDeepEquals(expected, result);
}
 
Example 2
Source File: DataTestIT.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateRaisesCorrectRemoteEvents()
    throws TestFailure, ExecutionException, TimeoutException, InterruptedException {
  List<DatabaseReference> refs = IntegrationTestUtils.getRandomNode(masterApp, 2);
  DatabaseReference writer = refs.get(0);
  DatabaseReference reader = refs.get(1);

  new WriteFuture(writer,
      new MapBuilder().put("a", 1).put("b", 2).put("c", 3).put("d", 4).build()).timedGet();

  EventHelper helper = new EventHelper().addValueExpectation(reader.child("a"))
      .addValueExpectation(reader.child("d"))
      .addChildExpectation(reader, Event.EventType.CHILD_CHANGED, "a")
      .addChildExpectation(reader, Event.EventType.CHILD_CHANGED, "d").addValueExpectation(reader)
      .startListening(true);

  writer.updateChildrenAsync(new MapBuilder().put("a", 4).put("d", 1).build());
  helper.waitForEvents();
  helper.cleanup();

  DataSnapshot snap = TestHelpers.getSnap(reader);
  Map<String, Object> expected = new MapBuilder().put("a", 4L).put("b", 2L).put("c", 3L)
      .put("d", 1L).build();
  Object result = snap.getValue();
  TestHelpers.assertDeepEquals(expected, result);
}
 
Example 3
Source File: DataTestIT.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateRaisesChildEventsOnNewListener() throws InterruptedException {
  DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);
  EventHelper helper = new EventHelper().addValueExpectation(ref.child("a"))
      .addValueExpectation(ref.child("d"))
      .addChildExpectation(ref, Event.EventType.CHILD_ADDED, "a")
      .addChildExpectation(ref, Event.EventType.CHILD_ADDED, "d")
      .addValueExpectation(ref.child("c")).addValueExpectation(ref.child("d"))
      .addChildExpectation(ref, Event.EventType.CHILD_ADDED, "c")
      .addChildExpectation(ref, Event.EventType.CHILD_CHANGED, "d").startListening();

  ref.updateChildrenAsync(new MapBuilder().put("a", 11).put("d", 44).build());
  ref.updateChildrenAsync(new MapBuilder().put("c", 33).put("d", 45).build());
  helper.waitForEvents();
  helper.cleanup();
}
 
Example 4
Source File: DataTestIT.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateAfterSetLeafNodeWorks() throws InterruptedException {
  DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);
  final Semaphore semaphore = new Semaphore(0);
  final Map<String, Object> expected = new MapBuilder().put("a", 1L).put("b", 2L).build();

  ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
      if (DeepEquals.deepEquals(snapshot.getValue(), expected)) {
        semaphore.release();
      }
    }

    @Override
    public void onCancelled(DatabaseError error) {
    }
  });
  ref.setValueAsync(42);
  ref.updateChildrenAsync(expected);

  TestHelpers.waitFor(semaphore);
}
 
Example 5
Source File: DataTestIT.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateAffectPriorityLocally()
    throws TestFailure, TimeoutException, InterruptedException {
  DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);

  ReadFuture readFuture = ReadFuture.untilCountAfterNull(ref, 2);

  ref.setValueAsync(new MapBuilder().put("a", 1).put("b", 2).put("c", 3).build(), "testpri");
  ref.updateChildrenAsync(MapBuilder.of("a", 4));

  List<EventRecord> events = readFuture.timedGet();
  DataSnapshot snap = events.get(0).getSnapshot();
  assertEquals("testpri", snap.getPriority());

  snap = events.get(1).getSnapshot();
  assertEquals(4L, snap.child("a").getValue());
  assertEquals("testpri", snap.getPriority());
}
 
Example 6
Source File: OrderByTestIT.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testChildAddedEvents() throws InterruptedException {
  DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp) ;

  Map<String, Object> initial =
      new MapBuilder()
          .put("a", MapBuilder.of("value", 5L))
          .put("c", MapBuilder.of("value", 3L))
          .build();

  final List<String> snapshotNames = new ArrayList<>();
  final List<String> prevNames = new ArrayList<>();
  final Semaphore semaphore = new Semaphore(0);
  final ChildEventListener testListener =
      ref.orderByChild("value")
          .addChildEventListener(
              new TestChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot snap, String prevName) {
                  snapshotNames.add(snap.getKey());
                  prevNames.add(prevName);
                  semaphore.release();
                }
              });

  ref.setValueAsync(initial);
  TestHelpers.waitFor(semaphore, 2);
  Assert.assertEquals(Arrays.asList("c", "a"), snapshotNames);
  Assert.assertEquals(Arrays.asList(null, "c"), prevNames);

  Map<String, Object> updates = new HashMap<>();
  updates.put("b", MapBuilder.of("value", 4));
  updates.put("d", MapBuilder.of("value", 2));
  ref.updateChildrenAsync(updates);

  TestHelpers.waitFor(semaphore, 2);
  Assert.assertEquals(Arrays.asList("c", "a", "d", "b"), snapshotNames);
  Assert.assertEquals(Arrays.asList(null, "c", null, "c"), prevNames);
  ref.removeEventListener(testListener);
}
 
Example 7
Source File: QueryTestIT.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeletingEntireQueryWindow()
    throws InterruptedException, TestFailure, TimeoutException {
  DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);

  final ReadFuture readFuture = ReadFuture.untilCount(ref.limitToLast(2), 3);

  // wait for null event
  TestHelpers.waitForRoundtrip(ref);

  ref.setValueAsync(
      MapBuilder.of(
          "a", MapBuilder.of(".value", 1, ".priority", 1),
          "b", MapBuilder.of(".value", 2, ".priority", 2),
          "c", MapBuilder.of(".value", 3, ".priority", 3)));

  ref.updateChildrenAsync(new MapBuilder().put("b", null).put("c", null).build());
  List<EventRecord> events = readFuture.timedGet();
  DataSnapshot snap = events.get(1).getSnapshot();

  Map<String, Object> expected = MapBuilder.of("b", 2L, "c", 3L);
  Object result = snap.getValue();
  TestHelpers.assertDeepEquals(expected, result);

  // The original set is still outstanding (synchronous API), so we have a
  // full cache to re-window against
  snap = events.get(2).getSnapshot();
  result = snap.getValue();
  TestHelpers.assertDeepEquals(MapBuilder.of("a", 1L), result);
}
 
Example 8
Source File: QueryTestIT.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testOutOfViewQueryOnAChild()
    throws TestFailure, ExecutionException, TimeoutException, InterruptedException {
  DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);

  final ReadFuture parentFuture = ReadFuture.untilCountAfterNull(ref.limitToLast(1), 2);

  final List<DataSnapshot> childSnaps = new ArrayList<>();
  ref.child("a").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
      childSnaps.add(snapshot);
    }

    @Override
    public void onCancelled(DatabaseError error) {
    }
  });

  new WriteFuture(ref, MapBuilder.of("a", 1, "b", 2)).timedGet();
  assertEquals(1L, childSnaps.get(0).getValue());
  ref.updateChildrenAsync(MapBuilder.of("c", 3));
  List<EventRecord> events = parentFuture.timedGet();
  DataSnapshot snap = events.get(0).getSnapshot();
  Object result = snap.getValue();

  Map<String, Object> expected = MapBuilder.of("b", 2L);
  TestHelpers.assertDeepEquals(expected, result);

  snap = events.get(1).getSnapshot();
  result = snap.getValue();

  expected = MapBuilder.of("c", 3L);
  TestHelpers.assertDeepEquals(expected, result);
  assertEquals(1, childSnaps.size());
  assertEquals(1L, childSnaps.get(0).getValue());
}
 
Example 9
Source File: DataTestIT.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpdateFiresCorrectEventWhenDeletingChild()
    throws TestFailure, ExecutionException, TimeoutException, InterruptedException {
  List<DatabaseReference> refs = IntegrationTestUtils.getRandomNode(masterApp, 2);
  DatabaseReference writer = refs.get(0);
  DatabaseReference reader = refs.get(1);

  final ReadFuture writerFuture = ReadFuture.untilCountAfterNull(writer, 2);
  new WriteFuture(writer, new MapBuilder().put("a", 12).put("b", 6).build()).timedGet();
  final Semaphore semaphore = new Semaphore(0);
  final ReadFuture readerFuture = new ReadFuture(reader, new ReadFuture.CompletionCondition() {
    @Override
    public boolean isComplete(List<EventRecord> events) {
      if (events.size() == 1) {
        semaphore.release();
      }
      return events.size() == 2;
    }
  });

  TestHelpers.waitFor(semaphore);

  writer.updateChildrenAsync(MapBuilder.of("a", null));
  DataSnapshot snap = writerFuture.timedGet().get(1).getSnapshot();

  Map<String, Object> expected = MapBuilder.of("b", 6L);
  TestHelpers.assertDeepEquals(expected, snap.getValue());

  snap = readerFuture.timedGet().get(1).getSnapshot();

  TestHelpers.assertDeepEquals(expected, snap.getValue());
}
 
Example 10
Source File: DataTestIT.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpdateFiresCorrectEventWhenAllChildrenAreDeleted()
    throws TestFailure, ExecutionException, TimeoutException, InterruptedException {
  List<DatabaseReference> refs = IntegrationTestUtils.getRandomNode(masterApp, 2);
  DatabaseReference writer = refs.get(0);
  DatabaseReference reader = refs.get(1);

  final ReadFuture writerFuture = ReadFuture.untilCountAfterNull(writer, 2);
  new WriteFuture(writer, MapBuilder.of("a", 12)).timedGet();
  final Semaphore semaphore = new Semaphore(0);
  final ReadFuture readerFuture = new ReadFuture(reader, new ReadFuture.CompletionCondition() {
    @Override
    public boolean isComplete(List<EventRecord> events) {
      if (events.size() == 1) {
        semaphore.release();
      }
      return events.size() == 2;
    }
  });

  TestHelpers.waitFor(semaphore);

  writer.updateChildrenAsync(MapBuilder.of("a", null));
  DataSnapshot snap = writerFuture.timedGet().get(1).getSnapshot();

  assertNull(snap.getValue());

  snap = readerFuture.timedGet().get(1).getSnapshot();

  assertNull(snap.getValue());
}
 
Example 11
Source File: DataTestIT.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpdateFiresCorrectEventOnChangedChildren()
    throws TestFailure, ExecutionException, TimeoutException, InterruptedException {
  List<DatabaseReference> refs = IntegrationTestUtils.getRandomNode(masterApp, 2);
  DatabaseReference writer = refs.get(0);
  DatabaseReference reader = refs.get(1);

  final ReadFuture writerFuture = ReadFuture.untilCountAfterNull(writer, 2);
  new WriteFuture(writer, MapBuilder.of("a", 12)).timedGet();
  final Semaphore semaphore = new Semaphore(0);
  final ReadFuture readerFuture = new ReadFuture(reader, new ReadFuture.CompletionCondition() {
    @Override
    public boolean isComplete(List<EventRecord> events) {
      if (events.size() == 1) {
        semaphore.release();
      }
      return events.size() == 2;
    }
  });

  TestHelpers.waitFor(semaphore);

  writer.updateChildrenAsync(MapBuilder.of("a", 11));
  DataSnapshot snap = writerFuture.timedGet().get(1).getSnapshot();

  Map<String, Object> expected = MapBuilder.of("a", 11L);
  TestHelpers.assertDeepEquals(expected, snap.getValue());

  snap = readerFuture.timedGet().get(1).getSnapshot();

  TestHelpers.assertDeepEquals(expected, snap.getValue());
}