use of com.google.api.gax.rpc.testing.FakeTransportChannel in project gax-java by googleapis.
the class ClientContextTest method testMergeHeaders_noQuotaProjectIdSet.
@Test
public void testMergeHeaders_noQuotaProjectIdSet() throws IOException {
final String QUOTA_PROJECT_ID_KEY = "x-goog-user-project";
FakeClientSettings.Builder builder = new FakeClientSettings.Builder();
InterceptingExecutor executor = new InterceptingExecutor(1);
FakeTransportChannel transportChannel = FakeTransportChannel.create(new FakeChannel());
FakeTransportProvider transportProvider = new FakeTransportProvider(transportChannel, executor, true, null, null);
HeaderProvider headerProvider = Mockito.mock(HeaderProvider.class);
Mockito.when(headerProvider.getHeaders()).thenReturn(ImmutableMap.of("header_k1", "v1"));
HeaderProvider internalHeaderProvider = Mockito.mock(HeaderProvider.class);
Mockito.when(internalHeaderProvider.getHeaders()).thenReturn(ImmutableMap.of("internal_header_k1", "v1"));
builder.setTransportChannelProvider(transportProvider);
builder.setCredentialsProvider(FixedCredentialsProvider.create(Mockito.mock(Credentials.class)));
builder.setHeaderProvider(headerProvider);
builder.setInternalHeaderProvider(internalHeaderProvider);
ClientContext context = ClientContext.create(builder.build());
List<BackgroundResource> resources = context.getBackgroundResources();
FakeTransportChannel fakeTransportChannel = (FakeTransportChannel) resources.get(0);
assertThat(fakeTransportChannel.getHeaders().size()).isEqualTo(headerProvider.getHeaders().size() + internalHeaderProvider.getHeaders().size());
assertThat(fakeTransportChannel.getHeaders().containsKey(QUOTA_PROJECT_ID_KEY)).isFalse();
}
use of com.google.api.gax.rpc.testing.FakeTransportChannel in project gax-java by googleapis.
the class ClientContextTest method testUserAgentExternalOnly.
@Test
public void testUserAgentExternalOnly() throws Exception {
TransportChannelProvider transportChannelProvider = new FakeTransportProvider(FakeTransportChannel.create(new FakeChannel()), null, true, null, null);
ClientSettings.Builder builder = new FakeClientSettings.Builder().setExecutorProvider(FixedExecutorProvider.create(Mockito.mock(ScheduledExecutorService.class))).setTransportChannelProvider(transportChannelProvider).setCredentialsProvider(FixedCredentialsProvider.create(Mockito.mock(GoogleCredentials.class)));
builder.setHeaderProvider(FixedHeaderProvider.create("user-agent", "user-supplied-agent"));
ClientContext clientContext = ClientContext.create(builder.build());
FakeTransportChannel transportChannel = (FakeTransportChannel) clientContext.getTransportChannel();
assertThat(transportChannel.getHeaders()).containsEntry("user-agent", "user-supplied-agent");
}
use of com.google.api.gax.rpc.testing.FakeTransportChannel in project gax-java by googleapis.
the class ClientContextTest method testExecutorSettings.
@Test
public void testExecutorSettings() throws Exception {
TransportChannelProvider transportChannelProvider = new FakeTransportProvider(FakeTransportChannel.create(new FakeChannel()), null, true, null, null);
ClientSettings.Builder builder = new FakeClientSettings.Builder().setTransportChannelProvider(transportChannelProvider).setCredentialsProvider(FixedCredentialsProvider.create(Mockito.mock(GoogleCredentials.class)));
// By default, if executor is not set, channel provider should not have an executor set
ClientContext context = ClientContext.create(builder.build());
FakeTransportChannel transportChannel = (FakeTransportChannel) context.getTransportChannel();
assertThat(transportChannel.getExecutor()).isNull();
ExecutorProvider channelExecutorProvider = FixedExecutorProvider.create(Mockito.mock(ScheduledExecutorService.class));
builder.setTransportChannelProvider(transportChannelProvider.withExecutor((Executor) channelExecutorProvider.getExecutor()));
context = ClientContext.create(builder.build());
transportChannel = (FakeTransportChannel) context.getTransportChannel();
assertThat(transportChannel.getExecutor()).isSameInstanceAs(channelExecutorProvider.getExecutor());
ExecutorProvider executorProvider = FixedExecutorProvider.create(Mockito.mock(ScheduledExecutorService.class));
assertThat(channelExecutorProvider.getExecutor()).isNotSameInstanceAs(executorProvider.getExecutor());
// For backward compatibility, if executor is set from stubSettings.setExecutor, and transport
// channel already has an executor, the ExecutorProvider set in stubSettings won't override
// transport channel's executor
builder.setExecutorProvider(executorProvider);
context = ClientContext.create(builder.build());
transportChannel = (FakeTransportChannel) context.getTransportChannel();
assertThat(transportChannel.getExecutor()).isSameInstanceAs(channelExecutorProvider.getExecutor());
// For backward compatibility, if executor is set from stubSettings.setExecutor, and transport
// channel doesn't have an executor, transport channel will get the executor from
// stubSettings.setExecutor
builder.setExecutorProvider(executorProvider);
builder.setTransportChannelProvider(new FakeTransportProvider(FakeTransportChannel.create(new FakeChannel()), null, true, null, null));
context = ClientContext.create(builder.build());
transportChannel = (FakeTransportChannel) context.getTransportChannel();
assertThat(transportChannel.getExecutor()).isSameInstanceAs(executorProvider.getExecutor());
}
use of com.google.api.gax.rpc.testing.FakeTransportChannel in project gax-java by googleapis.
the class ClientContextTest method testHidingQuotaProjectId_quotaSetFromSetting.
@Test
public void testHidingQuotaProjectId_quotaSetFromSetting() throws IOException {
final String QUOTA_PROJECT_ID_KEY = "x-goog-user-project";
final String QUOTA_PROJECT_ID_FROM_CREDENTIALS_VALUE = "quota_project_id_from_credentials";
FakeClientSettings.Builder builder = new FakeClientSettings.Builder();
InterceptingExecutor executor = new InterceptingExecutor(1);
FakeTransportChannel transportChannel = FakeTransportChannel.create(new FakeChannel());
FakeTransportProvider transportProvider = new FakeTransportProvider(transportChannel, executor, true, null, null);
Map<String, List<String>> metaDataWithQuota = ImmutableMap.of("k1", Collections.singletonList("v1"), QUOTA_PROJECT_ID_KEY, Collections.singletonList(QUOTA_PROJECT_ID_FROM_CREDENTIALS_VALUE));
final Credentials credentialsWithQuotaProjectId = Mockito.mock(GoogleCredentials.class);
Mockito.when(credentialsWithQuotaProjectId.getRequestMetadata(null)).thenReturn(metaDataWithQuota);
HeaderProvider headerProviderWithQuota = Mockito.mock(HeaderProvider.class);
HeaderProvider internalHeaderProvider = Mockito.mock(HeaderProvider.class);
builder.setExecutorProvider(new FakeExecutorProvider(executor, true));
builder.setTransportChannelProvider(transportProvider);
builder.setCredentialsProvider(new CredentialsProvider() {
@Override
public Credentials getCredentials() throws IOException {
return credentialsWithQuotaProjectId;
}
});
builder.setHeaderProvider(headerProviderWithQuota);
builder.setInternalHeaderProvider(internalHeaderProvider);
builder.setQuotaProjectId(QUOTA_PROJECT_ID_FROM_CREDENTIALS_VALUE);
ClientContext clientContext = ClientContext.create(builder.build());
assertThat(clientContext.getCredentials().getRequestMetadata().size()).isEqualTo(metaDataWithQuota.size() - 1);
assertThat(clientContext.getCredentials().getRequestMetadata().containsKey(QUOTA_PROJECT_ID_KEY)).isFalse();
}
use of com.google.api.gax.rpc.testing.FakeTransportChannel in project gax-java by googleapis.
the class ClientContextTest method testWatchdogProvider.
@Test
public void testWatchdogProvider() throws IOException {
FakeClientSettings.Builder builder = new FakeClientSettings.Builder();
InterceptingExecutor executor = new InterceptingExecutor(1);
FakeTransportChannel transportChannel = FakeTransportChannel.create(new FakeChannel());
FakeTransportProvider transportProvider = new FakeTransportProvider(transportChannel, executor, true, null, null);
ApiClock clock = Mockito.mock(ApiClock.class);
builder.setClock(clock);
builder.setCredentialsProvider(FixedCredentialsProvider.create(Mockito.mock(Credentials.class)));
builder.setExecutorProvider(new FakeExecutorProvider(executor, true));
builder.setTransportChannelProvider(transportProvider);
Duration watchdogCheckInterval = Duration.ofSeconds(11);
builder.setWatchdogProvider(InstantiatingWatchdogProvider.create().withClock(clock).withCheckInterval(watchdogCheckInterval).withExecutor(executor));
builder.setWatchdogCheckInterval(watchdogCheckInterval);
HeaderProvider headerProvider = Mockito.mock(HeaderProvider.class);
Mockito.when(headerProvider.getHeaders()).thenReturn(ImmutableMap.of("k1", "v1"));
HeaderProvider internalHeaderProvider = Mockito.mock(HeaderProvider.class);
Mockito.when(internalHeaderProvider.getHeaders()).thenReturn(ImmutableMap.of("k2", "v2"));
builder.setHeaderProvider(headerProvider);
builder.setInternalHeaderProvider(internalHeaderProvider);
ClientContext context = ClientContext.create(builder.build());
List<BackgroundResource> resources = context.getBackgroundResources();
assertThat(resources.get(2)).isInstanceOf(Watchdog.class);
}
Aggregations