Search in sources :

Example 6 with BulkWriter

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

the class ITBulkWriterTest method bulkWriterOnResult.

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

        public Thread newThread(Runnable r) {
            return new Thread(r, "bulkWriterSuccess");
        }
    }
    Executor executor = Executors.newSingleThreadExecutor(new NamedThreadFactory());
    final List<String> operations = new ArrayList<>();
    BulkWriter writer = firestore.bulkWriter();
    writer.addWriteResultListener(executor, (documentReference, result) -> {
        operations.add("operation");
        assertTrue(Thread.currentThread().getName().contains("bulkWriterSuccess"));
    });
    writer.set(randomDoc, Collections.singletonMap("foo", "bar"));
    writer.flush().get();
    assertEquals("operation", 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)

Example 7 with BulkWriter

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

the class ITBulkWriterTest method bulkWriterUpdate.

@Test
public void bulkWriterUpdate() throws Exception {
    DocumentReference docRef = randomColl.document();
    docRef.set(Collections.singletonMap("foo", "oldValue")).get();
    BulkWriter writer = firestore.bulkWriter();
    ApiFuture<WriteResult> result = writer.update(docRef, "foo", "newValue");
    writer.close();
    assertNotNull(result.get().getUpdateTime());
    DocumentSnapshot snapshot = docRef.get().get();
    assertEquals("newValue", 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 8 with BulkWriter

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

the class ITBulkWriterTest method bulkWriterUpdateAddsPrecondition.

@Test
public void bulkWriterUpdateAddsPrecondition() throws Exception {
    DocumentReference docRef = randomColl.document();
    BulkWriter writer = firestore.bulkWriter();
    ApiFuture<WriteResult> result = writer.update(docRef, "foo", "newValue");
    writer.close();
    try {
        result.get();
        fail("Update operation should have thrown exception");
    } catch (Exception e) {
        assertTrue(e.getMessage().matches(".* No (document|entity) to update.*"));
    }
}
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 9 with BulkWriter

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

the class ITBulkWriterTest method bulkWriterSet.

@Test
public void bulkWriterSet() throws Exception {
    DocumentReference docRef = randomColl.document();
    BulkWriter writer = firestore.bulkWriter();
    ApiFuture<WriteResult> result = writer.set(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 10 with BulkWriter

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

the class ITSystemTest method testEnforcesTimeouts.

@Test
public void testEnforcesTimeouts() {
    FirestoreOptions firestoreOptions = FirestoreOptions.newBuilder().setRetrySettings(RetrySettings.newBuilder().setMaxRpcTimeout(Duration.ofMillis(1)).setTotalTimeout(Duration.ofMillis(1)).setInitialRpcTimeout(Duration.ofMillis(1)).build()).build();
    firestore = firestoreOptions.getService();
    CollectionReference collection = firestore.collection("timeout");
    // RunQuery
    assertThrows(ExecutionException.class, () -> collection.get().get());
    // CommitRequest
    assertThrows(ExecutionException.class, () -> collection.add(map()).get());
    // BulkCommit
    assertThrows(ExecutionException.class, () -> {
        BulkWriter bulkWriter = firestore.bulkWriter();
        ApiFuture<WriteResult> op = bulkWriter.set(collection.document(), map());
        bulkWriter.close();
        op.get();
    });
    // BatchGetDocuments
    assertThrows(ExecutionException.class, () -> collection.document().get().get());
    // ListDocuments
    assertThrows(FirestoreException.class, () -> collection.listDocuments().iterator().hasNext());
    // ListCollections
    assertThrows(FirestoreException.class, () -> collection.document().listCollections().iterator().hasNext());
}
Also used : WriteResult(com.google.cloud.firestore.WriteResult) BulkWriter(com.google.cloud.firestore.BulkWriter) CollectionReference(com.google.cloud.firestore.CollectionReference) FirestoreOptions(com.google.cloud.firestore.FirestoreOptions) 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