From 02e68a319986f864e3ad52f58d9a2d5f993dee4c Mon Sep 17 00:00:00 2001 From: Ajay Date: Wed, 5 Sep 2018 11:35:47 -0400 Subject: [PATCH] Fix #3637 Bigtable RowMutation should allow passing of a Mutation --- .../bigtable/data/v2/models/RowMutation.java | 14 ++++++++++ .../data/v2/models/RowMutationTest.java | 26 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/RowMutation.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/RowMutation.java index 5bd88e55332a..814e2045e504 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/RowMutation.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/RowMutation.java @@ -43,6 +43,12 @@ private RowMutation(String tableId, ByteString key) { this.mutation = Mutation.create(); } + private RowMutation(String tableId, ByteString key, Mutation mutation) { + this.tableId = tableId; + this.key = key; + this.mutation = mutation; + } + public static RowMutation create(@Nonnull String tableId, @Nonnull String key) { return create(tableId, ByteString.copyFromUtf8(key)); } @@ -51,6 +57,14 @@ public static RowMutation create(@Nonnull String tableId, @Nonnull ByteString ke return new RowMutation(tableId, key); } + public static RowMutation create(@Nonnull String tableId, @Nonnull String key, Mutation mutation) { + return create(tableId, ByteString.copyFromUtf8(key), mutation); + } + + public static RowMutation create(@Nonnull String tableId, @Nonnull ByteString key, Mutation mutation) { + return new RowMutation(tableId, key, mutation); + } + @Override public RowMutation setCell( @Nonnull String familyName, @Nonnull String qualifier, @Nonnull String value) { diff --git a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/models/RowMutationTest.java b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/models/RowMutationTest.java index 080459de83ba..b82b35e52124 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/models/RowMutationTest.java +++ b/google-cloud-clients/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/models/RowMutationTest.java @@ -90,6 +90,32 @@ public void toBulkProtoTest() { .isIn(timestampRange); } + @Test + public void toProtoTestWithProvidedMutation() { + long timestampMin = System.currentTimeMillis() * 1_000; + + Mutation mutation = Mutation.create().setCell("fake-family", "fake-qualifier", "fake-value"); + RowMutation rowMutation = RowMutation.create("fake-table", "fake-key", mutation); + + MutateRowsRequest actualRowMutation = rowMutation.toBulkProto(REQUEST_CONTEXT); + + com.google.common.collect.Range timestampRange = + com.google.common.collect.Range.closed(timestampMin, System.currentTimeMillis() * 1_000); + + assertThat(actualRowMutation.getTableName()) + .isEqualTo( + TableName.of(INSTANCE_NAME.getProject(), INSTANCE_NAME.getInstance(), "fake-table") + .toString()); + assertThat(actualRowMutation.getAppProfileId()).isEqualTo(APP_PROFILE_ID); + assertThat(actualRowMutation.getEntriesList()).hasSize(1); + assertThat(actualRowMutation.getEntries(0).getMutationsList()).hasSize(1); + assertThat(actualRowMutation.getEntries(0).getMutations(0).getSetCell().getValue()) + .isEqualTo(ByteString.copyFromUtf8("fake-value")); + + assertThat(actualRowMutation.getEntries(0).getMutations(0).getSetCell().getTimestampMicros()) + .isIn(timestampRange); + } + @Test public void serializationTest() throws IOException, ClassNotFoundException { RowMutation expected =