|
32 | 32 | import com.google.api.gax.rpc.TransportChannel; |
33 | 33 | import com.google.api.gax.rpc.TransportChannelProvider; |
34 | 34 | import com.google.cloud.NoCredentials; |
| 35 | +import com.google.cloud.TransportOptions; |
35 | 36 | import com.google.cloud.datastore.spi.DatastoreRpcFactory; |
36 | 37 | import com.google.cloud.datastore.spi.v1.DatastoreRpc; |
37 | 38 | import com.google.cloud.datastore.v1.DatastoreSettings; |
@@ -438,4 +439,63 @@ public void testGrpcDefaultChannelProviderDefersEndpointToGax() { |
438 | 439 | assertFalse(customChannelProvider.needsEndpoint()); |
439 | 440 | assertEquals(customHost, customChannelProvider.getEndpoint()); |
440 | 441 | } |
| 442 | + |
| 443 | + @Test |
| 444 | + public void testToBuilder_grpcToHttpResetsDefaultHost() { |
| 445 | + DatastoreOptions grpcDefault = |
| 446 | + DatastoreOptions.newBuilder() |
| 447 | + .setProjectId(PROJECT_ID) |
| 448 | + .setCredentials(NoCredentials.getInstance()) |
| 449 | + .build(); |
| 450 | + assertThat(grpcDefault.getHost()).isEqualTo(DatastoreUtils.DEFAULT_GRPC_DATASTORE_ENDPOINT); |
| 451 | + |
| 452 | + DatastoreOptions switchedToHttp = |
| 453 | + grpcDefault.toBuilder() |
| 454 | + .setTransportOptions(HttpTransportOptions.newBuilder().build()) |
| 455 | + .setUniverseDomain("custom.example.com") |
| 456 | + .build(); |
| 457 | + assertThat(switchedToHttp.getTransportOptions()).isInstanceOf(HttpTransportOptions.class); |
| 458 | + assertThat(switchedToHttp.getHost()).isEqualTo(DatastoreFactory.DEFAULT_HOST); |
| 459 | + assertThat(switchedToHttp.getUniverseDomain()).isEqualTo("custom.example.com"); |
| 460 | + } |
| 461 | + |
| 462 | + @Test |
| 463 | + public void testToBuilder_httpToGrpcResetsDefaultHost() { |
| 464 | + DatastoreOptions httpDefault = |
| 465 | + DatastoreOptions.newBuilder() |
| 466 | + .setProjectId(PROJECT_ID) |
| 467 | + .setCredentials(NoCredentials.getInstance()) |
| 468 | + .setTransportOptions(HttpTransportOptions.newBuilder().build()) |
| 469 | + .build(); |
| 470 | + assertThat(httpDefault.getHost()).isEqualTo(DatastoreFactory.DEFAULT_HOST); |
| 471 | + |
| 472 | + DatastoreOptions switchedToGrpc = |
| 473 | + httpDefault.toBuilder() |
| 474 | + .setTransportOptions((TransportOptions) GrpcTransportOptions.newBuilder().build()) |
| 475 | + .setUniverseDomain("custom.example.com") |
| 476 | + .build(); |
| 477 | + assertThat(switchedToGrpc.getTransportOptions()).isInstanceOf(GrpcTransportOptions.class); |
| 478 | + assertThat(switchedToGrpc.getHost()).isEqualTo(DatastoreUtils.DEFAULT_GRPC_DATASTORE_ENDPOINT); |
| 479 | + InstantiatingGrpcChannelProvider channelProvider = |
| 480 | + (InstantiatingGrpcChannelProvider) switchedToGrpc.getTransportChannelProvider(); |
| 481 | + assertTrue(channelProvider.needsEndpoint()); |
| 482 | + assertThat(channelProvider.getEndpoint()).isNull(); |
| 483 | + } |
| 484 | + |
| 485 | + @Test |
| 486 | + public void testToBuilder_customHostPreservedAcrossTransportSwitch() { |
| 487 | + String customHost = "custom-datastore.example.com:443"; |
| 488 | + DatastoreOptions grpcCustom = |
| 489 | + DatastoreOptions.newBuilder() |
| 490 | + .setProjectId(PROJECT_ID) |
| 491 | + .setCredentials(NoCredentials.getInstance()) |
| 492 | + .setHost(customHost) |
| 493 | + .build(); |
| 494 | + |
| 495 | + DatastoreOptions switchedToHttp = |
| 496 | + grpcCustom.toBuilder() |
| 497 | + .setTransportOptions(HttpTransportOptions.newBuilder().build()) |
| 498 | + .build(); |
| 499 | + assertThat(switchedToHttp.getHost()).isEqualTo(customHost); |
| 500 | + } |
441 | 501 | } |
0 commit comments