Search in sources :

Example 46 with OperationSnapshot

use of com.google.api.gax.longrunning.OperationSnapshot in project gax-java by googleapis.

the class ProtoOperationTransformersTest method testAnyMetadataTransformer_mismatchedTypes.

@Test
public void testAnyMetadataTransformer_mismatchedTypes() {
    MetadataTransformer<Money> transformer = MetadataTransformer.create(Money.class);
    Status status = Status.newBuilder().setCode(Code.OK.value()).build();
    OperationSnapshot operationSnapshot = GrpcOperationSnapshot.create(Operation.newBuilder().setMetadata(Any.pack(Color.getDefaultInstance())).setError(status).build());
    Exception exception = assertThrows(UnknownException.class, () -> transformer.apply(operationSnapshot));
    Truth.assertThat(exception).hasMessageThat().contains("encountered a problem unpacking it");
}
Also used : Status(com.google.rpc.Status) Money(com.google.type.Money) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) UnknownException(com.google.api.gax.rpc.UnknownException) UnavailableException(com.google.api.gax.rpc.UnavailableException) Test(org.junit.Test)

Example 47 with OperationSnapshot

use of com.google.api.gax.longrunning.OperationSnapshot in project gax-java by googleapis.

the class ApiMessageOperationTransformersTest method testMetadataTransformer_mismatchedTypes.

@Test
public void testMetadataTransformer_mismatchedTypes() {
    MetadataTransformer<FakeOperationMessage> transformer = MetadataTransformer.create(FakeOperationMessage.class);
    FakeMetadataMessage<?> metadataMessage = new FakeMetadataMessage<>(Status.PENDING, Code.OK);
    ApiMessage bananaResponse = new FakeApiMessage(ImmutableMap.<String, Object>of("name", "banana"), null, null);
    FakeOperationMessage<?, ?> metadata = new FakeOperationMessage<>("No response method", bananaResponse, metadataMessage);
    OperationSnapshot operationSnapshot = new OperationSnapshotImpl(metadata);
    try {
        transformer.apply(operationSnapshot);
        Assert.fail("MetadataTransformer should have thrown an exception");
    } catch (ApiException expected) {
        Truth.assertThat(expected).hasMessageThat().contains("cannot be cast");
    }
}
Also used : FakeApiMessage(com.google.api.gax.httpjson.testing.FakeApiMessage) FakeApiMessage(com.google.api.gax.httpjson.testing.FakeApiMessage) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) ApiException(com.google.api.gax.rpc.ApiException) Test(org.junit.Test)

Example 48 with OperationSnapshot

use of com.google.api.gax.longrunning.OperationSnapshot in project gax-java by googleapis.

the class ApiMessageOperationTransformersTest method testMetadataTransformer.

@Test
public void testMetadataTransformer() {
    MetadataTransformer<FakeMetadataMessage> transformer = MetadataTransformer.create(FakeMetadataMessage.class);
    EmptyMessage returnType = EmptyMessage.getDefaultInstance();
    FakeMetadataMessage<?> metadataMessage = new FakeMetadataMessage<>(Status.PENDING, Code.OK);
    FakeOperationMessage<?, ?> operation = new FakeOperationMessage<>("foo", returnType, metadataMessage);
    OperationSnapshot operationSnapshot = new OperationSnapshotImpl(operation);
    Truth.assertThat(transformer.apply(operationSnapshot)).isEqualTo(metadataMessage);
}
Also used : OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) Test(org.junit.Test)

Example 49 with OperationSnapshot

use of com.google.api.gax.longrunning.OperationSnapshot in project gax-java by googleapis.

the class HttpJsonOperationSnapshotCallableTest method futureCallTest.

@Test
public void futureCallTest() throws ExecutionException, InterruptedException {
    Option request = Option.newBuilder().setName("Arizona").build();
    Field field = Field.newBuilder().setName("Georgia").build();
    ApiCallContext context = mock(ApiCallContext.class);
    OperationSnapshot operationSnapshot = HttpJsonOperationSnapshot.newBuilder().setName("California").setMetadata(2).setDone(true).setResponse("Florida").setError(0, "no error").build();
    SettableApiFuture<Field> settableApiFuture = SettableApiFuture.create();
    settableApiFuture.set(field);
    when(operationSnapshotFactory.create(request, field)).thenReturn(operationSnapshot);
    when(innerCallable.futureCall(request, context)).thenReturn(settableApiFuture);
    ApiFuture<OperationSnapshot> futureCall = operationSnapCallable.futureCall(request, context);
    Truth.assertThat(futureCall.get().getName()).isEqualTo("California");
}
Also used : Field(com.google.protobuf.Field) Option(com.google.protobuf.Option) ApiCallContext(com.google.api.gax.rpc.ApiCallContext) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) Test(org.junit.Test)

Example 50 with OperationSnapshot

use of com.google.api.gax.longrunning.OperationSnapshot in project gax-java by googleapis.

the class ProtoOperationTransformersTest method testAnyResponseTransformer.

@Test
public void testAnyResponseTransformer() {
    ResponseTransformer<Money> transformer = ResponseTransformer.create(Money.class);
    Money inputMoney = Money.newBuilder().setCurrencyCode("USD").build();
    OperationSnapshot operationSnapshot = HttpJsonOperationSnapshot.create(Operation.newBuilder().setResponse(Any.pack(inputMoney)).build());
    Truth.assertThat(transformer.apply(operationSnapshot)).isEqualTo(inputMoney);
}
Also used : Money(com.google.type.Money) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) Test(org.junit.Test)

Aggregations

OperationSnapshot (com.google.api.gax.longrunning.OperationSnapshot)55 Test (org.junit.Test)44 FakeOperationSnapshot (com.google.api.gax.rpc.testing.FakeOperationSnapshot)28 Color (java.awt.Color)23 Currency (java.util.Currency)23 Money (com.google.type.Money)13 UnavailableException (com.google.api.gax.rpc.UnavailableException)7 UnknownException (com.google.api.gax.rpc.UnknownException)6 Status (com.google.rpc.Status)6 FakeStatusCode (com.google.api.gax.rpc.testing.FakeStatusCode)4 ExecutionException (java.util.concurrent.ExecutionException)4 FakeApiClock (com.google.api.gax.core.FakeApiClock)2 FakeApiMessage (com.google.api.gax.httpjson.testing.FakeApiMessage)2 TimedRetryAlgorithm (com.google.api.gax.retrying.TimedRetryAlgorithm)2 ApiCallContext (com.google.api.gax.rpc.ApiCallContext)2 ApiException (com.google.api.gax.rpc.ApiException)2 FakeChannel (com.google.api.gax.rpc.testing.FakeChannel)2 CancellationException (java.util.concurrent.CancellationException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 BatchingSettings (com.google.api.gax.batching.BatchingSettings)1