Skip to content

Commit

Permalink
test: Rewrite SampleRowKeys IT on AuthorizedView (#2187)
Browse files Browse the repository at this point in the history
* test: Rewrite SampleRowKeys IT on authorized view

Change-Id: I37927d1b0a3502f6554e575ac44d4d92417d7d26

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

---------

Co-authored-by: Lixia Chen <[email protected]>
Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Mar 28, 2024
1 parent eba0898 commit 3ccd8fc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 32 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ implementation 'com.google.cloud:google-cloud-bigtable'
If you are using Gradle without BOM, add this to your dependencies:

```Groovy
implementation 'com.google.cloud:google-cloud-bigtable:2.36.0'
implementation 'com.google.cloud:google-cloud-bigtable:2.37.0'
```

If you are using SBT, add this to your dependencies:

```Scala
libraryDependencies += "com.google.cloud" % "google-cloud-bigtable" % "2.36.0"
libraryDependencies += "com.google.cloud" % "google-cloud-bigtable" % "2.37.0"
```
<!-- {x-version-update-end} -->

Expand Down Expand Up @@ -609,7 +609,7 @@ Java is a registered trademark of Oracle and/or its affiliates.
[kokoro-badge-link-5]: http://storage.googleapis.com/cloud-devrel-public/java/badges/java-bigtable/java11.html
[stability-image]: https://img.shields.io/badge/stability-stable-green
[maven-version-image]: https://img.shields.io/maven-central/v/com.google.cloud/google-cloud-bigtable.svg
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-bigtable/2.36.0
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-bigtable/2.37.0
[authentication]: https://github.com/googleapis/google-cloud-java#authentication
[auth-scopes]: https://developers.google.com/identity/protocols/oauth2/scopes
[predefined-iam-roles]: https://cloud.google.com/iam/docs/understanding-roles#predefined_roles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,24 @@
*/
package com.google.cloud.bigtable.data.v2.it;

import static com.google.cloud.bigtable.misc_utilities.AuthorizedViewTestHelper.AUTHORIZED_VIEW_COLUMN_QUALIFIER;
import static com.google.cloud.bigtable.misc_utilities.AuthorizedViewTestHelper.AUTHORIZED_VIEW_ROW_PREFIX;
import static com.google.cloud.bigtable.misc_utilities.AuthorizedViewTestHelper.createTestAuthorizedView;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.TruthJUnit.assume;

import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.cloud.bigtable.admin.v2.models.AuthorizedView;
import com.google.cloud.bigtable.admin.v2.models.CreateAuthorizedViewRequest;
import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest;
import com.google.cloud.bigtable.admin.v2.models.SubsetView;
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
import com.google.cloud.bigtable.data.v2.models.AuthorizedViewId;
import com.google.cloud.bigtable.data.v2.models.KeyOffset;
import com.google.cloud.bigtable.data.v2.models.RowMutation;
import com.google.cloud.bigtable.test_helpers.env.EmulatorEnv;
import com.google.cloud.bigtable.test_helpers.env.TestEnvRule;
import com.google.common.collect.Lists;
import com.google.protobuf.ByteString;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -75,42 +78,50 @@ public void testOnAuthorizedView()
.withMessage("AuthorizedView is not supported on Emulator")
.that(testEnvRule.env())
.isNotInstanceOf(EmulatorEnv.class);

AuthorizedView testAuthorizedView = createTestAuthorizedView(testEnvRule);
AuthorizedView testAuthorizedView = createPreSplitTableAndAuthorizedView();

BigtableDataClient client = testEnvRule.env().getDataClient();
String rowPrefix = AUTHORIZED_VIEW_ROW_PREFIX + UUID.randomUUID();
String rowPrefixOutsideAuthorizedView = UUID.randomUUID() + "-outside-authorized-view";

// Create some data so that sample row keys has something to show
List<ApiFuture<?>> futures = Lists.newArrayList();
for (int i = 0; i < 10; i++) {
ApiFuture<Void> future =
client.mutateRowAsync(
RowMutation.create(testEnvRule.env().getTableId(), rowPrefix + "-" + i)
.setCell(
testEnvRule.env().getFamilyId(), AUTHORIZED_VIEW_COLUMN_QUALIFIER, "value"));
futures.add(future);
ApiFuture<Void> futureOutsideAuthorizedView =
client.mutateRowAsync(
RowMutation.create(
testEnvRule.env().getTableId(), rowPrefixOutsideAuthorizedView + "-" + i)
.setCell(
testEnvRule.env().getFamilyId(), AUTHORIZED_VIEW_COLUMN_QUALIFIER, "value"));
futures.add(futureOutsideAuthorizedView);
ApiFuture<List<KeyOffset>> future =
client.sampleRowKeysAsync(
AuthorizedViewId.of(testAuthorizedView.getTableId(), testAuthorizedView.getId()));

List<KeyOffset> results = future.get(1, TimeUnit.MINUTES);

List<ByteString> resultKeys = new ArrayList<>();
for (KeyOffset keyOffset : results) {
resultKeys.add(keyOffset.getKey());
}
ApiFutures.allAsList(futures).get(1, TimeUnit.MINUTES);

ApiFuture<List<KeyOffset>> future = client.sampleRowKeysAsync(testEnvRule.env().getTableId());
assertThat(resultKeys)
.containsExactly(
ByteString.copyFromUtf8("food"),
ByteString.copyFromUtf8("fool"),
ByteString.copyFromUtf8("fop"));

List<KeyOffset> results = future.get(1, TimeUnit.MINUTES);
testEnvRule
.env()
.getTableAdminClient()
.deleteAuthorizedView(testAuthorizedView.getTableId(), testAuthorizedView.getId());
}

assertThat(results).isNotEmpty();
assertThat(results.get(results.size() - 1).getOffsetBytes()).isGreaterThan(0L);
private static AuthorizedView createPreSplitTableAndAuthorizedView() {
String tableId = UUID.randomUUID().toString();
String authorizedViewId = UUID.randomUUID().toString();

testEnvRule
.env()
.getTableAdminClient()
.deleteAuthorizedView(testEnvRule.env().getTableId(), testAuthorizedView.getId());
.createTable(
CreateTableRequest.of(tableId)
.addSplit(ByteString.copyFromUtf8("apple"))
.addSplit(ByteString.copyFromUtf8("food"))
.addSplit(ByteString.copyFromUtf8("fool"))
.addSplit(ByteString.copyFromUtf8("good")));
CreateAuthorizedViewRequest request =
CreateAuthorizedViewRequest.of(tableId, authorizedViewId)
.setAuthorizedViewType(SubsetView.create().addRowPrefix("foo"))
.setDeletionProtection(false);
return testEnvRule.env().getTableAdminClient().createAuthorizedView(request);
}
}

0 comments on commit 3ccd8fc

Please sign in to comment.