Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@
import com.google.firestore.v1.CommitRequest;
import com.google.firestore.v1.CommitResponse;
import com.google.firestore.v1.Value;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -48,15 +55,6 @@
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RunWith(MockitoJUnitRunner.class)
public class RecordDocumentReferenceTest {

Expand Down Expand Up @@ -84,7 +82,8 @@ public void serializeBasicTypes() throws Exception {
doReturn(SINGLE_WRITE_COMMIT_RESPONSE)
.when(firestoreMock)
.sendRequest(
commitCapture.capture(), ArgumentMatchers.<UnaryCallable<CommitRequest, CommitResponse>>any());
commitCapture.capture(),
ArgumentMatchers.<UnaryCallable<CommitRequest, CommitResponse>>any());

documentReference.set(ALL_SUPPORTED_TYPES_OBJECT).get();

Expand All @@ -99,19 +98,22 @@ public void doesNotSerializeAdvancedNumberTypes() {
InvalidRecord record = new InvalidRecord(new BigInteger("0"), null, null);
expectedErrorMessages.put(
record,
"Could not serialize object. Numbers of type BigInteger are not supported, please use an int, long, float, double or BigDecimal (found in field 'bigIntegerValue')");
"Could not serialize object. Numbers of type BigInteger are not supported, please use an"
+ " int, long, float, double or BigDecimal (found in field 'bigIntegerValue')");

record = new InvalidRecord(null, (byte) 0, null);
expectedErrorMessages.put(
record,
"Could not serialize object. Numbers of type Byte are not supported, please use an int, long, float, double or BigDecimal (found in field 'byteValue')");
"Could not serialize object. Numbers of type Byte are not supported, please use an int,"
+ " long, float, double or BigDecimal (found in field 'byteValue')");

record = new InvalidRecord(null, null, (short) 0);
expectedErrorMessages.put(
record,
"Could not serialize object. Numbers of type Short are not supported, please use an int, long, float, double or BigDecimal (found in field 'shortValue')");
"Could not serialize object. Numbers of type Short are not supported, please use an int,"
+ " long, float, double or BigDecimal (found in field 'shortValue')");

for (Map.Entry<InvalidRecord, String> testCase : expectedErrorMessages.entrySet()) {
for (Map.Entry<InvalidRecord, String> testCase : expectedErrorMessages.entrySet()) {
try {
documentReference.set(testCase.getKey());
fail();
Expand All @@ -138,14 +140,15 @@ public void doesNotDeserializeAdvancedNumberTypes() throws Exception {
streamObserverCapture.capture(),
ArgumentMatchers.<ServerStreamingCallable>any());

DocumentSnapshot snapshot = documentReference.get().get();
DocumentSnapshot snapshot = documentReference.get().get();
try {
snapshot.toObject(InvalidRecord.class);
fail();
} catch (RuntimeException e) {
assertEquals(
String.format(
"Could not deserialize object. Deserializing values to %s is not supported (found in field '%s')",
"Could not deserialize object. Deserializing values to %s is not supported (found"
+ " in field '%s')",
typeName, fieldName),
e.getMessage());
}
Expand All @@ -157,7 +160,8 @@ public void createDocument() throws Exception {
doReturn(SINGLE_WRITE_COMMIT_RESPONSE)
.when(firestoreMock)
.sendRequest(
commitCapture.capture(), ArgumentMatchers.<UnaryCallable<CommitRequest, CommitResponse>>any());
commitCapture.capture(),
ArgumentMatchers.<UnaryCallable<CommitRequest, CommitResponse>>any());

documentReference.create(SINGLE_COMPONENT_OBJECT).get();

Expand All @@ -172,7 +176,8 @@ public void createWithServerTimestamp() throws Exception {
doReturn(SINGLE_WRITE_COMMIT_RESPONSE)
.when(firestoreMock)
.sendRequest(
commitCapture.capture(), ArgumentMatchers.<UnaryCallable<CommitRequest, CommitResponse>>any());
commitCapture.capture(),
ArgumentMatchers.<UnaryCallable<CommitRequest, CommitResponse>>any());

documentReference.create(SERVER_TIMESTAMP_OBJECT).get();

Expand All @@ -181,7 +186,7 @@ public void createWithServerTimestamp() throws Exception {
create(Collections.emptyMap()),
transform("foo", serverTimestamp(), "inner.bar", serverTimestamp()));

List<CommitRequest> commitRequests = commitCapture.getAllValues();
List<CommitRequest> commitRequests = commitCapture.getAllValues();
assertCommitEquals(create, commitRequests.get(0));
}

Expand All @@ -190,7 +195,8 @@ public void setWithServerTimestamp() throws Exception {
doReturn(FIELD_TRANSFORM_COMMIT_RESPONSE)
.when(firestoreMock)
.sendRequest(
commitCapture.capture(), ArgumentMatchers.<UnaryCallable<CommitRequest, CommitResponse>>any());
commitCapture.capture(),
ArgumentMatchers.<UnaryCallable<CommitRequest, CommitResponse>>any());

documentReference.set(SERVER_TIMESTAMP_OBJECT).get();

Expand All @@ -199,7 +205,7 @@ public void setWithServerTimestamp() throws Exception {
set(SERVER_TIMESTAMP_PROTO),
transform("foo", serverTimestamp(), "inner.bar", serverTimestamp()));

List<CommitRequest> commitRequests = commitCapture.getAllValues();
List<CommitRequest> commitRequests = commitCapture.getAllValues();
assertCommitEquals(set, commitRequests.get(0));
}

Expand All @@ -208,18 +214,17 @@ public void mergeWithServerTimestamps() throws Exception {
doReturn(SINGLE_WRITE_COMMIT_RESPONSE)
.when(firestoreMock)
.sendRequest(
commitCapture.capture(), ArgumentMatchers.<UnaryCallable<CommitRequest, CommitResponse>>any());
commitCapture.capture(),
ArgumentMatchers.<UnaryCallable<CommitRequest, CommitResponse>>any());

documentReference
.set(SERVER_TIMESTAMP_OBJECT, SetOptions.mergeFields("inner.bar"))
.get();
documentReference.set(SERVER_TIMESTAMP_OBJECT, SetOptions.mergeFields("inner.bar")).get();

CommitRequest set =
CommitRequest set =
commit(
set(SERVER_TIMESTAMP_PROTO, new ArrayList<>()),
transform("inner.bar", serverTimestamp()));

List<CommitRequest> commitRequests = commitCapture.getAllValues();
List<CommitRequest> commitRequests = commitCapture.getAllValues();
assertCommitEquals(set, commitRequests.get(0));
}

