|
| 1 | +/* |
| 2 | + * Copyright 2018 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 | +package com.google.cloud.bigtable.data.v2.stub; |
| 17 | + |
| 18 | +import static com.google.common.truth.Truth.assertWithMessage; |
| 19 | + |
| 20 | +import com.google.api.gax.core.NoCredentialsProvider; |
| 21 | +import com.google.api.gax.grpc.testing.InProcessServer; |
| 22 | +import com.google.api.gax.grpc.testing.LocalChannelProvider; |
| 23 | +import com.google.bigtable.v2.BigtableGrpc; |
| 24 | +import com.google.bigtable.v2.TableName; |
| 25 | +import com.google.cloud.bigtable.data.v2.BigtableDataClient; |
| 26 | +import com.google.cloud.bigtable.data.v2.BigtableDataSettings; |
| 27 | +import com.google.cloud.bigtable.data.v2.models.BulkMutationBatcher; |
| 28 | +import com.google.cloud.bigtable.data.v2.models.BulkMutationBatcher.BulkMutationFailure; |
| 29 | +import com.google.cloud.bigtable.data.v2.models.ConditionalRowMutation; |
| 30 | +import com.google.cloud.bigtable.data.v2.models.InstanceName; |
| 31 | +import com.google.cloud.bigtable.data.v2.models.Mutation; |
| 32 | +import com.google.cloud.bigtable.data.v2.models.Query; |
| 33 | +import com.google.cloud.bigtable.data.v2.models.ReadModifyWriteRow; |
| 34 | +import com.google.cloud.bigtable.data.v2.models.RowMutation; |
| 35 | +import java.util.concurrent.TimeoutException; |
| 36 | +import java.util.regex.Pattern; |
| 37 | +import org.junit.After; |
| 38 | +import org.junit.Before; |
| 39 | +import org.junit.Test; |
| 40 | +import org.junit.runner.RunWith; |
| 41 | +import org.junit.runners.JUnit4; |
| 42 | + |
| 43 | +@RunWith(JUnit4.class) |
| 44 | +public class ResourceHeaderTest { |
| 45 | + private static final String NAME = "resource-header-test:123"; |
| 46 | + private static final TableName TABLE_NAME = |
| 47 | + TableName.of("fake-project", "fake-instance", "fake-table"); |
| 48 | + private static final Pattern EXPECTED_HEADER_PATTERN = |
| 49 | + Pattern.compile(".*" + TABLE_NAME.toString() + ".*"); |
| 50 | + private static final String HEADER_NAME = "x-goog-request-params"; |
| 51 | + |
| 52 | + private InProcessServer<?> server; |
| 53 | + private LocalChannelProvider channelProvider; |
| 54 | + private BigtableDataClient client; |
| 55 | + |
| 56 | + @Before |
| 57 | + public void setUp() throws Exception { |
| 58 | + server = new InProcessServer<>(new BigtableGrpc.BigtableImplBase() {}, NAME); |
| 59 | + server.start(); |
| 60 | + channelProvider = LocalChannelProvider.create(NAME); |
| 61 | + |
| 62 | + BigtableDataSettings.Builder settings = |
| 63 | + BigtableDataSettings.newBuilder() |
| 64 | + .setInstanceName(InstanceName.of(TABLE_NAME.getProject(), TABLE_NAME.getInstance())) |
| 65 | + .setTransportChannelProvider(channelProvider) |
| 66 | + .setCredentialsProvider(NoCredentialsProvider.create()); |
| 67 | + |
| 68 | + // Force immediate flush |
| 69 | + settings |
| 70 | + .bulkMutationsSettings() |
| 71 | + .setBatchingSettings( |
| 72 | + settings |
| 73 | + .bulkMutationsSettings() |
| 74 | + .getBatchingSettings() |
| 75 | + .toBuilder() |
| 76 | + .setElementCountThreshold(1L) |
| 77 | + .build()); |
| 78 | + |
| 79 | + client = BigtableDataClient.create(settings.build()); |
| 80 | + } |
| 81 | + |
| 82 | + @After |
| 83 | + public void tearDown() throws Exception { |
| 84 | + client.close(); |
| 85 | + server.stop(); |
| 86 | + server.blockUntilShutdown(); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void readRowsTest() { |
| 91 | + client.readRows(Query.create(TABLE_NAME.getTable())); |
| 92 | + verifyHeaderSent(); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void sampleRowKeysTest() { |
| 97 | + client.sampleRowKeysAsync(TABLE_NAME.getTable()); |
| 98 | + verifyHeaderSent(); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void mutateRowTest() { |
| 103 | + client.mutateRowAsync(RowMutation.create(TABLE_NAME.getTable(), "fake-key").deleteRow()); |
| 104 | + verifyHeaderSent(); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void mutateRowsTest() throws TimeoutException, InterruptedException { |
| 109 | + try (BulkMutationBatcher batcher = client.newBulkMutationBatcher()) { |
| 110 | + batcher.add(RowMutation.create(TABLE_NAME.getTable(), "fake-key").deleteRow()); |
| 111 | + } catch (BulkMutationFailure e) { |
| 112 | + // Ignore the errors: none of the methods are actually implemented |
| 113 | + } |
| 114 | + verifyHeaderSent(); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void checkAndMutateRowTest() { |
| 119 | + client.checkAndMutateRowAsync( |
| 120 | + ConditionalRowMutation.create(TABLE_NAME.getTable(), "fake-key") |
| 121 | + .then(Mutation.create().deleteRow())); |
| 122 | + verifyHeaderSent(); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void readModifyWriteTest() { |
| 127 | + client.readModifyWriteRowAsync( |
| 128 | + ReadModifyWriteRow.create(TABLE_NAME.getTable(), "fake-key").increment("cf", "q", 1)); |
| 129 | + verifyHeaderSent(); |
| 130 | + } |
| 131 | + |
| 132 | + private void verifyHeaderSent() { |
| 133 | + boolean headerSent = channelProvider.isHeaderSent(HEADER_NAME, EXPECTED_HEADER_PATTERN); |
| 134 | + assertWithMessage("Header was sent").that(headerSent).isTrue(); |
| 135 | + } |
| 136 | +} |
0 commit comments