Skip to content

feat(bigtable): Add integration tests for Materialized/Logical Views #2518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/*
* Copyright 2025 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.admin.v2.it;

import static com.google.common.truth.Truth.assertWithMessage;
import static com.google.common.truth.TruthJUnit.assume;
import static org.junit.Assert.fail;

import com.google.api.gax.rpc.NotFoundException;
import com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminClient;
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
import com.google.cloud.bigtable.admin.v2.models.CreateLogicalViewRequest;
import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest;
import com.google.cloud.bigtable.admin.v2.models.LogicalView;
import com.google.cloud.bigtable.admin.v2.models.Table;
import com.google.cloud.bigtable.admin.v2.models.UpdateLogicalViewRequest;
import com.google.cloud.bigtable.test_helpers.env.EmulatorEnv;
import com.google.cloud.bigtable.test_helpers.env.PrefixGenerator;
import com.google.cloud.bigtable.test_helpers.env.TestEnvRule;
import io.grpc.StatusRuntimeException;
import java.util.List;
import java.util.logging.Logger;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class BigtableLogicalViewIT {
@ClassRule public static final TestEnvRule testEnvRule = new TestEnvRule();
@Rule public final PrefixGenerator prefixGenerator = new PrefixGenerator();
private static final Logger LOGGER = Logger.getLogger(BigtableLogicalViewIT.class.getName());
private static final int[] BACKOFF_DURATION = {2, 4, 8, 16, 32, 64, 128, 256, 512, 1024};

private static BigtableInstanceAdminClient client;
private static Table testTable;

private String instanceId = testEnvRule.env().getInstanceId();

// TODO: Update this test once emulator supports InstanceAdmin operation
// https://github.com/googleapis/google-cloud-go/issues/1069
@BeforeClass
public static void validatePlatform() {
assume()
.withMessage("BigtableInstanceAdminClient doesn't support on Emulator")
.that(testEnvRule.env())
.isNotInstanceOf(EmulatorEnv.class);
}

@Before
public void setUp() throws InterruptedException {
client = testEnvRule.env().getInstanceAdminClient();
testTable = createTestTable(testEnvRule.env().getTableAdminClient());
}

@Test
public void createLogicalViewAndGetLogicalViewTest() {
String logicalViewId = "createLogicalViewAndGetLogicalViewTest-logical-view";

CreateLogicalViewRequest request =
CreateLogicalViewRequest.of(instanceId, logicalViewId).setQuery(getQuery());
try {
LogicalView response = client.createLogicalView(request);
assertWithMessage("Got wrong logical view Id in CreateLogicalView")
.that(response.getId())
.isEqualTo(logicalViewId);
assertWithMessage("Got wrong query in CreateLogicalView")
.that(response.getQuery())
.isEqualTo(getQuery());

response = client.getLogicalView(instanceId, logicalViewId);
assertWithMessage("Got wrong logical view Id in getLogicalView")
.that(response.getId())
.isEqualTo(logicalViewId);
assertWithMessage("Got wrong query in getLogicalView")
.that(response.getQuery())
.isEqualTo(getQuery());
} finally {
client.deleteLogicalView(instanceId, logicalViewId);
}
}

@Test
public void listLogicalViewsTest() {
String logicalViewId = "listLogicalViewsTest-logical-view";

try {
LogicalView logicalView = client.createLogicalView(createLogicalViewRequest(logicalViewId));

List<LogicalView> response = client.listLogicalViews(instanceId);
assertWithMessage("Got wrong logical view Ids in listLogicalViews")
.that(response)
.contains(logicalView);
} finally {
client.deleteLogicalView(instanceId, logicalViewId);
}
}

@Test
public void updateLogicalViewAndDeleteLogicalViewTest() throws InterruptedException {
String logicalViewId = "updateLogicalViewAndDeleteLogicalViewTest-logical-view";

// Create a logical view.
CreateLogicalViewRequest request = createLogicalViewRequest(logicalViewId);

LogicalView response = client.createLogicalView(request);

// Update the query of the logical view.
String query = "SELECT 1 AS value";
UpdateLogicalViewRequest updateRequest = UpdateLogicalViewRequest.of(response).setQuery(query);
response = client.updateLogicalView(updateRequest);
assertWithMessage("Got wrong query in UpdateLogicalView")
.that(response.getQuery())
.isEqualTo(query);

// Now we should be able to successfully delete the LogicalView.
client.deleteLogicalView(instanceId, logicalViewId);
try {
for (int i = 0; i < BACKOFF_DURATION.length; i++) {
client.getLogicalView(instanceId, logicalViewId);

LOGGER.info(
"Wait for "
+ BACKOFF_DURATION[i]
+ " seconds for deleting logical view "
+ logicalViewId);
Thread.sleep(BACKOFF_DURATION[i] * 1000);
}
fail("LogicalView was not deleted.");
} catch (NotFoundException e) {
assertWithMessage("Incorrect exception type")
.that(e.getCause())
.isInstanceOf(StatusRuntimeException.class);
}
}

private CreateLogicalViewRequest createLogicalViewRequest(String logicalViewId) {
return CreateLogicalViewRequest.of(instanceId, logicalViewId).setQuery(getQuery());
}

private String getQuery() {
return "SELECT _key, cf1['column'] as column FROM `" + testTable.getId() + "`";
}

private static Table createTestTable(BigtableTableAdminClient tableAdmin)
throws InterruptedException {
String tableId = PrefixGenerator.newPrefix("BigtableLogicalViewIT#createTestTable");
Table testTable = tableAdmin.createTable(CreateTableRequest.of(tableId).addFamily("cf1"));

return testTable;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/*
* Copyright 2025 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.admin.v2.it;

import static com.google.common.truth.Truth.assertWithMessage;
import static com.google.common.truth.TruthJUnit.assume;
import static org.junit.Assert.fail;

import com.google.api.gax.rpc.FailedPreconditionException;
import com.google.api.gax.rpc.NotFoundException;
import com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminClient;
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
import com.google.cloud.bigtable.admin.v2.models.CreateMaterializedViewRequest;
import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest;
import com.google.cloud.bigtable.admin.v2.models.MaterializedView;
import com.google.cloud.bigtable.admin.v2.models.Table;
import com.google.cloud.bigtable.admin.v2.models.UpdateMaterializedViewRequest;
import com.google.cloud.bigtable.test_helpers.env.EmulatorEnv;
import com.google.cloud.bigtable.test_helpers.env.PrefixGenerator;
import com.google.cloud.bigtable.test_helpers.env.TestEnvRule;
import io.grpc.StatusRuntimeException;
import java.util.List;
import java.util.logging.Logger;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class BigtableMaterializedViewIT {
@ClassRule public static final TestEnvRule testEnvRule = new TestEnvRule();
@Rule public final PrefixGenerator prefixGenerator = new PrefixGenerator();
private static final Logger LOGGER = Logger.getLogger(BigtableMaterializedViewIT.class.getName());
private static final int[] BACKOFF_DURATION = {2, 4, 8, 16, 32, 64, 128, 256, 512, 1024};

private static BigtableInstanceAdminClient client;
private static Table testTable;

private String instanceId = testEnvRule.env().getInstanceId();

// TODO: Update this test once emulator supports InstanceAdmin operation
// https://github.com/googleapis/google-cloud-go/issues/1069
@BeforeClass
public static void validatePlatform() {
assume()
.withMessage("BigtableInstanceAdminClient doesn't support on Emulator")
.that(testEnvRule.env())
.isNotInstanceOf(EmulatorEnv.class);
}

@Before
public void setUp() throws InterruptedException {
client = testEnvRule.env().getInstanceAdminClient();
testTable = createTestTable(testEnvRule.env().getTableAdminClient());
}

@Test
public void createMaterializedViewAndGetMaterializedViewTest() {
String materializedViewId = prefixGenerator.newPrefix();

CreateMaterializedViewRequest request =
CreateMaterializedViewRequest.of(instanceId, materializedViewId)
.setQuery(getQuery())
.setDeletionProtection(false);
try {
MaterializedView response = client.createMaterializedView(request);
assertWithMessage("Got wrong materialized view Id in CreateMaterializedView")
.that(response.getId())
.isEqualTo(materializedViewId);
assertWithMessage("Got wrong deletion protection in CreateMaterializedView")
.that(response.isDeletionProtected())
.isFalse();
assertWithMessage("Got wrong deletion protection in CreateMaterializedView")
.that(response.getQuery())
.isEqualTo(getQuery());

response = client.getMaterializedView(instanceId, materializedViewId);
assertWithMessage("Got wrong materialized view Id in getMaterializedView")
.that(response.getId())
.isEqualTo(materializedViewId);
assertWithMessage("Got wrong deletion protection in getMaterializedView")
.that(response.isDeletionProtected())
.isFalse();
assertWithMessage("Got wrong deletion protection in getMaterializedView")
.that(response.getQuery())
.isEqualTo(getQuery());
} finally {
client.deleteMaterializedView(instanceId, materializedViewId);
}
}

@Test
public void listMaterializedViewsTest() {
String materializedViewId = prefixGenerator.newPrefix();

try {
MaterializedView materializedView =
client.createMaterializedView(createMaterializedViewRequest(materializedViewId));

List<MaterializedView> response = client.listMaterializedViews(instanceId);
assertWithMessage("Got wrong materialized view Ids in listMaterializedViews")
.that(response)
.contains(materializedView);
} finally {
client.deleteMaterializedView(instanceId, materializedViewId);
}
}

@Test
public void updateMaterializedViewAndDeleteMaterializedViewTest() throws InterruptedException {
String materializedViewId = prefixGenerator.newPrefix();

// Create a deletion-protected materialized view.
CreateMaterializedViewRequest request =
createMaterializedViewRequest(materializedViewId).setDeletionProtection(true);

MaterializedView response = client.createMaterializedView(request);
assertWithMessage("Got wrong deletion protection in CreateMaterializedView")
.that(response.isDeletionProtected())
.isTrue();

// We should not be able to delete the materialized view.
try {
client.deleteMaterializedView(instanceId, materializedViewId);
fail("A delete-protected materialized view should not have been able to be deleted");
} catch (FailedPreconditionException e) {
assertWithMessage("Incorrect exception type")
.that(e.getCause())
.isInstanceOf(StatusRuntimeException.class);
}

// Update the deletion protection bit of the materialized view.
UpdateMaterializedViewRequest updateRequest =
UpdateMaterializedViewRequest.of(response).setDeletionProtection(false);
response = client.updateMaterializedView(updateRequest);
assertWithMessage("Got wrong deletion protection in UpdateMaterializedView")
.that(response.isDeletionProtected())
.isFalse();

// Now we should be able to successfully delete the MaterializedView.
client.deleteMaterializedView(instanceId, materializedViewId);
try {
for (int i = 0; i < BACKOFF_DURATION.length; i++) {
client.getMaterializedView(instanceId, materializedViewId);

LOGGER.info(
"Wait for "
+ BACKOFF_DURATION[i]
+ " seconds for deleting materialized view "
+ materializedViewId);
Thread.sleep(BACKOFF_DURATION[i] * 1000);
}
fail("MaterializedView was not deleted.");
} catch (NotFoundException e) {
assertWithMessage("Incorrect exception type")
.that(e.getCause())
.isInstanceOf(StatusRuntimeException.class);
}
}

private CreateMaterializedViewRequest createMaterializedViewRequest(String materializedViewId) {
return CreateMaterializedViewRequest.of(instanceId, materializedViewId).setQuery(getQuery());
}

private String getQuery() {
return "SELECT _key, MAX(cf1['column']) as column FROM `"
+ testTable.getId()
+ "` GROUP BY _key";
}

private static Table createTestTable(BigtableTableAdminClient tableAdmin)
throws InterruptedException {
String tableId = PrefixGenerator.newPrefix("BigtableMaterializedViewIT#createTestTable");
Table testTable = tableAdmin.createTable(CreateTableRequest.of(tableId).addFamily("cf1"));

return testTable;
}
}
Loading