Skip to content

Commit 571a013

Browse files
fix(bigtable): delegate MaterializedView/LogicalView toString() to proto and fix list*ViewsTest (#15243)
`MaterializedView.equals()` and `LogicalView.equals()` delegate to protobuf message equality, which compares every field including server-populated read-only metadata and `UnknownFieldSet` entries. When `ListMaterializedViews` or `ListLogicalViews` populates read-only metadata fields that are not populated in the `CreateMaterializedView` / `CreateLogicalView` LRO response, `assertThat(response).contains(view)` fails even when the view is returned with the expected ID, deletion-protection setting, and query. - Delegate `MaterializedView#toString()` and `LogicalView#toString()` to `proto.toString()` (with unit tests in `MaterializedViewTest` and `LogicalViewTest`) so failure messages print readable protobuf contents instead of default `Object#toString()` identity hashes. - Update `BigtableMaterializedViewIT#listMaterializedViewsTest` and `BigtableLogicalViewIT#listLogicalViewsTest` to compare elements via Truth `Correspondence` (`comparingElementsUsing(...)`) on `getId()`, `isDeletionProtected()`, and `getQuery()`.
1 parent c33b2d1 commit 571a013

6 files changed

Lines changed: 64 additions & 0 deletions

File tree

‎java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/LogicalView.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,9 @@ public boolean equals(Object o) {
105105
public int hashCode() {
106106
return Objects.hashCode(proto);
107107
}
108+
109+
@Override
110+
public String toString() {
111+
return proto.toString();
112+
}
108113
}

‎java-bigtable/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/MaterializedView.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,9 @@ public boolean equals(Object o) {
108108
public int hashCode() {
109109
return Objects.hashCode(proto);
110110
}
111+
112+
@Override
113+
public String toString() {
114+
return proto.toString();
115+
}
111116
}

‎java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableLogicalViewIT.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
import com.google.cloud.bigtable.test_helpers.env.EmulatorEnv;
3333
import com.google.cloud.bigtable.test_helpers.env.PrefixGenerator;
3434
import com.google.cloud.bigtable.test_helpers.env.TestEnvRule;
35+
import com.google.common.truth.Correspondence;
3536
import io.grpc.StatusRuntimeException;
3637
import java.util.List;
38+
import java.util.Objects;
3739
import java.util.logging.Logger;
3840
import org.junit.After;
3941
import org.junit.Before;
@@ -54,6 +56,15 @@ public class BigtableLogicalViewIT {
5456
// sleeping for minutes past that point.
5557
private static final long DELETE_POLL_INTERVAL_SECONDS = 2;
5658
private static final int DELETE_POLL_ATTEMPTS = 60;
59+
private static final Correspondence<LogicalView, LogicalView> LOGICAL_VIEW_CORRESPONDENCE =
60+
Correspondence.from(
61+
(LogicalView actual, LogicalView expected) ->
62+
actual != null
63+
&& expected != null
64+
&& Objects.equals(actual.getId(), expected.getId())
65+
&& actual.isDeletionProtected() == expected.isDeletionProtected()
66+
&& Objects.equals(actual.getQuery(), expected.getQuery()),
67+
"has same id, deletionProtection, and query as");
5768

5869
private static BigtableInstanceAdminClient client;
5970
private static Table testTable;
@@ -128,6 +139,7 @@ public void listLogicalViewsTest() {
128139
List<LogicalView> response = client.listLogicalViews(instanceId);
129140
assertWithMessage("Got wrong logical view Ids in listLogicalViews")
130141
.that(response)
142+
.comparingElementsUsing(LOGICAL_VIEW_CORRESPONDENCE)
131143
.contains(logicalView);
132144
} finally {
133145
client.deleteLogicalView(instanceId, logicalViewId);

‎java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableMaterializedViewIT.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@
3232
import com.google.cloud.bigtable.test_helpers.env.EmulatorEnv;
3333
import com.google.cloud.bigtable.test_helpers.env.PrefixGenerator;
3434
import com.google.cloud.bigtable.test_helpers.env.TestEnvRule;
35+
import com.google.common.truth.Correspondence;
3536
import io.grpc.StatusRuntimeException;
3637
import java.io.IOException;
3738
import java.util.List;
39+
import java.util.Objects;
3840
import java.util.logging.Logger;
3941
import org.junit.After;
4042
import org.junit.Before;
@@ -56,6 +58,16 @@ public class BigtableMaterializedViewIT {
5658
// sleeping for minutes past that point.
5759
private static final long DELETE_POLL_INTERVAL_SECONDS = 2;
5860
private static final int DELETE_POLL_ATTEMPTS = 60;
61+
private static final Correspondence<MaterializedView, MaterializedView>
62+
MATERIALIZED_VIEW_CORRESPONDENCE =
63+
Correspondence.from(
64+
(MaterializedView actual, MaterializedView expected) ->
65+
actual != null
66+
&& expected != null
67+
&& Objects.equals(actual.getId(), expected.getId())
68+
&& actual.isDeletionProtected() == expected.isDeletionProtected()
69+
&& Objects.equals(actual.getQuery(), expected.getQuery()),
70+
"has same id, deletionProtection, and query as");
5971

6072
private BigtableInstanceAdminClient client;
6173
private Table testTable;
@@ -130,6 +142,7 @@ public void listMaterializedViewsTest() {
130142
List<MaterializedView> response = client.listMaterializedViews(instanceId);
131143
assertWithMessage("Got wrong materialized view Ids in listMaterializedViews")
132144
.that(response)
145+
.comparingElementsUsing(MATERIALIZED_VIEW_CORRESPONDENCE)
133146
.contains(materializedView);
134147
} finally {
135148
client.deleteMaterializedView(instanceId, materializedViewId);

‎java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/LogicalViewTest.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,18 @@ public void testHashCode() {
110110
.build()
111111
.hashCode());
112112
}
113+
114+
@Test
115+
public void testToString() {
116+
LogicalViewName logicalViewName = LogicalViewName.of(PROJECT_ID, INSTANCE_ID, LOGICAL_VIEW_ID);
117+
com.google.bigtable.admin.v2.LogicalView proto =
118+
com.google.bigtable.admin.v2.LogicalView.newBuilder()
119+
.setName(logicalViewName.toString())
120+
.setQuery("SELECT 1 FROM Table")
121+
.setDeletionProtection(true)
122+
.build();
123+
LogicalView logicalView = LogicalView.fromProto(proto);
124+
125+
assertThat(logicalView.toString()).isEqualTo(proto.toString());
126+
}
113127
}

‎java-bigtable/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/MaterializedViewTest.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,19 @@ public void testHashCode() {
114114
.build()
115115
.hashCode());
116116
}
117+
118+
@Test
119+
public void testToString() {
120+
MaterializedViewName materializedViewName =
121+
MaterializedViewName.of(PROJECT_ID, INSTANCE_ID, MATERIALIZED_VIEW_ID);
122+
com.google.bigtable.admin.v2.MaterializedView proto =
123+
com.google.bigtable.admin.v2.MaterializedView.newBuilder()
124+
.setName(materializedViewName.toString())
125+
.setDeletionProtection(true)
126+
.setQuery("SELECT 1 FROM Table")
127+
.build();
128+
MaterializedView materializedView = MaterializedView.fromProto(proto);
129+
130+
assertThat(materializedView.toString()).isEqualTo(proto.toString());
131+
}
117132
}

0 commit comments

Comments
 (0)