Search in sources :

Example 1 with LongRunningClient

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

the class GrpcLongRunningClientTest method get.

@Test
public void get() {
    OperationsStub operationsStub = mock(OperationsStub.class);
    when(operationsStub.getOperationCallable()).thenReturn(new UnaryCallable<GetOperationRequest, Operation>() {

        @Override
        public ApiFuture<Operation> futureCall(GetOperationRequest request, ApiCallContext context) {
            if (!request.getName().equals("poseidon")) {
                return ApiFutures.immediateFailedFuture(new IllegalStateException("Unexpected request: " + request));
            } else {
                return ApiFutures.immediateFuture(Operation.newBuilder().setName("greece").build());
            }
        }
    });
    LongRunningClient longRunningClient = new GrpcLongRunningClient(operationsStub);
    Truth.assertThat(longRunningClient.getOperationCallable().call("poseidon").getName()).isEqualTo("greece");
}
Also used : ApiFuture(com.google.api.core.ApiFuture) OperationsStub(com.google.longrunning.stub.OperationsStub) GetOperationRequest(com.google.longrunning.GetOperationRequest) LongRunningClient(com.google.api.gax.rpc.LongRunningClient) Operation(com.google.longrunning.Operation) ApiCallContext(com.google.api.gax.rpc.ApiCallContext) Test(org.junit.Test)

Example 2 with LongRunningClient

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

the class GrpcLongRunningClientTest method delete.

@Test
public void delete() {
    OperationsStub operationsStub = mock(OperationsStub.class);
    when(operationsStub.deleteOperationCallable()).thenReturn(new UnaryCallable<DeleteOperationRequest, Empty>() {

        @Override
        public ApiFuture<Empty> futureCall(DeleteOperationRequest request, ApiCallContext context) {
            if (!request.getName().equals("poseidon")) {
                return ApiFutures.immediateFailedFuture(new IllegalStateException("Unexpected request: " + request));
            } else {
                return ApiFutures.immediateFuture(Empty.getDefaultInstance());
            }
        }
    });
    LongRunningClient longRunningClient = new GrpcLongRunningClient(operationsStub);
    Truth.assertThat(longRunningClient.deleteOperationCallable().call("poseidon")).isNull();
}
Also used : ApiFuture(com.google.api.core.ApiFuture) OperationsStub(com.google.longrunning.stub.OperationsStub) Empty(com.google.protobuf.Empty) LongRunningClient(com.google.api.gax.rpc.LongRunningClient) ApiCallContext(com.google.api.gax.rpc.ApiCallContext) DeleteOperationRequest(com.google.longrunning.DeleteOperationRequest) Test(org.junit.Test)

Example 3 with LongRunningClient

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

the class GrpcLongRunningClientTest method cancel.

@Test
public void cancel() {
    OperationsStub operationsStub = mock(OperationsStub.class);
    when(operationsStub.cancelOperationCallable()).thenReturn(new UnaryCallable<CancelOperationRequest, Empty>() {

        @Override
        public ApiFuture<Empty> futureCall(CancelOperationRequest request, ApiCallContext context) {
            if (!request.getName().equals("poseidon")) {
                return ApiFutures.immediateFailedFuture(new IllegalStateException("Unexpected request: " + request));
            } else {
                return ApiFutures.immediateFuture(Empty.getDefaultInstance());
            }
        }
    });
    LongRunningClient longRunningClient = new GrpcLongRunningClient(operationsStub);
    Truth.assertThat(longRunningClient.cancelOperationCallable().call("poseidon")).isNull();
}
Also used : ApiFuture(com.google.api.core.ApiFuture) OperationsStub(com.google.longrunning.stub.OperationsStub) Empty(com.google.protobuf.Empty) LongRunningClient(com.google.api.gax.rpc.LongRunningClient) CancelOperationRequest(com.google.longrunning.CancelOperationRequest) ApiCallContext(com.google.api.gax.rpc.ApiCallContext) Test(org.junit.Test)

Example 4 with LongRunningClient

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

the class GrpcCallableFactory method createOperationCallable.

/**
 * Creates a callable object that represents a long-running operation. Designed for use by
 * generated code.
 *
 * @param grpcCallSettings the gRPC call settings
 * @param operationCallSettings {@link OperationCallSettings} to configure the method-level
 *     settings with.
 * @param clientContext {@link ClientContext} to use to connect to the service.
 * @param operationsStub {@link OperationsStub} to use to poll for updates on the Operation.
 * @return {@link com.google.api.gax.rpc.OperationCallable} callable object.
 */
public static <RequestT, ResponseT, MetadataT> OperationCallable<RequestT, ResponseT, MetadataT> createOperationCallable(GrpcCallSettings<RequestT, Operation> grpcCallSettings, OperationCallSettings<RequestT, ResponseT, MetadataT> operationCallSettings, ClientContext clientContext, OperationsStub operationsStub) {
    SpanName initialSpanName = getSpanName(grpcCallSettings.getMethodDescriptor());
    SpanName operationSpanName = SpanName.of(initialSpanName.getClientName(), initialSpanName.getMethodName() + "Operation");
    UnaryCallable<RequestT, Operation> initialGrpcCallable = createBaseUnaryCallable(grpcCallSettings, operationCallSettings.getInitialCallSettings(), clientContext);
    UnaryCallable<RequestT, OperationSnapshot> initialCallable = new GrpcOperationSnapshotCallable<>(initialGrpcCallable);
    // Create a sub-trace for the initial RPC that starts the operation.
    UnaryCallable<RequestT, OperationSnapshot> tracedInitialCallable = new TracedOperationInitialCallable<>(initialCallable, clientContext.getTracerFactory(), initialSpanName);
    LongRunningClient longRunningClient = new GrpcLongRunningClient(operationsStub);
    OperationCallable<RequestT, ResponseT, MetadataT> operationCallable = Callables.longRunningOperation(tracedInitialCallable, operationCallSettings, clientContext, longRunningClient);
    OperationCallable<RequestT, ResponseT, MetadataT> tracedOperationCallable = new TracedOperationCallable<>(operationCallable, clientContext.getTracerFactory(), operationSpanName);
    return tracedOperationCallable.withDefaultCallContext(clientContext.getDefaultCallContext());
}
Also used : TracedOperationInitialCallable(com.google.api.gax.tracing.TracedOperationInitialCallable) SpanName(com.google.api.gax.tracing.SpanName) Operation(com.google.longrunning.Operation) TracedOperationCallable(com.google.api.gax.tracing.TracedOperationCallable) LongRunningClient(com.google.api.gax.rpc.LongRunningClient) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot)

Aggregations

LongRunningClient (com.google.api.gax.rpc.LongRunningClient)4 ApiFuture (com.google.api.core.ApiFuture)3 ApiCallContext (com.google.api.gax.rpc.ApiCallContext)3 OperationsStub (com.google.longrunning.stub.OperationsStub)3 Test (org.junit.Test)3 Operation (com.google.longrunning.Operation)2 Empty (com.google.protobuf.Empty)2 OperationSnapshot (com.google.api.gax.longrunning.OperationSnapshot)1 SpanName (com.google.api.gax.tracing.SpanName)1 TracedOperationCallable (com.google.api.gax.tracing.TracedOperationCallable)1 TracedOperationInitialCallable (com.google.api.gax.tracing.TracedOperationInitialCallable)1 CancelOperationRequest (com.google.longrunning.CancelOperationRequest)1 DeleteOperationRequest (com.google.longrunning.DeleteOperationRequest)1 GetOperationRequest (com.google.longrunning.GetOperationRequest)1