Search in sources :

Example 21 with WatchTargetChange

use of com.google.firebase.firestore.remote.WatchChange.WatchTargetChange in project firebase-android-sdk by firebase.

the class RemoteEventTest method testExistenceFilterMismatchClearsTarget.

@Test
public void testExistenceFilterMismatchClearsTarget() {
    Map<Integer, TargetData> targetMap = activeQueries(1, 2);
    MutableDocument doc1 = doc("docs/1", 1, map("value", 1));
    MutableDocument doc2 = doc("docs/2", 2, map("value", 2));
    WatchChange change1 = new DocumentChange(asList(1), emptyList(), doc1.getKey(), doc1);
    WatchChange change2 = new DocumentChange(asList(1), emptyList(), doc2.getKey(), doc2);
    WatchChange change3 = new WatchTargetChange(WatchTargetChangeType.Current, asList(1));
    WatchChangeAggregator aggregator = createAggregator(targetMap, noOutstandingResponses, keySet(doc1.getKey(), doc2.getKey()), change1, change2, change3);
    RemoteEvent event = aggregator.createRemoteEvent(version(3));
    assertEquals(version(3), event.getSnapshotVersion());
    assertEquals(2, event.getDocumentUpdates().size());
    assertEquals(doc1, event.getDocumentUpdates().get(doc1.getKey()));
    assertEquals(doc2, event.getDocumentUpdates().get(doc2.getKey()));
    assertEquals(2, event.getTargetChanges().size());
    TargetChange mapping1 = targetChange(resumeToken, true, null, asList(doc1, doc2), null);
    assertEquals(mapping1, event.getTargetChanges().get(1));
    TargetChange mapping2 = targetChange(resumeToken, false, null, null, null);
    assertEquals(mapping2, event.getTargetChanges().get(2));
    WatchChange.ExistenceFilterWatchChange watchChange = new WatchChange.ExistenceFilterWatchChange(1, new ExistenceFilter(1));
    aggregator.handleExistenceFilter(watchChange);
    event = aggregator.createRemoteEvent(version(3));
    TargetChange mapping3 = targetChange(ByteString.EMPTY, false, null, null, asList(doc1, doc2));
    assertEquals(1, event.getTargetChanges().size());
    assertEquals(mapping3, event.getTargetChanges().get(1));
    assertEquals(1, event.getTargetMismatches().size());
    assertEquals(0, event.getDocumentUpdates().size());
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) DocumentChange(com.google.firebase.firestore.remote.WatchChange.DocumentChange) WatchTargetChange(com.google.firebase.firestore.remote.WatchChange.WatchTargetChange) TargetData(com.google.firebase.firestore.local.TargetData) WatchTargetChange(com.google.firebase.firestore.remote.WatchChange.WatchTargetChange) Test(org.junit.Test)

Example 22 with WatchTargetChange

use of com.google.firebase.firestore.remote.WatchChange.WatchTargetChange in project firebase-android-sdk by firebase.

the class RemoteEventTest method testWillIgnoreEventsForPendingTargets.

@Test
public void testWillIgnoreEventsForPendingTargets() {
    Map<Integer, TargetData> targetMap = activeQueries(1);
    MutableDocument doc1 = doc("docs/1", 1, map("value", 1));
    MutableDocument doc2 = doc("docs/2", 2, map("value", 2));
    // We're waiting for the watch and unwatch ack
    Map<Integer, Integer> outstanding = new HashMap<>();
    outstanding.put(1, 2);
    WatchChange change1 = new DocumentChange(asList(1), emptyList(), doc1.getKey(), doc1);
    WatchChange change2 = new WatchTargetChange(WatchTargetChangeType.Removed, asList(1));
    WatchChange change3 = new WatchTargetChange(WatchTargetChangeType.Added, asList(1));
    WatchChange change4 = new DocumentChange(asList(1), emptyList(), doc2.getKey(), doc2);
    RemoteEvent event = createRemoteEvent(3, targetMap, outstanding, noExistingKeys, change1, change2, change3, change4);
    assertEquals(version(3), event.getSnapshotVersion());
    // doc1 is ignored because the target was not active at the time, but for
    // doc2 the target is active.
    assertEquals(1, event.getDocumentUpdates().size());
    assertEquals(doc2, event.getDocumentUpdates().get(doc2.getKey()));
    assertEquals(1, event.getTargetChanges().size());
}
Also used : TargetData(com.google.firebase.firestore.local.TargetData) HashMap(java.util.HashMap) MutableDocument(com.google.firebase.firestore.model.MutableDocument) DocumentChange(com.google.firebase.firestore.remote.WatchChange.DocumentChange) WatchTargetChange(com.google.firebase.firestore.remote.WatchChange.WatchTargetChange) Test(org.junit.Test)

Example 23 with WatchTargetChange

