Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add support for updating an individual field with pojo in all update method #136

Merged
merged 3 commits into from Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -86,7 +86,10 @@ static DocumentSnapshot fromObject(
Map<String, Value> fields = new HashMap<>();
for (Map.Entry<String, Object> 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);
}
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -255,6 +258,7 @@ public void deserializeBasicTypes() throws Exception {
assertEquals(Timestamp.ofTimeSecondsAndNanos(5, 6), snapshot.getReadTime());

assertEquals(get(), getAllCapture.getValue());
assertEquals("foo", ((Map<String, Object>) snapshot.get("model")).get("stringValue"));
}

@Test
Expand Down Expand Up @@ -839,7 +843,8 @@ public void extractFieldMaskFromMerge() throws Exception {
"second.nullValue",
"second.objectValue.foo",
"second.timestampValue",
"second.trueValue");
"second.trueValue",
"second.model.stringValue");

CommitRequest expectedCommit = commit(set(nestedUpdate, updateMask));
assertCommitEquals(expectedCommit, commitCapture.getValue());
Expand Down Expand Up @@ -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.<UnaryCallable<CommitRequest, CommitResponse>>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);
}
}
}
Expand Up @@ -87,12 +87,14 @@ public final class LocalFirestoreHelper {
public static final Map<String, Object> SINGLE_FIELD_MAP;
public static final SingleField SINGLE_FIELD_OBJECT;
public static final Map<String, Value> SINGLE_FIELD_PROTO;
public static final Map<String, Value> SINGLE_POJO_PROTO;
public static final DocumentSnapshot SINGLE_FIELD_SNAPSHOT;
public static final Value SINGLE_FIELD_VALUE;
public static final SingleField UPDATE_SINGLE_FIELD_OBJECT;
public static final Map<String, Object> UPDATED_FIELD_MAP;
public static final Map<String, Value> UPDATED_FIELD_PROTO;
public static final Map<String, Value> UPDATED_SINGLE_FIELD_PROTO;
public static final Map<String, Value> UPDATED_POJO_PROTO;

public static final Map<String, Float> SINGLE_FLOAT_MAP;
public static final Map<String, Value> SINGLE_FLOAT_PROTO;
Expand All @@ -114,6 +116,11 @@ public final class LocalFirestoreHelper {

public static final ApiFuture<CommitResponse> FIELD_TRANSFORM_COMMIT_RESPONSE;

public static final Map<String, Object> SINGLE_POJO;
public static final Map<String, Object> UPDATED_POJO;
public static final FooModel MODEL_OBJECT;
public static final FooModel UPDATE_MODEL_OBJECT;

public static final Date DATE;
public static final Timestamp TIMESTAMP;
public static final GeoPoint GEO_POINT;
Expand Down Expand Up @@ -191,6 +198,31 @@ public void setShortValue(@Nullable Short shortValue) {
}
}

public static class FooModel {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind re-using the SingleField example Pojo that exists in this class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

private @Nullable String stringValue;

@Nullable
public String getStringValue() {
return stringValue;
}

public void setStringValue(@Nullable String stringValue) {
this.stringValue = stringValue;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
FooModel that = (FooModel) o;
return ((this.stringValue != null) && this.stringValue.equals(that.stringValue));
}
}

public static <K, V> Map<K, V> map(K key, V value, Object... moreKeysAndValues) {
Map<K, V> map = new HashMap<>();
map.put(key, value);
Expand Down Expand Up @@ -660,6 +692,8 @@ public static class AllSupportedTypes {
public String nullValue = null;
public Blob bytesValue = BLOB;
public GeoPoint geoPointValue = GEO_POINT;
public Map<String, Object> model =
ImmutableMap.of("stringValue", (Object) MODEL_OBJECT.getStringValue());

@Override
public boolean equals(Object o) {
Expand All @@ -684,7 +718,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);
}
}

Expand All @@ -706,6 +741,8 @@ public boolean equals(Object o) {
SINGLE_FLOAT_MAP = map("float", 0.1F);
SINGLE_FLOAT_PROTO = map("float", Value.newBuilder().setDoubleValue((Float) 0.1F).build());

MODEL_OBJECT = new FooModel();
MODEL_OBJECT.setStringValue("foo");
DATABASE_NAME = "projects/test-project/databases/(default)";
COLLECTION_ID = "coll";
DOCUMENT_PATH = "coll/doc";
Expand All @@ -717,6 +754,20 @@ 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());
SINGLE_POJO_PROTO = map("stringValue", Value.newBuilder().setStringValue("foo").build());
UPDATED_POJO_PROTO =
map(
"model",
Value.newBuilder()
.setMapValue(
MapValue.newBuilder()
.putFields(
"stringValue", Value.newBuilder().setStringValue("foobar").build()))
.build());
SINGLE_POJO = map("model", (Object) MODEL_OBJECT);
UPDATE_MODEL_OBJECT = new FooModel();
UPDATE_MODEL_OBJECT.setStringValue("foobar");
UPDATED_POJO = map("model", (Object) UPDATE_MODEL_OBJECT);
SINGLE_FIELD_SNAPSHOT =
new DocumentSnapshot(
null,
Expand Down Expand Up @@ -777,7 +828,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("stringValue", MODEL_OBJECT.getStringValue()));
ALL_SUPPORTED_TYPES_PROTO =
ImmutableMap.<String, Value>builder()
.put("foo", Value.newBuilder().setStringValue("bar").build())
Expand Down Expand Up @@ -823,9 +874,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_POJO_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));
Expand Down
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.firestore.it;

import static com.google.cloud.firestore.LocalFirestoreHelper.UPDATE_MODEL_OBJECT;
import static com.google.cloud.firestore.LocalFirestoreHelper.map;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -247,6 +249,11 @@ public void updateDocument() throws Exception {
documentSnapshot = randomDoc.get().get();
expectedResult.foo = "updated";
assertEquals(expectedResult, documentSnapshot.toObject(AllSupportedTypes.class));
expectedResult.model =
ImmutableMap.of("stringValue", (Object) UPDATE_MODEL_OBJECT.getStringValue());
randomDoc.update("model", UPDATE_MODEL_OBJECT).get();
documentSnapshot = randomDoc.get().get();
assertEquals(expectedResult, documentSnapshot.toObject(AllSupportedTypes.class));
}

@Test(expected = ExecutionException.class)
Expand Down