Search in sources :

Example 76 with StatusRuntimeException

use of io.grpc.StatusRuntimeException in project grpc-java by grpc.

the class HeaderClientInterceptorTest method clientHeaderDeliveredToServer.

@Test
public void clientHeaderDeliveredToServer() {
    GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub(inProcessChannel);
    ArgumentCaptor<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class);
    try {
        blockingStub.sayHello(HelloRequest.getDefaultInstance());
        fail();
    } catch (StatusRuntimeException expected) {
    // expected because the method is not implemented at server side
    }
    verify(mockServerInterceptor).interceptCall(Matchers.<ServerCall<HelloRequest, HelloReply>>any(), metadataCaptor.capture(), Matchers.<ServerCallHandler<HelloRequest, HelloReply>>any());
    assertEquals("customRequestValue", metadataCaptor.getValue().get(HeaderClientInterceptor.CUSTOM_HEADER_KEY));
}
Also used : GreeterBlockingStub(io.grpc.examples.helloworld.GreeterGrpc.GreeterBlockingStub) Metadata(io.grpc.Metadata) StatusRuntimeException(io.grpc.StatusRuntimeException) HelloRequest(io.grpc.examples.helloworld.HelloRequest) HelloReply(io.grpc.examples.helloworld.HelloReply) Test(org.junit.Test)

Example 77 with StatusRuntimeException

use of io.grpc.StatusRuntimeException in project grpc-java by grpc.

the class AbstractInteropTest method statusCodeAndMessage.

@Test(timeout = 10000)
public void statusCodeAndMessage() throws Exception {
    int errorCode = 2;
    String errorMessage = "test status message";
    EchoStatus responseStatus = EchoStatus.newBuilder().setCode(errorCode).setMessage(errorMessage).build();
    SimpleRequest simpleRequest = SimpleRequest.newBuilder().setResponseStatus(responseStatus).build();
    StreamingOutputCallRequest streamingRequest = StreamingOutputCallRequest.newBuilder().setResponseStatus(responseStatus).build();
    // Test UnaryCall
    try {
        blockingStub.unaryCall(simpleRequest);
        fail();
    } catch (StatusRuntimeException e) {
        assertEquals(Status.UNKNOWN.getCode(), e.getStatus().getCode());
        assertEquals(errorMessage, e.getStatus().getDescription());
    }
    if (metricsExpected()) {
        assertClientMetrics("grpc.testing.TestService/UnaryCall", Status.Code.UNKNOWN);
    }
    // Test FullDuplexCall
    @SuppressWarnings("unchecked") StreamObserver<StreamingOutputCallResponse> responseObserver = mock(StreamObserver.class);
    StreamObserver<StreamingOutputCallRequest> requestObserver = asyncStub.fullDuplexCall(responseObserver);
    requestObserver.onNext(streamingRequest);
    requestObserver.onCompleted();
    ArgumentCaptor<Throwable> captor = ArgumentCaptor.forClass(Throwable.class);
    verify(responseObserver, timeout(operationTimeoutMillis())).onError(captor.capture());
    assertEquals(Status.UNKNOWN.getCode(), Status.fromThrowable(captor.getValue()).getCode());
    assertEquals(errorMessage, Status.fromThrowable(captor.getValue()).getDescription());
    verifyNoMoreInteractions(responseObserver);
    if (metricsExpected()) {
        assertClientMetrics("grpc.testing.TestService/FullDuplexCall", Status.Code.UNKNOWN);
    }
}
Also used : EchoStatus(io.grpc.testing.integration.Messages.EchoStatus) StatusRuntimeException(io.grpc.StatusRuntimeException) ByteString(com.google.protobuf.ByteString) SimpleRequest(io.grpc.testing.integration.Messages.SimpleRequest) StreamingOutputCallRequest(io.grpc.testing.integration.Messages.StreamingOutputCallRequest) StreamingOutputCallResponse(io.grpc.testing.integration.Messages.StreamingOutputCallResponse) Test(org.junit.Test)

