|
| 1 | +/* |
| 2 | + * Copyright 2020 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 | + * http://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.hbase.wrappers.veneer; |
| 17 | + |
| 18 | +import static com.google.cloud.bigtable.admin.v2.internal.NameUtil.extractTableIdFromTableName; |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertSame; |
| 21 | +import static org.junit.Assert.assertTrue; |
| 22 | + |
| 23 | +import com.google.bigtable.admin.v2.BigtableTableAdminGrpc; |
| 24 | +import com.google.bigtable.admin.v2.DeleteTableRequest; |
| 25 | +import com.google.bigtable.v2.BigtableGrpc; |
| 26 | +import com.google.bigtable.v2.ReadRowsRequest; |
| 27 | +import com.google.bigtable.v2.ReadRowsResponse; |
| 28 | +import com.google.cloud.bigtable.data.v2.models.Query; |
| 29 | +import com.google.cloud.bigtable.hbase.BigtableOptionsFactory; |
| 30 | +import com.google.cloud.bigtable.hbase.wrappers.BigtableApi; |
| 31 | +import com.google.cloud.bigtable.hbase.wrappers.BigtableHBaseSettings; |
| 32 | +import com.google.cloud.bigtable.hbase.wrappers.classic.BigtableHBaseClassicSettings; |
| 33 | +import com.google.common.collect.Queues; |
| 34 | +import com.google.protobuf.Empty; |
| 35 | +import io.grpc.Server; |
| 36 | +import io.grpc.ServerBuilder; |
| 37 | +import io.grpc.stub.StreamObserver; |
| 38 | +import java.io.IOException; |
| 39 | +import java.net.ServerSocket; |
| 40 | +import java.util.concurrent.BlockingQueue; |
| 41 | +import java.util.concurrent.TimeUnit; |
| 42 | +import org.apache.hadoop.conf.Configuration; |
| 43 | +import org.junit.After; |
| 44 | +import org.junit.Before; |
| 45 | +import org.junit.Test; |
| 46 | +import org.junit.runner.RunWith; |
| 47 | +import org.junit.runners.JUnit4; |
| 48 | + |
| 49 | +@RunWith(JUnit4.class) |
| 50 | +public class TestBigtableVeneerApi { |
| 51 | + |
| 52 | + private static final String TEST_PROJECT_ID = "fake-project-id"; |
| 53 | + private static final String TEST_INSTANCE_ID = "fake-instance-id"; |
| 54 | + private static final String ROW_KEY = "test-row-key"; |
| 55 | + private static final String TABLE_ID = "test-table-id"; |
| 56 | + private Server server; |
| 57 | + private FakeDataService fakeDataService = new FakeDataService(); |
| 58 | + private FakeAdminService fakeAdminService = new FakeAdminService(); |
| 59 | + |
| 60 | + private BigtableHBaseSettings bigtableHBaseSettings; |
| 61 | + private BigtableApi bigtableApi; |
| 62 | + |
| 63 | + @Before |
| 64 | + public void setUp() throws IOException { |
| 65 | + final int port; |
| 66 | + try (ServerSocket s = new ServerSocket(0)) { |
| 67 | + port = s.getLocalPort(); |
| 68 | + } |
| 69 | + server = |
| 70 | + ServerBuilder.forPort(port) |
| 71 | + .addService(fakeDataService) |
| 72 | + .addService(fakeAdminService) |
| 73 | + .build(); |
| 74 | + server.start(); |
| 75 | + |
| 76 | + Configuration configuration = new Configuration(false); |
| 77 | + configuration.set(BigtableOptionsFactory.PROJECT_ID_KEY, TEST_PROJECT_ID); |
| 78 | + configuration.set(BigtableOptionsFactory.INSTANCE_ID_KEY, TEST_INSTANCE_ID); |
| 79 | + configuration.set(BigtableOptionsFactory.BIGTABLE_NULL_CREDENTIAL_ENABLE_KEY, "true"); |
| 80 | + configuration.set(BigtableOptionsFactory.BIGTABLE_DATA_CHANNEL_COUNT_KEY, "1"); |
| 81 | + configuration.set(BigtableOptionsFactory.BIGTABLE_EMULATOR_HOST_KEY, "localhost:" + port); |
| 82 | + configuration.set(BigtableOptionsFactory.BIGTABLE_USE_GCJ_CLIENT, "true"); |
| 83 | + bigtableHBaseSettings = BigtableHBaseClassicSettings.create(configuration); |
| 84 | + bigtableApi = BigtableApi.create(bigtableHBaseSettings); |
| 85 | + } |
| 86 | + |
| 87 | + @After |
| 88 | + public void tearDown() throws Exception { |
| 89 | + bigtableApi.close(); |
| 90 | + server.shutdownNow(); |
| 91 | + server.awaitTermination(); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testBigtableHBaseSettings() { |
| 96 | + assertTrue(bigtableApi instanceof BigtableVeneerApi); |
| 97 | + assertSame(bigtableHBaseSettings, bigtableApi.getBigtableHBaseSettings()); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testGetAdminClient() throws Exception { |
| 102 | + assertTrue(bigtableApi.getAdminClient() instanceof AdminClientVeneerApi); |
| 103 | + |
| 104 | + bigtableApi.getAdminClient().deleteTableAsync(TABLE_ID).get(); |
| 105 | + DeleteTableRequest deleteTableRequest = fakeAdminService.popLastRequest(); |
| 106 | + assertEquals(TABLE_ID, extractTableIdFromTableName(deleteTableRequest.getName())); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testGetDataClient() throws Exception { |
| 111 | + assertTrue(bigtableApi.getDataClient() instanceof DataClientVeneerApi); |
| 112 | + |
| 113 | + Query query = Query.create(TABLE_ID).rowKey(ROW_KEY); |
| 114 | + bigtableApi.getDataClient().readRowsAsync(query); |
| 115 | + ReadRowsRequest request = fakeDataService.popLastRequest(); |
| 116 | + assertEquals(ROW_KEY, request.getRows().getRowKeys(0).toStringUtf8()); |
| 117 | + } |
| 118 | + |
| 119 | + private static class FakeDataService extends BigtableGrpc.BigtableImplBase { |
| 120 | + final BlockingQueue<Object> requests = Queues.newLinkedBlockingDeque(); |
| 121 | + |
| 122 | + @SuppressWarnings("unchecked") |
| 123 | + <T> T popLastRequest() throws InterruptedException { |
| 124 | + return (T) requests.poll(1, TimeUnit.SECONDS); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public void readRows( |
| 129 | + ReadRowsRequest request, StreamObserver<ReadRowsResponse> responseObserver) { |
| 130 | + requests.add(request); |
| 131 | + responseObserver.onNext(ReadRowsResponse.getDefaultInstance()); |
| 132 | + responseObserver.onCompleted(); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + private static class FakeAdminService extends BigtableTableAdminGrpc.BigtableTableAdminImplBase { |
| 137 | + |
| 138 | + final BlockingQueue<Object> requests = Queues.newLinkedBlockingDeque(); |
| 139 | + |
| 140 | + @SuppressWarnings("unchecked") |
| 141 | + <T> T popLastRequest() throws InterruptedException { |
| 142 | + return (T) requests.poll(1, TimeUnit.SECONDS); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public void deleteTable(DeleteTableRequest request, StreamObserver<Empty> responseObserver) { |
| 147 | + requests.add(request); |
| 148 | + responseObserver.onNext(Empty.getDefaultInstance()); |
| 149 | + responseObserver.onCompleted(); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments