From fe5dab59a4a7dd5565ac6e8066be3e815d2e251c Mon Sep 17 00:00:00 2001 From: Igor Bernstein Date: Thu, 16 Aug 2018 19:28:02 -0400 Subject: [PATCH 1/3] Bigtable: minor cleanup of futures --- .../admin/v2/BigtableTableAdminClient.java | 63 ++++++++++--------- .../data/v2/models/BulkMutationBatcher.java | 4 +- .../v2/stub/CheckAndMutateRowCallable.java | 4 +- .../data/v2/stub/MutateRowCallable.java | 4 +- .../v2/stub/ReadModifyWriteRowCallable.java | 4 +- .../data/v2/stub/SampleRowKeysCallable.java | 4 +- .../mutaterows/MutateRowsAttemptCallable.java | 4 +- 7 files changed, 50 insertions(+), 37 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClient.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClient.java index a0d2f911aa7e..b3d291766d32 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClient.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClient.java @@ -15,6 +15,7 @@ */ package com.google.cloud.bigtable.admin.v2; +import com.google.common.util.concurrent.MoreExecutors; import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -144,9 +145,7 @@ public void close() { * @see CreateTableRequest for createTable configurations */ public Table createTable(CreateTableRequest request) { - com.google.bigtable.admin.v2.Table table = - this.stub.createTableCallable().call(request.toProto(instanceName)); - return Table.fromProto(table); + return awaitFuture(createTableAsync(request)); } /** @@ -200,9 +199,7 @@ public ApiFuture createTableAsync(CreateTableRequest request) { * @see ModifyColumnFamiliesRequest for modifyFamily options */ public Table modifyFamilies(ModifyColumnFamiliesRequest request) { - com.google.bigtable.admin.v2.Table table = - this.stub.modifyColumnFamiliesCallable().call(request.toProto(instanceName)); - return Table.fromProto(table); + return awaitFuture(modifyFamiliesAsync(request)); } /** @@ -252,7 +249,7 @@ public ApiFuture
modifyFamiliesAsync(ModifyColumnFamiliesRequest request) * } */ public void deleteTable(String tableId) { - this.stub.deleteTableCallable().call(composeDeleteTableRequest(tableId)); + awaitFuture(deleteTableAsync(tableId)); } /** @@ -283,9 +280,7 @@ public ApiFuture deleteTableAsync(String tableId) { * } */ public Table getTable(String tableId) { - com.google.bigtable.admin.v2.Table table = - this.stub.getTableCallable().call(composeGetTableRequest(tableId)); - return Table.fromProto(table); + return awaitFuture(getTableAsync(tableId)); } /** @@ -316,8 +311,7 @@ public ApiFuture
getTableAsync(String tableId) { * } */ public List listTables() { - ListTablesResponse listResp = this.stub.listTablesCallable().call(composeListTableRequest()); - return convertToTableNames(listResp); + return awaitFuture(listTablesAsync()); } /** @@ -342,7 +336,8 @@ public ApiFuture> listTablesAsync() { public List apply(ListTablesResponse input) { return convertToTableNames(input); } - }); + }, + MoreExecutors.directExecutor()); } /** @@ -357,7 +352,7 @@ public List apply(ListTablesResponse input) { * } */ public void dropRowRange(String tableId, String rowKeyPrefix) { - dropRowRange(tableId, ByteString.copyFromUtf8(rowKeyPrefix)); + awaitFuture(dropRowRangeAsync(tableId, rowKeyPrefix)); } /** @@ -387,7 +382,7 @@ public ApiFuture dropRowRangeAsync(String tableId, String rowKeyPrefix) { * } */ public void dropRowRange(String tableId, ByteString rowKeyPrefix) { - this.stub.dropRowRangeCallable().call(composeDropRowRangeRequest(tableId, rowKeyPrefix, false)); + awaitFuture(dropRowRangeAsync(tableId, rowKeyPrefix)); } /** @@ -420,7 +415,7 @@ public ApiFuture dropRowRangeAsync(String tableId, ByteString rowKeyPrefix * } */ public void dropAllRows(String tableId) { - this.stub.dropRowRangeCallable().call(composeDropRowRangeRequest(tableId, null, true)); + awaitFuture(dropAllRowsAsync(tableId)); } /** @@ -454,10 +449,7 @@ public ApiFuture dropAllRowsAsync(String tableId) { * } */ public ConsistencyToken generateConsistencyToken(String tableId) { - return ConsistencyToken.fromProto( - this.stub - .generateConsistencyTokenCallable() - .call(composeGenerateConsistencyTokenRequest(tableId))); + return awaitFuture(generateConsistencyTokenAsync(tableId)); } /** @@ -485,7 +477,8 @@ public ApiFuture generateConsistencyTokenAsync(String tableId) public ConsistencyToken apply(GenerateConsistencyTokenResponse input) { return ConsistencyToken.fromProto(input); } - }); + }, + MoreExecutors.directExecutor()); } /** @@ -500,9 +493,7 @@ public ConsistencyToken apply(GenerateConsistencyTokenResponse input) { * } */ public boolean isConsistent(String tableId, ConsistencyToken token) { - return stub.checkConsistencyCallable() - .call(token.toProto(getTableName(tableId))) - .getConsistent(); + return awaitFuture(isConsistentAsync(tableId, token)); } /** @@ -527,7 +518,8 @@ public ApiFuture isConsistentAsync(String tableId, ConsistencyToken tok public Boolean apply(CheckConsistencyResponse input) { return input.getConsistent(); } - }); + }, + MoreExecutors.directExecutor()); } /** @@ -604,8 +596,7 @@ static List convertToTableNames(ListTablesResponse listTablesResponse /** * Helper method to transform ApiFuture to ApiFuture
*/ - @VisibleForTesting - static ApiFuture
transformToTableResponse( + private static ApiFuture
transformToTableResponse( ApiFuture future) { return ApiFutures.transform( future, @@ -614,14 +605,14 @@ static ApiFuture
transformToTableResponse( public Table apply(com.google.bigtable.admin.v2.Table table) { return Table.fromProto(table); } - }); + }, + MoreExecutors.directExecutor()); } /** * Helper method to transform ApiFuture to ApiFuture */ - @VisibleForTesting - static ApiFuture transformToVoid(ApiFuture future) { + private static ApiFuture transformToVoid(ApiFuture future) { return ApiFutures.transform( future, new ApiFunction() { @@ -629,6 +620,16 @@ static ApiFuture transformToVoid(ApiFuture future) { public Void apply(Empty empty) { return null; } - }); + }, + MoreExecutors.directExecutor()); + } + + private T awaitFuture(ApiFuture future) { + try { + return future.get(); + } catch(Throwable t) { + // TODO(igorbernstein2): figure out a better wrapper exception. + throw new RuntimeException(t); + } } } diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/BulkMutationBatcher.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/BulkMutationBatcher.java index 79b632478925..4321b60c4229 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/BulkMutationBatcher.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/BulkMutationBatcher.java @@ -22,6 +22,7 @@ import com.google.api.core.InternalApi; import com.google.api.gax.rpc.UnaryCallable; import com.google.common.base.Preconditions; +import com.google.common.util.concurrent.MoreExecutors; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicLong; import org.threeten.bp.Duration; @@ -110,7 +111,8 @@ public void onFailure(Throwable throwable) { public void onSuccess(Void aVoid) { onMutationComplete(true); } - }); + }, + MoreExecutors.directExecutor()); return future; } diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/CheckAndMutateRowCallable.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/CheckAndMutateRowCallable.java index 7bb639a5dcdf..549e10f44b95 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/CheckAndMutateRowCallable.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/CheckAndMutateRowCallable.java @@ -24,6 +24,7 @@ import com.google.bigtable.v2.CheckAndMutateRowResponse; import com.google.cloud.bigtable.data.v2.internal.RequestContext; import com.google.cloud.bigtable.data.v2.models.ConditionalRowMutation; +import com.google.common.util.concurrent.MoreExecutors; /** Simple wrapper for CheckAndMutateRow to wrap the request and response protobufs. */ class CheckAndMutateRowCallable extends UnaryCallable { @@ -49,6 +50,7 @@ public ApiFuture futureCall(ConditionalRowMutation request, ApiCallCont public Boolean apply(CheckAndMutateRowResponse checkAndMutateRowResponse) { return checkAndMutateRowResponse.getPredicateMatched(); } - }); + }, + MoreExecutors.directExecutor()); } } diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/MutateRowCallable.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/MutateRowCallable.java index a88efdeee5a2..36f47c2d1f92 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/MutateRowCallable.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/MutateRowCallable.java @@ -24,6 +24,7 @@ import com.google.bigtable.v2.MutateRowResponse; import com.google.cloud.bigtable.data.v2.internal.RequestContext; import com.google.cloud.bigtable.data.v2.models.RowMutation; +import com.google.common.util.concurrent.MoreExecutors; /** Simple wrapper for MutateRow to wrap the request and response protobufs. */ class MutateRowCallable extends UnaryCallable { @@ -49,6 +50,7 @@ public ApiFuture futureCall(RowMutation request, ApiCallContext context) { public Void apply(MutateRowResponse mutateRowResponse) { return null; } - }); + }, + MoreExecutors.directExecutor()); } } diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/ReadModifyWriteRowCallable.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/ReadModifyWriteRowCallable.java index 7ee1704bbd41..314465a3d170 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/ReadModifyWriteRowCallable.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/ReadModifyWriteRowCallable.java @@ -30,6 +30,7 @@ import com.google.cloud.bigtable.data.v2.models.Row; import com.google.cloud.bigtable.data.v2.models.RowCell; import com.google.common.collect.ImmutableList; +import com.google.common.util.concurrent.MoreExecutors; /** Simple wrapper for ReadModifyWriteRow to wrap the request and response protobufs. */ class ReadModifyWriteRowCallable extends UnaryCallable { @@ -55,7 +56,8 @@ public ApiFuture futureCall(ReadModifyWriteRow request, ApiCallContext cont public Row apply(ReadModifyWriteRowResponse readModifyWriteRowResponse) { return convertResponse(readModifyWriteRowResponse); } - }); + }, + MoreExecutors.directExecutor()); } private Row convertResponse(ReadModifyWriteRowResponse response) { diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/SampleRowKeysCallable.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/SampleRowKeysCallable.java index b3442668c115..554bf2bb08a8 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/SampleRowKeysCallable.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/SampleRowKeysCallable.java @@ -26,6 +26,7 @@ import com.google.cloud.bigtable.data.v2.internal.RequestContext; import com.google.cloud.bigtable.data.v2.models.KeyOffset; import com.google.common.collect.ImmutableList; +import com.google.common.util.concurrent.MoreExecutors; import java.util.List; /** Simple wrapper for SampleRowKeys to wrap the request and response protobufs. */ @@ -64,7 +65,8 @@ public ApiFuture> futureCall(String tableId, ApiCallContext cont public List apply(List rawResponse) { return convert(rawResponse); } - }); + }, + MoreExecutors.directExecutor()); } private static List convert(List rawResponse) { diff --git a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/mutaterows/MutateRowsAttemptCallable.java b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/mutaterows/MutateRowsAttemptCallable.java index 1023e9ad7d3b..fcebb9c8784e 100644 --- a/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/mutaterows/MutateRowsAttemptCallable.java +++ b/google-cloud-clients/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/mutaterows/MutateRowsAttemptCallable.java @@ -35,6 +35,7 @@ import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; +import com.google.common.util.concurrent.MoreExecutors; import com.google.rpc.Code; import java.util.List; import java.util.Set; @@ -188,7 +189,8 @@ public Void call() { // Inspect the results and either propagate the success, or prepare to retry the failed // mutations - ApiFuture transformed = ApiFutures.transform(catching, attemptSuccessfulCallback); + ApiFuture transformed = ApiFutures.transform(catching, attemptSuccessfulCallback, + MoreExecutors.directExecutor()); // Notify the parent of the attempt externalFuture.setAttemptFuture(transformed); From 7467d6dee41cbf18ba22d8b6cc053f77af85faf8 Mon Sep 17 00:00:00 2001 From: Igor Bernstein Date: Fri, 17 Aug 2018 12:22:12 -0400 Subject: [PATCH 2/3] add equals & hashCode fix tests fix list tables --- .../admin/v2/BigtableTableAdminClient.java | 15 +- .../admin/v2/models/ClusterState.java | 19 + .../admin/v2/models/ColumnFamily.java | 19 + .../admin/v2/models/ConsistencyToken.java | 18 + .../admin/v2/models/CreateTableRequest.java | 19 + .../bigtable/admin/v2/models/GCRules.java | 87 +++ .../cloud/bigtable/admin/v2/models/Table.java | 20 + .../v2/BigtableTableAdminClientTest.java | 612 +++++++++++++----- 8 files changed, 632 insertions(+), 177 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClient.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClient.java index b3d291766d32..f5a2c51ccada 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClient.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClient.java @@ -15,6 +15,7 @@ */ package com.google.cloud.bigtable.admin.v2; +import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPagedResponse; import com.google.common.util.concurrent.MoreExecutors; import java.io.IOException; import java.util.ArrayList; @@ -326,15 +327,15 @@ public List listTables() { * } */ public ApiFuture> listTablesAsync() { - ApiFuture listResp = - this.stub.listTablesCallable().futureCall(composeListTableRequest()); + ApiFuture listResp = + this.stub.listTablesPagedCallable().futureCall(composeListTableRequest()); return ApiFutures.transform( listResp, - new ApiFunction>() { + new ApiFunction>() { @Override - public List apply(ListTablesResponse input) { - return convertToTableNames(input); + public List apply(ListTablesPagedResponse input) { + return convertToTableNames(input.iterateAll()); } }, MoreExecutors.directExecutor()); @@ -583,10 +584,10 @@ GenerateConsistencyTokenRequest composeGenerateConsistencyTokenRequest(String ta * Helper method to convert ListTablesResponse to List */ @VisibleForTesting - static List convertToTableNames(ListTablesResponse listTablesResponse) { + static List convertToTableNames(Iterable listTablesResponse) { List tableNames = new ArrayList<>(); - for (com.google.bigtable.admin.v2.Table table : listTablesResponse.getTablesList()) { + for (com.google.bigtable.admin.v2.Table table : listTablesResponse) { tableNames.add(TableName.parse(table.getName())); } return tableNames; diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ClusterState.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ClusterState.java index 13aa1ac8710e..674294771ef3 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ClusterState.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ClusterState.java @@ -17,6 +17,7 @@ import com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState; import com.google.common.base.MoreObjects; +import com.google.common.base.Objects; // TODO(igorbernstein2): remove this class and promote Replication State to Table. /** Wrapper for {@link ClusterState} protocol buffer object */ @@ -47,6 +48,24 @@ public ReplicationState getReplicationState() { return replicationState; } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + ClusterState that = (ClusterState) o; + return Objects.equal(id, that.id) && + replicationState == that.replicationState; + } + + @Override + public int hashCode() { + return Objects.hashCode(id, replicationState); + } + @Override public String toString() { return MoreObjects.toStringHelper(this) diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ColumnFamily.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ColumnFamily.java index 738927beace4..bebe94d9a858 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ColumnFamily.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ColumnFamily.java @@ -22,6 +22,7 @@ import com.google.bigtable.admin.v2.GcRule.RuleCase; import com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule; import com.google.common.base.MoreObjects; +import com.google.common.base.Objects; /** Wrapper for {@link ColumnFamily} protocol buffer object */ public final class ColumnFamily { @@ -63,6 +64,24 @@ public boolean hasGCRule() { return !RuleCase.RULE_NOT_SET.equals(rule.toProto().getRuleCase()); } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + ColumnFamily that = (ColumnFamily) o; + return Objects.equal(id, that.id) && + Objects.equal(rule, that.rule); + } + + @Override + public int hashCode() { + return Objects.hashCode(id, rule); + } + @Override public String toString() { return MoreObjects.toStringHelper(this).add("id", id).add("GCRule", rule).toString(); diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ConsistencyToken.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ConsistencyToken.java index 9b3d32be5a59..22961585641f 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ConsistencyToken.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ConsistencyToken.java @@ -20,6 +20,7 @@ import com.google.bigtable.admin.v2.GenerateConsistencyTokenResponse; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.MoreObjects; +import com.google.common.base.Objects; /** * Wrapper for {@link GenerateConsistencyTokenResponse#getConsistencyToken()} @@ -53,6 +54,23 @@ String getToken() { return token; } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + ConsistencyToken that = (ConsistencyToken) o; + return Objects.equal(token, that.token); + } + + @Override + public int hashCode() { + return Objects.hashCode(token); + } + @Override public String toString() { return MoreObjects.toStringHelper(this).add("token", token).toString(); diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateTableRequest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateTableRequest.java index f00836957e7f..11a8c8c22f06 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateTableRequest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateTableRequest.java @@ -20,6 +20,7 @@ import com.google.bigtable.admin.v2.InstanceName; import com.google.bigtable.admin.v2.Table; import com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule; +import com.google.common.base.Objects; import com.google.common.base.Preconditions; import com.google.protobuf.ByteString; @@ -90,6 +91,24 @@ public CreateTableRequest addSplit(ByteString key) { return this; } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + CreateTableRequest that = (CreateTableRequest) o; + return Objects.equal(createTableRequest, that.createTableRequest) && + Objects.equal(tableRequest, that.tableRequest); + } + + @Override + public int hashCode() { + return Objects.hashCode(createTableRequest, tableRequest); + } + @InternalApi public com.google.bigtable.admin.v2.CreateTableRequest toProto(InstanceName instanceName) { Preconditions.checkNotNull(instanceName); diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java index 71db5281413c..9dc6c846dc12 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java @@ -15,6 +15,7 @@ */ package com.google.cloud.bigtable.admin.v2.models; +import com.google.common.base.Objects; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; @@ -148,6 +149,23 @@ public List getRulesList() { return rulesList; } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + IntersectionRule that = (IntersectionRule) o; + return Objects.equal(rulesList, that.rulesList); + } + + @Override + public int hashCode() { + return Objects.hashCode(rulesList); + } + @Override public String toString() { return MoreObjects.toStringHelper(this).add("rulesList", rulesList).toString(); @@ -197,6 +215,24 @@ public List getRulesList() { return rulesList; } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + UnionRule unionRule = (UnionRule) o; + return Objects.equal(rulesList, unionRule.rulesList); + } + + @Override + public int hashCode() { + return Objects.hashCode(rulesList); + } + @Override public String toString() { return MoreObjects.toStringHelper(this).add("rulesList", rulesList).toString(); @@ -232,6 +268,23 @@ public int getMaxVersions() { return builder.getMaxNumVersions(); } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + VersionRule that = (VersionRule) o; + return Objects.equal(builder, that.builder); + } + + @Override + public int hashCode() { + return Objects.hashCode(builder); + } + @Override public String toString() { return MoreObjects.toStringHelper(this).add("maxNumVersions", getMaxVersions()).toString(); @@ -262,6 +315,23 @@ public Duration getMaxAge() { return Duration.ofSeconds(builder.getSeconds(), builder.getNanos()); } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + DurationRule that = (DurationRule) o; + return Objects.equal(builder, that.builder); + } + + @Override + public int hashCode() { + return Objects.hashCode(builder); + } + @Override public String toString() { return MoreObjects.toStringHelper(this).add("maxAge", getMaxAge()).toString(); @@ -284,6 +354,23 @@ public GcRule toProto() { return GcRule.getDefaultInstance(); } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + return true; + } + + @Override + public int hashCode() { + return Objects.hashCode(1); + } + + @Override public String toString() { return MoreObjects.toStringHelper(this).toString(); diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Table.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Table.java index c070f81fd84d..be1ee7408711 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Table.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Table.java @@ -18,6 +18,7 @@ import com.google.api.core.InternalApi; import com.google.bigtable.admin.v2.TableName; import com.google.common.base.MoreObjects; +import com.google.common.base.Objects; import com.google.common.collect.ImmutableMap; import java.util.Collection; import java.util.Map; @@ -97,6 +98,25 @@ public Collection getColumnFamiles() { return columnFamilies.values(); } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Table table = (Table) o; + return Objects.equal(tableName, table.tableName) && + Objects.equal(clusterStates, table.clusterStates) && + Objects.equal(columnFamilies, table.columnFamilies); + } + + @Override + public int hashCode() { + return Objects.hashCode(tableName, clusterStates, columnFamilies); + } + @Override public String toString() { return MoreObjects.toStringHelper(this) diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTest.java index 38d078b115d9..229ba2f2c2ad 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTest.java @@ -16,49 +16,66 @@ package com.google.cloud.bigtable.admin.v2; import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.assertEquals; -import static org.mockito.Matchers.any; -import java.util.List; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.ArgumentCaptor; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.runners.MockitoJUnitRunner; + import com.google.api.core.ApiFuture; import com.google.api.core.ApiFutures; import com.google.api.gax.rpc.UnaryCallable; import com.google.bigtable.admin.v2.CheckConsistencyRequest; import com.google.bigtable.admin.v2.CheckConsistencyResponse; +import com.google.bigtable.admin.v2.ColumnFamily; import com.google.bigtable.admin.v2.DeleteTableRequest; import com.google.bigtable.admin.v2.DropRowRangeRequest; +import com.google.bigtable.admin.v2.GcRule; import com.google.bigtable.admin.v2.GenerateConsistencyTokenRequest; import com.google.bigtable.admin.v2.GenerateConsistencyTokenResponse; import com.google.bigtable.admin.v2.GetTableRequest; import com.google.bigtable.admin.v2.InstanceName; import com.google.bigtable.admin.v2.ListTablesRequest; -import com.google.bigtable.admin.v2.ListTablesResponse; -import com.google.bigtable.admin.v2.Table; +import com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.Modification; import com.google.bigtable.admin.v2.TableName; +import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPagedResponse; +import com.google.cloud.bigtable.admin.v2.models.ConsistencyToken; import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest; import com.google.cloud.bigtable.admin.v2.models.ModifyColumnFamiliesRequest; -import com.google.cloud.bigtable.admin.v2.models.ConsistencyToken; +import com.google.cloud.bigtable.admin.v2.models.Table; import com.google.cloud.bigtable.admin.v2.stub.BigtableTableAdminStub; +import com.google.common.collect.Lists; import com.google.protobuf.ByteString; import com.google.protobuf.Empty; +import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.runners.MockitoJUnitRunner; +import org.mockito.stubbing.Answer; @RunWith(MockitoJUnitRunner.class) public class BigtableTableAdminClientTest { + + private static final InstanceName INSTANCE_NAME = InstanceName.of("my-project", "my-instance"); + private static final TableName TABLE_NAME = TableName + .of(INSTANCE_NAME.getProject(), INSTANCE_NAME.getInstance(), "my-table"); + private BigtableTableAdminClient adminClient; - @Mock private BigtableTableAdminStub mockStub; + @Mock + private BigtableTableAdminStub mockStub; - @Mock private UnaryCallable mockCreateTableCallable; - @Mock private UnaryCallable mockModifyTableCallable; - @Mock private UnaryCallable mockDeleteTableCallable; - @Mock private UnaryCallable mockGetTableCallable; - @Mock private UnaryCallable mockListTableCallable; - @Mock private UnaryCallable mockDropRowRangeCallable; + @Mock + private UnaryCallable mockCreateTableCallable; + @Mock + private UnaryCallable mockModifyTableCallable; + @Mock + private UnaryCallable mockDeleteTableCallable; + @Mock + private UnaryCallable mockGetTableCallable; + @Mock + private UnaryCallable mockListTableCallable; + @Mock + private UnaryCallable mockDropRowRangeCallable; @Mock private UnaryCallable @@ -69,35 +86,18 @@ public class BigtableTableAdminClientTest { mockCheckConsistencyCallable; @Before - public void setUp() throws Exception { - adminClient = BigtableTableAdminClient - .create(InstanceName.of("[PROJECT]", "[INSTANCE]"), mockStub); + public void setUp() { + adminClient = BigtableTableAdminClient.create(INSTANCE_NAME, mockStub); Mockito.when(mockStub.createTableCallable()).thenReturn(mockCreateTableCallable); Mockito.when(mockStub.modifyColumnFamiliesCallable()).thenReturn(mockModifyTableCallable); Mockito.when(mockStub.deleteTableCallable()).thenReturn(mockDeleteTableCallable); Mockito.when(mockStub.getTableCallable()).thenReturn(mockGetTableCallable); - Mockito.when(mockStub.listTablesCallable()).thenReturn(mockListTableCallable); + Mockito.when(mockStub.listTablesPagedCallable()).thenReturn(mockListTableCallable); Mockito.when(mockStub.dropRowRangeCallable()).thenReturn(mockDropRowRangeCallable); Mockito.when(mockStub.generateConsistencyTokenCallable()) .thenReturn(mockGenerateConsistencyTokenCallable); Mockito.when(mockStub.checkConsistencyCallable()).thenReturn(mockCheckConsistencyCallable); - - Table table = Table.newBuilder().build(); - ApiFuture
futureTable = ApiFutures.immediateFuture(table); - Mockito.when(mockCreateTableCallable.call(any(com.google.bigtable.admin.v2.CreateTableRequest.class))).thenReturn(table); - Mockito.when(mockCreateTableCallable.futureCall(any( - com.google.bigtable.admin.v2.CreateTableRequest.class))) - .thenReturn(futureTable); - Mockito.when(mockModifyTableCallable.call(any( - com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.class))) - .thenReturn(table); - Mockito.when(mockModifyTableCallable.futureCall(any( - com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.class))) - .thenReturn(futureTable); - Mockito.when(mockGetTableCallable.call(any(GetTableRequest.class))).thenReturn(table); - Mockito.when(mockGetTableCallable.futureCall(any(GetTableRequest.class))) - .thenReturn(futureTable); } @Test @@ -107,199 +107,471 @@ public void close() { } @Test - public void createTable() { - CreateTableRequest createTableReq = CreateTableRequest.of("tableId"); - adminClient.createTable(createTableReq); - Mockito.verify(mockCreateTableCallable) - .call(createTableReq.toProto(adminClient.getInstanceName())); - } + public void testCreateTable() { + // Setup + com.google.bigtable.admin.v2.CreateTableRequest expectedRequest = + com.google.bigtable.admin.v2.CreateTableRequest + .newBuilder() + .setParent(INSTANCE_NAME.toString()) + .setTableId(TABLE_NAME.getTable()) + .setTable(com.google.bigtable.admin.v2.Table.getDefaultInstance()) + .build(); - @Test - public void createTableAsync() { - CreateTableRequest createTableReq = CreateTableRequest.of("tableId"); - adminClient.createTableAsync(createTableReq); - Mockito.verify(mockCreateTableCallable) - .futureCall(createTableReq.toProto(adminClient.getInstanceName())); + com.google.bigtable.admin.v2.Table expectedResponse = com.google.bigtable.admin.v2.Table + .newBuilder() + .setName(TABLE_NAME.toString()) + .build(); + + Mockito.when(mockCreateTableCallable.futureCall(expectedRequest)) + .thenReturn(ApiFutures.immediateFuture(expectedResponse)); + + // Execute + Table result = adminClient.createTable(CreateTableRequest.of(TABLE_NAME.getTable())); + + // Verify + assertThat(result).isEqualTo(Table.fromProto(expectedResponse)); } @Test - public void modifyFamilies() { - ModifyColumnFamiliesRequest modifyFamReq = ModifyColumnFamiliesRequest.of("tableId"); - adminClient.modifyFamilies(modifyFamReq); - Mockito.verify(mockModifyTableCallable) - .call(modifyFamReq.toProto(adminClient.getInstanceName())); + public void testCreateTableAsync() throws Exception { + // Setup + com.google.bigtable.admin.v2.CreateTableRequest expectedRequest = com.google.bigtable.admin.v2.CreateTableRequest + .newBuilder() + .setParent(INSTANCE_NAME.toString()) + .setTableId(TABLE_NAME.getTable()) + .setTable(com.google.bigtable.admin.v2.Table.getDefaultInstance()) + .build(); + + com.google.bigtable.admin.v2.Table expectedResponse = com.google.bigtable.admin.v2.Table + .newBuilder() + .setName(TABLE_NAME.toString()) + .build(); + + Mockito.when(mockCreateTableCallable.futureCall(expectedRequest)) + .thenReturn(ApiFutures.immediateFuture(expectedResponse)); + + // Execute + ApiFuture
result = adminClient + .createTableAsync(CreateTableRequest.of(TABLE_NAME.getTable())); + + // Verify + assertThat(result.get()).isEqualTo(Table.fromProto(expectedResponse)); } @Test - public void modifyFamiliesAsync() { - ModifyColumnFamiliesRequest modifyFamReq = ModifyColumnFamiliesRequest.of("tableId"); - adminClient.modifyFamiliesAsync(modifyFamReq); - Mockito.verify(mockModifyTableCallable) - .futureCall(modifyFamReq.toProto(adminClient.getInstanceName())); + public void testModifyFamilies() { + // Setup + com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest expectedRequest = + com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest + .newBuilder() + .setName(TABLE_NAME.toString()) + .addModifications( + Modification.newBuilder() + .setId("cf") + .setCreate(ColumnFamily.newBuilder().setGcRule(GcRule.getDefaultInstance()))) + .build(); + + com.google.bigtable.admin.v2.Table fakeResponse = com.google.bigtable.admin.v2.Table + .newBuilder() + .setName(TABLE_NAME.toString()) + .putColumnFamilies("cf", + ColumnFamily.newBuilder().setGcRule(GcRule.getDefaultInstance()).build()) + .build(); + + Mockito.when(mockModifyTableCallable.futureCall(expectedRequest)) + .thenReturn(ApiFutures.immediateFuture(fakeResponse)); + + // Execute + Table actualResult = adminClient.modifyFamilies( + ModifyColumnFamiliesRequest + .of(TABLE_NAME.getTable()) + .addFamily("cf") + ); + + // Verify + assertThat(actualResult).isEqualTo(Table.fromProto(fakeResponse)); } @Test - public void deleteTable() { - adminClient.deleteTable("tableId"); - Mockito.verify(mockDeleteTableCallable).call(adminClient.composeDeleteTableRequest("tableId")); + public void testModifyFamiliesAsync() throws Exception { + // Setup + com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest expectedRequest = + com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest + .newBuilder() + .setName(TABLE_NAME.toString()) + .addModifications( + Modification.newBuilder() + .setId("cf") + .setCreate( + ColumnFamily.newBuilder() + .setGcRule(GcRule.getDefaultInstance()) + ) + ) + .build(); + + com.google.bigtable.admin.v2.Table expectedResponse = com.google.bigtable.admin.v2.Table + .newBuilder() + .setName(TABLE_NAME.toString()) + .putColumnFamilies("cf", + ColumnFamily.newBuilder().setGcRule(GcRule.getDefaultInstance()).build()) + .build(); + + Mockito.when(mockModifyTableCallable.futureCall(expectedRequest)) + .thenReturn(ApiFutures.immediateFuture(expectedResponse)); + + // Execute + ApiFuture
actualResult = adminClient.modifyFamiliesAsync( + ModifyColumnFamiliesRequest.of(TABLE_NAME.getTable()) + .addFamily("cf") + ); + + // Verify + assertThat(actualResult.get()).isEqualTo(Table.fromProto(expectedResponse)); } @Test - public void deleteTableAsync() { - ApiFuture empty = ApiFutures.immediateFuture(Empty.newBuilder().build()); - Mockito.when(mockDeleteTableCallable.futureCall(any(DeleteTableRequest.class))) - .thenReturn(empty); - - adminClient.deleteTableAsync("tableId"); - Mockito.verify(mockDeleteTableCallable) - .futureCall(adminClient.composeDeleteTableRequest("tableId")); + public void testDeleteTable() { + // Setup + com.google.bigtable.admin.v2.DeleteTableRequest expectedRequest = + com.google.bigtable.admin.v2.DeleteTableRequest + .newBuilder() + .setName(TABLE_NAME.toString()) + .build(); + + final AtomicBoolean wasCalled = new AtomicBoolean(false); + + Mockito.when(mockDeleteTableCallable.futureCall(expectedRequest)) + .thenAnswer(new Answer>() { + @Override + public ApiFuture answer(InvocationOnMock invocationOnMock) { + wasCalled.set(true); + return ApiFutures.immediateFuture(Empty.getDefaultInstance()); + } + }); + + // Execute + adminClient.deleteTable(TABLE_NAME.getTable()); + + // Verify + assertThat(wasCalled.get()).isTrue(); } @Test - public void getTable() { - adminClient.getTable("tableId"); - Mockito.verify(mockGetTableCallable).call(adminClient.composeGetTableRequest("tableId")); + public void testDeleteTableAsync() throws Exception { + // Setup + com.google.bigtable.admin.v2.DeleteTableRequest expectedRequest = + com.google.bigtable.admin.v2.DeleteTableRequest + .newBuilder() + .setName(TABLE_NAME.toString()) + .build(); + + final AtomicBoolean wasCalled = new AtomicBoolean(false); + + Mockito.when(mockDeleteTableCallable.futureCall(expectedRequest)) + .thenAnswer(new Answer>() { + @Override + public ApiFuture answer(InvocationOnMock invocationOnMock) { + wasCalled.set(true); + return ApiFutures.immediateFuture(Empty.getDefaultInstance()); + } + }); + + // Execute + ApiFuture result = adminClient.deleteTableAsync(TABLE_NAME.getTable()); + result.get(); + + // Verify + assertThat(wasCalled.get()).isTrue(); } @Test - public void getTableAsync() { - adminClient.getTableAsync("tableId"); - Mockito.verify(mockGetTableCallable).futureCall(adminClient.composeGetTableRequest("tableId")); + public void testGetTable() { + // Setup + com.google.bigtable.admin.v2.GetTableRequest expectedRequest = + com.google.bigtable.admin.v2.GetTableRequest + .newBuilder() + .setName(TABLE_NAME.toString()) + .build(); + + com.google.bigtable.admin.v2.Table expectedResponse = com.google.bigtable.admin.v2.Table + .newBuilder() + .setName(TABLE_NAME.toString()) + .build(); + + Mockito.when(mockGetTableCallable.futureCall(expectedRequest)) + .thenReturn(ApiFutures.immediateFuture(expectedResponse)); + + // Execute + Table actualResult = adminClient.getTable(TABLE_NAME.getTable()); + + // Verify + assertThat(actualResult).isEqualTo(Table.fromProto(expectedResponse)); } @Test - public void listTables() { - ListTablesResponse listTablesResponse = ListTablesResponse.newBuilder().build(); - Mockito.when(mockListTableCallable.call(adminClient.composeListTableRequest())) - .thenReturn(listTablesResponse); + public void testGetTableAsync() throws Exception { + // Setup + com.google.bigtable.admin.v2.GetTableRequest expectedRequest = + com.google.bigtable.admin.v2.GetTableRequest + .newBuilder() + .setName(TABLE_NAME.toString()) + .build(); + + com.google.bigtable.admin.v2.Table expectedResponse = com.google.bigtable.admin.v2.Table + .newBuilder() + .setName(TABLE_NAME.toString()) + .build(); + + Mockito.when(mockGetTableCallable.futureCall(expectedRequest)) + .thenReturn(ApiFutures.immediateFuture(expectedResponse)); + + // Execute + ApiFuture
actualResult = adminClient.getTableAsync(TABLE_NAME.getTable()); - adminClient.listTables(); - Mockito.verify(mockListTableCallable).call(adminClient.composeListTableRequest()); + // Verify + assertThat(actualResult.get()).isEqualTo(Table.fromProto(expectedResponse)); } @Test - public void listTablesAsync() { - ApiFuture listTablesResponse = - ApiFutures.immediateFuture(ListTablesResponse.newBuilder().build()); - Mockito.when(mockListTableCallable.futureCall(adminClient.composeListTableRequest())) - .thenReturn(listTablesResponse); - - adminClient.listTablesAsync(); - Mockito.verify(mockListTableCallable).futureCall(adminClient.composeListTableRequest()); + public void testListTables() { + // Setup + ListTablesRequest expectedRequest = ListTablesRequest.newBuilder() + .setParent(INSTANCE_NAME.toString()) + .build(); + + ListTablesPagedResponse expectedResponseWrapper = Mockito.mock(ListTablesPagedResponse.class); + + Iterable expectedResults = Lists.newArrayList( + com.google.bigtable.admin.v2.Table.newBuilder() + .setName(TABLE_NAME.toString() + "1") + .build(), + com.google.bigtable.admin.v2.Table.newBuilder() + .setName(TABLE_NAME.toString() + "2") + .build()); + + Mockito.when(mockListTableCallable.futureCall(expectedRequest)) + .thenReturn(ApiFutures.immediateFuture(expectedResponseWrapper)); + + Mockito.when(expectedResponseWrapper.iterateAll()) + .thenReturn(expectedResults); + + // Execute + List actualResults = adminClient.listTables(); + + // Verify + assertThat(actualResults).containsExactly( + TableName.parse(TABLE_NAME.toString() + "1"), + TableName.parse(TABLE_NAME.toString() + "2") + ); } @Test - public void dropRowRange() { - adminClient.dropRowRange("tableId", "rowKeyPrefix"); - Mockito.verify(mockDropRowRangeCallable) - .call( - adminClient.composeDropRowRangeRequest( - "tableId", ByteString.copyFromUtf8("rowKeyPrefix"), false)); + public void testListTablesAsync() throws Exception { + // Setup + ListTablesRequest expectedRequest = ListTablesRequest.newBuilder() + .setParent(INSTANCE_NAME.toString()) + .build(); + + ListTablesPagedResponse expectedResponseWrapper = Mockito.mock(ListTablesPagedResponse.class); + + Iterable expectedResults = Lists.newArrayList( + com.google.bigtable.admin.v2.Table.newBuilder() + .setName(TABLE_NAME.toString() + "1") + .build(), + com.google.bigtable.admin.v2.Table.newBuilder() + .setName(TABLE_NAME.toString() + "2") + .build()); + + Mockito.when(mockListTableCallable.futureCall(expectedRequest)) + .thenReturn(ApiFutures.immediateFuture(expectedResponseWrapper)); + + Mockito.when(expectedResponseWrapper.iterateAll()) + .thenReturn(expectedResults); + + // Execute + ApiFuture> actualResults = adminClient.listTablesAsync(); + + // Verify + assertThat(actualResults.get()).containsExactly( + TableName.parse(TABLE_NAME.toString() + "1"), + TableName.parse(TABLE_NAME.toString() + "2") + ); } @Test - public void getDropRowRangeRequest() { - DropRowRangeRequest actual = - adminClient.composeDropRowRangeRequest( - "tableId", ByteString.copyFromUtf8("rowKeyPrefix"), false); + public void testDropRowRange() { + // Setup + DropRowRangeRequest expectedRequest = DropRowRangeRequest.newBuilder() + .setName(TABLE_NAME.toString()) + .setRowKeyPrefix(ByteString.copyFromUtf8("rowKeyPrefix")) + .build(); + + final Empty expectedResponse = Empty.getDefaultInstance(); + + final AtomicBoolean wasCalled = new AtomicBoolean(false); + + Mockito.when(mockDropRowRangeCallable.futureCall(expectedRequest)) + .thenAnswer(new Answer>() { + @Override + public ApiFuture answer(InvocationOnMock invocationOnMock) { + wasCalled.set(true); + return ApiFutures.immediateFuture(expectedResponse); + } + }); + + // Execute + adminClient.dropRowRange(TABLE_NAME.getTable(), "rowKeyPrefix"); + + // Verify + assertThat(wasCalled.get()).isTrue(); + } + @Test + public void testGetDropRowRangeRequest() { DropRowRangeRequest expected = DropRowRangeRequest.newBuilder() .setName(adminClient.getTableName("tableId")) .setRowKeyPrefix(ByteString.copyFromUtf8("rowKeyPrefix")) .build(); + DropRowRangeRequest actual = + adminClient.composeDropRowRangeRequest( + "tableId", ByteString.copyFromUtf8("rowKeyPrefix"), false); + assertThat(actual).isEqualTo(expected); } @Test - public void getDropRowRangeRequestDropAllData() { - DropRowRangeRequest actual = adminClient.composeDropRowRangeRequest("tableId", null, true); - + public void testGetDropRowRangeRequestDropAllData() { DropRowRangeRequest expected = DropRowRangeRequest.newBuilder() .setName(adminClient.getTableName("tableId")) .setDeleteAllDataFromTable(true) .build(); + DropRowRangeRequest actual = adminClient.composeDropRowRangeRequest("tableId", null, true); + assertThat(actual).isEqualTo(expected); } @Test - public void dropRowRangeAsync() { - ApiFuture empty = ApiFutures.immediateFuture(Empty.newBuilder().build()); - Mockito.when(mockDropRowRangeCallable.futureCall(any(DropRowRangeRequest.class))) - .thenReturn(empty); - - adminClient.dropRowRangeAsync("tableId", "rowKeyPrefix"); - Mockito.verify(mockDropRowRangeCallable) - .futureCall( - adminClient.composeDropRowRangeRequest( - "tableId", ByteString.copyFromUtf8("rowKeyPrefix"), false)); + public void testDropRowRangeAsync() throws Exception { + // Setup + DropRowRangeRequest expectedRequest = DropRowRangeRequest.newBuilder() + .setName(TABLE_NAME.toString()) + .setRowKeyPrefix(ByteString.copyFromUtf8("rowKeyPrefix")) + .build(); + + final Empty expectedResponse = Empty.getDefaultInstance(); + + final AtomicBoolean wasCalled = new AtomicBoolean(false); + + Mockito.when(mockDropRowRangeCallable.futureCall(expectedRequest)) + .thenAnswer(new Answer>() { + @Override + public ApiFuture answer(InvocationOnMock invocationOnMock) { + wasCalled.set(true); + return ApiFutures.immediateFuture(expectedResponse); + } + }); + + // Execute + ApiFuture actualResult = adminClient + .dropRowRangeAsync(TABLE_NAME.getTable(), "rowKeyPrefix"); + + actualResult.get(); + + // Verify + assertThat(wasCalled.get()).isTrue(); } @Test - public void generateAndCheckConsistency() { - GenerateConsistencyTokenResponse genResp = - GenerateConsistencyTokenResponse.newBuilder().build(); - Mockito.when( - mockGenerateConsistencyTokenCallable.call( - adminClient.composeGenerateConsistencyTokenRequest("tableId"))) - .thenReturn(genResp); - - ConsistencyToken consistencyToken = adminClient.generateConsistencyToken("tableId"); - Mockito.verify(mockGenerateConsistencyTokenCallable) - .call(adminClient.composeGenerateConsistencyTokenRequest("tableId")); - - ArgumentCaptor requestCaptor = - ArgumentCaptor.forClass(CheckConsistencyRequest.class); - CheckConsistencyResponse consistencyResp = CheckConsistencyResponse.newBuilder().build(); - Mockito.when(mockCheckConsistencyCallable.call(any(CheckConsistencyRequest.class))) - .thenReturn(consistencyResp); - - adminClient.isConsistent("tableId", consistencyToken); - Mockito.verify(mockCheckConsistencyCallable).call(requestCaptor.capture()); + public void testGenerateConsistencyToken() { + // Setup + GenerateConsistencyTokenRequest expectedRequest = GenerateConsistencyTokenRequest.newBuilder() + .setName(TABLE_NAME.toString()) + .build(); + + GenerateConsistencyTokenResponse expectedResponse = + GenerateConsistencyTokenResponse.newBuilder() + .setConsistencyToken("fakeToken") + .build(); + + Mockito.when(mockGenerateConsistencyTokenCallable.futureCall(expectedRequest)) + .thenReturn(ApiFutures.immediateFuture(expectedResponse)); + + // Execute + ConsistencyToken actualResult = adminClient.generateConsistencyToken(TABLE_NAME.getTable()); + + // Verify + assertThat(actualResult).isEqualTo(ConsistencyToken.fromProto(expectedResponse)); + } + + @Test + public void testGenerateConsistencyTokenAsync() throws Exception { + // Setup + GenerateConsistencyTokenRequest expectedRequest = GenerateConsistencyTokenRequest.newBuilder() + .setName(TABLE_NAME.toString()) + .build(); + + GenerateConsistencyTokenResponse expectedResponse = + GenerateConsistencyTokenResponse.newBuilder() + .setConsistencyToken("fakeToken") + .build(); + + Mockito.when(mockGenerateConsistencyTokenCallable.futureCall(expectedRequest)) + .thenReturn(ApiFutures.immediateFuture(expectedResponse)); + + // Execute + ApiFuture actualResult = adminClient + .generateConsistencyTokenAsync(TABLE_NAME.getTable()); + + // Verify + assertThat(actualResult.get()).isEqualTo(ConsistencyToken.fromProto(expectedResponse)); } @Test - public void generateAndCheckConsistencyAsync() throws Exception { - ApiFuture genResp = - ApiFutures.immediateFuture(GenerateConsistencyTokenResponse.newBuilder().build()); - Mockito.when( - mockGenerateConsistencyTokenCallable.futureCall( - adminClient.composeGenerateConsistencyTokenRequest("tableId"))) - .thenReturn(genResp); - - ApiFuture consistencyTokenFuture = - adminClient.generateConsistencyTokenAsync("tableId"); - Mockito.verify(mockGenerateConsistencyTokenCallable) - .futureCall(adminClient.composeGenerateConsistencyTokenRequest("tableId")); - - ArgumentCaptor requestCaptor = - ArgumentCaptor.forClass(CheckConsistencyRequest.class); - ApiFuture consistencyResp = - ApiFutures.immediateFuture(CheckConsistencyResponse.newBuilder().build()); - Mockito.when(mockCheckConsistencyCallable.futureCall(any(CheckConsistencyRequest.class))) - .thenReturn(consistencyResp); - - adminClient.isConsistentAsync("tableId", consistencyTokenFuture.get()); - Mockito.verify(mockCheckConsistencyCallable).futureCall(requestCaptor.capture()); + public void testCheckConsistencyToken() { + // Setup + CheckConsistencyRequest expectedRequest = CheckConsistencyRequest.newBuilder() + .setName(TABLE_NAME.toString()) + .setConsistencyToken("fakeToken") + .build(); + + CheckConsistencyResponse expectedResponse = CheckConsistencyResponse.newBuilder() + .setConsistent(true) + .build(); + + Mockito.when(mockCheckConsistencyCallable.futureCall(expectedRequest)) + .thenReturn(ApiFutures.immediateFuture(expectedResponse)); + + // Execute + ConsistencyToken actualToken = ConsistencyToken.fromProto( + GenerateConsistencyTokenResponse.newBuilder() + .setConsistencyToken("fakeToken") + .build() + ); + + boolean actualResult = adminClient.isConsistent(TABLE_NAME.getTable(), actualToken); + + // Verify + assertThat(actualResult).isTrue(); } @Test - public void convertToTableNames() { - ListTablesResponse listTablesResponse = - ListTablesResponse.newBuilder() - .addTables(Table.newBuilder().setName("projects/p/instances/i/tables/t1")) - .addTables(Table.newBuilder().setName("projects/p/instances/i/tables/t2")) - .build(); + public void testConvertToTableNames() { + List expected = Lists.newArrayList( + TableName.of("p", "i", "t1"), + TableName.of("p", "i", "t2") + ); + + List input = Lists.newArrayList( + com.google.bigtable.admin.v2.Table.newBuilder().setName("projects/p/instances/i/tables/t1").build(), + com.google.bigtable.admin.v2.Table.newBuilder().setName("projects/p/instances/i/tables/t2").build() + ); - List tableNames = BigtableTableAdminClient.convertToTableNames(listTablesResponse); - assertEquals(2, tableNames.size()); - assertEquals("projects/p/instances/i/tables/t1", tableNames.get(0).toString()); - assertEquals("projects/p/instances/i/tables/t2", tableNames.get(1).toString()); + List actual = BigtableTableAdminClient.convertToTableNames(input); - listTablesResponse = ListTablesResponse.newBuilder().build(); - assertEquals(0, BigtableTableAdminClient.convertToTableNames(listTablesResponse).size()); + assertThat(actual).containsExactlyElementsIn(expected).inOrder(); } } From e51a5c9326e80e05ec20ce5dbc37d1f89719ded5 Mon Sep 17 00:00:00 2001 From: Igor Bernstein Date: Fri, 17 Aug 2018 13:09:24 -0400 Subject: [PATCH 3/3] codacy fixes --- .../bigtable/admin/v2/models/GCRules.java | 5 ++- .../v2/BigtableTableAdminClientTest.java | 38 ++++++++----------- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java index 9dc6c846dc12..5c80559991e5 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/GCRules.java @@ -359,10 +359,11 @@ public boolean equals(Object o) { if (this == o) { return true; } - if (o == null || getClass() != o.getClass()) { + if (o == null) { return false; } - return true; + + return getClass() == o.getClass(); } @Override diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTest.java index 229ba2f2c2ad..3ab9b5d99bfc 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTest.java @@ -232,11 +232,9 @@ public void testModifyFamiliesAsync() throws Exception { @Test public void testDeleteTable() { // Setup - com.google.bigtable.admin.v2.DeleteTableRequest expectedRequest = - com.google.bigtable.admin.v2.DeleteTableRequest - .newBuilder() - .setName(TABLE_NAME.toString()) - .build(); + DeleteTableRequest expectedRequest = DeleteTableRequest.newBuilder() + .setName(TABLE_NAME.toString()) + .build(); final AtomicBoolean wasCalled = new AtomicBoolean(false); @@ -259,11 +257,9 @@ public ApiFuture answer(InvocationOnMock invocationOnMock) { @Test public void testDeleteTableAsync() throws Exception { // Setup - com.google.bigtable.admin.v2.DeleteTableRequest expectedRequest = - com.google.bigtable.admin.v2.DeleteTableRequest - .newBuilder() - .setName(TABLE_NAME.toString()) - .build(); + DeleteTableRequest expectedRequest = DeleteTableRequest.newBuilder() + .setName(TABLE_NAME.toString()) + .build(); final AtomicBoolean wasCalled = new AtomicBoolean(false); @@ -287,11 +283,9 @@ public ApiFuture answer(InvocationOnMock invocationOnMock) { @Test public void testGetTable() { // Setup - com.google.bigtable.admin.v2.GetTableRequest expectedRequest = - com.google.bigtable.admin.v2.GetTableRequest - .newBuilder() - .setName(TABLE_NAME.toString()) - .build(); + GetTableRequest expectedRequest = GetTableRequest.newBuilder() + .setName(TABLE_NAME.toString()) + .build(); com.google.bigtable.admin.v2.Table expectedResponse = com.google.bigtable.admin.v2.Table .newBuilder() @@ -311,11 +305,9 @@ public void testGetTable() { @Test public void testGetTableAsync() throws Exception { // Setup - com.google.bigtable.admin.v2.GetTableRequest expectedRequest = - com.google.bigtable.admin.v2.GetTableRequest - .newBuilder() - .setName(TABLE_NAME.toString()) - .build(); + GetTableRequest expectedRequest = GetTableRequest.newBuilder() + .setName(TABLE_NAME.toString()) + .build(); com.google.bigtable.admin.v2.Table expectedResponse = com.google.bigtable.admin.v2.Table .newBuilder() @@ -566,8 +558,10 @@ public void testConvertToTableNames() { ); List input = Lists.newArrayList( - com.google.bigtable.admin.v2.Table.newBuilder().setName("projects/p/instances/i/tables/t1").build(), - com.google.bigtable.admin.v2.Table.newBuilder().setName("projects/p/instances/i/tables/t2").build() + com.google.bigtable.admin.v2.Table.newBuilder().setName("projects/p/instances/i/tables/t1") + .build(), + com.google.bigtable.admin.v2.Table.newBuilder().setName("projects/p/instances/i/tables/t2") + .build() ); List actual = BigtableTableAdminClient.convertToTableNames(input);