Skip to content

Commit

Permalink
feat: Implement toString for Bigtable*Settings (#488)
Browse files Browse the repository at this point in the history
* test: Refactor tests to extract common setup logic

* test: minor fix
  • Loading branch information
dmitry-fa authored Oct 23, 2020
1 parent adefcc0 commit 4d821f8
Show file tree
Hide file tree
Showing 8 changed files with 355 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.google.api.gax.core.CredentialsProvider;
import com.google.cloud.bigtable.admin.v2.stub.BigtableInstanceAdminStubSettings;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.base.Verify;
import java.io.IOException;
Expand Down Expand Up @@ -74,6 +75,39 @@ public BigtableInstanceAdminStubSettings getStubSettings() {
return stubSettings;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("projectId", projectId)
.add("createInstanceSettings", stubSettings.createInstanceSettings())
.add("createInstanceOperationSettings", stubSettings.createInstanceOperationSettings())
.add("getInstanceSettings", stubSettings.getInstanceSettings())
.add("listInstancesSettings", stubSettings.listInstancesSettings())
.add("partialUpdateInstanceSettings", stubSettings.partialUpdateInstanceSettings())
.add(
"partialUpdateInstanceOperationSettings",
stubSettings.partialUpdateInstanceOperationSettings())
.add("deleteInstanceSettings", stubSettings.deleteInstanceSettings())
.add("createClusterSettings", stubSettings.createClusterSettings())
.add("createClusterOperationSettings", stubSettings.createClusterOperationSettings())
.add("getClusterSettings", stubSettings.getClusterSettings())
.add("listClustersSettings", stubSettings.listClustersSettings())
.add("updateClusterSettings", stubSettings.updateClusterSettings())
.add("updateClusterOperationSettings", stubSettings.updateClusterOperationSettings())
.add("deleteClusterSettings", stubSettings.deleteClusterSettings())
.add("createAppProfileSettings", stubSettings.createAppProfileSettings())
.add("getAppProfileSettings", stubSettings.getAppProfileSettings())
.add("listAppProfilesSettings", stubSettings.listAppProfilesSettings())
.add("updateAppProfileSettings", stubSettings.updateAppProfileSettings())
.add("updateAppProfileOperationSettings", stubSettings.updateAppProfileOperationSettings())
.add("deleteAppProfileSettings", stubSettings.deleteAppProfileSettings())
.add("getIamPolicySettings", stubSettings.getIamPolicySettings())
.add("setIamPolicySettings", stubSettings.setIamPolicySettings())
.add("testIamPermissionsSettings", stubSettings.testIamPermissionsSettings())
.add("stubSettings", stubSettings)
.toString();
}

/** Returns a builder containing all the values of this settings class. */
public Builder toBuilder() {
return new Builder(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.api.gax.core.NoCredentialsProvider;
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
import com.google.cloud.bigtable.admin.v2.stub.BigtableTableAdminStubSettings;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.base.Verify;
Expand Down Expand Up @@ -85,6 +86,43 @@ public BigtableTableAdminStubSettings getStubSettings() {
return stubSettings;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("projectId", projectId)
.add("instanceId", instanceId)
.add("createTableSettings", stubSettings.createTableSettings())
.add("createTableFromSnapshotSettings", stubSettings.createTableFromSnapshotSettings())
.add(
"createTableFromSnapshotOperationSettings",
stubSettings.createTableFromSnapshotOperationSettings())
.add("listTablesSettings", stubSettings.listTablesSettings())
.add("getTableSettings", stubSettings.getTableSettings())
.add("deleteTableSettings", stubSettings.deleteTableSettings())
.add("modifyColumnFamiliesSettings", stubSettings.modifyColumnFamiliesSettings())
.add("dropRowRangeSettings", stubSettings.dropRowRangeSettings())
.add("generateConsistencyTokenSettings", stubSettings.generateConsistencyTokenSettings())
.add("checkConsistencySettings", stubSettings.checkConsistencySettings())
.add("getIamPolicySettings", stubSettings.getIamPolicySettings())
.add("setIamPolicySettings", stubSettings.setIamPolicySettings())
.add("testIamPermissionsSettings", stubSettings.testIamPermissionsSettings())
.add("snapshotTableSettings", stubSettings.snapshotTableSettings())
.add("snapshotTableOperationSettings", stubSettings.snapshotTableOperationSettings())
.add("getSnapshotSettings", stubSettings.getSnapshotSettings())
.add("listSnapshotsSettings", stubSettings.listSnapshotsSettings())
.add("deleteSnapshotSettings", stubSettings.deleteSnapshotSettings())
.add("createBackupSettings", stubSettings.createBackupSettings())
.add("createBackupOperationSettings", stubSettings.createBackupOperationSettings())
.add("getBackupSettings", stubSettings.getBackupSettings())
.add("listBackupsSettings", stubSettings.listBackupsSettings())
.add("updateBackupSettings", stubSettings.updateBackupSettings())
.add("deleteBackupSettings", stubSettings.deleteBackupSettings())
.add("restoreTableSettings", stubSettings.restoreTableSettings())
.add("restoreTableOperationSettings", stubSettings.restoreTableOperationSettings())
.add("stubSettings", stubSettings)
.toString();
}

/** Returns a builder containing all the values of this settings class. */
public Builder toBuilder() {
return new Builder(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.cloud.bigtable.data.v2.models.Query;
import com.google.cloud.bigtable.data.v2.models.Row;
import com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStubSettings;
import com.google.common.base.MoreObjects;
import com.google.common.base.Strings;
import io.grpc.ManagedChannelBuilder;
import java.util.List;
Expand Down Expand Up @@ -215,6 +216,11 @@ public UnaryCallSettings<Query, Row> readRowSettings() {
return stubSettings.readRowSettings();
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("stubSettings", stubSettings).toString();
}

/** Returns a builder containing all the values of this settings class. */
public Builder toBuilder() {
return new Builder(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.google.cloud.bigtable.data.v2.models.RowMutation;
import com.google.cloud.bigtable.data.v2.stub.mutaterows.MutateRowsBatchingDescriptor;
import com.google.cloud.bigtable.data.v2.stub.readrows.ReadRowsBatchingDescriptor;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -811,4 +812,24 @@ public EnhancedBigtableStubSettings build() {
}
// </editor-fold>
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("projectId", projectId)
.add("instanceId", instanceId)
.add("appProfileId", appProfileId)
.add("isRefreshingChannel", isRefreshingChannel)
.add("primedTableIds", primedTableIds)
.add("readRowsSettings", readRowsSettings)
.add("readRowSettings", readRowSettings)
.add("sampleRowKeysSettings", sampleRowKeysSettings)
.add("mutateRowSettings", mutateRowSettings)
.add("bulkMutateRowsSettings", bulkMutateRowsSettings)
.add("bulkReadRowsSettings", bulkReadRowsSettings)
.add("checkAndMutateRowSettings", checkAndMutateRowSettings)
.add("readModifyWriteRowSettings", readModifyWriteRowSettings)
.add("parent", super.toString())
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
import static com.google.common.truth.Truth.assertThat;

import com.google.api.gax.core.CredentialsProvider;
import com.google.api.gax.retrying.RetrySettings;
import com.google.api.gax.rpc.StatusCode.Code;
import com.google.cloud.bigtable.admin.v2.stub.BigtableInstanceAdminStubSettings;
import java.io.IOException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mockito;
import org.threeten.bp.Duration;

@RunWith(JUnit4.class)
public class BigtableInstanceAdminSettingsTest {
Expand Down Expand Up @@ -95,4 +98,63 @@ public void testStubSettings() throws IOException {
.getRetryableCodes())
.containsExactly(Code.INVALID_ARGUMENT);
}

static final String[] SETTINGS_LIST = {
"createInstanceSettings",
"createInstanceOperationSettings",
"getInstanceSettings",
"listInstancesSettings",
"partialUpdateInstanceSettings",
"partialUpdateInstanceOperationSettings",
"deleteInstanceSettings",
"createClusterSettings",
"createClusterOperationSettings",
"getClusterSettings",
"listClustersSettings",
"updateClusterSettings",
"updateClusterOperationSettings",
"deleteClusterSettings",
"createAppProfileSettings",
"getAppProfileSettings",
"listAppProfilesSettings",
"updateAppProfileSettings",
"updateAppProfileOperationSettings",
"deleteAppProfileSettings",
"getIamPolicySettings",
"setIamPolicySettings",
"testIamPermissionsSettings",
};

@Test
public void testToString() throws IOException {
BigtableInstanceAdminSettings defaultSettings =
BigtableInstanceAdminSettings.newBuilder().setProjectId("our-project-212").build();

checkToString(defaultSettings);

BigtableInstanceAdminSettings.Builder builder = defaultSettings.toBuilder();
BigtableInstanceAdminStubSettings.Builder stubSettings =
builder.stubSettings().setEndpoint("example.com:1234");

stubSettings
.getInstanceSettings()
.setRetrySettings(
RetrySettings.newBuilder().setTotalTimeout(Duration.ofMinutes(812)).build());

BigtableInstanceAdminSettings settings = builder.build();
checkToString(settings);
assertThat(settings.toString()).contains("endpoint=example.com:1234");
assertThat(settings.toString()).contains("totalTimeout=PT13H32M");
}

void checkToString(BigtableInstanceAdminSettings settings) {
String projectId = settings.getProjectId();
String toString = settings.toString();
assertThat(toString).isEqualTo(settings.toString()); // no variety
assertThat(toString).startsWith("BigtableInstanceAdminSettings{projectId=" + projectId);
for (String subSettings : SETTINGS_LIST) {
assertThat(toString).contains(subSettings + "=");
}
assertThat(toString.contains(settings.getStubSettings().toString()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@
import static com.google.common.truth.Truth.assertThat;

import com.google.api.gax.core.CredentialsProvider;
import com.google.api.gax.retrying.RetrySettings;
import com.google.api.gax.rpc.StatusCode.Code;
import com.google.cloud.bigtable.admin.v2.stub.BigtableTableAdminStubSettings;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mockito;
import org.threeten.bp.Duration;

@RunWith(JUnit4.class)
public class BigtableTableAdminSettingsTest {
Expand Down Expand Up @@ -97,4 +102,83 @@ public void testStubSettings() throws IOException {
.getRetryableCodes())
.containsExactly(Code.INVALID_ARGUMENT);
}

static final String[] SETTINGS_LIST = {
"createTableSettings",
"createTableFromSnapshotSettings",
"createTableFromSnapshotOperationSettings",
"listTablesSettings",
"getTableSettings",
"deleteTableSettings",
"modifyColumnFamiliesSettings",
"dropRowRangeSettings",
"generateConsistencyTokenSettings",
"checkConsistencySettings",
"getIamPolicySettings",
"setIamPolicySettings",
"testIamPermissionsSettings",
"snapshotTableSettings",
"snapshotTableOperationSettings",
"getSnapshotSettings",
"listSnapshotsSettings",
"deleteSnapshotSettings",
"createBackupSettings",
"createBackupOperationSettings",
"getBackupSettings",
"listBackupsSettings",
"updateBackupSettings",
"deleteBackupSettings",
"restoreTableSettings",
"restoreTableOperationSettings",
};

@Test
public void testToString() throws IOException {
BigtableTableAdminSettings defaultSettings =
BigtableTableAdminSettings.newBuilder()
.setProjectId("our-project-85")
.setInstanceId("our-instance-06")
.build();

checkToString(defaultSettings);

BigtableTableAdminSettings.Builder builder = defaultSettings.toBuilder();
BigtableTableAdminStubSettings.Builder stubSettings =
builder.stubSettings().setEndpoint("example.com:1234");

stubSettings
.getBackupSettings()
.setRetrySettings(
RetrySettings.newBuilder().setTotalTimeout(Duration.ofMinutes(812)).build());

BigtableTableAdminSettings settings = builder.build();
checkToString(settings);
assertThat(defaultSettings.toString()).doesNotContain("endpoint=example.com:1234");
assertThat(settings.toString()).contains("endpoint=example.com:1234");
assertThat(defaultSettings.toString()).doesNotContain("totalTimeout=PT13H32M");
assertThat(settings.toString()).contains("totalTimeout=PT13H32M");

int nonStaticFields = 0;
for (Field field : BigtableTableAdminStubSettings.class.getDeclaredFields()) {
if (!Modifier.isStatic(field.getModifiers())) {
nonStaticFields++;
}
}
// failure will signal about adding a new settings property
assertThat(SETTINGS_LIST.length).isEqualTo(nonStaticFields);
}

void checkToString(BigtableTableAdminSettings settings) {
String projectId = settings.getProjectId();
String instanceId = settings.getInstanceId();
String toString = settings.toString();
assertThat(toString).isEqualTo(settings.toString()); // no variety
assertThat(toString)
.startsWith(
"BigtableTableAdminSettings{projectId=" + projectId + ", instanceId=" + instanceId);
for (String subSettings : SETTINGS_LIST) {
assertThat(toString).contains(subSettings + "=");
}
assertThat(toString.contains(settings.getStubSettings().toString()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.bigtable.data.v2;

import static com.google.common.truth.Truth.assertThat;

import com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStubSettings;
import org.junit.Test;

public class BigtableDataSettingsTest {

@Test
public void testToString() {
BigtableDataSettings settings =
BigtableDataSettings.newBuilder()
.setProjectId("our-project-2-12")
.setInstanceId("our-instance-85")
.setAppProfileId("our-appProfile-06")
.build();
EnhancedBigtableStubSettings stubSettings = settings.getStubSettings();
assertThat(settings.toString())
.isEqualTo("BigtableDataSettings{stubSettings=" + stubSettings.toString() + "}");
}
}
Loading

0 comments on commit 4d821f8

Please sign in to comment.