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();
}
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);
}
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);
}
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);
}
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);
}
Aggregations