use of com.google.firebase.firestore.remote.WatchChange.WatchTargetChange in project firebase-android-sdk by firebase.

the class RemoteEventTest method testSynthesizeDeletes.

@Test
public void testSynthesizeDeletes() {
    Map<Integer, TargetData> targetMap = activeLimboQueries("foo/doc", 1);
    WatchTargetChange shouldSynthesize = new WatchTargetChange(WatchTargetChangeType.Current, asList(1));
    RemoteEvent event = createRemoteEvent(3, targetMap, noOutstandingResponses, noExistingKeys, shouldSynthesize);
    DocumentKey synthesized = key("docs/2");
    assertNull(event.getDocumentUpdates().get(synthesized));
    MutableDocument expected = deletedDoc("foo/doc", 3);
    assertEquals(expected, event.getDocumentUpdates().get(expected.getKey()));
    assertTrue(event.getResolvedLimboDocuments().contains(expected.getKey()));
}
Also used : TargetData(com.google.firebase.firestore.local.TargetData) DocumentKey(com.google.firebase.firestore.model.DocumentKey) MutableDocument(com.google.firebase.firestore.model.MutableDocument) WatchTargetChange(com.google.firebase.firestore.remote.WatchChange.WatchTargetChange) Test(org.junit.Test)

Example 24 with WatchTargetChange

use of com.google.firebase.firestore.remote.WatchChange.WatchTargetChange in project firebase-android-sdk by firebase.

the class RemoteSerializerTest method testConvertsTargetChangeWithRemoved.

@Test
public void testConvertsTargetChangeWithRemoved() {
    WatchTargetChange expected = new WatchTargetChange(WatchTargetChangeType.Removed, asList(1, 4), ByteString.copyFrom(new byte[] { 0, 1, 2 }), Status.PERMISSION_DENIED);
    WatchTargetChange actual = (WatchTargetChange) serializer.decodeWatchChange(ListenResponse.newBuilder().setTargetChange(TargetChange.newBuilder().setTargetChangeType(TargetChangeType.REMOVE).addTargetIds(1).addTargetIds(4).setCause(com.google.rpc.Status.newBuilder().setCode(7)).setResumeToken(ByteString.copyFrom(new byte[] { 0, 1, 2 }))).build());
    assertEquals(expected, actual);
}
Also used : WatchTargetChange(com.google.firebase.firestore.remote.WatchChange.WatchTargetChange) Test(org.junit.Test)

Example 25 with WatchTargetChange

use of com.google.firebase.firestore.remote.WatchChange.WatchTargetChange in project firebase-android-sdk by firebase.

the class SpecTestCase method doWatchRemove.

private void doWatchRemove(JSONObject watchRemoveSpec) throws Exception {
    Status error = null;
    JSONObject cause = watchRemoveSpec.optJSONObject("cause");
    if (cause != null) {
        int code = cause.optInt("code");
        if (code != 0) {
            error = Status.fromCodeValue(code);
        }
    }
    List<Integer> targetIds = parseIntList(watchRemoveSpec.getJSONArray("targetIds"));
    WatchTargetChange change = new WatchTargetChange(WatchTargetChangeType.Removed, targetIds, WatchStream.EMPTY_RESUME_TOKEN, error);
    writeWatchChange(change, SnapshotVersion.NONE);
// Unlike web, the MockDatastore detects a watch removal with cause and will remove active
// targets
}
Also used : Status(io.grpc.Status) JSONObject(org.json.JSONObject) WatchTargetChange(com.google.firebase.firestore.remote.WatchChange.WatchTargetChange)

Aggregations

WatchTargetChange (com.google.firebase.firestore.remote.WatchChange.WatchTargetChange)26 Test (org.junit.Test)18 TargetData (com.google.firebase.firestore.local.TargetData)16 MutableDocument (com.google.firebase.firestore.model.MutableDocument)9 DocumentChange (com.google.firebase.firestore.remote.WatchChange.DocumentChange)9 ExistenceFilterWatchChange (com.google.firebase.firestore.remote.WatchChange.ExistenceFilterWatchChange)4 ByteString (com.google.protobuf.ByteString)4 HashMap (java.util.HashMap)4 DocumentKey (com.google.firebase.firestore.model.DocumentKey)3 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)2 WatchChange (com.google.firebase.firestore.remote.WatchChange)2 Status (io.grpc.Status)2 ObjectValue (com.google.firebase.firestore.model.ObjectValue)1 WatchTargetChangeType (com.google.firebase.firestore.remote.WatchChange.WatchTargetChangeType)1 DocumentChange (com.google.firestore.v1.DocumentChange)1 DocumentDelete (com.google.firestore.v1.DocumentDelete)1 DocumentRemove (com.google.firestore.v1.DocumentRemove)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 JSONObject (org.json.JSONObject)1