Search in sources :

Example 6 with FakeTransportChannel

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();
}
Also used : FakeTransportChannel(com.google.api.gax.rpc.testing.FakeTransportChannel) FakeClientSettings(com.google.api.gax.rpc.testing.FakeClientSettings) BackgroundResource(com.google.api.gax.core.BackgroundResource) FakeChannel(com.google.api.gax.rpc.testing.FakeChannel) Test(org.junit.Test)

Example 7 with FakeTransportChannel

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");
}
Also used : FakeTransportChannel(com.google.api.gax.rpc.testing.FakeTransportChannel) FakeClientSettings(com.google.api.gax.rpc.testing.FakeClientSettings) FakeClientSettings(com.google.api.gax.rpc.testing.FakeClientSettings) FakeChannel(com.google.api.gax.rpc.testing.FakeChannel) Test(org.junit.Test)

Example 8 with FakeTransportChannel

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());
}
Also used : FakeTransportChannel(com.google.api.gax.rpc.testing.FakeTransportChannel) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Executor(java.util.concurrent.Executor) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) FakeClientSettings(com.google.api.gax.rpc.testing.FakeClientSettings) FixedExecutorProvider(com.google.api.gax.core.FixedExecutorProvider) ExecutorProvider(com.google.api.gax.core.ExecutorProvider) FakeChannel(com.google.api.gax.rpc.testing.FakeChannel) Test(org.junit.Test)

Example 9 with FakeTransportChannel

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();
}
Also used : FakeTransportChannel(com.google.api.gax.rpc.testing.FakeTransportChannel) FakeClientSettings(com.google.api.gax.rpc.testing.FakeClientSettings) FixedCredentialsProvider(com.google.api.gax.core.FixedCredentialsProvider) CredentialsProvider(com.google.api.gax.core.CredentialsProvider) IOException(java.io.IOException) List(java.util.List) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) Credentials(com.google.auth.Credentials) FakeChannel(com.google.api.gax.rpc.testing.FakeChannel) Test(org.junit.Test)

Example 10 with FakeTransportChannel

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);
}
Also used : FakeTransportChannel(com.google.api.gax.rpc.testing.FakeTransportChannel) FakeClientSettings(com.google.api.gax.rpc.testing.FakeClientSettings) ApiClock(com.google.api.core.ApiClock) Duration(org.threeten.bp.Duration) BackgroundResource(com.google.api.gax.core.BackgroundResource) FakeChannel(com.google.api.gax.rpc.testing.FakeChannel) Test(org.junit.Test)

Aggregations

FakeChannel (com.google.api.gax.rpc.testing.FakeChannel)11 FakeClientSettings (com.google.api.gax.rpc.testing.FakeClientSettings)11 FakeTransportChannel (com.google.api.gax.rpc.testing.FakeTransportChannel)11 Test (org.junit.Test)10 BackgroundResource (com.google.api.gax.core.BackgroundResource)5 Credentials (com.google.auth.Credentials)3 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)3 ApiClock (com.google.api.core.ApiClock)2 CredentialsProvider (com.google.api.gax.core.CredentialsProvider)2 ExecutorProvider (com.google.api.gax.core.ExecutorProvider)2 FixedCredentialsProvider (com.google.api.gax.core.FixedCredentialsProvider)2 FixedExecutorProvider (com.google.api.gax.core.FixedExecutorProvider)2 IOException (java.io.IOException)2 List (java.util.List)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 Duration (org.threeten.bp.Duration)2 Executor (java.util.concurrent.Executor)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1