Search in sources :

Example 16 with FakeChannel

use of com.google.api.gax.rpc.testing.FakeChannel 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 17 with FakeChannel

use of com.google.api.gax.rpc.testing.FakeChannel 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)

Example 18 with FakeChannel

use of com.google.api.gax.rpc.testing.FakeChannel 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)

Example 19 with FakeChannel

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

the class OperationCallableImplTest method callWithContext.

@Test
public void callWithContext() {
    FakeChannel channel = new FakeChannel();
    Credentials credentials = Mockito.mock(Credentials.class);
    ApiCallContext context = FakeCallContext.createDefault().withChannel(channel).withCredentials(credentials);
    OperationStashCallable stashCallable = new OperationStashCallable();
    OperationCallable<Integer, String, Long> callable = stashCallable.withDefaultCallContext(FakeCallContext.createDefault());
    String response = callable.call(2, context);
    Truth.assertThat(response).isEqualTo("2");
    FakeCallContext actualContext = (FakeCallContext) stashCallable.getContext();
    Truth.assertThat(actualContext.getChannel()).isSameInstanceAs(channel);
    Truth.assertThat(actualContext.getCredentials()).isSameInstanceAs(credentials);
}
Also used : FakeCallContext(com.google.api.gax.rpc.testing.FakeCallContext) OperationStashCallable(com.google.api.gax.rpc.testing.FakeOperationApi.OperationStashCallable) Credentials(com.google.auth.Credentials) FakeChannel(com.google.api.gax.rpc.testing.FakeChannel) Test(org.junit.Test)

Example 20 with FakeChannel

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

the class OperationCallableImplTest method callCancelWithContext.

@Test
public void callCancelWithContext() throws Exception {
    FakeChannel channel = new FakeChannel();
    Credentials credentials = Mockito.mock(Credentials.class);
    ApiCallContext context = FakeCallContext.createDefault().withChannel(channel).withCredentials(credentials);
    OperationStashCallable stashCallable = new OperationStashCallable();
    OperationCallable<Integer, String, Long> callable = stashCallable.withDefaultCallContext(FakeCallContext.createDefault());
    OperationFuture<String, Long> operationFuture = callable.futureCall(45);
    callable.cancel(operationFuture.getName(), context).get();
    Truth.assertThat(stashCallable.wasCancelCalled()).isTrue();
    FakeCallContext actualContext = (FakeCallContext) stashCallable.getCancelContext();
    Truth.assertThat(actualContext.getChannel()).isSameInstanceAs(channel);
    Truth.assertThat(actualContext.getCredentials()).isSameInstanceAs(credentials);
}
Also used : FakeCallContext(com.google.api.gax.rpc.testing.FakeCallContext) OperationStashCallable(com.google.api.gax.rpc.testing.FakeOperationApi.OperationStashCallable) Credentials(com.google.auth.Credentials) FakeChannel(com.google.api.gax.rpc.testing.FakeChannel) Test(org.junit.Test)

Aggregations

FakeChannel (com.google.api.gax.rpc.testing.FakeChannel)28 Test (org.junit.Test)24 FakeTransportChannel (com.google.api.gax.rpc.testing.FakeTransportChannel)12 Credentials (com.google.auth.Credentials)12 FakeClientSettings (com.google.api.gax.rpc.testing.FakeClientSettings)11 FakeCallContext (com.google.api.gax.rpc.testing.FakeCallContext)9 BackgroundResource (com.google.api.gax.core.BackgroundResource)5 ServerStreamingStashCallable (com.google.api.gax.rpc.testing.FakeStreamingApi.ServerStreamingStashCallable)4 OperationStashCallable (com.google.api.gax.rpc.testing.FakeOperationApi.OperationStashCallable)3 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)3 List (java.util.List)3 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)3 Before (org.junit.Before)3 ApiClock (com.google.api.core.ApiClock)2 CredentialsProvider (com.google.api.gax.core.CredentialsProvider)2 ExecutorProvider (com.google.api.gax.core.ExecutorProvider)2 FakeApiClock (com.google.api.gax.core.FakeApiClock)2 FixedCredentialsProvider (com.google.api.gax.core.FixedCredentialsProvider)2 FixedExecutorProvider (com.google.api.gax.core.FixedExecutorProvider)2 RetrySettings (com.google.api.gax.retrying.RetrySettings)2