Search in sources :

Example 11 with FakeTransportChannel

use of com.google.api.gax.rpc.testing.FakeTransportChannel in project gax-java by googleapis.

the class ClientContextTest method testMergeHeaders_getQuotaProjectIdFromHeadersProvider.

@Test
public void testMergeHeaders_getQuotaProjectIdFromHeadersProvider() throws IOException {
    final String QUOTA_PROJECT_ID_KEY = "x-goog-user-project";
    final String QUOTA_PROJECT_ID_FROM_SETTINGS = "quota_project_id_from_settings";
    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);
    builder.setQuotaProjectId(QUOTA_PROJECT_ID_FROM_SETTINGS);
    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() + 1);
    assertThat(fakeTransportChannel.getHeaders().get(QUOTA_PROJECT_ID_KEY)).isEqualTo(QUOTA_PROJECT_ID_FROM_SETTINGS);
}
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)

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