Search in sources :

Example 1 with FakeCallContext

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

the class OperationCallableImplTest method callResumeWithContext.

@Test
public void callResumeWithContext() 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);
    String response = callable.resumeFutureCall(operationFuture.getName(), context).get();
    Truth.assertThat(response).isEqualTo("45");
    FakeCallContext actualContext = (FakeCallContext) stashCallable.getResumeContext();
    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 2 with FakeCallContext

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

the class UnaryCallableTest method simpleCall.

@Test
public void simpleCall() throws Exception {
    StashCallable<Integer, Integer> stashCallable = new StashCallable<>(1);
    Integer response = stashCallable.call(2, FakeCallContext.createDefault());
    assertEquals(Integer.valueOf(1), response);
    FakeCallContext callContext = (FakeCallContext) stashCallable.getContext();
    assertNull(callContext.getChannel());
    assertNull(callContext.getCredentials());
}
Also used : FakeCallContext(com.google.api.gax.rpc.testing.FakeCallContext) StashCallable(com.google.api.gax.rpc.testing.FakeSimpleApi.StashCallable) Test(org.junit.Test)

Example 3 with FakeCallContext

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

the class ServerStreamingCallableTest method testIteratedServerStreamingCallWithContext.

@Test
public void testIteratedServerStreamingCallWithContext() {
    FakeChannel channel = new FakeChannel();
    Credentials credentials = Mockito.mock(Credentials.class);
    ApiCallContext context = FakeCallContext.createDefault().withChannel(channel).withCredentials(credentials);
    ServerStreamingStashCallable<Integer, Integer> stashCallable = new ServerStreamingStashCallable<>();
    ServerStreamingCallable<Integer, Integer> callable = stashCallable.withDefaultCallContext(FakeCallContext.createDefault());
    Integer request = 1;
    callable.call(request, context);
    Truth.assertThat(stashCallable.getActualRequest()).isSameInstanceAs(request);
    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) ServerStreamingStashCallable(com.google.api.gax.rpc.testing.FakeStreamingApi.ServerStreamingStashCallable) Credentials(com.google.auth.Credentials) FakeChannel(com.google.api.gax.rpc.testing.FakeChannel) Test(org.junit.Test)

Example 4 with FakeCallContext

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

the class OperationCallableImplTest method mockGetOpSnapshotCallable.

private <RequestT> UnaryCallable<RequestT, OperationSnapshot> mockGetOpSnapshotCallable(final StatusCode.Code returnStatusCode, final OperationSnapshot... results) {
    return new UnaryCallable<RequestT, OperationSnapshot>() {

        private int index = 0;

        @Override
        public ApiFuture<OperationSnapshot> futureCall(RequestT request, ApiCallContext context) {
            FakeCallContext fakeCallContext = (FakeCallContext) context;
            if (fakeCallContext != null && fakeCallContext.getTimeout() != null && fakeCallContext.getTimeout().isZero()) {
                throw new DeadlineExceededException("Invalid timeout of 0 s", null, FakeStatusCode.of(StatusCode.Code.DEADLINE_EXCEEDED), true);
            }
            OperationSnapshot response = results[index];
            if (index < results.length - 1) {
                index += 1;
            }
            return newFuture(returnStatusCode, response);
        }
    };
}
Also used : FakeCallContext(com.google.api.gax.rpc.testing.FakeCallContext) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) FakeOperationSnapshot(com.google.api.gax.rpc.testing.FakeOperationSnapshot)

Example 5 with FakeCallContext

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

Aggregations

FakeCallContext (com.google.api.gax.rpc.testing.FakeCallContext)12 Test (org.junit.Test)11 Credentials (com.google.auth.Credentials)10 FakeChannel (com.google.api.gax.rpc.testing.FakeChannel)9 ServerStreamingStashCallable (com.google.api.gax.rpc.testing.FakeStreamingApi.ServerStreamingStashCallable)4 OperationStashCallable (com.google.api.gax.rpc.testing.FakeOperationApi.OperationStashCallable)3 StashCallable (com.google.api.gax.rpc.testing.FakeSimpleApi.StashCallable)3 RetrySettings (com.google.api.gax.retrying.RetrySettings)2 OperationSnapshot (com.google.api.gax.longrunning.OperationSnapshot)1 Code (com.google.api.gax.rpc.StatusCode.Code)1 FakeOperationSnapshot (com.google.api.gax.rpc.testing.FakeOperationSnapshot)1 FakeStatusCode (com.google.api.gax.rpc.testing.FakeStatusCode)1 ClientStreamingStashCallable (com.google.api.gax.rpc.testing.FakeStreamingApi.ClientStreamingStashCallable)1 ImmutableList (com.google.common.collect.ImmutableList)1 List (java.util.List)1