Skip to content
This repository was archived by the owner on May 8, 2026. It is now read-only.

Commit 3b4e6b4

Browse files
committed
fix more tests
1 parent 07f0688 commit 3b4e6b4

1 file changed

Lines changed: 220 additions & 0 deletions

File tree

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
/*
2+
* Copyright 2024 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.batching.Batcher;
24+
import com.google.api.gax.rpc.NotFoundException;
25+
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
26+
import com.google.cloud.bigtable.admin.v2.models.CreateSchemaBundleRequest;
27+
import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest;
28+
import com.google.cloud.bigtable.admin.v2.models.SchemaBundle;
29+
import com.google.cloud.bigtable.admin.v2.models.Table;
30+
import com.google.cloud.bigtable.admin.v2.models.UpdateSchemaBundleRequest;
31+
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
32+
import com.google.cloud.bigtable.data.v2.models.RowMutationEntry;
33+
import com.google.cloud.bigtable.test_helpers.env.EmulatorEnv;
34+
import com.google.cloud.bigtable.test_helpers.env.PrefixGenerator;
35+
import com.google.cloud.bigtable.test_helpers.env.TestEnvRule;
36+
import com.google.protobuf.ByteString;
37+
import io.grpc.StatusRuntimeException;
38+
import java.io.IOException;
39+
import java.net.URISyntaxException;
40+
import java.net.URL;
41+
import java.nio.file.Files;
42+
import java.nio.file.Paths;
43+
import java.util.List;
44+
import java.util.Random;
45+
import java.util.logging.Logger;
46+
import org.junit.AfterClass;
47+
import org.junit.BeforeClass;
48+
import org.junit.ClassRule;
49+
import org.junit.Rule;
50+
import org.junit.Test;
51+
import org.junit.runner.RunWith;
52+
import org.junit.runners.JUnit4;
53+
54+
@RunWith(JUnit4.class)
55+
public class BigtableSchemaBundleIT {
56+
@ClassRule public static final TestEnvRule testEnvRule = new TestEnvRule();
57+
@Rule public final PrefixGenerator prefixGenerator = new PrefixGenerator();
58+
private static final Logger LOGGER = Logger.getLogger(BigtableSchemaBundleIT.class.getName());
59+
private static final int[] BACKOFF_DURATION = {2, 4, 8, 16, 32, 64, 128, 256, 512, 1024};
60+
// Location: `google-cloud-bigtable/src/test/resources/proto_schema_bundle.pb`
61+
private static final String TEST_PROTO_SCHEMA_BUNDLE = "proto_schema_bundle.pb";
62+
// Location:
63+
// `google-cloud-bigtable/src/test/resources/updated_proto_schema_bundle.pb`
64+
private static final String TEST_UPDATED_PROTO_SCHEMA_BUNDLE = "updated_proto_schema_bundle.pb";
65+
66+
private static BigtableTableAdminClient tableAdmin;
67+
private static BigtableDataClient dataClient;
68+
private static Table testTable;
69+
70+
@BeforeClass
71+
public static void setUpClass() throws InterruptedException {
72+
assume()
73+
.withMessage("BigtableInstanceAdminClient is not supported on Emulator")
74+
.that(testEnvRule.env())
75+
.isNotInstanceOf(EmulatorEnv.class);
76+
77+
tableAdmin = testEnvRule.env().getTableAdminClient();
78+
dataClient = testEnvRule.env().getDataClient();
79+
80+
testTable = createAndPopulateTestTable(tableAdmin, dataClient);
81+
}
82+
83+
@AfterClass
84+
public static void tearDownClass() {
85+
if (testTable != null) {
86+
try {
87+
tableAdmin.deleteTable(testTable.getId());
88+
} catch (Exception e) {
89+
// Ignore.
90+
}
91+
}
92+
}
93+
94+
@Test
95+
public void createSchemaBundleAndGetSchemaBundleTest() throws IOException, URISyntaxException {
96+
String SchemaBundleId = prefixGenerator.newPrefix();
97+
byte[] content = Files.readAllBytes(Paths.get(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE)));
98+
99+
CreateSchemaBundleRequest request =
100+
CreateSchemaBundleRequest.of(testTable.getId(), SchemaBundleId)
101+
.setProtoSchema(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE));
102+
try {
103+
SchemaBundle response = tableAdmin.createSchemaBundle(request);
104+
assertWithMessage("Got wrong schema bundle Id in createSchemaBundle")
105+
.that(response.getId())
106+
.isEqualTo(SchemaBundleId);
107+
assertWithMessage("Got wrong proto schema in createSchemaBundle")
108+
.that(response.getProtoSchema())
109+
.isEqualTo(ByteString.copyFrom(content));
110+
111+
response = tableAdmin.getSchemaBundle(testTable.getId(), SchemaBundleId);
112+
assertWithMessage("Got wrong schema bundle Id in getSchemaBundle")
113+
.that(response.getId())
114+
.isEqualTo(SchemaBundleId);
115+
assertWithMessage("Got wrong proto schema in getSchemaBundle")
116+
.that(response.getProtoSchema())
117+
.isEqualTo(ByteString.copyFrom(content));
118+
} finally {
119+
tableAdmin.deleteSchemaBundle(testTable.getId(), SchemaBundleId);
120+
}
121+
}
122+
123+
@Test
124+
public void listSchemaBundlesTest() throws IOException, URISyntaxException {
125+
String SchemaBundleId1 = prefixGenerator.newPrefix();
126+
String SchemaBundleId2 = prefixGenerator.newPrefix();
127+
128+
try {
129+
tableAdmin.createSchemaBundle(createSchemaBundleRequest(SchemaBundleId1));
130+
tableAdmin.createSchemaBundle(createSchemaBundleRequest(SchemaBundleId2));
131+
132+
List<String> response = tableAdmin.listSchemaBundles(testTable.getId());
133+
// Concurrent tests running may cause flakiness. Use containsAtLeast instead of
134+
// containsExactly.
135+
assertWithMessage("Got wrong schema bundle Ids in listSchemaBundles")
136+
.that(response)
137+
.containsAtLeast(
138+
tableAdmin.getSchemaBundle(testTable.getId(), SchemaBundleId1).getId(),
139+
tableAdmin.getSchemaBundle(testTable.getId(), SchemaBundleId2).getId());
140+
} finally {
141+
tableAdmin.deleteSchemaBundle(testTable.getId(), SchemaBundleId1);
142+
tableAdmin.deleteSchemaBundle(testTable.getId(), SchemaBundleId2);
143+
}
144+
}
145+
146+
@Test
147+
public void updateSchemaBundleAndDeleteSchemaBundleTest()
148+
throws InterruptedException, IOException, URISyntaxException {
149+
String SchemaBundleId = prefixGenerator.newPrefix();
150+
151+
// Create a schema bundle.
152+
CreateSchemaBundleRequest request = createSchemaBundleRequest(SchemaBundleId);
153+
154+
SchemaBundle response = tableAdmin.createSchemaBundle(request);
155+
156+
// Update the schema bundle.
157+
byte[] content =
158+
Files.readAllBytes(Paths.get(getResourceFilePath(TEST_UPDATED_PROTO_SCHEMA_BUNDLE)));
159+
UpdateSchemaBundleRequest updateRequest =
160+
UpdateSchemaBundleRequest.of(testTable.getId(), SchemaBundleId)
161+
.setProtoSchema(getResourceFilePath(TEST_UPDATED_PROTO_SCHEMA_BUNDLE));
162+
response = tableAdmin.updateSchemaBundle(updateRequest);
163+
assertWithMessage("Got wrong deletion protection in UpdateSchemaBundle")
164+
.that(response.getProtoSchema())
165+
.isEqualTo(ByteString.copyFrom(content));
166+
167+
// Now we should be able to successfully delete the SchemaBundle.
168+
tableAdmin.deleteSchemaBundle(testTable.getId(), SchemaBundleId);
169+
try {
170+
for (int i = 0; i < BACKOFF_DURATION.length; i++) {
171+
tableAdmin.getSchemaBundle(testTable.getId(), SchemaBundleId);
172+
173+
LOGGER.info(
174+
"Wait for "
175+
+ BACKOFF_DURATION[i]
176+
+ " seconds for deleting schema bundle "
177+
+ SchemaBundleId);
178+
Thread.sleep(BACKOFF_DURATION[i] * 1000);
179+
}
180+
fail("SchemaBundle was not deleted.");
181+
} catch (NotFoundException e) {
182+
assertWithMessage("Incorrect exception type")
183+
.that(e.getCause())
184+
.isInstanceOf(StatusRuntimeException.class);
185+
}
186+
}
187+
188+
private CreateSchemaBundleRequest createSchemaBundleRequest(String SchemaBundleId)
189+
throws IOException, URISyntaxException {
190+
return CreateSchemaBundleRequest.of(testTable.getId(), SchemaBundleId)
191+
.setProtoSchema(getResourceFilePath(TEST_PROTO_SCHEMA_BUNDLE));
192+
}
193+
194+
private static Table createAndPopulateTestTable(
195+
BigtableTableAdminClient tableAdmin, BigtableDataClient dataClient)
196+
throws InterruptedException {
197+
String tableId = PrefixGenerator.newPrefix("BigtableSchemaBundleIT#createAndPopulateTestTable");
198+
Table testTable = tableAdmin.createTable(CreateTableRequest.of(tableId).addFamily("cf1"));
199+
200+
// Populate test data.
201+
byte[] rowBytes = new byte[1024];
202+
Random random = new Random();
203+
random.nextBytes(rowBytes);
204+
205+
try (Batcher<RowMutationEntry, Void> batcher = dataClient.newBulkMutationBatcher(tableId)) {
206+
for (int i = 0; i < 10; i++) {
207+
batcher.add(
208+
RowMutationEntry.create("test-row-" + i)
209+
.setCell("cf1", ByteString.EMPTY, ByteString.copyFrom(rowBytes)));
210+
}
211+
}
212+
return testTable;
213+
}
214+
215+
private String getResourceFilePath(String filePath) throws URISyntaxException {
216+
ClassLoader cl = Thread.currentThread().getContextClassLoader();
217+
URL protoSchema = cl.getResource(filePath);
218+
return Paths.get(protoSchema.toURI()).toAbsolutePath().toString();
219+
}
220+
}

0 commit comments

Comments
 (0)