Search in sources :

Example 1 with BulkWriter

use of com.google.cloud.firestore.BulkWriter in project java-firestore by googleapis.

the class ITSystemTest method testRecursiveDeleteWithCustomBulkWriterInstance.

@Test
public void testRecursiveDeleteWithCustomBulkWriterInstance() throws Exception {
    setupRecursiveDeleteTest();
    BulkWriter bulkWriter = firestore.bulkWriter();
    final int[] callbackCount = { 0 };
    bulkWriter.addWriteResultListener((documentReference, result) -> callbackCount[0]++);
    firestore.recursiveDelete(randomColl, bulkWriter).get();
    assertEquals(0, countCollectionChildren(randomColl));
    assertEquals(6, callbackCount[0]);
}
Also used : BulkWriter(com.google.cloud.firestore.BulkWriter) Test(org.junit.Test)

Example 2 with BulkWriter

use of com.google.cloud.firestore.BulkWriter in project java-firestore by googleapis.

the class ITBulkWriterTest method bulkWriterCreate.

@Test
public void bulkWriterCreate() throws Exception {
    DocumentReference docRef = randomColl.document();
    firestore.bulkWriter();
    BulkWriter writer = firestore.bulkWriter();
    ApiFuture<WriteResult> result = writer.create(docRef, Collections.singletonMap("foo", "bar"));
    writer.close();
    assertNotNull(result.get().getUpdateTime());
    DocumentSnapshot snapshot = docRef.get().get();
    assertEquals("bar", snapshot.get("foo"));
}
Also used : DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) WriteResult(com.google.cloud.firestore.WriteResult) BulkWriter(com.google.cloud.firestore.BulkWriter) DocumentReference(com.google.cloud.firestore.DocumentReference) Test(org.junit.Test)

Example 3 with BulkWriter

use of com.google.cloud.firestore.BulkWriter in project java-firestore by googleapis.

the class ITBulkWriterTest method bulkWriterCreateAddsPrecondition.

@Test
public void bulkWriterCreateAddsPrecondition() throws Exception {
    DocumentReference docRef = randomColl.document();
    docRef.set(Collections.singletonMap("foo", "bar")).get();
    BulkWriter writer = firestore.bulkWriter();
    ApiFuture<WriteResult> result = writer.create(docRef, Collections.singletonMap("foo", "bar"));
    writer.close();
    try {
        result.get();
        fail("Create operation should have thrown exception");
    } catch (Exception e) {
        assertTrue(e.getMessage().contains("Document already exists"));
    }
}
Also used : WriteResult(com.google.cloud.firestore.WriteResult) BulkWriter(com.google.cloud.firestore.BulkWriter) DocumentReference(com.google.cloud.firestore.DocumentReference) Test(org.junit.Test)

Example 4 with BulkWriter

use of com.google.cloud.firestore.BulkWriter in project java-firestore by googleapis.

the class ITBulkWriterTest method bulkWriterDelete.

@Test
public void bulkWriterDelete() throws Exception {
    DocumentReference docRef = randomColl.document();
    docRef.set(Collections.singletonMap("foo", "oldValue")).get();
    BulkWriter writer = firestore.bulkWriter();
    ApiFuture<WriteResult> result = writer.delete(docRef);
    writer.close();
    assertNotNull(result.get().getUpdateTime());
    // TODO(b/158502664): Remove this check once we can get write times.
    assertEquals(Timestamp.ofTimeSecondsAndNanos(0, 0), result.get().getUpdateTime());
    DocumentSnapshot snapshot = docRef.get().get();
    assertNull(snapshot.get("foo"));
}
Also used : DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) WriteResult(com.google.cloud.firestore.WriteResult) BulkWriter(com.google.cloud.firestore.BulkWriter) DocumentReference(com.google.cloud.firestore.DocumentReference) Test(org.junit.Test)

Example 5 with BulkWriter

use of com.google.cloud.firestore.BulkWriter in project java-firestore by googleapis.

the class ITBulkWriterTest method bulkWriterOnError.

@Test
public void bulkWriterOnError() throws Exception {
    class NamedThreadFactory implements ThreadFactory {

        public Thread newThread(Runnable r) {
            return new Thread(r, "bulkWriterException");
        }
    }
    Executor executor = Executors.newSingleThreadExecutor(new NamedThreadFactory());
    final List<String> operations = new ArrayList<>();
    BulkWriter writer = firestore.bulkWriter();
    writer.addWriteErrorListener(executor, error -> {
        operations.add("operation-error");
        assertTrue(Thread.currentThread().getName().contains("bulkWriterException"));
        return false;
    });
    writer.addWriteResultListener(executor, (documentReference, result) -> fail("The success listener shouldn't be called"));
    writer.update(randomDoc, "foo", "bar");
    writer.flush().get();
    assertEquals("operation-error", operations.get(0));
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) Executor(java.util.concurrent.Executor) ArrayList(java.util.ArrayList) BulkWriter(com.google.cloud.firestore.BulkWriter) Test(org.junit.Test)

Aggregations

BulkWriter (com.google.cloud.firestore.BulkWriter)10 Test (org.junit.Test)10 WriteResult (com.google.cloud.firestore.WriteResult)7 DocumentReference (com.google.cloud.firestore.DocumentReference)6 DocumentSnapshot (com.google.cloud.firestore.DocumentSnapshot)4 ArrayList (java.util.ArrayList)2 Executor (java.util.concurrent.Executor)2 ThreadFactory (java.util.concurrent.ThreadFactory)2 CollectionReference (com.google.cloud.firestore.CollectionReference)1 FirestoreOptions (com.google.cloud.firestore.FirestoreOptions)1