Expand All @@ -228,7 +233,8 @@ public void setDocumentWithMerge() throws Exception {
doReturn(SINGLE_WRITE_COMMIT_RESPONSE)
.when(firestoreMock)
.sendRequest(
commitCapture.capture(), ArgumentMatchers.<UnaryCallable<CommitRequest, CommitResponse>>any());
commitCapture.capture(),
ArgumentMatchers.<UnaryCallable<CommitRequest, CommitResponse>>any());

documentReference.set(SINGLE_COMPONENT_OBJECT, SetOptions.merge()).get();
documentReference.set(ALL_SUPPORTED_TYPES_OBJECT, SetOptions.mergeFields("foo")).get();
Expand All @@ -241,7 +247,7 @@ public void setDocumentWithMerge() throws Exception {
SetOptions.mergeFieldPaths(Arrays.asList(FieldPath.of("foo"))))
.get();

CommitRequest expectedCommit = commit(set(SINGLE_COMPONENT_PROTO, Arrays.asList("foo")));
CommitRequest expectedCommit = commit(set(SINGLE_COMPONENT_PROTO, Arrays.asList("foo")));

for (int i = 0; i < 4; ++i) {
assertCommitEquals(expectedCommit, commitCapture.getAllValues().get(i));
Expand All @@ -253,7 +259,8 @@ public void setDocumentWithNestedMerge() throws Exception {
doReturn(SINGLE_WRITE_COMMIT_RESPONSE)
.when(firestoreMock)
.sendRequest(
commitCapture.capture(), ArgumentMatchers.<UnaryCallable<CommitRequest, CommitResponse>>any());
commitCapture.capture(),
ArgumentMatchers.<UnaryCallable<CommitRequest, CommitResponse>>any());

documentReference.set(NESTED_RECORD_OBJECT, SetOptions.mergeFields("first.foo")).get();
documentReference
Expand Down Expand Up @@ -282,7 +289,8 @@ public void setMultipleFieldsWithMerge() throws Exception {
doReturn(SINGLE_WRITE_COMMIT_RESPONSE)
.when(firestoreMock)
.sendRequest(
commitCapture.capture(), ArgumentMatchers.<UnaryCallable<CommitRequest, CommitResponse>>any());
commitCapture.capture(),
ArgumentMatchers.<UnaryCallable<CommitRequest, CommitResponse>>any());

documentReference
.set(
Expand Down Expand Up @@ -310,7 +318,8 @@ public void setNestedMapWithMerge() throws Exception {
doReturn(SINGLE_WRITE_COMMIT_RESPONSE)
.when(firestoreMock)
.sendRequest(
commitCapture.capture(), ArgumentMatchers.<UnaryCallable<CommitRequest, CommitResponse>>any());
commitCapture.capture(),
ArgumentMatchers.<UnaryCallable<CommitRequest, CommitResponse>>any());

documentReference.set(NESTED_RECORD_OBJECT, SetOptions.mergeFields("first", "second")).get();

Expand All @@ -330,7 +339,8 @@ public void extractFieldMaskFromMerge() throws Exception {
doReturn(SINGLE_WRITE_COMMIT_RESPONSE)
.when(firestoreMock)
.sendRequest(
commitCapture.capture(), ArgumentMatchers.<UnaryCallable<CommitRequest, CommitResponse>>any());
commitCapture.capture(),
ArgumentMatchers.<UnaryCallable<CommitRequest, CommitResponse>>any());

documentReference.set(NESTED_RECORD_OBJECT, SetOptions.merge()).get();

Expand All @@ -341,24 +351,25 @@ public void extractFieldMaskFromMerge() throws Exception {
nestedProto.getMapValueBuilder().putAllFields(ALL_SUPPORTED_TYPES_PROTO);
nestedUpdate.put("second", nestedProto.build());

List<String> updateMask = Arrays.asList(
"first.foo",
"second.arrayValue",
"second.bytesValue",
"second.dateValue",
"second.doubleValue",
"second.falseValue",
"second.foo",
"second.geoPointValue",
"second.infValue",
"second.longValue",
"second.nanValue",
"second.negInfValue",
"second.nullValue",
"second.objectValue.foo",
"second.timestampValue",
"second.trueValue",
"second.model.foo");
List<String> updateMask =
Arrays.asList(
"first.foo",
"second.arrayValue",
"second.bytesValue",
"second.dateValue",
"second.doubleValue",
"second.falseValue",
"second.foo",
"second.geoPointValue",
"second.infValue",
"second.longValue",
"second.nanValue",
"second.negInfValue",
"second.nullValue",
"second.objectValue.foo",
"second.timestampValue",
"second.trueValue",
"second.model.foo");

CommitRequest expectedCommit = commit(set(nestedUpdate, updateMask));
assertCommitEquals(expectedCommit, commitCapture.getValue());
Expand All @@ -369,9 +380,12 @@ public void setNestedRecordWithPojoMapWithMerge() throws Exception {
doReturn(SINGLE_WRITE_COMMIT_RESPONSE)
.when(firestoreMock)
.sendRequest(
commitCapture.capture(), ArgumentMatchers.<UnaryCallable<CommitRequest, CommitResponse>>any());
commitCapture.capture(),
ArgumentMatchers.<UnaryCallable<CommitRequest, CommitResponse>>any());

documentReference.set(NESTED_RECORD_WITH_POJO_OBJECT, SetOptions.mergeFields("first", "second")).get();
documentReference
.set(NESTED_RECORD_WITH_POJO_OBJECT, SetOptions.mergeFields("first", "second"))
.get();

Map<String, Value> nestedUpdate = new HashMap<>();
Value.Builder nestedProto = Value.newBuilder();
Expand All @@ -384,15 +398,17 @@ public void setNestedRecordWithPojoMapWithMerge() throws Exception {
assertCommitEquals(expectedCommit, commitCapture.getValue());
}

@Test
@Test
public void setNestedPojoWithRecordMapWithMerge() throws Exception {
doReturn(SINGLE_WRITE_COMMIT_RESPONSE)
.when(firestoreMock)
.sendRequest(
commitCapture.capture(),
ArgumentMatchers.<UnaryCallable<CommitRequest, CommitResponse>>any());

documentReference.set(NESTED_POJO_WITH_RECORD_OBJECT, SetOptions.mergeFields("first", "second")).get();
documentReference
.set(NESTED_POJO_WITH_RECORD_OBJECT, SetOptions.mergeFields("first", "second"))
.get();

Map<String, Value> nestedUpdate = new HashMap<>();
Value.Builder nestedProto = Value.newBuilder();
Expand Down
Loading
Loading