Search in sources :

Example 11 with BackgroundResource

use of com.google.api.gax.core.BackgroundResource 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

BackgroundResource (com.google.api.gax.core.BackgroundResource)11 FakeChannel (com.google.api.gax.rpc.testing.FakeChannel)5 FakeClientSettings (com.google.api.gax.rpc.testing.FakeClientSettings)5 FakeTransportChannel (com.google.api.gax.rpc.testing.FakeTransportChannel)5 Test (org.junit.Test)4 ApiClock (com.google.api.core.ApiClock)3 ExecutorProvider (com.google.api.gax.core.ExecutorProvider)2 Credentials (com.google.auth.Credentials)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 Duration (org.threeten.bp.Duration)2 ExecutorAsBackgroundResource (com.google.api.gax.core.ExecutorAsBackgroundResource)1 FixedExecutorProvider (com.google.api.gax.core.FixedExecutorProvider)1 ClientContext (com.google.api.gax.rpc.ClientContext)1 QuotaProjectIdHidingCredentials (com.google.api.gax.rpc.internal.QuotaProjectIdHidingCredentials)1 MtlsProvider (com.google.api.gax.rpc.mtls.MtlsProvider)1 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)1 ImmutableList (com.google.common.collect.ImmutableList)1 Nullable (javax.annotation.Nullable)1