|
16 | 16 | package com.google.cloud.bigquery.storage.v1alpha2; |
17 | 17 |
|
18 | 18 | import com.google.api.gax.core.NoCredentialsProvider; |
| 19 | +import com.google.api.gax.grpc.GaxGrpcProperties; |
19 | 20 | import com.google.api.gax.grpc.testing.LocalChannelProvider; |
20 | 21 | import com.google.api.gax.grpc.testing.MockGrpcService; |
21 | 22 | import com.google.api.gax.grpc.testing.MockServiceHelper; |
22 | 23 | import com.google.api.gax.grpc.testing.MockStreamObserver; |
| 24 | +import com.google.api.gax.rpc.ApiClientHeaderProvider; |
23 | 25 | import com.google.api.gax.rpc.ApiStreamObserver; |
24 | 26 | import com.google.api.gax.rpc.BidiStreamingCallable; |
25 | 27 | import com.google.api.gax.rpc.InvalidArgumentException; |
26 | 28 | import com.google.api.gax.rpc.StatusCode; |
27 | 29 | import com.google.cloud.bigquery.storage.v1alpha2.Storage.AppendRowsRequest; |
28 | 30 | import com.google.cloud.bigquery.storage.v1alpha2.Storage.AppendRowsResponse; |
| 31 | +import com.google.cloud.bigquery.storage.v1alpha2.Storage.BatchCommitWriteStreamsRequest; |
| 32 | +import com.google.cloud.bigquery.storage.v1alpha2.Storage.BatchCommitWriteStreamsResponse; |
| 33 | +import com.google.cloud.bigquery.storage.v1alpha2.Storage.CreateWriteStreamRequest; |
| 34 | +import com.google.cloud.bigquery.storage.v1alpha2.Storage.FinalizeWriteStreamRequest; |
| 35 | +import com.google.cloud.bigquery.storage.v1alpha2.Storage.FinalizeWriteStreamResponse; |
| 36 | +import com.google.cloud.bigquery.storage.v1alpha2.Storage.GetWriteStreamRequest; |
| 37 | +import com.google.cloud.bigquery.storage.v1alpha2.Stream.WriteStream; |
| 38 | +import com.google.protobuf.AbstractMessage; |
29 | 39 | import io.grpc.Status; |
30 | 40 | import io.grpc.StatusRuntimeException; |
31 | 41 | import java.io.IOException; |
@@ -78,6 +88,46 @@ public void tearDown() throws Exception { |
78 | 88 | client.close(); |
79 | 89 | } |
80 | 90 |
|
| 91 | + @Test |
| 92 | + @SuppressWarnings("all") |
| 93 | + public void createWriteStreamTest() { |
| 94 | + String name = "name3373707"; |
| 95 | + String externalId = "externalId-1153075697"; |
| 96 | + WriteStream expectedResponse = |
| 97 | + WriteStream.newBuilder().setName(name).setExternalId(externalId).build(); |
| 98 | + mockBigQueryWrite.addResponse(expectedResponse); |
| 99 | + |
| 100 | + CreateWriteStreamRequest request = CreateWriteStreamRequest.newBuilder().build(); |
| 101 | + |
| 102 | + WriteStream actualResponse = client.createWriteStream(request); |
| 103 | + Assert.assertEquals(expectedResponse, actualResponse); |
| 104 | + |
| 105 | + List<AbstractMessage> actualRequests = mockBigQueryWrite.getRequests(); |
| 106 | + Assert.assertEquals(1, actualRequests.size()); |
| 107 | + CreateWriteStreamRequest actualRequest = (CreateWriteStreamRequest) actualRequests.get(0); |
| 108 | + |
| 109 | + Assert.assertTrue( |
| 110 | + channelProvider.isHeaderSent( |
| 111 | + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), |
| 112 | + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + @SuppressWarnings("all") |
| 117 | + public void createWriteStreamExceptionTest() throws Exception { |
| 118 | + StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); |
| 119 | + mockBigQueryWrite.addException(exception); |
| 120 | + |
| 121 | + try { |
| 122 | + CreateWriteStreamRequest request = CreateWriteStreamRequest.newBuilder().build(); |
| 123 | + |
| 124 | + client.createWriteStream(request); |
| 125 | + Assert.fail("No exception raised"); |
| 126 | + } catch (InvalidArgumentException e) { |
| 127 | + // Expected exception |
| 128 | + } |
| 129 | + } |
| 130 | + |
81 | 131 | @Test |
82 | 132 | @SuppressWarnings("all") |
83 | 133 | public void appendRowsTest() throws Exception { |
@@ -126,4 +176,122 @@ public void appendRowsExceptionTest() throws Exception { |
126 | 176 | Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); |
127 | 177 | } |
128 | 178 | } |
| 179 | + |
| 180 | + @Test |
| 181 | + @SuppressWarnings("all") |
| 182 | + public void getWriteStreamTest() { |
| 183 | + String name = "name3373707"; |
| 184 | + String externalId = "externalId-1153075697"; |
| 185 | + WriteStream expectedResponse = |
| 186 | + WriteStream.newBuilder().setName(name).setExternalId(externalId).build(); |
| 187 | + mockBigQueryWrite.addResponse(expectedResponse); |
| 188 | + |
| 189 | + GetWriteStreamRequest request = GetWriteStreamRequest.newBuilder().build(); |
| 190 | + |
| 191 | + WriteStream actualResponse = client.getWriteStream(request); |
| 192 | + Assert.assertEquals(expectedResponse, actualResponse); |
| 193 | + |
| 194 | + List<AbstractMessage> actualRequests = mockBigQueryWrite.getRequests(); |
| 195 | + Assert.assertEquals(1, actualRequests.size()); |
| 196 | + GetWriteStreamRequest actualRequest = (GetWriteStreamRequest) actualRequests.get(0); |
| 197 | + |
| 198 | + Assert.assertTrue( |
| 199 | + channelProvider.isHeaderSent( |
| 200 | + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), |
| 201 | + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); |
| 202 | + } |
| 203 | + |
| 204 | + @Test |
| 205 | + @SuppressWarnings("all") |
| 206 | + public void getWriteStreamExceptionTest() throws Exception { |
| 207 | + StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); |
| 208 | + mockBigQueryWrite.addException(exception); |
| 209 | + |
| 210 | + try { |
| 211 | + GetWriteStreamRequest request = GetWriteStreamRequest.newBuilder().build(); |
| 212 | + |
| 213 | + client.getWriteStream(request); |
| 214 | + Assert.fail("No exception raised"); |
| 215 | + } catch (InvalidArgumentException e) { |
| 216 | + // Expected exception |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + @Test |
| 221 | + @SuppressWarnings("all") |
| 222 | + public void finalizeWriteStreamTest() { |
| 223 | + long rowCount = 1340416618L; |
| 224 | + FinalizeWriteStreamResponse expectedResponse = |
| 225 | + FinalizeWriteStreamResponse.newBuilder().setRowCount(rowCount).build(); |
| 226 | + mockBigQueryWrite.addResponse(expectedResponse); |
| 227 | + |
| 228 | + FinalizeWriteStreamRequest request = FinalizeWriteStreamRequest.newBuilder().build(); |
| 229 | + |
| 230 | + FinalizeWriteStreamResponse actualResponse = client.finalizeWriteStream(request); |
| 231 | + Assert.assertEquals(expectedResponse, actualResponse); |
| 232 | + |
| 233 | + List<AbstractMessage> actualRequests = mockBigQueryWrite.getRequests(); |
| 234 | + Assert.assertEquals(1, actualRequests.size()); |
| 235 | + FinalizeWriteStreamRequest actualRequest = (FinalizeWriteStreamRequest) actualRequests.get(0); |
| 236 | + |
| 237 | + Assert.assertTrue( |
| 238 | + channelProvider.isHeaderSent( |
| 239 | + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), |
| 240 | + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); |
| 241 | + } |
| 242 | + |
| 243 | + @Test |
| 244 | + @SuppressWarnings("all") |
| 245 | + public void finalizeWriteStreamExceptionTest() throws Exception { |
| 246 | + StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); |
| 247 | + mockBigQueryWrite.addException(exception); |
| 248 | + |
| 249 | + try { |
| 250 | + FinalizeWriteStreamRequest request = FinalizeWriteStreamRequest.newBuilder().build(); |
| 251 | + |
| 252 | + client.finalizeWriteStream(request); |
| 253 | + Assert.fail("No exception raised"); |
| 254 | + } catch (InvalidArgumentException e) { |
| 255 | + // Expected exception |
| 256 | + } |
| 257 | + } |
| 258 | + |
| 259 | + @Test |
| 260 | + @SuppressWarnings("all") |
| 261 | + public void batchCommitWriteStreamsTest() { |
| 262 | + BatchCommitWriteStreamsResponse expectedResponse = |
| 263 | + BatchCommitWriteStreamsResponse.newBuilder().build(); |
| 264 | + mockBigQueryWrite.addResponse(expectedResponse); |
| 265 | + |
| 266 | + BatchCommitWriteStreamsRequest request = BatchCommitWriteStreamsRequest.newBuilder().build(); |
| 267 | + |
| 268 | + BatchCommitWriteStreamsResponse actualResponse = client.batchCommitWriteStreams(request); |
| 269 | + Assert.assertEquals(expectedResponse, actualResponse); |
| 270 | + |
| 271 | + List<AbstractMessage> actualRequests = mockBigQueryWrite.getRequests(); |
| 272 | + Assert.assertEquals(1, actualRequests.size()); |
| 273 | + BatchCommitWriteStreamsRequest actualRequest = |
| 274 | + (BatchCommitWriteStreamsRequest) actualRequests.get(0); |
| 275 | + |
| 276 | + Assert.assertTrue( |
| 277 | + channelProvider.isHeaderSent( |
| 278 | + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), |
| 279 | + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); |
| 280 | + } |
| 281 | + |
| 282 | + @Test |
| 283 | + @SuppressWarnings("all") |
| 284 | + public void batchCommitWriteStreamsExceptionTest() throws Exception { |
| 285 | + StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); |
| 286 | + mockBigQueryWrite.addException(exception); |
| 287 | + |
| 288 | + try { |
| 289 | + BatchCommitWriteStreamsRequest request = BatchCommitWriteStreamsRequest.newBuilder().build(); |
| 290 | + |
| 291 | + client.batchCommitWriteStreams(request); |
| 292 | + Assert.fail("No exception raised"); |
| 293 | + } catch (InvalidArgumentException e) { |
| 294 | + // Expected exception |
| 295 | + } |
| 296 | + } |
129 | 297 | } |
0 commit comments