|
| 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.assertThat; |
| 19 | + |
| 20 | +import com.google.api.core.ApiFuture; |
| 21 | +import com.google.api.core.SettableApiFuture; |
| 22 | +import com.google.api.gax.grpc.GrpcStatusCode; |
| 23 | +import com.google.api.gax.rpc.ApiCallContext; |
| 24 | +import com.google.api.gax.rpc.NotFoundException; |
| 25 | +import com.google.api.gax.rpc.UnaryCallable; |
| 26 | +import com.google.bigtable.admin.v2.InstanceName; |
| 27 | +import com.google.bigtable.v2.SampleRowKeysRequest; |
| 28 | +import com.google.bigtable.v2.SampleRowKeysResponse; |
| 29 | +import com.google.cloud.bigtable.data.v2.internal.RequestContext; |
| 30 | +import com.google.cloud.bigtable.data.v2.models.KeyOffset; |
| 31 | +import com.google.common.collect.ImmutableList; |
| 32 | +import com.google.protobuf.ByteString; |
| 33 | +import io.grpc.Status.Code; |
| 34 | +import java.util.List; |
| 35 | +import java.util.concurrent.ExecutionException; |
| 36 | +import java.util.concurrent.TimeUnit; |
| 37 | +import org.junit.Before; |
| 38 | +import org.junit.Test; |
| 39 | +import org.junit.runner.RunWith; |
| 40 | +import org.junit.runners.JUnit4; |
| 41 | + |
| 42 | +@RunWith(JUnit4.class) |
| 43 | +public class SampleRowKeysCallableTest { |
| 44 | + private final RequestContext requestContext = |
| 45 | + RequestContext.create(InstanceName.of("my-project", "my-instance"), "my-app-profile"); |
| 46 | + private FakeCallable inner; |
| 47 | + private SampleRowKeysCallable callable; |
| 48 | + |
| 49 | + @Before |
| 50 | + public void setUp() { |
| 51 | + inner = new FakeCallable(); |
| 52 | + callable = new SampleRowKeysCallable(inner, requestContext); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void requestIsCorrect() { |
| 57 | + callable.futureCall("my-table"); |
| 58 | + |
| 59 | + assertThat(inner.request) |
| 60 | + .isEqualTo( |
| 61 | + SampleRowKeysRequest.newBuilder() |
| 62 | + .setTableName(requestContext.getInstanceName() + "/tables/my-table") |
| 63 | + .setAppProfileId(requestContext.getAppProfileId()) |
| 64 | + .build()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void responseCorrectlyTransformed() throws Exception { |
| 69 | + ApiFuture<List<KeyOffset>> result = callable.futureCall("my-table"); |
| 70 | + |
| 71 | + inner.response.set( |
| 72 | + ImmutableList.of( |
| 73 | + SampleRowKeysResponse.newBuilder() |
| 74 | + .setRowKey(ByteString.copyFromUtf8("key1")) |
| 75 | + .setOffsetBytes(100) |
| 76 | + .build(), |
| 77 | + SampleRowKeysResponse.newBuilder() |
| 78 | + .setRowKey(ByteString.copyFromUtf8("")) |
| 79 | + .setOffsetBytes(1000) |
| 80 | + .build())); |
| 81 | + |
| 82 | + assertThat(result.get(1, TimeUnit.SECONDS)) |
| 83 | + .isEqualTo( |
| 84 | + ImmutableList.of( |
| 85 | + KeyOffset.create(ByteString.copyFromUtf8("key1"), 100), |
| 86 | + KeyOffset.create(ByteString.EMPTY, 1000))); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void errorIsPropagated() throws Exception { |
| 91 | + ApiFuture<List<KeyOffset>> result = callable.futureCall("my-table"); |
| 92 | + |
| 93 | + Throwable expectedError = |
| 94 | + new NotFoundException("fake error", null, GrpcStatusCode.of(Code.NOT_FOUND), false); |
| 95 | + inner.response.setException(expectedError); |
| 96 | + |
| 97 | + Throwable actualError = null; |
| 98 | + try { |
| 99 | + result.get(1, TimeUnit.SECONDS); |
| 100 | + } catch (ExecutionException e) { |
| 101 | + actualError = e.getCause(); |
| 102 | + } |
| 103 | + |
| 104 | + assertThat(actualError).isEqualTo(expectedError); |
| 105 | + } |
| 106 | + |
| 107 | + static class FakeCallable |
| 108 | + extends UnaryCallable<SampleRowKeysRequest, List<SampleRowKeysResponse>> { |
| 109 | + SampleRowKeysRequest request; |
| 110 | + ApiCallContext callContext; |
| 111 | + SettableApiFuture<List<SampleRowKeysResponse>> response = SettableApiFuture.create(); |
| 112 | + |
| 113 | + @Override |
| 114 | + public ApiFuture<List<SampleRowKeysResponse>> futureCall( |
| 115 | + SampleRowKeysRequest request, ApiCallContext context) { |
| 116 | + this.request = request; |
| 117 | + this.callContext = context; |
| 118 | + |
| 119 | + return response; |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments