|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.bigtable.admin.v2.it; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertWithMessage; |
| 20 | +import static com.google.common.truth.TruthJUnit.assume; |
| 21 | +import static org.junit.Assert.fail; |
| 22 | + |
| 23 | +import com.google.api.gax.rpc.NotFoundException; |
| 24 | +import com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminClient; |
| 25 | +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; |
| 26 | +import com.google.cloud.bigtable.admin.v2.models.CreateLogicalViewRequest; |
| 27 | +import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest; |
| 28 | +import com.google.cloud.bigtable.admin.v2.models.LogicalView; |
| 29 | +import com.google.cloud.bigtable.admin.v2.models.Table; |
| 30 | +import com.google.cloud.bigtable.admin.v2.models.UpdateLogicalViewRequest; |
| 31 | +import com.google.cloud.bigtable.test_helpers.env.EmulatorEnv; |
| 32 | +import com.google.cloud.bigtable.test_helpers.env.PrefixGenerator; |
| 33 | +import com.google.cloud.bigtable.test_helpers.env.TestEnvRule; |
| 34 | +import io.grpc.StatusRuntimeException; |
| 35 | +import java.util.List; |
| 36 | +import java.util.logging.Logger; |
| 37 | +import org.junit.Before; |
| 38 | +import org.junit.BeforeClass; |
| 39 | +import org.junit.ClassRule; |
| 40 | +import org.junit.Rule; |
| 41 | +import org.junit.Test; |
| 42 | +import org.junit.runner.RunWith; |
| 43 | +import org.junit.runners.JUnit4; |
| 44 | + |
| 45 | +@RunWith(JUnit4.class) |
| 46 | +public class BigtableLogicalViewIT { |
| 47 | + @ClassRule public static final TestEnvRule testEnvRule = new TestEnvRule(); |
| 48 | + @Rule public final PrefixGenerator prefixGenerator = new PrefixGenerator(); |
| 49 | + private static final Logger LOGGER = Logger.getLogger(BigtableLogicalViewIT.class.getName()); |
| 50 | + private static final int[] BACKOFF_DURATION = {2, 4, 8, 16, 32, 64, 128, 256, 512, 1024}; |
| 51 | + |
| 52 | + private static BigtableInstanceAdminClient client; |
| 53 | + private static Table testTable; |
| 54 | + |
| 55 | + private String instanceId = testEnvRule.env().getInstanceId(); |
| 56 | + |
| 57 | + // TODO: Update this test once emulator supports InstanceAdmin operation |
| 58 | + // https://github.com/googleapis/google-cloud-go/issues/1069 |
| 59 | + @BeforeClass |
| 60 | + public static void validatePlatform() { |
| 61 | + assume() |
| 62 | + .withMessage("BigtableInstanceAdminClient doesn't support on Emulator") |
| 63 | + .that(testEnvRule.env()) |
| 64 | + .isNotInstanceOf(EmulatorEnv.class); |
| 65 | + } |
| 66 | + |
| 67 | + @Before |
| 68 | + public void setUp() throws InterruptedException { |
| 69 | + client = testEnvRule.env().getInstanceAdminClient(); |
| 70 | + testTable = createTestTable(testEnvRule.env().getTableAdminClient()); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void createLogicalViewAndGetLogicalViewTest() { |
| 75 | + String logicalViewId = "createLogicalViewAndGetLogicalViewTest-logical-view"; |
| 76 | + |
| 77 | + CreateLogicalViewRequest request = |
| 78 | + CreateLogicalViewRequest.of(instanceId, logicalViewId).setQuery(getQuery()); |
| 79 | + try { |
| 80 | + LogicalView response = client.createLogicalView(request); |
| 81 | + assertWithMessage("Got wrong logical view Id in CreateLogicalView") |
| 82 | + .that(response.getId()) |
| 83 | + .isEqualTo(logicalViewId); |
| 84 | + assertWithMessage("Got wrong query in CreateLogicalView") |
| 85 | + .that(response.getQuery()) |
| 86 | + .isEqualTo(getQuery()); |
| 87 | + |
| 88 | + response = client.getLogicalView(instanceId, logicalViewId); |
| 89 | + assertWithMessage("Got wrong logical view Id in getLogicalView") |
| 90 | + .that(response.getId()) |
| 91 | + .isEqualTo(logicalViewId); |
| 92 | + assertWithMessage("Got wrong query in getLogicalView") |
| 93 | + .that(response.getQuery()) |
| 94 | + .isEqualTo(getQuery()); |
| 95 | + } finally { |
| 96 | + client.deleteLogicalView(instanceId, logicalViewId); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void listLogicalViewsTest() { |
| 102 | + String logicalViewId = "listLogicalViewsTest-logical-view"; |
| 103 | + |
| 104 | + try { |
| 105 | + LogicalView logicalView = client.createLogicalView(createLogicalViewRequest(logicalViewId)); |
| 106 | + |
| 107 | + List<LogicalView> response = client.listLogicalViews(instanceId); |
| 108 | + assertWithMessage("Got wrong logical view Ids in listLogicalViews") |
| 109 | + .that(response) |
| 110 | + .contains(logicalView); |
| 111 | + } finally { |
| 112 | + client.deleteLogicalView(instanceId, logicalViewId); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void updateLogicalViewAndDeleteLogicalViewTest() throws InterruptedException { |
| 118 | + String logicalViewId = "updateLogicalViewAndDeleteLogicalViewTest-logical-view"; |
| 119 | + |
| 120 | + // Create a logical view. |
| 121 | + CreateLogicalViewRequest request = createLogicalViewRequest(logicalViewId); |
| 122 | + |
| 123 | + LogicalView response = client.createLogicalView(request); |
| 124 | + |
| 125 | + // Update the query of the logical view. |
| 126 | + String query = "SELECT 1 AS value"; |
| 127 | + UpdateLogicalViewRequest updateRequest = UpdateLogicalViewRequest.of(response).setQuery(query); |
| 128 | + response = client.updateLogicalView(updateRequest); |
| 129 | + assertWithMessage("Got wrong query in UpdateLogicalView") |
| 130 | + .that(response.getQuery()) |
| 131 | + .isEqualTo(query); |
| 132 | + |
| 133 | + // Now we should be able to successfully delete the LogicalView. |
| 134 | + client.deleteLogicalView(instanceId, logicalViewId); |
| 135 | + try { |
| 136 | + for (int i = 0; i < BACKOFF_DURATION.length; i++) { |
| 137 | + client.getLogicalView(instanceId, logicalViewId); |
| 138 | + |
| 139 | + LOGGER.info( |
| 140 | + "Wait for " |
| 141 | + + BACKOFF_DURATION[i] |
| 142 | + + " seconds for deleting logical view " |
| 143 | + + logicalViewId); |
| 144 | + Thread.sleep(BACKOFF_DURATION[i] * 1000); |
| 145 | + } |
| 146 | + fail("LogicalView was not deleted."); |
| 147 | + } catch (NotFoundException e) { |
| 148 | + assertWithMessage("Incorrect exception type") |
| 149 | + .that(e.getCause()) |
| 150 | + .isInstanceOf(StatusRuntimeException.class); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private CreateLogicalViewRequest createLogicalViewRequest(String logicalViewId) { |
| 155 | + return CreateLogicalViewRequest.of(instanceId, logicalViewId).setQuery(getQuery()); |
| 156 | + } |
| 157 | + |
| 158 | + private String getQuery() { |
| 159 | + return "SELECT _key, cf1['column'] as column FROM `" + testTable.getId() + "`"; |
| 160 | + } |
| 161 | + |
| 162 | + private static Table createTestTable(BigtableTableAdminClient tableAdmin) |
| 163 | + throws InterruptedException { |
| 164 | + String tableId = PrefixGenerator.newPrefix("BigtableLogicalViewIT#createTestTable"); |
| 165 | + Table testTable = tableAdmin.createTable(CreateTableRequest.of(tableId).addFamily("cf1")); |
| 166 | + |
| 167 | + return testTable; |
| 168 | + } |
| 169 | +} |
0 commit comments