From 7d6c2c10be4eb5bf7250de4bb0ea447302464d05 Mon Sep 17 00:00:00 2001 From: Suraj Dhamecha <48670070+suraj-qlogic@users.noreply.github.com> Date: Fri, 27 Mar 2020 23:54:31 +0530 Subject: [PATCH] fix: add support for updating an individual field with pojo in all update method (#136) --- .../cloud/firestore/DocumentSnapshot.java | 5 +++- .../firestore/DocumentReferenceTest.java | 23 +++++++++++++++++- .../cloud/firestore/LocalFirestoreHelper.java | 24 ++++++++++++++++--- .../cloud/firestore/it/ITSystemTest.java | 6 +++++ 4 files changed, 53 insertions(+), 5 deletions(-) diff --git a/google-cloud-firestore/src/main/java/com/google/cloud/firestore/DocumentSnapshot.java b/google-cloud-firestore/src/main/java/com/google/cloud/firestore/DocumentSnapshot.java index 957759936..9133797a0 100644 --- a/google-cloud-firestore/src/main/java/com/google/cloud/firestore/DocumentSnapshot.java +++ b/google-cloud-firestore/src/main/java/com/google/cloud/firestore/DocumentSnapshot.java @@ -86,7 +86,10 @@ static DocumentSnapshot fromObject( Map fields = new HashMap<>(); for (Map.Entry entry : values.entrySet()) { Value encodedValue = - UserDataConverter.encodeValue(FieldPath.of(entry.getKey()), entry.getValue(), options); + UserDataConverter.encodeValue( + FieldPath.of(entry.getKey()), + CustomClassMapper.convertToPlainJavaTypes(entry.getValue()), + options); if (encodedValue != null) { fields.put(entry.getKey(), encodedValue); } diff --git a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/DocumentReferenceTest.java b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/DocumentReferenceTest.java index f3c7f7674..7110c270f 100644 --- a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/DocumentReferenceTest.java +++ b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/DocumentReferenceTest.java @@ -35,6 +35,8 @@ import static com.google.cloud.firestore.LocalFirestoreHelper.SINGLE_FIELD_PROTO; import static com.google.cloud.firestore.LocalFirestoreHelper.SINGLE_WRITE_COMMIT_RESPONSE; import static com.google.cloud.firestore.LocalFirestoreHelper.TIMESTAMP; +import static com.google.cloud.firestore.LocalFirestoreHelper.UPDATED_POJO; +import static com.google.cloud.firestore.LocalFirestoreHelper.UPDATED_POJO_PROTO; import static com.google.cloud.firestore.LocalFirestoreHelper.UPDATE_PRECONDITION; import static com.google.cloud.firestore.LocalFirestoreHelper.arrayRemove; import static com.google.cloud.firestore.LocalFirestoreHelper.arrayUnion; @@ -82,6 +84,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.concurrent.ExecutionException; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -255,6 +258,7 @@ public void deserializeBasicTypes() throws Exception { assertEquals(Timestamp.ofTimeSecondsAndNanos(5, 6), snapshot.getReadTime()); assertEquals(get(), getAllCapture.getValue()); + assertEquals("bar", ((Map) snapshot.get("model")).get("foo")); } @Test @@ -839,7 +843,8 @@ public void extractFieldMaskFromMerge() throws Exception { "second.nullValue", "second.objectValue.foo", "second.timestampValue", - "second.trueValue"); + "second.trueValue", + "second.model.foo"); CommitRequest expectedCommit = commit(set(nestedUpdate, updateMask)); assertCommitEquals(expectedCommit, commitCapture.getValue()); @@ -1028,4 +1033,20 @@ public void updateDocumentWithPreconditions() throws Exception { assertCommitEquals(expectedCommit, request); } } + + @Test + public void updateIndividualPojo() throws ExecutionException, InterruptedException { + doReturn(SINGLE_WRITE_COMMIT_RESPONSE) + .when(firestoreMock) + .sendRequest( + commitCapture.capture(), Matchers.>any()); + documentReference.update(UPDATED_POJO); + documentReference.update(UPDATED_POJO).get(); + CommitRequest expectedCommit = + commit(update(UPDATED_POJO_PROTO, Collections.singletonList("model"))); + + for (CommitRequest request : commitCapture.getAllValues()) { + assertCommitEquals(expectedCommit, request); + } + } } diff --git a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/LocalFirestoreHelper.java b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/LocalFirestoreHelper.java index 0aff92980..41be685cf 100644 --- a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/LocalFirestoreHelper.java +++ b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/LocalFirestoreHelper.java @@ -93,6 +93,7 @@ public final class LocalFirestoreHelper { public static final Map UPDATED_FIELD_MAP; public static final Map UPDATED_FIELD_PROTO; public static final Map UPDATED_SINGLE_FIELD_PROTO; + public static final Map UPDATED_POJO_PROTO; public static final Map SINGLE_FLOAT_MAP; public static final Map SINGLE_FLOAT_PROTO; @@ -114,6 +115,8 @@ public final class LocalFirestoreHelper { public static final ApiFuture FIELD_TRANSFORM_COMMIT_RESPONSE; + public static final Map UPDATED_POJO; + public static final Date DATE; public static final Timestamp TIMESTAMP; public static final GeoPoint GEO_POINT; @@ -660,6 +663,7 @@ public static class AllSupportedTypes { public String nullValue = null; public Blob bytesValue = BLOB; public GeoPoint geoPointValue = GEO_POINT; + public Map model = ImmutableMap.of("foo", (Object) SINGLE_FIELD_OBJECT.foo); @Override public boolean equals(Object o) { @@ -684,7 +688,8 @@ public boolean equals(Object o) { && Objects.equals(arrayValue, that.arrayValue) && Objects.equals(nullValue, that.nullValue) && Objects.equals(bytesValue, that.bytesValue) - && Objects.equals(geoPointValue, that.geoPointValue); + && Objects.equals(geoPointValue, that.geoPointValue) + && Objects.equals(model, that.model); } } @@ -717,6 +722,14 @@ public boolean equals(Object o) { SINGLE_FIELD_MAP = map("foo", (Object) "bar"); SINGLE_FIELD_OBJECT = new SingleField(); SINGLE_FIELD_PROTO = map("foo", Value.newBuilder().setStringValue("bar").build()); + UPDATED_POJO_PROTO = + map( + "model", + Value.newBuilder() + .setMapValue( + MapValue.newBuilder() + .putFields("foo", Value.newBuilder().setStringValue("foobar").build())) + .build()); SINGLE_FIELD_SNAPSHOT = new DocumentSnapshot( null, @@ -777,7 +790,7 @@ public boolean equals(Object o) { ALL_SUPPORTED_TYPES_MAP.put("nullValue", null); ALL_SUPPORTED_TYPES_MAP.put("bytesValue", BLOB); ALL_SUPPORTED_TYPES_MAP.put("geoPointValue", GEO_POINT); - + ALL_SUPPORTED_TYPES_MAP.put("model", map("foo", SINGLE_FIELD_OBJECT.foo)); ALL_SUPPORTED_TYPES_PROTO = ImmutableMap.builder() .put("foo", Value.newBuilder().setStringValue("bar").build()) @@ -823,9 +836,13 @@ public boolean equals(Object o) { .setGeoPointValue( LatLng.newBuilder().setLatitude(50.1430847).setLongitude(-122.9477780)) .build()) + .put( + "model", + Value.newBuilder() + .setMapValue(MapValue.newBuilder().putAllFields(SINGLE_FIELD_PROTO)) + .build()) .build(); ALL_SUPPORTED_TYPES_OBJECT = new AllSupportedTypes(); - SINGLE_WRITE_COMMIT_RESPONSE = commitResponse(/* adds= */ 1, /* deletes= */ 0); SINGLE_DELETE_COMMIT_RESPONSE = commitResponse(/* adds= */ 0, /* deletes= */ 1); SINGLE_CREATE_COMMIT_REQUEST = commit(create(SINGLE_FIELD_PROTO)); @@ -837,6 +854,7 @@ public boolean equals(Object o) { CREATE_PRECONDITION = Precondition.newBuilder().setExists(false).build(); UPDATE_PRECONDITION = Precondition.newBuilder().setExists(true).build(); + UPDATED_POJO = map("model", (Object) UPDATE_SINGLE_FIELD_OBJECT); } public static String autoId() { diff --git a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITSystemTest.java b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITSystemTest.java index a0fd2bef9..0565d05e3 100644 --- a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITSystemTest.java +++ b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITSystemTest.java @@ -16,6 +16,7 @@ package com.google.cloud.firestore.it; +import static com.google.cloud.firestore.LocalFirestoreHelper.UPDATE_SINGLE_FIELD_OBJECT; import static com.google.cloud.firestore.LocalFirestoreHelper.map; import static java.util.Arrays.asList; import static org.junit.Assert.assertEquals; @@ -55,6 +56,7 @@ import com.google.cloud.firestore.WriteBatch; import com.google.cloud.firestore.WriteResult; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -246,6 +248,10 @@ public void updateDocument() throws Exception { documentSnapshot = randomDoc.get().get(); expectedResult.foo = "updated"; assertEquals(expectedResult, documentSnapshot.toObject(AllSupportedTypes.class)); + expectedResult.model = ImmutableMap.of("foo", (Object) UPDATE_SINGLE_FIELD_OBJECT.foo); + randomDoc.update("model", UPDATE_SINGLE_FIELD_OBJECT).get(); + documentSnapshot = randomDoc.get().get(); + assertEquals(expectedResult, documentSnapshot.toObject(AllSupportedTypes.class)); } @Test(expected = ExecutionException.class)