Example 78 with StatusRuntimeException

use of io.grpc.StatusRuntimeException in project grpc-java by grpc.

the class ClientCallsTest method unaryBlockingCallFailed.

@Test
public void unaryBlockingCallFailed() throws Exception {
    Integer req = 2;
    final Status status = Status.INTERNAL.withDescription("Unique status");
    final Metadata trailers = new Metadata();
    NoopClientCall<Integer, String> call = new NoopClientCall<Integer, String>() {

        @Override
        public void start(io.grpc.ClientCall.Listener<String> listener, Metadata headers) {
            listener.onClose(status, trailers);
        }
    };
    try {
        ClientCalls.blockingUnaryCall(call, req);
        fail("Should fail");
    } catch (StatusRuntimeException e) {
        assertSame(status, e.getStatus());
        assertSame(trailers, e.getTrailers());
    }
}
Also used : Status(io.grpc.Status) NoopClientCall(io.grpc.testing.NoopClientCall) Metadata(io.grpc.Metadata) StatusRuntimeException(io.grpc.StatusRuntimeException) Test(org.junit.Test)

Example 79 with StatusRuntimeException

use of io.grpc.StatusRuntimeException in project grpc-java by grpc.

the class ProtoUtilsTest method testJsonInvalidProto.

@Test
public void testJsonInvalidProto() throws Exception {
    Marshaller<Type> marshaller = ProtoUtils.jsonMarshaller(Type.getDefaultInstance());
    try {
        marshaller.parse(new ByteArrayInputStream("{\"\":3}".getBytes("UTF-8")));
        fail("Expected exception");
    } catch (StatusRuntimeException ex) {
        assertEquals(Status.Code.INTERNAL, ex.getStatus().getCode());
        assertNotNull(ex.getCause());
    }
}
Also used : Type(com.google.protobuf.Type) ByteArrayInputStream(java.io.ByteArrayInputStream) StatusRuntimeException(io.grpc.StatusRuntimeException) Test(org.junit.Test)

Example 80 with StatusRuntimeException

use of io.grpc.StatusRuntimeException in project grpc-java by grpc.

the class ProtoUtilsTest method testJsonInvalid.

@Ignore("https://github.com/google/protobuf/issues/1470")
@Test
public void testJsonInvalid() throws Exception {
    Marshaller<Type> marshaller = ProtoUtils.jsonMarshaller(Type.getDefaultInstance());
    try {
        marshaller.parse(new ByteArrayInputStream("{]".getBytes("UTF-8")));
        fail("Expected exception");
    } catch (StatusRuntimeException ex) {
        assertEquals(Status.Code.INTERNAL, ex.getStatus().getCode());
        assertNotNull(ex.getCause());
    }
}
Also used : Type(com.google.protobuf.Type) ByteArrayInputStream(java.io.ByteArrayInputStream) StatusRuntimeException(io.grpc.StatusRuntimeException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

StatusRuntimeException (io.grpc.StatusRuntimeException)131 Test (org.junit.Test)110 ApiException (com.google.api.gax.grpc.ApiException)74 ByteString (com.google.protobuf.ByteString)12 Status (io.grpc.Status)12 ArrayList (java.util.ArrayList)11 Metadata (io.grpc.Metadata)10 SubscriptionName (com.google.pubsub.v1.SubscriptionName)9 ProjectName (com.google.monitoring.v3.ProjectName)6 TopicName (com.google.pubsub.v1.TopicName)6 StreamObserver (io.grpc.stub.StreamObserver)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 Document (com.google.cloud.language.v1beta2.Document)5 ParentNameOneof (com.google.logging.v2.ParentNameOneof)5 IOException (java.io.IOException)5 ExecutionException (java.util.concurrent.ExecutionException)5 Document (com.google.cloud.language.v1.Document)4 EncodingType (com.google.cloud.language.v1beta2.EncodingType)4 HelloReply (io.grpc.examples.helloworld.HelloReply)4 HelloRequest (io.grpc.examples.helloworld.HelloRequest)4