Search in sources :

Example 1 with Type

use of com.google.protobuf.Type in project grpc-java by grpc.

the class ProtoUtilsTest method testJsonRoundtrip.

@Test
public void testJsonRoundtrip() throws Exception {
    Marshaller<Type> marshaller = ProtoUtils.jsonMarshaller(Type.getDefaultInstance());
    InputStream is = marshaller.stream(proto);
    is = new ByteArrayInputStream(ByteStreams.toByteArray(is));
    assertEquals(proto, marshaller.parse(is));
}
Also used : Type(com.google.protobuf.Type) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 2 with Type

use of com.google.protobuf.Type in project grpc-java by grpc.

the class ProtoUtilsTest method testRoundtrip.

@Test
public void testRoundtrip() throws Exception {
    Marshaller<Type> marshaller = ProtoUtils.marshaller(Type.getDefaultInstance());
    InputStream is = marshaller.stream(proto);
    is = new ByteArrayInputStream(ByteStreams.toByteArray(is));
    assertEquals(proto, marshaller.parse(is));
}
Also used : Type(com.google.protobuf.Type) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 3 with Type

use of com.google.protobuf.Type in project grpc-java by grpc.

the class ProtoUtilsTest method testJsonIoException.

@Test
public void testJsonIoException() throws Exception {
    Marshaller<Type> marshaller = ProtoUtils.jsonMarshaller(Type.getDefaultInstance());
    final IOException ioe = new IOException();
    try {
        marshaller.parse(new ByteArrayInputStream("{}".getBytes("UTF-8")) {

            @Override
            public void close() throws IOException {
                throw ioe;
            }
        });
        fail("Exception expected");
    } catch (StatusRuntimeException ex) {
        assertEquals(Status.Code.INTERNAL, ex.getStatus().getCode());
        assertEquals(ioe, ex.getCause());
    }
}
Also used : Type(com.google.protobuf.Type) ByteArrayInputStream(java.io.ByteArrayInputStream) StatusRuntimeException(io.grpc.StatusRuntimeException) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with Type

use of com.google.protobuf.Type 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 5 with Type

use of com.google.protobuf.Type in project grpc-java by grpc.

the class ProtoUtilsTest method testJsonRepresentation.

@Test
public void testJsonRepresentation() throws Exception {
    Marshaller<Type> marshaller = ProtoUtils.jsonMarshaller(Type.getDefaultInstance());
    InputStream is = marshaller.stream(proto);
    String s = new String(ByteStreams.toByteArray(is), "UTF-8");
    assertEquals("{\"name\":\"value\"}", s.replaceAll("\\s", ""));
}
Also used : Type(com.google.protobuf.Type) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Test(org.junit.Test)

Aggregations

Type (com.google.protobuf.Type)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 Test (org.junit.Test)6 StatusRuntimeException (io.grpc.StatusRuntimeException)3 InputStream (java.io.InputStream)3 IOException (java.io.IOException)1 Ignore (org.junit.Ignore)1